User:MarkTraceur/uploadDialog.js

( function ( mw ) {	mw.uploadDialogPromise = mw.loader.using( [ 'oojs-ui', 'mediawiki.Upload' ] ).then( function { mw.UploadDialog = function ( config ) { mw.UploadDialog.parent.call( this, config ); };

OO.inheritClass( mw.UploadDialog, OO.ui.Dialog ); mw.UploadDialog.prototype.initialize = function { var dialog = this, fieldset = new OO.ui.FieldsetLayout( { label: 'Upload a file from your computer' } );

fieldset.addItems( this.getFields );

this.uploadbutton = new OO.ui.ButtonWidget( {				label: 'Save',				flags: [ 'constructive', 'primary' ]			} ).on( 'click', function {				dialog.stashPromise.then( function  { dialog.finalizeUpload; dialog.upload.finishStashUpload.then( function {						dialog.emit( 'upload-complete', dialog.upload );						dialog.close;					} ); } );			} );

this.submitfield = new OO.ui.FieldLayout( this.uploadbutton, {} );

fieldset.addItems( [ this.submitfield ] );

this.stashPromise = $.Deferred; this.upload = this.getUploadObject;

mw.UploadDialog.parent.prototype.initialize.call( this );

this.content = new OO.ui.PanelLayout( {				expanded: false,				framed: true,				padded: true,				content: [ fieldset ]			} );

this.$body.append( this.content.$element ); };

mw.UploadDialog.prototype.getUploadObject = function { return new mw.Upload; };

mw.UploadDialog.prototype.getBodyHeight = function { return this.content.$element.outerHeight( true ); };

mw.UploadDialog.prototype.getFields = function { var dialog = this;

this.filewidget = new OO.ui.SelectFileWidget( {				label: 'Select a file to upload'			} ).on( 'change', function {				dialog.fillFileWidget( dialog.filewidget.getValue );			} );

this.filefield = new OO.ui.FieldLayout( this.filewidget, {				label: 'Choose a file'			} );

this.filenamewidget = new OO.ui.TextInputWidget( {} );

this.filenamefield = new OO.ui.FieldLayout( this.filenamewidget, {				label: 'Filename'			} );

return [ this.filefield, this.filenamefield ];		};

mw.UploadDialog.prototype.fillFileWidget = function ( file ) { var dialog = this;

if ( file !== null ) { this.filewidget.setDisabled( true ); this.upload.setFile( file ); this.filenamewidget.setValue( file.name ); this.upload.uploadToStash.then( function {					dialog.stashPromise.resolve;				} ); }		};

mw.UploadDialog.prototype.finalizeUpload = function { this.upload.setFilename( this.filenamewidget.getValue ); };	} ); }( mediaWiki ) );