UploadBooklet: Show image thumbnail in both steps
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.ForeignStructuredUpload.BookletLayout.js
1 /*global moment */
2 ( function ( $, mw ) {
3
4 /**
5 * mw.ForeignStructuredUpload.BookletLayout encapsulates the process
6 * of uploading a file to MediaWiki using the mw.ForeignStructuredUpload model.
7 *
8 * var uploadDialog = new mw.Upload.Dialog( {
9 * bookletClass: mw.ForeignStructuredUpload.BookletLayout,
10 * booklet: {
11 * target: 'local'
12 * }
13 * } );
14 * var windowManager = new OO.ui.WindowManager();
15 * $( 'body' ).append( windowManager.$element );
16 * windowManager.addWindows( [ uploadDialog ] );
17 *
18 * @class mw.ForeignStructuredUpload.BookletLayout
19 * @uses mw.ForeignStructuredUpload
20 * @extends mw.Upload.BookletLayout
21 * @cfg {string} [target] Used to choose the target repository.
22 * If nothing is passed, the {@link mw.ForeignUpload#property-target default} is used.
23 */
24 mw.ForeignStructuredUpload.BookletLayout = function ( config ) {
25 config = config || {};
26 // Parent constructor
27 mw.ForeignStructuredUpload.BookletLayout.parent.call( this, config );
28
29 this.target = config.target;
30 };
31
32 /* Setup */
33
34 OO.inheritClass( mw.ForeignStructuredUpload.BookletLayout, mw.Upload.BookletLayout );
35
36 /* Uploading */
37
38 /**
39 * @inheritdoc
40 */
41 mw.ForeignStructuredUpload.BookletLayout.prototype.initialize = function () {
42 var booklet = this;
43 return mw.ForeignStructuredUpload.BookletLayout.parent.prototype.initialize.call( this ).then(
44 function () {
45 // Point the CategorySelector to the right wiki
46 return booklet.upload.getApi().then(
47 function ( api ) {
48 // If this is a ForeignApi, it will have a apiUrl, otherwise we don't need to do anything
49 if ( api.apiUrl ) {
50 // Can't reuse the same object, CategorySelector calls #abort on its mw.Api instance
51 booklet.categoriesWidget.api = new mw.ForeignApi( api.apiUrl );
52 }
53 return $.Deferred().resolve();
54 },
55 function () {
56 return $.Deferred().resolve();
57 }
58 );
59 },
60 function () {
61 return $.Deferred().resolve();
62 }
63 );
64 };
65
66 /**
67 * Returns a {@link mw.ForeignStructuredUpload mw.ForeignStructuredUpload}
68 * with the {@link #cfg-target target} specified in config.
69 *
70 * @protected
71 * @return {mw.Upload}
72 */
73 mw.ForeignStructuredUpload.BookletLayout.prototype.createUpload = function () {
74 return new mw.ForeignStructuredUpload( this.target );
75 };
76
77 /* Form renderers */
78
79 /**
80 * @inheritdoc
81 */
82 mw.ForeignStructuredUpload.BookletLayout.prototype.renderUploadForm = function () {
83 var fieldset, $ownWorkMessage, $notOwnWorkMessage,
84 ownWorkMessage, notOwnWorkMessage, notOwnWorkLocal,
85 validTargets = mw.config.get( 'wgForeignUploadTargets' ),
86 target = this.target || validTargets[ 0 ] || 'local',
87 layout = this;
88
89 // foreign-structured-upload-form-label-own-work-message-local
90 // foreign-structured-upload-form-label-own-work-message-shared
91 ownWorkMessage = mw.message( 'foreign-structured-upload-form-label-own-work-message-' + target );
92 // foreign-structured-upload-form-label-not-own-work-message-local
93 // foreign-structured-upload-form-label-not-own-work-message-shared
94 notOwnWorkMessage = mw.message( 'foreign-structured-upload-form-label-not-own-work-message-' + target );
95 // foreign-structured-upload-form-label-not-own-work-local-local
96 // foreign-structured-upload-form-label-not-own-work-local-shared
97 notOwnWorkLocal = mw.message( 'foreign-structured-upload-form-label-not-own-work-local-' + target );
98
99 if ( !ownWorkMessage.exists() ) {
100 ownWorkMessage = mw.message( 'foreign-structured-upload-form-label-own-work-message-default' );
101 }
102 if ( !notOwnWorkMessage.exists() ) {
103 notOwnWorkMessage = mw.message( 'foreign-structured-upload-form-label-not-own-work-message-default' );
104 }
105 if ( !notOwnWorkLocal.exists() ) {
106 notOwnWorkLocal = mw.message( 'foreign-structured-upload-form-label-not-own-work-local-default' );
107 }
108
109 $ownWorkMessage = $( '<p>' ).append( ownWorkMessage.parseDom() )
110 .addClass( 'mw-foreignStructuredUpload-bookletLayout-license' );
111 $notOwnWorkMessage = $( '<div>' ).append(
112 $( '<p>' ).append( notOwnWorkMessage.parseDom() ),
113 $( '<p>' ).append( notOwnWorkLocal.parseDom() )
114 );
115 $ownWorkMessage.add( $notOwnWorkMessage ).find( 'a' ).attr( 'target', '_blank' );
116
117 this.selectFileWidget = new OO.ui.SelectFileWidget( {
118 showDropTarget: true
119 } );
120 this.messageLabel = new OO.ui.LabelWidget( {
121 label: $notOwnWorkMessage
122 } );
123 this.ownWorkCheckbox = new OO.ui.CheckboxInputWidget().on( 'change', function ( on ) {
124 layout.messageLabel.toggle( !on );
125 } );
126
127 fieldset = new OO.ui.FieldsetLayout();
128 fieldset.addItems( [
129 new OO.ui.FieldLayout( this.selectFileWidget, {
130 align: 'top'
131 } ),
132 new OO.ui.FieldLayout( this.ownWorkCheckbox, {
133 align: 'inline',
134 label: $( '<div>' ).append(
135 $( '<p>' ).text( mw.msg( 'foreign-structured-upload-form-label-own-work' ) ),
136 $ownWorkMessage
137 )
138 } ),
139 new OO.ui.FieldLayout( this.messageLabel, {
140 align: 'top'
141 } )
142 ] );
143 this.uploadForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
144
145 // Validation
146 this.selectFileWidget.on( 'change', this.onUploadFormChange.bind( this ) );
147 this.ownWorkCheckbox.on( 'change', this.onUploadFormChange.bind( this ) );
148
149 this.selectFileWidget.on( 'change', function () {
150 var file = layout.getFile();
151
152 // Set the date to lastModified once we have the file
153 if ( layout.getDateFromLastModified( file ) !== undefined ) {
154 layout.dateWidget.setValue( layout.getDateFromLastModified( file ) );
155 }
156
157 // Check if we have EXIF data and set to that where available
158 layout.getDateFromExif( file ).done( function ( date ) {
159 layout.dateWidget.setValue( date );
160 } );
161
162 layout.updateFilePreview();
163 } );
164
165 return this.uploadForm;
166 };
167
168 /**
169 * @inheritdoc
170 */
171 mw.ForeignStructuredUpload.BookletLayout.prototype.onUploadFormChange = function () {
172 var file = this.selectFileWidget.getValue(),
173 ownWork = this.ownWorkCheckbox.isSelected(),
174 valid = !!file && ownWork;
175 this.emit( 'uploadValid', valid );
176 };
177
178 /**
179 * @inheritdoc
180 */
181 mw.ForeignStructuredUpload.BookletLayout.prototype.renderInfoForm = function () {
182 var fieldset;
183
184 this.filePreview = new OO.ui.Widget( {
185 classes: [ 'mw-upload-bookletLayout-filePreview' ]
186 } );
187 this.filenameWidget = new OO.ui.TextInputWidget( {
188 required: true,
189 validate: /.+/
190 } );
191 this.descriptionWidget = new OO.ui.TextInputWidget( {
192 required: true,
193 validate: /\S+/,
194 multiline: true,
195 autosize: true
196 } );
197 this.categoriesWidget = new mw.widgets.CategorySelector( {
198 // Can't be done here because we don't know the target wiki yet... done in #initialize.
199 // api: new mw.ForeignApi( ... ),
200 $overlay: this.$overlay
201 } );
202 this.dateWidget = new mw.widgets.DateInputWidget( {
203 $overlay: this.$overlay,
204 required: true,
205 mustBeBefore: moment().add( 1, 'day' ).locale( 'en' ).format( 'YYYY-MM-DD' ) // Tomorrow
206 } );
207
208 fieldset = new OO.ui.FieldsetLayout( {
209 label: mw.msg( 'upload-form-label-infoform-title' )
210 } );
211 fieldset.addItems( [
212 new OO.ui.FieldLayout( this.filenameWidget, {
213 label: mw.msg( 'upload-form-label-infoform-name' ),
214 align: 'top',
215 help: mw.msg( 'upload-form-label-infoform-name-tooltip' )
216 } ),
217 new OO.ui.FieldLayout( this.descriptionWidget, {
218 label: mw.msg( 'upload-form-label-infoform-description' ),
219 align: 'top',
220 help: mw.msg( 'upload-form-label-infoform-description-tooltip' )
221 } ),
222 new OO.ui.FieldLayout( this.categoriesWidget, {
223 label: mw.msg( 'foreign-structured-upload-form-label-infoform-categories' ),
224 align: 'top'
225 } ),
226 new OO.ui.FieldLayout( this.dateWidget, {
227 label: mw.msg( 'foreign-structured-upload-form-label-infoform-date' ),
228 align: 'top'
229 } )
230 ] );
231 this.infoForm = new OO.ui.FormLayout( {
232 classes: [ 'mw-upload-bookletLayout-infoForm' ],
233 items: [ this.filePreview, fieldset ]
234 } );
235
236 // Validation
237 this.filenameWidget.on( 'change', this.onInfoFormChange.bind( this ) );
238 this.descriptionWidget.on( 'change', this.onInfoFormChange.bind( this ) );
239 this.dateWidget.on( 'change', this.onInfoFormChange.bind( this ) );
240
241 return this.infoForm;
242 };
243
244 /**
245 * @inheritdoc
246 */
247 mw.ForeignStructuredUpload.BookletLayout.prototype.onInfoFormChange = function () {
248 var layout = this;
249 $.when(
250 this.filenameWidget.getValidity(),
251 this.descriptionWidget.getValidity(),
252 this.dateWidget.getValidity()
253 ).done( function () {
254 layout.emit( 'infoValid', true );
255 } ).fail( function () {
256 layout.emit( 'infoValid', false );
257 } );
258 };
259
260 /* Getters */
261
262 /**
263 * @inheritdoc
264 */
265 mw.ForeignStructuredUpload.BookletLayout.prototype.getText = function () {
266 var language = mw.config.get( 'wgContentLanguage' );
267 this.upload.clearDescriptions();
268 this.upload.addDescription( language, this.descriptionWidget.getValue() );
269 this.upload.setDate( this.dateWidget.getValue() );
270 this.upload.clearCategories();
271 this.upload.addCategories( this.categoriesWidget.getItemsData() );
272 return this.upload.getText();
273 };
274
275 /**
276 * Get original date from EXIF data
277 *
278 * @param {Object} file
279 * @return {jQuery.Promise} Promise resolved with the EXIF date
280 */
281 mw.ForeignStructuredUpload.BookletLayout.prototype.getDateFromExif = function ( file ) {
282 var fileReader,
283 deferred = $.Deferred();
284
285 if ( file && file.type === 'image/jpeg' ) {
286 fileReader = new FileReader();
287 fileReader.onload = function () {
288 var fileStr, arr, i, metadata;
289
290 if ( typeof fileReader.result === 'string' ) {
291 fileStr = fileReader.result;
292 } else {
293 // Array buffer; convert to binary string for the library.
294 arr = new Uint8Array( fileReader.result );
295 fileStr = '';
296 for ( i = 0; i < arr.byteLength; i++ ) {
297 fileStr += String.fromCharCode( arr[ i ] );
298 }
299 }
300
301 try {
302 metadata = mw.libs.jpegmeta( this.result, file.name );
303 } catch ( e ) {
304 metadata = null;
305 }
306
307 if ( metadata !== null && metadata.exif !== undefined && metadata.exif.DateTimeOriginal ) {
308 deferred.resolve( moment( metadata.exif.DateTimeOriginal, 'YYYY:MM:DD' ).format( 'YYYY-MM-DD' ) );
309 } else {
310 deferred.reject();
311 }
312 };
313
314 if ( 'readAsBinaryString' in fileReader ) {
315 fileReader.readAsBinaryString( file );
316 } else if ( 'readAsArrayBuffer' in fileReader ) {
317 fileReader.readAsArrayBuffer( file );
318 } else {
319 // We should never get here
320 deferred.reject();
321 throw new Error( 'Cannot read thumbnail as binary string or array buffer.' );
322 }
323 }
324
325 return deferred.promise();
326 };
327
328 /**
329 * Get last modified date from file
330 *
331 * @param {Object} file
332 * @return {Object} Last modified date from file
333 */
334 mw.ForeignStructuredUpload.BookletLayout.prototype.getDateFromLastModified = function ( file ) {
335 if ( file && file.lastModified ) {
336 return moment( file.lastModified ).format( 'YYYY-MM-DD' );
337 }
338 };
339
340 /* Setters */
341
342 /**
343 * @inheritdoc
344 */
345 mw.ForeignStructuredUpload.BookletLayout.prototype.clear = function () {
346 mw.ForeignStructuredUpload.BookletLayout.parent.prototype.clear.call( this );
347
348 this.ownWorkCheckbox.setSelected( false );
349 this.categoriesWidget.setItemsFromData( [] );
350 this.dateWidget.setValue( '' ).setValidityFlag( true );
351 };
352
353 }( jQuery, mediaWiki ) );