mw.Upload.BookletLayout: Add help text for name and description fields
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.Upload.BookletLayout.js
index 7401445..26eabc7 100644 (file)
@@ -52,7 +52,7 @@
         * {@link #createUpload createUpload} method to
         * return the new model. The {@link #saveFile saveFile}, and
         * the {@link #uploadFile uploadFile} methods need to be
-        * overriden to use the new model and data returned from the forms.
+        * overridden to use the new model and data returned from the forms.
         *
         * @class
         * @extends OO.ui.BookletLayout
         * @return {jQuery.Promise} Promise resolved when everything is initialized
         */
        mw.Upload.BookletLayout.prototype.initialize = function () {
+               var
+                       apiPromise,
+                       booklet = this,
+                       deferred = $.Deferred();
+
                this.clear();
                this.upload = this.createUpload();
                this.setPage( 'upload' );
-               return $.Deferred().resolve().promise();
+
+               apiPromise = this.upload.apiPromise || $.Deferred().resolve( this.upload.api );
+               apiPromise.done( function ( api ) {
+                       // If the user can't upload anything, don't give them the option to.
+                       api.getUserInfo().done( 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' );
+                               }
+                       } ).always( function () {
+                               deferred.resolve();
+                       } );
+               } );
+
+               return deferred.promise();
        };
 
        /**
                        layout = this,
                        file = this.getFile();
 
-               this.filenameWidget.setValue( file.name );
+               this.setFilename( file.name );
+
                this.setPage( 'info' );
 
+               if ( this.shouldRecordBucket ) {
+                       this.upload.bucket = this.bucket;
+               }
+
                this.upload.setFile( file );
-               // Explicitly set the filename so that the old filename isn't used in case of retry
-               this.upload.setFilenameFromFile();
+               // The original file name might contain invalid characters, so use our sanitized one
+               this.upload.setFilename( this.getFilename() );
 
                this.uploadPromise = this.upload.uploadToStash();
                this.uploadPromise.then( function () {
                        } 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
-                               this.filenameWidget.setValue( warnings.badfilename );
+                               this.setFilename( warnings.badfilename );
                                return new OO.ui.Error(
                                        $( '<p>' ).msg( 'badfilename', warnings.badfilename )
                                );
                this.descriptionWidget = new OO.ui.TextInputWidget( {
                        indicator: 'required',
                        required: true,
-                       validate: /.+/,
+                       validate: /\S+/,
                        multiline: true,
                        autosize: true
                } );
                fieldset.addItems( [
                        new OO.ui.FieldLayout( this.filenameWidget, {
                                label: mw.msg( 'upload-form-label-infoform-name' ),
-                               align: 'top'
+                               align: 'top',
+                               help: mw.msg( 'upload-form-label-infoform-name-tooltip' )
                        } ),
                        new OO.ui.FieldLayout( this.descriptionWidget, {
                                label: mw.msg( 'upload-form-label-infoform-description' ),
-                               align: 'top'
+                               align: 'top',
+                               help: mw.msg( 'upload-form-label-infoform-description-tooltip' )
                        } )
                ] );
                this.infoForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
         * @return {string}
         */
        mw.Upload.BookletLayout.prototype.getFilename = function () {
-               return this.filenameWidget.getValue();
+               var filename = this.filenameWidget.getValue();
+               if ( this.filenameExtension ) {
+                       filename += '.' + this.filenameExtension;
+               }
+               return filename;
+       };
+
+       /**
+        * Prefills the {@link #infoForm information form} with the given filename.
+        *
+        * @protected
+        * @param {string} filename
+        */
+       mw.Upload.BookletLayout.prototype.setFilename = function ( filename ) {
+               var title = mw.Title.newFromFileName( filename );
+
+               if ( title ) {
+                       this.filenameWidget.setValue( title.getNameText() );
+                       this.filenameExtension = mw.Title.normalizeExtension( title.getExtension() );
+               } else {
+                       // Seems to happen for files with no extension, which should fail some checks anyway...
+                       this.filenameWidget.setValue( filename );
+                       this.filenameExtension = null;
+               }
        };
 
        /**