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 if ( layout
.upload
.getState() === mw
.Upload
.State
.ERROR
) {
196 deferred
.reject( new OO
.ui
.Error( layout
.upload
.getStateDetails(), {
201 if ( layout
.upload
.getState() === mw
.Upload
.State
.WARNING
) {
202 deferred
.reject( new OO
.ui
.Error( layout
.upload
.getStateDetails(), {
209 // If there is an error in uploading, come back to the upload page
210 deferred
.fail( function () {
211 layout
.setPage( 'upload' );
218 * Saves the stash finalizes upload. Uses
219 * {@link #getFilename getFilename}, and
220 * {@link #getText getText} to get details from
225 * @returns {jQuery.Promise} Rejects the promise with an
226 * {@link OO.ui.Error error}, or resolves if the upload was successful.
228 mw
.Upload
.BookletLayout
.prototype.saveFile = function () {
230 deferred
= $.Deferred();
232 this.upload
.setFilename( this.getFilename() );
233 this.upload
.setText( this.getText() );
235 this.uploadPromise
.always( function () {
236 layout
.upload
.finishStashUpload().always( function () {
239 if ( layout
.upload
.getState() === mw
.Upload
.State
.ERROR
) {
240 deferred
.reject( new OO
.ui
.Error( mw
.msg( 'upload-process-error' ), {
246 if ( layout
.upload
.getState() === mw
.Upload
.State
.WARNING
) {
247 deferred
.reject( new OO
.ui
.Error( mw
.msg( 'upload-process-warning' ), {
253 // Normalize page name and localise the 'File:' prefix
254 name
= new mw
.Title( 'File:' + layout
.upload
.getFilename() ).toString();
255 layout
.filenameUsageWidget
.setValue( '[[' + name
+ ']]' );
256 layout
.setPage( 'insert' );
259 layout
.emit( 'fileSaved' );
263 return deferred
.promise();
269 * Renders and returns the upload form and sets the
270 * {@link #uploadForm uploadForm} property.
274 * @returns {OO.ui.FormLayout}
276 mw
.Upload
.BookletLayout
.prototype.renderUploadForm = function () {
279 this.selectFileWidget
= new OO
.ui
.SelectFileWidget();
280 fieldset
= new OO
.ui
.FieldsetLayout( { label
: mw
.msg( 'upload-form-label-select-file' ) } );
281 fieldset
.addItems( [ this.selectFileWidget
] );
282 this.uploadForm
= new OO
.ui
.FormLayout( { items
: [ fieldset
] } );
285 this.selectFileWidget
.on( 'change', this.onUploadFormChange
.bind( this ) );
287 return this.uploadForm
;
291 * Handle change events to the upload form
296 mw
.Upload
.BookletLayout
.prototype.onUploadFormChange = function () {
297 this.emit( 'uploadValid', !!this.selectFileWidget
.getValue() );
301 * Renders and returns the information form for collecting
302 * metadata and sets the {@link #infoForm infoForm}
306 * @returns {OO.ui.FormLayout}
308 mw
.Upload
.BookletLayout
.prototype.renderInfoForm = function () {
311 this.filenameWidget
= new OO
.ui
.TextInputWidget( {
312 indicator
: 'required',
316 this.descriptionWidget
= new OO
.ui
.TextInputWidget( {
317 indicator
: 'required',
324 fieldset
= new OO
.ui
.FieldsetLayout( {
325 label
: mw
.msg( 'upload-form-label-infoform-title' )
328 new OO
.ui
.FieldLayout( this.filenameWidget
, {
329 label
: mw
.msg( 'upload-form-label-infoform-name' ),
332 new OO
.ui
.FieldLayout( this.descriptionWidget
, {
333 label
: mw
.msg( 'upload-form-label-infoform-description' ),
337 this.infoForm
= new OO
.ui
.FormLayout( { items
: [ fieldset
] } );
339 this.filenameWidget
.on( 'change', this.onInfoFormChange
.bind( this ) );
340 this.descriptionWidget
.on( 'change', this.onInfoFormChange
.bind( this ) );
342 return this.infoForm
;
346 * Handle change events to the info form
351 mw
.Upload
.BookletLayout
.prototype.onInfoFormChange = function () {
354 this.filenameWidget
.getValidity(),
355 this.descriptionWidget
.getValidity()
356 ).done( function () {
357 layout
.emit( 'infoValid', true );
358 } ).fail( function () {
359 layout
.emit( 'infoValid', false );
364 * Renders and returns the insert form to show file usage and
365 * sets the {@link #insertForm insertForm} property.
368 * @returns {OO.ui.FormLayout}
370 mw
.Upload
.BookletLayout
.prototype.renderInsertForm = function () {
373 this.filenameUsageWidget
= new OO
.ui
.TextInputWidget();
374 fieldset
= new OO
.ui
.FieldsetLayout( {
375 label
: mw
.msg( 'upload-form-label-usage-title' )
378 new OO
.ui
.FieldLayout( this.filenameUsageWidget
, {
379 label
: mw
.msg( 'upload-form-label-usage-filename' ),
383 this.insertForm
= new OO
.ui
.FormLayout( { items
: [ fieldset
] } );
385 return this.insertForm
;
391 * Gets the file object from the
392 * {@link #uploadForm upload form}.
395 * @returns {File|null}
397 mw
.Upload
.BookletLayout
.prototype.getFile = function () {
398 return this.selectFileWidget
.getValue();
402 * Gets the file name from the
403 * {@link #infoForm information form}.
408 mw
.Upload
.BookletLayout
.prototype.getFilename = function () {
409 return this.filenameWidget
.getValue();
413 * Gets the page text from the
414 * {@link #infoForm information form}.
419 mw
.Upload
.BookletLayout
.prototype.getText = function () {
420 return this.descriptionWidget
.getValue();
426 * Clear the values of all fields
430 mw
.Upload
.BookletLayout
.prototype.clear = function () {
431 this.selectFileWidget
.setValue( null );
432 this.filenameWidget
.setValue( null ).setValidityFlag( true );
433 this.descriptionWidget
.setValue( null ).setValidityFlag( true );
434 this.filenameUsageWidget
.setValue( null );
437 }( jQuery
, mediaWiki
) );