mw.Upload.BookletLayout: Move error checking for uploadToStash to uploadFile
[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 this.uploadPromise = this.upload.uploadToStash();
187 this.uploadPromise.then( function () {
188 deferred.resolve();
189 layout.emit( 'fileUploaded' );
190 } );
191 this.uploadPromise.always( function () {
192 if ( layout.upload.getState() === mw.Upload.State.ERROR ) {
193 deferred.reject( new OO.ui.Error( mw.msg( 'upload-process-error' ) ) );
194 return false;
195 }
196 if ( layout.upload.getState() === mw.Upload.State.WARNING ) {
197 deferred.reject( new OO.ui.Error( mw.msg( 'upload-process-error' ) ) );
198 return false;
199 }
200 } );
201
202 return deferred;
203 };
204
205 /**
206 * Saves the stash finalizes upload. Uses
207 * {@link #getFilename getFilename}, and
208 * {@link #getText getText} to get details from
209 * the form.
210 *
211 * @protected
212 * @fires fileSaved
213 * @returns {jQuery.Promise} Rejects the promise with an
214 * {@link OO.ui.Error error}, or resolves if the upload was successful.
215 */
216 mw.Upload.BookletLayout.prototype.saveFile = function () {
217 var layout = this,
218 deferred = $.Deferred();
219
220 this.upload.setFilename( this.getFilename() );
221 this.upload.setText( this.getText() );
222
223 this.uploadPromise.always( function () {
224 layout.upload.finishStashUpload().always( function () {
225 var name;
226
227 if ( layout.upload.getState() === mw.Upload.State.ERROR ) {
228 deferred.reject( new OO.ui.Error( mw.msg( 'upload-process-error' ) ) );
229 return false;
230 }
231
232 if ( layout.upload.getState() === mw.Upload.State.WARNING ) {
233 deferred.reject( new OO.ui.Error( mw.msg( 'upload-process-warning' ) ) );
234 return false;
235 }
236
237 // Normalize page name and localise the 'File:' prefix
238 name = new mw.Title( 'File:' + layout.upload.getFilename() ).toString();
239 layout.filenameUsageWidget.setValue( '[[' + name + ']]' );
240 layout.setPage( 'insert' );
241
242 deferred.resolve();
243 layout.emit( 'fileSaved' );
244 } );
245 } );
246
247 return deferred.promise();
248 };
249
250 /* Form renderers */
251
252 /**
253 * Renders and returns the upload form and sets the
254 * {@link #uploadForm uploadForm} property.
255 *
256 * @protected
257 * @fires selectFile
258 * @returns {OO.ui.FormLayout}
259 */
260 mw.Upload.BookletLayout.prototype.renderUploadForm = function () {
261 var fieldset;
262
263 this.selectFileWidget = new OO.ui.SelectFileWidget();
264 fieldset = new OO.ui.FieldsetLayout( { label: mw.msg( 'upload-form-label-select-file' ) } );
265 fieldset.addItems( [ this.selectFileWidget ] );
266 this.uploadForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
267
268 // Validation
269 this.selectFileWidget.on( 'change', this.onUploadFormChange.bind( this ) );
270
271 return this.uploadForm;
272 };
273
274 /**
275 * Handle change events to the upload form
276 *
277 * @protected
278 * @fires uploadValid
279 */
280 mw.Upload.BookletLayout.prototype.onUploadFormChange = function () {
281 this.emit( 'uploadValid', !!this.selectFileWidget.getValue() );
282 };
283
284 /**
285 * Renders and returns the information form for collecting
286 * metadata and sets the {@link #infoForm infoForm}
287 * property.
288 *
289 * @protected
290 * @returns {OO.ui.FormLayout}
291 */
292 mw.Upload.BookletLayout.prototype.renderInfoForm = function () {
293 var fieldset;
294
295 this.filenameWidget = new OO.ui.TextInputWidget( {
296 indicator: 'required',
297 required: true,
298 validate: /.+/
299 } );
300 this.descriptionWidget = new OO.ui.TextInputWidget( {
301 indicator: 'required',
302 required: true,
303 validate: /.+/,
304 multiline: true,
305 autosize: true
306 } );
307
308 fieldset = new OO.ui.FieldsetLayout( {
309 label: mw.msg( 'upload-form-label-infoform-title' )
310 } );
311 fieldset.addItems( [
312 new OO.ui.FieldLayout( this.filenameWidget, {
313 label: mw.msg( 'upload-form-label-infoform-name' ),
314 align: 'top'
315 } ),
316 new OO.ui.FieldLayout( this.descriptionWidget, {
317 label: mw.msg( 'upload-form-label-infoform-description' ),
318 align: 'top'
319 } )
320 ] );
321 this.infoForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
322
323 this.filenameWidget.on( 'change', this.onInfoFormChange.bind( this ) );
324 this.descriptionWidget.on( 'change', this.onInfoFormChange.bind( this ) );
325
326 return this.infoForm;
327 };
328
329 /**
330 * Handle change events to the info form
331 *
332 * @protected
333 * @fires infoValid
334 */
335 mw.Upload.BookletLayout.prototype.onInfoFormChange = function () {
336 var layout = this;
337 $.when(
338 this.filenameWidget.getValidity(),
339 this.descriptionWidget.getValidity()
340 ).done( function () {
341 layout.emit( 'infoValid', true );
342 } ).fail( function () {
343 layout.emit( 'infoValid', false );
344 } );
345 };
346
347 /**
348 * Renders and returns the insert form to show file usage and
349 * sets the {@link #insertForm insertForm} property.
350 *
351 * @protected
352 * @returns {OO.ui.FormLayout}
353 */
354 mw.Upload.BookletLayout.prototype.renderInsertForm = function () {
355 var fieldset;
356
357 this.filenameUsageWidget = new OO.ui.TextInputWidget();
358 fieldset = new OO.ui.FieldsetLayout( {
359 label: mw.msg( 'upload-form-label-usage-title' )
360 } );
361 fieldset.addItems( [
362 new OO.ui.FieldLayout( this.filenameUsageWidget, {
363 label: mw.msg( 'upload-form-label-usage-filename' ),
364 align: 'top'
365 } )
366 ] );
367 this.insertForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
368
369 return this.insertForm;
370 };
371
372 /* Getters */
373
374 /**
375 * Gets the file object from the
376 * {@link #uploadForm upload form}.
377 *
378 * @protected
379 * @returns {File|null}
380 */
381 mw.Upload.BookletLayout.prototype.getFile = function () {
382 return this.selectFileWidget.getValue();
383 };
384
385 /**
386 * Gets the file name from the
387 * {@link #infoForm information form}.
388 *
389 * @protected
390 * @returns {string}
391 */
392 mw.Upload.BookletLayout.prototype.getFilename = function () {
393 return this.filenameWidget.getValue();
394 };
395
396 /**
397 * Gets the page text from the
398 * {@link #infoForm information form}.
399 *
400 * @protected
401 * @returns {string}
402 */
403 mw.Upload.BookletLayout.prototype.getText = function () {
404 return this.descriptionWidget.getValue();
405 };
406
407 /* Setters */
408
409 /**
410 * Clear the values of all fields
411 *
412 * @protected
413 */
414 mw.Upload.BookletLayout.prototype.clear = function () {
415 this.selectFileWidget.setValue( null );
416 this.filenameWidget.setValue( null ).setValidityFlag( true );
417 this.descriptionWidget.setValue( null ).setValidityFlag( true );
418 this.filenameUsageWidget.setValue( null );
419 };
420
421 }( jQuery, mediaWiki ) );