mw.Upload.BookletLayout: Show errors and try to recover from warnings
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.Upload.BookletLayout.js
1 ( function ( $, mw ) {
2
3 /**
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.
10 *
11 * ## Structure
12 *
13 * The {@link OO.ui.BookletLayout booklet layout} has three steps:
14 *
15 * - **Upload**: Has a {@link OO.ui.SelectFileWidget field} to get the file object.
16 *
17 * - **Information**: Has a {@link OO.ui.FormLayout form} to collect metadata. This can be
18 * extended.
19 *
20 * - **Insert**: Has details on how to use the file that was uploaded.
21 *
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}.
31 *
32 * ## Usage
33 *
34 * See the {@link mw.Upload.Dialog upload dialog}.
35 *
36 * The {@link #event-fileUploaded fileUploaded},
37 * and {@link #event-fileSaved fileSaved} events can
38 * be used to get details of the upload.
39 *
40 * ## Extending
41 *
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.
49 *
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.
56 *
57 * @class
58 * @extends OO.ui.BookletLayout
59 *
60 * @constructor
61 * @param {Object} config Configuration options
62 * @cfg {jQuery} [$overlay] Overlay to use for widgets in the booklet
63 */
64 mw.Upload.BookletLayout = function ( config ) {
65 // Parent constructor
66 mw.Upload.BookletLayout.parent.call( this, config );
67
68 this.$overlay = config.$overlay;
69
70 this.renderUploadForm();
71 this.renderInfoForm();
72 this.renderInsertForm();
73
74 this.addPages( [
75 new OO.ui.PageLayout( 'upload', {
76 scrollable: true,
77 padded: true,
78 content: [ this.uploadForm ]
79 } ),
80 new OO.ui.PageLayout( 'info', {
81 scrollable: true,
82 padded: true,
83 content: [ this.infoForm ]
84 } ),
85 new OO.ui.PageLayout( 'insert', {
86 scrollable: true,
87 padded: true,
88 content: [ this.insertForm ]
89 } )
90 ] );
91 };
92
93 /* Setup */
94
95 OO.inheritClass( mw.Upload.BookletLayout, OO.ui.BookletLayout );
96
97 /* Events */
98
99 /**
100 * The file has finished uploading
101 *
102 * @event fileUploaded
103 */
104
105 /**
106 * The file has been saved to the database
107 *
108 * @event fileSaved
109 */
110
111 /**
112 * The upload form has changed
113 *
114 * @event uploadValid
115 * @param {boolean} isValid The form is valid
116 */
117
118 /**
119 * The info form has changed
120 *
121 * @event infoValid
122 * @param {boolean} isValid The form is valid
123 */
124
125 /* Properties */
126
127 /**
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}.
131 */
132
133 /**
134 * @property {OO.ui.FormLayout} infoForm
135 * The form rendered in the second step to get metadata.
136 * Rendered in {@link #renderInfoForm renderInfoForm}
137 */
138
139 /**
140 * @property {OO.ui.FormLayout} insertForm
141 * The form rendered in the third step to show usage
142 * Rendered in {@link #renderInsertForm renderInsertForm}
143 */
144
145 /* Methods */
146
147 /**
148 * Initialize for a new upload
149 */
150 mw.Upload.BookletLayout.prototype.initialize = function () {
151 this.clear();
152 this.upload = this.createUpload();
153 this.setPage( 'upload' );
154 };
155
156 /**
157 * Create a new upload model
158 *
159 * @protected
160 * @return {mw.Upload} Upload model
161 */
162 mw.Upload.BookletLayout.prototype.createUpload = function () {
163 return new mw.Upload();
164 };
165
166 /* Uploading */
167
168 /**
169 * Uploads the file that was added in the upload form. Uses
170 * {@link #getFile getFile} to get the HTML5
171 * file object.
172 *
173 * @protected
174 * @fires fileUploaded
175 * @return {jQuery.Promise}
176 */
177 mw.Upload.BookletLayout.prototype.uploadFile = function () {
178 var deferred = $.Deferred(),
179 layout = this,
180 file = this.getFile();
181
182 this.filenameWidget.setValue( file.name );
183 this.setPage( 'info' );
184
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();
188
189 this.uploadPromise = this.upload.uploadToStash();
190 this.uploadPromise.then( function () {
191 deferred.resolve();
192 layout.emit( 'fileUploaded' );
193 }, function () {
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(), {
197 recoverable: false
198 } ) );
199 return false;
200 }
201 if ( layout.upload.getState() === mw.Upload.State.WARNING ) {
202 deferred.reject( new OO.ui.Error( layout.upload.getStateDetails(), {
203 recoverable: false
204 } ) );
205 return false;
206 }
207 } );
208
209 // If there is an error in uploading, come back to the upload page
210 deferred.fail( function () {
211 layout.setPage( 'upload' );
212 } );
213
214 return deferred;
215 };
216
217 /**
218 * Saves the stash finalizes upload. Uses
219 * {@link #getFilename getFilename}, and
220 * {@link #getText getText} to get details from
221 * the form.
222 *
223 * @protected
224 * @fires fileSaved
225 * @returns {jQuery.Promise} Rejects the promise with an
226 * {@link OO.ui.Error error}, or resolves if the upload was successful.
227 */
228 mw.Upload.BookletLayout.prototype.saveFile = function () {
229 var layout = this,
230 deferred = $.Deferred();
231
232 this.upload.setFilename( this.getFilename() );
233 this.upload.setText( this.getText() );
234
235 this.uploadPromise.then( function () {
236 layout.upload.finishStashUpload().then( function () {
237 var name;
238
239 // Normalize page name and localise the 'File:' prefix
240 name = new mw.Title( 'File:' + layout.upload.getFilename() ).toString();
241 layout.filenameUsageWidget.setValue( '[[' + name + ']]' );
242 layout.setPage( 'insert' );
243
244 deferred.resolve();
245 layout.emit( 'fileSaved' );
246 }, function () {
247 var stateDetails = layout.upload.getStateDetails();
248
249 if ( layout.upload.getState() === mw.Upload.State.ERROR ) {
250 deferred.reject( new OO.ui.Error( stateDetails, {
251 recoverable: false
252 } ) );
253 return false;
254 }
255
256 if ( layout.upload.getState() === mw.Upload.State.WARNING ) {
257 if ( stateDetails.exists !== undefined ) {
258 deferred.reject( new OO.ui.Error(
259 $( '<p>' ).html(
260 mw.message( 'filepageexists', stateDetails.exists ).parse()
261 ),
262 { recoverable: false }
263 ) );
264 } else if ( stateDetails.duplicate !== undefined ) {
265 deferred.reject( new OO.ui.Error(
266 $( '<p>' ).html(
267 mw.message( 'fileexists', stateDetails.duplicate[ 0 ] ).parse()
268 ),
269 { recoverable: false }
270 ) );
271 } else if ( stateDetails[ 'thumb-name' ] !== undefined ) {
272 deferred.reject( new OO.ui.Error(
273 $( '<p>' ).html(
274 mw.message( 'filename-thumb-name' ).parse()
275 ),
276 { recoverable: false }
277 ) );
278 } else if ( stateDetails[ 'bad-prefix' ] !== undefined ) {
279 deferred.reject( new OO.ui.Error(
280 $( '<p>' ).html(
281 mw.message( 'filename-bad-prefix', stateDetails[ 'bad-prefix' ] ).parse()
282 ),
283 { recoverable: false }
284 ) );
285 } else if ( stateDetails[ 'duplicate-archive' ] !== undefined ) {
286 deferred.reject( new OO.ui.Error(
287 $( '<p>' ).html(
288 mw.message( 'api-error-duplicate-archive', stateDetails[ 'duplicate-archive' ] ).parse()
289 ),
290 { recoverable: false }
291 ) );
292 } else if ( stateDetails.badfilename !== undefined ) {
293 // Change the name if the current name isn't acceptable
294 layout.filenameWidget.setValue( stateDetails.badfilename );
295 deferred.reject( new OO.ui.Error(
296 $( '<p>' ).html(
297 mw.message( 'badfilename', stateDetails.badfilename ).parse()
298 )
299 ) );
300 }
301
302 return false;
303 }
304 } );
305 } );
306
307 return deferred.promise();
308 };
309
310 /* Form renderers */
311
312 /**
313 * Renders and returns the upload form and sets the
314 * {@link #uploadForm uploadForm} property.
315 *
316 * @protected
317 * @fires selectFile
318 * @returns {OO.ui.FormLayout}
319 */
320 mw.Upload.BookletLayout.prototype.renderUploadForm = function () {
321 var fieldset;
322
323 this.selectFileWidget = new OO.ui.SelectFileWidget();
324 fieldset = new OO.ui.FieldsetLayout( { label: mw.msg( 'upload-form-label-select-file' ) } );
325 fieldset.addItems( [ this.selectFileWidget ] );
326 this.uploadForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
327
328 // Validation
329 this.selectFileWidget.on( 'change', this.onUploadFormChange.bind( this ) );
330
331 return this.uploadForm;
332 };
333
334 /**
335 * Handle change events to the upload form
336 *
337 * @protected
338 * @fires uploadValid
339 */
340 mw.Upload.BookletLayout.prototype.onUploadFormChange = function () {
341 this.emit( 'uploadValid', !!this.selectFileWidget.getValue() );
342 };
343
344 /**
345 * Renders and returns the information form for collecting
346 * metadata and sets the {@link #infoForm infoForm}
347 * property.
348 *
349 * @protected
350 * @returns {OO.ui.FormLayout}
351 */
352 mw.Upload.BookletLayout.prototype.renderInfoForm = function () {
353 var fieldset;
354
355 this.filenameWidget = new OO.ui.TextInputWidget( {
356 indicator: 'required',
357 required: true,
358 validate: /.+/
359 } );
360 this.descriptionWidget = new OO.ui.TextInputWidget( {
361 indicator: 'required',
362 required: true,
363 validate: /.+/,
364 multiline: true,
365 autosize: true
366 } );
367
368 fieldset = new OO.ui.FieldsetLayout( {
369 label: mw.msg( 'upload-form-label-infoform-title' )
370 } );
371 fieldset.addItems( [
372 new OO.ui.FieldLayout( this.filenameWidget, {
373 label: mw.msg( 'upload-form-label-infoform-name' ),
374 align: 'top'
375 } ),
376 new OO.ui.FieldLayout( this.descriptionWidget, {
377 label: mw.msg( 'upload-form-label-infoform-description' ),
378 align: 'top'
379 } )
380 ] );
381 this.infoForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
382
383 this.filenameWidget.on( 'change', this.onInfoFormChange.bind( this ) );
384 this.descriptionWidget.on( 'change', this.onInfoFormChange.bind( this ) );
385
386 return this.infoForm;
387 };
388
389 /**
390 * Handle change events to the info form
391 *
392 * @protected
393 * @fires infoValid
394 */
395 mw.Upload.BookletLayout.prototype.onInfoFormChange = function () {
396 var layout = this;
397 $.when(
398 this.filenameWidget.getValidity(),
399 this.descriptionWidget.getValidity()
400 ).done( function () {
401 layout.emit( 'infoValid', true );
402 } ).fail( function () {
403 layout.emit( 'infoValid', false );
404 } );
405 };
406
407 /**
408 * Renders and returns the insert form to show file usage and
409 * sets the {@link #insertForm insertForm} property.
410 *
411 * @protected
412 * @returns {OO.ui.FormLayout}
413 */
414 mw.Upload.BookletLayout.prototype.renderInsertForm = function () {
415 var fieldset;
416
417 this.filenameUsageWidget = new OO.ui.TextInputWidget();
418 fieldset = new OO.ui.FieldsetLayout( {
419 label: mw.msg( 'upload-form-label-usage-title' )
420 } );
421 fieldset.addItems( [
422 new OO.ui.FieldLayout( this.filenameUsageWidget, {
423 label: mw.msg( 'upload-form-label-usage-filename' ),
424 align: 'top'
425 } )
426 ] );
427 this.insertForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
428
429 return this.insertForm;
430 };
431
432 /* Getters */
433
434 /**
435 * Gets the file object from the
436 * {@link #uploadForm upload form}.
437 *
438 * @protected
439 * @returns {File|null}
440 */
441 mw.Upload.BookletLayout.prototype.getFile = function () {
442 return this.selectFileWidget.getValue();
443 };
444
445 /**
446 * Gets the file name from the
447 * {@link #infoForm information form}.
448 *
449 * @protected
450 * @returns {string}
451 */
452 mw.Upload.BookletLayout.prototype.getFilename = function () {
453 return this.filenameWidget.getValue();
454 };
455
456 /**
457 * Gets the page text from the
458 * {@link #infoForm information form}.
459 *
460 * @protected
461 * @returns {string}
462 */
463 mw.Upload.BookletLayout.prototype.getText = function () {
464 return this.descriptionWidget.getValue();
465 };
466
467 /* Setters */
468
469 /**
470 * Clear the values of all fields
471 *
472 * @protected
473 */
474 mw.Upload.BookletLayout.prototype.clear = function () {
475 this.selectFileWidget.setValue( null );
476 this.filenameWidget.setValue( null ).setValidityFlag( true );
477 this.descriptionWidget.setValue( null ).setValidityFlag( true );
478 this.filenameUsageWidget.setValue( null );
479 };
480
481 }( jQuery, mediaWiki ) );