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 * overriden 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
112 * The upload form has changed
115 * @param {boolean} isValid The form is valid
119 * The info form has changed
122 * @param {boolean} isValid The form is valid
128 * @property {OO.ui.FormLayout} uploadForm
129 * The form rendered in the first step to get the file object.
130 * Rendered in {@link #renderUploadForm renderUploadForm}.
134 * @property {OO.ui.FormLayout} infoForm
135 * The form rendered in the second step to get metadata.
136 * Rendered in {@link #renderInfoForm renderInfoForm}
140 * @property {OO.ui.FormLayout} insertForm
141 * The form rendered in the third step to show usage
142 * Rendered in {@link #renderInsertForm renderInsertForm}
148 * Initialize for a new upload
150 mw
.Upload
.BookletLayout
.prototype.initialize = function () {
152 this.upload
= this.createUpload();
153 this.setPage( 'upload' );
157 * Create a new upload model
160 * @return {mw.Upload} Upload model
162 mw
.Upload
.BookletLayout
.prototype.createUpload = function () {
163 return new mw
.Upload();
169 * Uploads the file that was added in the upload form. Uses
170 * {@link #getFile getFile} to get the HTML5
174 * @fires fileUploaded
175 * @return {jQuery.Promise}
177 mw
.Upload
.BookletLayout
.prototype.uploadFile = function () {
178 var deferred
= $.Deferred(),
180 file
= this.getFile();
182 this.filenameWidget
.setValue( file
.name
);
183 this.setPage( 'info' );
185 this.upload
.setFile( file
);
186 // Explicitly set the filename so that the old filename isn't used in case of retry
187 this.upload
.setFilenameFromFile();
189 this.uploadPromise
= this.upload
.uploadToStash();
190 this.uploadPromise
.then( function () {
192 layout
.emit( 'fileUploaded' );
194 // These errors will be thrown while the user is on the info page.
195 // Pretty sure it's impossible to get a warning other than 'stashfailed' here, which should
196 // really be an error...
197 var errorMessage
= layout
.getErrorMessageForStateDetails();
198 deferred
.reject( errorMessage
);
201 // If there is an error in uploading, come back to the upload page
202 deferred
.fail( function () {
203 layout
.setPage( 'upload' );
210 * Saves the stash finalizes upload. Uses
211 * {@link #getFilename getFilename}, and
212 * {@link #getText getText} to get details from
217 * @returns {jQuery.Promise} Rejects the promise with an
218 * {@link OO.ui.Error error}, or resolves if the upload was successful.
220 mw
.Upload
.BookletLayout
.prototype.saveFile = function () {
222 deferred
= $.Deferred();
224 this.upload
.setFilename( this.getFilename() );
225 this.upload
.setText( this.getText() );
227 this.uploadPromise
.then( function () {
228 layout
.upload
.finishStashUpload().then( function () {
231 // Normalize page name and localise the 'File:' prefix
232 name
= new mw
.Title( 'File:' + layout
.upload
.getFilename() ).toString();
233 layout
.filenameUsageWidget
.setValue( '[[' + name
+ ']]' );
234 layout
.setPage( 'insert' );
237 layout
.emit( 'fileSaved' );
239 var errorMessage
= layout
.getErrorMessageForStateDetails();
240 deferred
.reject( errorMessage
);
244 return deferred
.promise();
248 * Get an error message (as OO.ui.Error object) that should be displayed to the user for current
249 * state and state details.
252 * @returns {OO.ui.Error} Error to display for given state and details.
254 mw
.Upload
.BookletLayout
.prototype.getErrorMessageForStateDetails = function () {
256 state
= this.upload
.getState(),
257 stateDetails
= this.upload
.getStateDetails(),
258 error
= stateDetails
.error
,
259 warnings
= stateDetails
.upload
&& stateDetails
.upload
.warnings
;
261 if ( state
=== mw
.Upload
.State
.ERROR
) {
262 // HACK We should either have a hook here to allow TitleBlacklist to handle this, or just have
263 // TitleBlacklist produce sane error messages that can be displayed without arcane knowledge
264 if ( error
.info
=== 'TitleBlacklist prevents this title from being created' ) {
265 // HACK Apparently the only reliable way to determine whether TitleBlacklist was involved
266 return new OO
.ui
.Error(
268 // HACK TitleBlacklist doesn't have a sensible message, this one is from UploadWizard
269 mw
.message( 'api-error-blacklisted' ).parse()
271 { recoverable
: false }
275 message
= mw
.message( 'api-error-' + error
.code
);
276 if ( !message
.exists() ) {
277 message
= mw
.message( 'api-error-unknownerror', JSON
.stringify( stateDetails
) );
279 return new OO
.ui
.Error(
283 { recoverable
: false }
287 if ( state
=== mw
.Upload
.State
.WARNING
) {
288 // We could get more than one of these errors, these are in order
289 // of importance. For example fixing the thumbnail like file name
290 // won't help the fact that the file already exists.
291 if ( warnings
.stashfailed
!== undefined ) {
292 return new OO
.ui
.Error(
294 mw
.message( 'api-error-stashfailed' ).parse()
296 { recoverable
: false }
298 } else if ( warnings
.exists
!== undefined ) {
299 return new OO
.ui
.Error(
301 mw
.message( 'fileexists', 'File:' + warnings
.exists
).parse()
303 { recoverable
: false }
305 } else if ( warnings
[ 'page-exists' ] !== undefined ) {
306 return new OO
.ui
.Error(
308 mw
.message( 'filepageexists', 'File:' + warnings
[ 'page-exists' ] ).parse()
310 { recoverable
: false }
312 } else if ( warnings
.duplicate
!== undefined ) {
313 return new OO
.ui
.Error(
315 mw
.message( 'api-error-duplicate', warnings
.duplicate
.length
).parse()
317 { recoverable
: false }
319 } else if ( warnings
[ 'thumb-name' ] !== undefined ) {
320 return new OO
.ui
.Error(
322 mw
.message( 'filename-thumb-name' ).parse()
324 { recoverable
: false }
326 } else if ( warnings
[ 'bad-prefix' ] !== undefined ) {
327 return new OO
.ui
.Error(
329 mw
.message( 'filename-bad-prefix', warnings
[ 'bad-prefix' ] ).parse()
331 { recoverable
: false }
333 } else if ( warnings
[ 'duplicate-archive' ] !== undefined ) {
334 return new OO
.ui
.Error(
336 mw
.message( 'api-error-duplicate-archive', 1 ).parse()
338 { recoverable
: false }
340 } else if ( warnings
.badfilename
!== undefined ) {
341 // Change the name if the current name isn't acceptable
342 // TODO This might not really be the best place to do this
343 this.filenameWidget
.setValue( warnings
.badfilename
);
344 return new OO
.ui
.Error(
346 mw
.message( 'badfilename', warnings
.badfilename
).parse()
350 return new OO
.ui
.Error(
352 // Let's get all the help we can if we can't pin point the error
353 mw
.message( 'api-error-unknown-warning', JSON
.stringify( stateDetails
) ).parse()
355 { recoverable
: false }
364 * Renders and returns the upload form and sets the
365 * {@link #uploadForm uploadForm} property.
369 * @returns {OO.ui.FormLayout}
371 mw
.Upload
.BookletLayout
.prototype.renderUploadForm = function () {
374 this.selectFileWidget
= new OO
.ui
.SelectFileWidget();
375 fieldset
= new OO
.ui
.FieldsetLayout( { label
: mw
.msg( 'upload-form-label-select-file' ) } );
376 fieldset
.addItems( [ this.selectFileWidget
] );
377 this.uploadForm
= new OO
.ui
.FormLayout( { items
: [ fieldset
] } );
380 this.selectFileWidget
.on( 'change', this.onUploadFormChange
.bind( this ) );
382 return this.uploadForm
;
386 * Handle change events to the upload form
391 mw
.Upload
.BookletLayout
.prototype.onUploadFormChange = function () {
392 this.emit( 'uploadValid', !!this.selectFileWidget
.getValue() );
396 * Renders and returns the information form for collecting
397 * metadata and sets the {@link #infoForm infoForm}
401 * @returns {OO.ui.FormLayout}
403 mw
.Upload
.BookletLayout
.prototype.renderInfoForm = function () {
406 this.filenameWidget
= new OO
.ui
.TextInputWidget( {
407 indicator
: 'required',
411 this.descriptionWidget
= new OO
.ui
.TextInputWidget( {
412 indicator
: 'required',
419 fieldset
= new OO
.ui
.FieldsetLayout( {
420 label
: mw
.msg( 'upload-form-label-infoform-title' )
423 new OO
.ui
.FieldLayout( this.filenameWidget
, {
424 label
: mw
.msg( 'upload-form-label-infoform-name' ),
427 new OO
.ui
.FieldLayout( this.descriptionWidget
, {
428 label
: mw
.msg( 'upload-form-label-infoform-description' ),
432 this.infoForm
= new OO
.ui
.FormLayout( { items
: [ fieldset
] } );
434 this.filenameWidget
.on( 'change', this.onInfoFormChange
.bind( this ) );
435 this.descriptionWidget
.on( 'change', this.onInfoFormChange
.bind( this ) );
437 return this.infoForm
;
441 * Handle change events to the info form
446 mw
.Upload
.BookletLayout
.prototype.onInfoFormChange = function () {
449 this.filenameWidget
.getValidity(),
450 this.descriptionWidget
.getValidity()
451 ).done( function () {
452 layout
.emit( 'infoValid', true );
453 } ).fail( function () {
454 layout
.emit( 'infoValid', false );
459 * Renders and returns the insert form to show file usage and
460 * sets the {@link #insertForm insertForm} property.
463 * @returns {OO.ui.FormLayout}
465 mw
.Upload
.BookletLayout
.prototype.renderInsertForm = function () {
468 this.filenameUsageWidget
= new OO
.ui
.TextInputWidget();
469 fieldset
= new OO
.ui
.FieldsetLayout( {
470 label
: mw
.msg( 'upload-form-label-usage-title' )
473 new OO
.ui
.FieldLayout( this.filenameUsageWidget
, {
474 label
: mw
.msg( 'upload-form-label-usage-filename' ),
478 this.insertForm
= new OO
.ui
.FormLayout( { items
: [ fieldset
] } );
480 return this.insertForm
;
486 * Gets the file object from the
487 * {@link #uploadForm upload form}.
490 * @returns {File|null}
492 mw
.Upload
.BookletLayout
.prototype.getFile = function () {
493 return this.selectFileWidget
.getValue();
497 * Gets the file name from the
498 * {@link #infoForm information form}.
503 mw
.Upload
.BookletLayout
.prototype.getFilename = function () {
504 return this.filenameWidget
.getValue();
508 * Gets the page text from the
509 * {@link #infoForm information form}.
514 mw
.Upload
.BookletLayout
.prototype.getText = function () {
515 return this.descriptionWidget
.getValue();
521 * Sets the file object
524 * @param {File|null} file File to select
526 mw
.Upload
.BookletLayout
.prototype.setFile = function ( file
) {
527 this.selectFileWidget
.setValue( file
);
531 * Clear the values of all fields
535 mw
.Upload
.BookletLayout
.prototype.clear = function () {
536 this.selectFileWidget
.setValue( null );
537 this.filenameWidget
.setValue( null ).setValidityFlag( true );
538 this.descriptionWidget
.setValue( null ).setValidityFlag( true );
539 this.filenameUsageWidget
.setValue( null );
542 }( jQuery
, mediaWiki
) );