mw.Upload.BookletLayout: Fail when unable to load config from foreign wiki
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.Upload.BookletLayout.js
index eaab8c7..bbd0f1b 100644 (file)
@@ -1,4 +1,5 @@
-( function ( $, mw ) {
+/*global moment*/
+( function ( $, mw, moment ) {
 
        /**
         * mw.Upload.BookletLayout encapsulates the process of uploading a file
@@ -60,6 +61,7 @@
         * @constructor
         * @param {Object} config Configuration options
         * @cfg {jQuery} [$overlay] Overlay to use for widgets in the booklet
+        * @cfg {string} [filekey] Sets the stashed file to finish uploading. Overrides most of the file selection process, and fetches a thumbnail from the server.
         */
        mw.Upload.BookletLayout = function ( config ) {
                // Parent constructor
@@ -67,6 +69,8 @@
 
                this.$overlay = config.$overlay;
 
+               this.filekey = config.filekey;
+
                this.renderUploadForm();
                this.renderInfoForm();
                this.renderInsertForm();
 
        /* Events */
 
+       /**
+        * Progress events for the uploaded file
+        *
+        * @event fileUploadProgress
+        * @param {number} progress In percentage
+        * @param {Object} duration Duration object from `moment.duration()`
+        */
+
        /**
         * The file has finished uploading
         *
 
                this.clear();
                this.upload = this.createUpload();
+
                this.setPage( 'upload' );
 
+               if ( this.filekey ) {
+                       this.setFilekey( this.filekey );
+               }
+
                return this.upload.getApi().then(
                        function ( api ) {
-                               // If the user can't upload anything, don't give them the option to.
-                               return api.getUserInfo().then(
-                                       function ( userInfo ) {
+                               return $.when(
+                                       booklet.upload.loadConfig().then(
+                                               null,
+                                               function ( errorMsg ) {
+                                                       booklet.getPage( 'upload' ).$element.msg( errorMsg );
+                                                       return $.Deferred().resolve();
+                                               }
+                                       ),
+                                       // If the user can't upload anything, don't give them the option to.
+                                       api.getUserInfo().then( function ( userInfo ) {
                                                if ( userInfo.rights.indexOf( 'upload' ) === -1 ) {
                                                        // TODO Use a better error message when not all logged-in users can upload
                                                        booklet.getPage( 'upload' ).$element.msg( 'api-error-mustbeloggedin' );
                                                }
                                                return $.Deferred().resolve();
-                                       },
-                                       function () {
-                                               return $.Deferred().resolve();
-                                       }
+                                       } )
+                               ).then(
+                                       null,
+                                       // Always resolve, never reject
+                                       function () { return $.Deferred().resolve(); }
                                );
                        },
                        function ( errorMsg ) {
         * file object.
         *
         * @protected
+        * @fires fileUploadProgress
         * @fires fileUploaded
         * @return {jQuery.Promise}
         */
        mw.Upload.BookletLayout.prototype.uploadFile = function () {
                var deferred = $.Deferred(),
+                       startTime = new Date(),
                        layout = this,
                        file = this.getFile();
 
-               this.setFilename( file.name );
-
                this.setPage( 'info' );
 
+               if ( this.filekey ) {
+                       if ( file === null ) {
+                               // Someone gonna get-a hurt real bad
+                               throw new Error( 'filekey not passed into file select widget, which is impossible. Quitting while we\'re behind.' );
+                       }
+
+                       // Stashed file already uploaded.
+                       deferred.resolve();
+                       this.uploadPromise = deferred;
+                       this.emit( 'fileUploaded' );
+                       return deferred;
+               }
+
+               this.setFilename( file.name );
+
                this.upload.setFile( file );
                // The original file name might contain invalid characters, so use our sanitized one
                this.upload.setFilename( this.getFilename() );
                        // really be an error...
                        var errorMessage = layout.getErrorMessageForStateDetails();
                        deferred.reject( errorMessage );
+               }, function ( progress ) {
+                       var elapsedTime = new Date() - startTime,
+                               estimatedTotalTime = ( 1 / progress ) * elapsedTime,
+                               estimatedRemainingTime = moment.duration( estimatedTotalTime - elapsedTime );
+                       layout.emit( 'fileUploadProgress', progress, estimatedRemainingTime );
                } );
 
                // If there is an error in uploading, come back to the upload page
                                );
                        }
 
-                       message = mw.message( 'api-error-' + error.code );
-                       if ( !message.exists() ) {
-                               message = mw.message( 'api-error-unknownerror', JSON.stringify( stateDetails ) );
+                       if ( error.code === 'protectedpage' ) {
+                               message = mw.message( 'protectedpagetext' );
+                       } else {
+                               message = mw.message( 'api-error-' + error.code );
+                               if ( !message.exists() ) {
+                                       message = mw.message( 'api-error-unknownerror', JSON.stringify( stateDetails ) );
+                               }
                        }
                        return new OO.ui.Error(
                                $( '<p>' ).append( message.parseDom() ),
                                        $( '<p>' ).msg( 'fileexists', 'File:' + warnings.exists ),
                                        { recoverable: false }
                                );
+                       } else if ( warnings[ 'exists-normalized' ] !== undefined ) {
+                               return new OO.ui.Error(
+                                       $( '<p>' ).msg( 'fileexists', 'File:' + warnings[ 'exists-normalized' ] ),
+                                       { recoverable: false }
+                               );
                        } else if ( warnings[ 'page-exists' ] !== undefined ) {
                                return new OO.ui.Error(
                                        $( '<p>' ).msg( 'filepageexists', 'File:' + warnings[ 'page-exists' ] ),
                                        $( '<p>' ).msg( 'api-error-duplicate-archive', 1 ),
                                        { recoverable: false }
                                );
+                       } else if ( warnings[ 'was-deleted' ] !== undefined ) {
+                               return new OO.ui.Error(
+                                       $( '<p>' ).msg( 'api-error-was-deleted' ),
+                                       { recoverable: false }
+                               );
                        } else if ( warnings.badfilename !== undefined ) {
                                // Change the name if the current name isn't acceptable
                                // TODO This might not really be the best place to do this
                var fieldset,
                        layout = this;
 
-               this.selectFileWidget = new OO.ui.SelectFileWidget( {
-                       showDropTarget: true
-               } );
+               this.selectFileWidget = this.getFileWidget();
                fieldset = new OO.ui.FieldsetLayout();
                fieldset.addItems( [ this.selectFileWidget ] );
                this.uploadForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
 
-               // Validation
+               // Validation (if the SFW is for a stashed file, this never fires)
                this.selectFileWidget.on( 'change', this.onUploadFormChange.bind( this ) );
 
                this.selectFileWidget.on( 'change', function () {
                return this.uploadForm;
        };
 
+       /**
+        * Gets the widget for displaying or inputting the file to upload.
+        *
+        * @return {OO.ui.SelectFileWidget|mw.widgets.StashedFileWidget}
+        */
+       mw.Upload.BookletLayout.prototype.getFileWidget = function () {
+               if ( this.filekey ) {
+                       return new mw.widgets.StashedFileWidget( {
+                               filekey: this.filekey
+                       } );
+               }
+
+               return new OO.ui.SelectFileWidget( {
+                       showDropTarget: true
+               } );
+       };
+
        /**
         * Updates the file preview on the info form when a file is added.
         *
                this.filePreview = new OO.ui.Widget( {
                        classes: [ 'mw-upload-bookletLayout-filePreview' ]
                } );
+               this.progressBarWidget = new OO.ui.ProgressBarWidget( {
+                       progress: 0
+               } );
+               this.filePreview.$element.append( this.progressBarWidget.$element );
+
                this.filenameWidget = new OO.ui.TextInputWidget( {
                        indicator: 'required',
                        required: true,
                        items: [ this.filePreview, fieldset ]
                } );
 
+               this.on( 'fileUploadProgress', function ( progress ) {
+                       this.progressBarWidget.setProgress( progress * 100 );
+               }.bind( this ) );
+
                this.filenameWidget.on( 'change', this.onInfoFormChange.bind( this ) );
                this.descriptionWidget.on( 'change', this.onInfoFormChange.bind( this ) );
 
                this.selectFileWidget.setValue( file );
        };
 
+       /**
+        * Sets the filekey of a file already stashed on the server
+        * as the target of this upload operation.
+        *
+        * @protected
+        * @param {string} filekey
+        */
+       mw.Upload.BookletLayout.prototype.setFilekey = function ( filekey ) {
+               this.upload.setFilekey( this.filekey );
+               this.selectFileWidget.setValue( filekey );
+
+               this.onUploadFormChange();
+       };
+
        /**
         * Clear the values of all fields
         *
         */
        mw.Upload.BookletLayout.prototype.clear = function () {
                this.selectFileWidget.setValue( null );
+               this.progressBarWidget.setProgress( 0 );
                this.filenameWidget.setValue( null ).setValidityFlag( true );
                this.descriptionWidget.setValue( null ).setValidityFlag( true );
                this.filenameUsageWidget.setValue( null );
        };
 
-}( jQuery, mediaWiki ) );
+}( jQuery, mediaWiki, moment ) );