mw.Upload.BookletLayout: Fail when unable to load config from foreign wiki
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.Upload.BookletLayout.js
index 33b10bd..bbd0f1b 100644 (file)
@@ -61,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
@@ -68,6 +69,8 @@
 
                this.$overlay = config.$overlay;
 
+               this.filekey = config.filekey;
+
                this.renderUploadForm();
                this.renderInfoForm();
                this.renderInsertForm();
 
                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 ) {
                        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() );
                                );
                        }
 
-                       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.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
         *