Merge "mw.Upload.BookletLayout: Don't explode when the API call fails with 'exception'"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.Upload.js
index aafc37a..8a74ffc 100644 (file)
                this.comment = '';
                this.filename = null;
                this.file = null;
-               this.state = Upload.State.NEW;
+               this.setState( Upload.State.NEW );
 
                this.imageinfo = undefined;
        }
 
        UP = Upload.prototype;
 
+       /**
+        * Get the mw.Api instance used by this Upload object.
+        *
+        * @return {jQuery.Promise}
+        * @return {Function} return.done
+        * @return {mw.Api} return.done.api
+        */
+       UP.getApi = function () {
+               return $.Deferred().resolve( this.api ).promise();
+       };
+
        /**
         * Set the text of the file page, to be created on file upload.
         *
         */
        UP.setFilenameFromFile = function () {
                var file = this.getFile();
+               if ( !file ) {
+                       return;
+               }
                if ( file.nodeType && file.nodeType === Node.ELEMENT_NODE ) {
                        // File input element, use getBasename to cut out the path
                        this.setFilename( this.getBasename( file.value ) );
-               } else if ( file.name && file.lastModified ) {
+               } else if ( file.name ) {
                        // HTML5 FileAPI File object, but use getBasename to be safe
                        this.setFilename( this.getBasename( file.name ) );
+               } else {
+                       // If we ever implement uploading files from clipboard, they might not have a name
+                       this.setFilename( '?' );
                }
        };
 
                );
        };
 
+       /**
+        * Sets the state and state details (if any) of the upload.
+        *
+        * @param {mw.Upload.State} state
+        * @param {Object} stateDetails
+        */
+       UP.setState = function ( state, stateDetails ) {
+               this.state = state;
+               this.stateDetails = stateDetails;
+       };
+
        /**
         * Gets the state of the upload.
         *
                return this.state;
        };
 
+       /**
+        * Gets details of the current state.
+        *
+        * @return {string}
+        */
+       UP.getStateDetails = function () {
+               return this.stateDetails;
+       };
+
        /**
         * Get the imageinfo object for the finished upload.
         * Only available once the upload is finished! Don't try to get it
                        return $.Deferred().reject( 'No filename set. Call setFilename to add one.' );
                }
 
-               this.state = Upload.State.UPLOADING;
+               this.setState( Upload.State.UPLOADING );
 
                return this.api.upload( this.getFile(), {
                        watchlist: ( this.getWatchlist() ) ? 1 : undefined,
                        filename: this.getFilename(),
                        text: this.getText()
                } ).then( function ( result ) {
-                       upload.state = Upload.State.UPLOADED;
+                       upload.setState( Upload.State.UPLOADED );
                        upload.imageinfo = result.upload.imageinfo;
                        return result;
-               }, function () {
-                       upload.state = Upload.State.ERROR;
+               }, function ( errorCode, result ) {
+                       if ( result && result.upload && result.upload.warnings ) {
+                               upload.setState( Upload.State.WARNING, result );
+                       } else {
+                               upload.setState( Upload.State.ERROR, result );
+                       }
+                       return $.Deferred().reject( errorCode, result );
                } );
        };
 
                        this.setFilenameFromFile();
                }
 
-               this.state = Upload.State.UPLOADING;
+               this.setState( Upload.State.UPLOADING );
 
                this.stashPromise = this.api.uploadToStash( this.getFile(), {
                        filename: this.getFilename()
                } ).then( function ( finishStash ) {
-                       upload.state = Upload.State.STASHED;
+                       upload.setState( Upload.State.STASHED );
                        return finishStash;
-               }, function () {
-                       upload.state = Upload.State.ERROR;
+               }, function ( errorCode, result ) {
+                       if ( result && result.upload && result.upload.warnings ) {
+                               upload.setState( Upload.State.WARNING, result );
+                       } else {
+                               upload.setState( Upload.State.ERROR, result );
+                       }
+                       return $.Deferred().reject( errorCode, result );
                } );
 
                return this.stashPromise;
                }
 
                return this.stashPromise.then( function ( finishStash ) {
-                       upload.state = Upload.State.UPLOADING;
+                       upload.setState( Upload.State.UPLOADING );
 
                        return finishStash( {
+                               bucket: upload.bucket, // Automatically ignored if undefined
                                watchlist: ( upload.getWatchlist() ) ? 1 : undefined,
                                comment: upload.getComment(),
                                filename: upload.getFilename(),
                                text: upload.getText()
                        } ).then( function ( result ) {
-                               upload.state = Upload.State.UPLOADED;
+                               upload.setState( Upload.State.UPLOADED );
                                upload.imageinfo = result.upload.imageinfo;
                                return result;
-                       }, function () {
-                               upload.state = Upload.State.ERROR;
+                       }, function ( errorCode, result ) {
+                               if ( result && result.upload && result.upload.warnings ) {
+                                       upload.setState( Upload.State.WARNING, result );
+                               } else {
+                                       upload.setState( Upload.State.ERROR, result );
+                               }
+                               return $.Deferred().reject( errorCode, result );
                        } );
                } );
        };