Merge "Remove PHP < 5.3 support from maintenance/checkSyntax.php"
[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 classes: [ 'mw-foreignStructuredUploa-bookletLayout-small-notice' ],
216 notices: [ mw.msg( 'upload-form-label-infoform-name-tooltip' ) ]
217 } ),
218 new OO.ui.FieldLayout( this.descriptionWidget, {
219 label: mw.msg( 'upload-form-label-infoform-description' ),
220 align: 'top',
221 classes: [ 'mw-foreignStructuredUploa-bookletLayout-small-notice' ],
222 notices: [ mw.msg( 'upload-form-label-infoform-description-tooltip' ) ]
223 } ),
224 new OO.ui.FieldLayout( this.categoriesWidget, {
225 label: mw.msg( 'foreign-structured-upload-form-label-infoform-categories' ),
226 align: 'top'
227 } ),
228 new OO.ui.FieldLayout( this.dateWidget, {
229 label: mw.msg( 'foreign-structured-upload-form-label-infoform-date' ),
230 align: 'top'
231 } )
232 ] );
233 this.infoForm = new OO.ui.FormLayout( {
234 classes: [ 'mw-upload-bookletLayout-infoForm' ],
235 items: [ this.filePreview, fieldset ]
236 } );
237
238 // Validation
239 this.filenameWidget.on( 'change', this.onInfoFormChange.bind( this ) );
240 this.descriptionWidget.on( 'change', this.onInfoFormChange.bind( this ) );
241 this.dateWidget.on( 'change', this.onInfoFormChange.bind( this ) );
242
243 return this.infoForm;
244 };
245
246 /**
247 * @inheritdoc
248 */
249 mw.ForeignStructuredUpload.BookletLayout.prototype.onInfoFormChange = function () {
250 var layout = this;
251 $.when(
252 this.filenameWidget.getValidity(),
253 this.descriptionWidget.getValidity(),
254 this.dateWidget.getValidity()
255 ).done( function () {
256 layout.emit( 'infoValid', true );
257 } ).fail( function () {
258 layout.emit( 'infoValid', false );
259 } );
260 };
261
262 /* Getters */
263
264 /**
265 * @inheritdoc
266 */
267 mw.ForeignStructuredUpload.BookletLayout.prototype.getText = function () {
268 var language = mw.config.get( 'wgContentLanguage' );
269 this.upload.clearDescriptions();
270 this.upload.addDescription( language, this.descriptionWidget.getValue() );
271 this.upload.setDate( this.dateWidget.getValue() );
272 this.upload.clearCategories();
273 this.upload.addCategories( this.categoriesWidget.getItemsData() );
274 return this.upload.getText();
275 };
276
277 /**
278 * Get original date from EXIF data
279 *
280 * @param {Object} file
281 * @return {jQuery.Promise} Promise resolved with the EXIF date
282 */
283 mw.ForeignStructuredUpload.BookletLayout.prototype.getDateFromExif = function ( file ) {
284 var fileReader,
285 deferred = $.Deferred();
286
287 if ( file && file.type === 'image/jpeg' ) {
288 fileReader = new FileReader();
289 fileReader.onload = function () {
290 var fileStr, arr, i, metadata;
291
292 if ( typeof fileReader.result === 'string' ) {
293 fileStr = fileReader.result;
294 } else {
295 // Array buffer; convert to binary string for the library.
296 arr = new Uint8Array( fileReader.result );
297 fileStr = '';
298 for ( i = 0; i < arr.byteLength; i++ ) {
299 fileStr += String.fromCharCode( arr[ i ] );
300 }
301 }
302
303 try {
304 metadata = mw.libs.jpegmeta( this.result, file.name );
305 } catch ( e ) {
306 metadata = null;
307 }
308
309 if ( metadata !== null && metadata.exif !== undefined && metadata.exif.DateTimeOriginal ) {
310 deferred.resolve( moment( metadata.exif.DateTimeOriginal, 'YYYY:MM:DD' ).format( 'YYYY-MM-DD' ) );
311 } else {
312 deferred.reject();
313 }
314 };
315
316 if ( 'readAsBinaryString' in fileReader ) {
317 fileReader.readAsBinaryString( file );
318 } else if ( 'readAsArrayBuffer' in fileReader ) {
319 fileReader.readAsArrayBuffer( file );
320 } else {
321 // We should never get here
322 deferred.reject();
323 throw new Error( 'Cannot read thumbnail as binary string or array buffer.' );
324 }
325 }
326
327 return deferred.promise();
328 };
329
330 /**
331 * Get last modified date from file
332 *
333 * @param {Object} file
334 * @return {Object} Last modified date from file
335 */
336 mw.ForeignStructuredUpload.BookletLayout.prototype.getDateFromLastModified = function ( file ) {
337 if ( file && file.lastModified ) {
338 return moment( file.lastModified ).format( 'YYYY-MM-DD' );
339 }
340 };
341
342 /* Setters */
343
344 /**
345 * @inheritdoc
346 */
347 mw.ForeignStructuredUpload.BookletLayout.prototype.clear = function () {
348 mw.ForeignStructuredUpload.BookletLayout.parent.prototype.clear.call( this );
349
350 this.ownWorkCheckbox.setSelected( false );
351 this.categoriesWidget.setItemsFromData( [] );
352 this.dateWidget.setValue( '' ).setValidityFlag( true );
353 };
354
355 }( jQuery, mediaWiki ) );