4 * mw.Upload.BookletLayout encapsulates the process of uploading a file
5 * to MediaWiki using the {@link mw.Upload upload model}.
6 * The booklet emits events that can be used to get the stashed
7 * upload and the final file. It can be extended to accept
8 * additional fields from the user for specific scenarios like
9 * for Commons, or campaigns.
13 * The {@link OO.ui.BookletLayout booklet layout} has three steps:
15 * - **Upload**: Has a {@link OO.ui.SelectFileWidget field} to get the file object.
17 * - **Information**: Has a {@link OO.ui.FormLayout form} to collect metadata. This can be
20 * - **Insert**: Has details on how to use the file that was uploaded.
22 * Each step has a form associated with it defined in
23 * {@link #renderUploadForm renderUploadForm},
24 * {@link #renderInfoForm renderInfoForm}, and
25 * {@link #renderInsertForm renderInfoForm}. The
26 * {@link #getFile getFile},
27 * {@link #getFilename getFilename}, and
28 * {@link #getText getText} methods are used to get
29 * the information filled in these forms, required to call
30 * {@link mw.Upload mw.Upload}.
34 * See the {@link mw.Upload.Dialog upload dialog}.
36 * The {@link #event-fileUploaded fileUploaded},
37 * and {@link #event-fileSaved fileSaved} events can
38 * be used to get details of the upload.
42 * To extend using {@link mw.Upload mw.Upload}, override
43 * {@link #renderInfoForm renderInfoForm} to render
44 * the form required for the specific use-case. Update the
45 * {@link #getFilename getFilename}, and
46 * {@link #getText getText} methods to return data
47 * from your newly created form. If you added new fields you'll also have
48 * to update the {@link #clear} method.
50 * If you plan to use a different upload model, apart from what is mentioned
51 * above, you'll also have to override the
52 * {@link #createUpload createUpload} method to
53 * return the new model. The {@link #saveFile saveFile}, and
54 * the {@link #uploadFile uploadFile} methods need to be
55 * overridden to use the new model and data returned from the forms.
58 * @extends OO.ui.BookletLayout
61 * @param {Object} config Configuration options
62 * @cfg {jQuery} [$overlay] Overlay to use for widgets in the booklet
64 mw
.Upload
.BookletLayout = function ( config
) {
66 mw
.Upload
.BookletLayout
.parent
.call( this, config
);
68 this.$overlay
= config
.$overlay
;
70 this.renderUploadForm();
71 this.renderInfoForm();
72 this.renderInsertForm();
75 new OO
.ui
.PageLayout( 'upload', {
78 content
: [ this.uploadForm
]
80 new OO
.ui
.PageLayout( 'info', {
83 content
: [ this.infoForm
]
85 new OO
.ui
.PageLayout( 'insert', {
88 content
: [ this.insertForm
]
95 OO
.inheritClass( mw
.Upload
.BookletLayout
, OO
.ui
.BookletLayout
);
100 * The file has finished uploading
102 * @event fileUploaded
106 * The file has been saved to the database
109 * @param {Object} imageInfo See mw.Upload#getImageInfo
113 * The upload form has changed
116 * @param {boolean} isValid The form is valid
120 * The info form has changed
123 * @param {boolean} isValid The form is valid
129 * @property {OO.ui.FormLayout} uploadForm
130 * The form rendered in the first step to get the file object.
131 * Rendered in {@link #renderUploadForm renderUploadForm}.
135 * @property {OO.ui.FormLayout} infoForm
136 * The form rendered in the second step to get metadata.
137 * Rendered in {@link #renderInfoForm renderInfoForm}
141 * @property {OO.ui.FormLayout} insertForm
142 * The form rendered in the third step to show usage
143 * Rendered in {@link #renderInsertForm renderInsertForm}
149 * Initialize for a new upload
151 * @return {jQuery.Promise} Promise resolved when everything is initialized
153 mw
.Upload
.BookletLayout
.prototype.initialize = function () {
157 this.upload
= this.createUpload();
158 this.setPage( 'upload' );
160 return this.upload
.getApi().then(
162 // If the user can't upload anything, don't give them the option to.
163 return api
.getUserInfo().then(
164 function ( userInfo
) {
165 if ( userInfo
.rights
.indexOf( 'upload' ) === -1 ) {
166 // TODO Use a better error message when not all logged-in users can upload
167 booklet
.getPage( 'upload' ).$element
.msg( 'api-error-mustbeloggedin' );
169 return $.Deferred().resolve();
172 return $.Deferred().resolve();
176 function ( errorMsg
) {
177 booklet
.getPage( 'upload' ).$element
.msg( errorMsg
);
178 return $.Deferred().resolve();
184 * Create a new upload model
187 * @return {mw.Upload} Upload model
189 mw
.Upload
.BookletLayout
.prototype.createUpload = function () {
190 return new mw
.Upload();
196 * Uploads the file that was added in the upload form. Uses
197 * {@link #getFile getFile} to get the HTML5
201 * @fires fileUploaded
202 * @return {jQuery.Promise}
204 mw
.Upload
.BookletLayout
.prototype.uploadFile = function () {
205 var deferred
= $.Deferred(),
207 file
= this.getFile();
209 this.setFilename( file
.name
);
211 this.setPage( 'info' );
213 this.upload
.setFile( file
);
214 // The original file name might contain invalid characters, so use our sanitized one
215 this.upload
.setFilename( this.getFilename() );
217 this.uploadPromise
= this.upload
.uploadToStash();
218 this.uploadPromise
.then( function () {
220 layout
.emit( 'fileUploaded' );
222 // These errors will be thrown while the user is on the info page.
223 // Pretty sure it's impossible to get a warning other than 'stashfailed' here, which should
224 // really be an error...
225 var errorMessage
= layout
.getErrorMessageForStateDetails();
226 deferred
.reject( errorMessage
);
229 // If there is an error in uploading, come back to the upload page
230 deferred
.fail( function () {
231 layout
.setPage( 'upload' );
238 * Saves the stash finalizes upload. Uses
239 * {@link #getFilename getFilename}, and
240 * {@link #getText getText} to get details from
245 * @return {jQuery.Promise} Rejects the promise with an
246 * {@link OO.ui.Error error}, or resolves if the upload was successful.
248 mw
.Upload
.BookletLayout
.prototype.saveFile = function () {
250 deferred
= $.Deferred();
252 this.upload
.setFilename( this.getFilename() );
253 this.upload
.setText( this.getText() );
255 this.uploadPromise
.then( function () {
256 layout
.upload
.finishStashUpload().then( function () {
259 // Normalize page name and localise the 'File:' prefix
260 name
= new mw
.Title( 'File:' + layout
.upload
.getFilename() ).toString();
261 layout
.filenameUsageWidget
.setValue( '[[' + name
+ ']]' );
262 layout
.setPage( 'insert' );
265 layout
.emit( 'fileSaved', layout
.upload
.getImageInfo() );
267 var errorMessage
= layout
.getErrorMessageForStateDetails();
268 deferred
.reject( errorMessage
);
272 return deferred
.promise();
276 * Get an error message (as OO.ui.Error object) that should be displayed to the user for current
277 * state and state details.
280 * @return {OO.ui.Error} Error to display for given state and details.
282 mw
.Upload
.BookletLayout
.prototype.getErrorMessageForStateDetails = function () {
284 state
= this.upload
.getState(),
285 stateDetails
= this.upload
.getStateDetails(),
286 error
= stateDetails
.error
,
287 warnings
= stateDetails
.upload
&& stateDetails
.upload
.warnings
;
289 if ( state
=== mw
.Upload
.State
.ERROR
) {
291 // If there's an 'exception' key, this might be a timeout, or other connection problem
292 return new OO
.ui
.Error(
293 $( '<p>' ).msg( 'api-error-unknownerror', JSON
.stringify( stateDetails
) ),
294 { recoverable
: false }
298 // HACK We should either have a hook here to allow TitleBlacklist to handle this, or just have
299 // TitleBlacklist produce sane error messages that can be displayed without arcane knowledge
300 if ( error
.info
=== 'TitleBlacklist prevents this title from being created' ) {
301 // HACK Apparently the only reliable way to determine whether TitleBlacklist was involved
302 return new OO
.ui
.Error(
303 // HACK TitleBlacklist doesn't have a sensible message, this one is from UploadWizard
304 $( '<p>' ).msg( 'api-error-blacklisted' ),
305 { recoverable
: false }
309 message
= mw
.message( 'api-error-' + error
.code
);
310 if ( !message
.exists() ) {
311 message
= mw
.message( 'api-error-unknownerror', JSON
.stringify( stateDetails
) );
313 return new OO
.ui
.Error(
314 $( '<p>' ).append( message
.parseDom() ),
315 { recoverable
: false }
319 if ( state
=== mw
.Upload
.State
.WARNING
) {
320 // We could get more than one of these errors, these are in order
321 // of importance. For example fixing the thumbnail like file name
322 // won't help the fact that the file already exists.
323 if ( warnings
.stashfailed
!== undefined ) {
324 return new OO
.ui
.Error(
325 $( '<p>' ).msg( 'api-error-stashfailed' ),
326 { recoverable
: false }
328 } else if ( warnings
.exists
!== undefined ) {
329 return new OO
.ui
.Error(
330 $( '<p>' ).msg( 'fileexists', 'File:' + warnings
.exists
),
331 { recoverable
: false }
333 } else if ( warnings
[ 'page-exists' ] !== undefined ) {
334 return new OO
.ui
.Error(
335 $( '<p>' ).msg( 'filepageexists', 'File:' + warnings
[ 'page-exists' ] ),
336 { recoverable
: false }
338 } else if ( warnings
.duplicate
!== undefined ) {
339 return new OO
.ui
.Error(
340 $( '<p>' ).msg( 'api-error-duplicate', warnings
.duplicate
.length
),
341 { recoverable
: false }
343 } else if ( warnings
[ 'thumb-name' ] !== undefined ) {
344 return new OO
.ui
.Error(
345 $( '<p>' ).msg( 'filename-thumb-name' ),
346 { recoverable
: false }
348 } else if ( warnings
[ 'bad-prefix' ] !== undefined ) {
349 return new OO
.ui
.Error(
350 $( '<p>' ).msg( 'filename-bad-prefix', warnings
[ 'bad-prefix' ] ),
351 { recoverable
: false }
353 } else if ( warnings
[ 'duplicate-archive' ] !== undefined ) {
354 return new OO
.ui
.Error(
355 $( '<p>' ).msg( 'api-error-duplicate-archive', 1 ),
356 { recoverable
: false }
358 } else if ( warnings
.badfilename
!== undefined ) {
359 // Change the name if the current name isn't acceptable
360 // TODO This might not really be the best place to do this
361 this.setFilename( warnings
.badfilename
);
362 return new OO
.ui
.Error(
363 $( '<p>' ).msg( 'badfilename', warnings
.badfilename
)
366 return new OO
.ui
.Error(
367 // Let's get all the help we can if we can't pin point the error
368 $( '<p>' ).msg( 'api-error-unknown-warning', JSON
.stringify( stateDetails
) ),
369 { recoverable
: false }
378 * Renders and returns the upload form and sets the
379 * {@link #uploadForm uploadForm} property.
383 * @return {OO.ui.FormLayout}
385 mw
.Upload
.BookletLayout
.prototype.renderUploadForm = function () {
388 this.selectFileWidget
= new OO
.ui
.SelectFileWidget();
389 fieldset
= new OO
.ui
.FieldsetLayout( { label
: mw
.msg( 'upload-form-label-select-file' ) } );
390 fieldset
.addItems( [ this.selectFileWidget
] );
391 this.uploadForm
= new OO
.ui
.FormLayout( { items
: [ fieldset
] } );
394 this.selectFileWidget
.on( 'change', this.onUploadFormChange
.bind( this ) );
396 return this.uploadForm
;
400 * Handle change events to the upload form
405 mw
.Upload
.BookletLayout
.prototype.onUploadFormChange = function () {
406 this.emit( 'uploadValid', !!this.selectFileWidget
.getValue() );
410 * Renders and returns the information form for collecting
411 * metadata and sets the {@link #infoForm infoForm}
415 * @return {OO.ui.FormLayout}
417 mw
.Upload
.BookletLayout
.prototype.renderInfoForm = function () {
420 this.filenameWidget
= new OO
.ui
.TextInputWidget( {
421 indicator
: 'required',
425 this.descriptionWidget
= new OO
.ui
.TextInputWidget( {
426 indicator
: 'required',
433 fieldset
= new OO
.ui
.FieldsetLayout( {
434 label
: mw
.msg( 'upload-form-label-infoform-title' )
437 new OO
.ui
.FieldLayout( this.filenameWidget
, {
438 label
: mw
.msg( 'upload-form-label-infoform-name' ),
440 help
: mw
.msg( 'upload-form-label-infoform-name-tooltip' )
442 new OO
.ui
.FieldLayout( this.descriptionWidget
, {
443 label
: mw
.msg( 'upload-form-label-infoform-description' ),
445 help
: mw
.msg( 'upload-form-label-infoform-description-tooltip' )
448 this.infoForm
= new OO
.ui
.FormLayout( { items
: [ fieldset
] } );
450 this.filenameWidget
.on( 'change', this.onInfoFormChange
.bind( this ) );
451 this.descriptionWidget
.on( 'change', this.onInfoFormChange
.bind( this ) );
453 return this.infoForm
;
457 * Handle change events to the info form
462 mw
.Upload
.BookletLayout
.prototype.onInfoFormChange = function () {
465 this.filenameWidget
.getValidity(),
466 this.descriptionWidget
.getValidity()
467 ).done( function () {
468 layout
.emit( 'infoValid', true );
469 } ).fail( function () {
470 layout
.emit( 'infoValid', false );
475 * Renders and returns the insert form to show file usage and
476 * sets the {@link #insertForm insertForm} property.
479 * @return {OO.ui.FormLayout}
481 mw
.Upload
.BookletLayout
.prototype.renderInsertForm = function () {
484 this.filenameUsageWidget
= new OO
.ui
.TextInputWidget();
485 fieldset
= new OO
.ui
.FieldsetLayout( {
486 label
: mw
.msg( 'upload-form-label-usage-title' )
489 new OO
.ui
.FieldLayout( this.filenameUsageWidget
, {
490 label
: mw
.msg( 'upload-form-label-usage-filename' ),
494 this.insertForm
= new OO
.ui
.FormLayout( { items
: [ fieldset
] } );
496 return this.insertForm
;
502 * Gets the file object from the
503 * {@link #uploadForm upload form}.
506 * @return {File|null}
508 mw
.Upload
.BookletLayout
.prototype.getFile = function () {
509 return this.selectFileWidget
.getValue();
513 * Gets the file name from the
514 * {@link #infoForm information form}.
519 mw
.Upload
.BookletLayout
.prototype.getFilename = function () {
520 var filename
= this.filenameWidget
.getValue();
521 if ( this.filenameExtension
) {
522 filename
+= '.' + this.filenameExtension
;
528 * Prefills the {@link #infoForm information form} with the given filename.
531 * @param {string} filename
533 mw
.Upload
.BookletLayout
.prototype.setFilename = function ( filename
) {
534 var title
= mw
.Title
.newFromFileName( filename
);
537 this.filenameWidget
.setValue( title
.getNameText() );
538 this.filenameExtension
= mw
.Title
.normalizeExtension( title
.getExtension() );
540 // Seems to happen for files with no extension, which should fail some checks anyway...
541 this.filenameWidget
.setValue( filename
);
542 this.filenameExtension
= null;
547 * Gets the page text from the
548 * {@link #infoForm information form}.
553 mw
.Upload
.BookletLayout
.prototype.getText = function () {
554 return this.descriptionWidget
.getValue();
560 * Sets the file object
563 * @param {File|null} file File to select
565 mw
.Upload
.BookletLayout
.prototype.setFile = function ( file
) {
566 this.selectFileWidget
.setValue( file
);
570 * Clear the values of all fields
574 mw
.Upload
.BookletLayout
.prototype.clear = function () {
575 this.selectFileWidget
.setValue( null );
576 this.filenameWidget
.setValue( null ).setValidityFlag( true );
577 this.descriptionWidget
.setValue( null ).setValidityFlag( true );
578 this.filenameUsageWidget
.setValue( null );
581 }( jQuery
, mediaWiki
) );