7 * Used to represent an upload in progress on the frontend.
8 * Most of the functionality is implemented in mw.Api.plugin.upload,
9 * but this model class will tie it together as well as let you perform
10 * actions in a logical way.
14 * var file = new OO.ui.SelectFileWidget(),
15 * button = new OO.ui.ButtonWidget( { label: 'Save' } ),
16 * upload = new mw.Upload;
18 * button.on( 'click', function () {
19 * upload.setFile( file.getValue() );
20 * upload.setFilename( file.getValue().name );
24 * $( 'body' ).append( file.$element, button.$element );
26 * You can also choose to {@link #uploadToStash stash the upload} and
27 * {@link #finishStashUpload finalize} it later:
29 * var file, // Some file object
30 * upload = new mw.Upload,
31 * stashPromise = $.Deferred();
33 * upload.setFile( file );
34 * upload.uploadToStash().then( function () {
35 * stashPromise.resolve();
38 * stashPromise.then( function () {
39 * upload.setFilename( 'foo' );
40 * upload.setText( 'bar' );
41 * upload.finishStashUpload().then( function () {
42 * console.log( 'Done!' );
47 * @param {Object|mw.Api} [apiconfig] A mw.Api object (or subclass), or configuration
48 * to pass to the constructor of mw.Api.
50 function Upload( apiconfig
) {
51 this.api
= ( apiconfig
instanceof mw
.Api
) ? apiconfig
: new mw
.Api( apiconfig
);
53 this.watchlist
= false;
58 this.setState( Upload
.State
.NEW
);
60 this.imageinfo
= undefined;
63 UP
= Upload
.prototype;
66 * Get the mw.Api instance used by this Upload object.
68 * @return {jQuery.Promise}
69 * @return {Function} return.done
70 * @return {mw.Api} return.done.api
72 UP
.getApi = function () {
73 return $.Deferred().resolve( this.api
).promise();
77 * Set the text of the file page, to be created on file upload.
79 * @param {string} text
81 UP
.setText = function ( text
) {
86 * Set the filename, to be finalized on upload.
88 * @param {string} filename
90 UP
.setFilename = function ( filename
) {
91 this.filename
= filename
;
95 * Sets the filename based on the filename as it was on the upload.
97 UP
.setFilenameFromFile = function () {
98 var file
= this.getFile();
102 if ( file
.nodeType
&& file
.nodeType
=== Node
.ELEMENT_NODE
) {
103 // File input element, use getBasename to cut out the path
104 this.setFilename( this.getBasename( file
.value
) );
105 } else if ( file
.name
) {
106 // HTML5 FileAPI File object, but use getBasename to be safe
107 this.setFilename( this.getBasename( file
.name
) );
109 // If we ever implement uploading files from clipboard, they might not have a name
110 this.setFilename( '?' );
115 * Set the file to be uploaded.
117 * @param {HTMLInputElement|File|Blob} file
119 UP
.setFile = function ( file
) {
124 * Set whether the file should be watchlisted after upload.
126 * @param {boolean} watchlist
128 UP
.setWatchlist = function ( watchlist
) {
129 this.watchlist
= watchlist
;
133 * Set the edit comment for the upload.
135 * @param {string} comment
137 UP
.setComment = function ( comment
) {
138 this.comment
= comment
;
142 * Get the text of the file page, to be created on file upload.
146 UP
.getText = function () {
151 * Get the filename, to be finalized on upload.
155 UP
.getFilename = function () {
156 return this.filename
;
160 * Get the file being uploaded.
162 * @return {HTMLInputElement|File|Blob}
164 UP
.getFile = function () {
169 * Get the boolean for whether the file will be watchlisted after upload.
173 UP
.getWatchlist = function () {
174 return this.watchlist
;
178 * Get the current value of the edit comment for the upload.
182 UP
.getComment = function () {
187 * Gets the base filename from a path name.
189 * @param {string} path
192 UP
.getBasename = function ( path
) {
193 if ( path
=== undefined || path
=== null ) {
197 // Find the index of the last path separator in the
198 // path, and add 1. Then, take the entire string after that.
201 path
.lastIndexOf( '/' ),
202 path
.lastIndexOf( '\\' )
208 * Sets the state and state details (if any) of the upload.
210 * @param {mw.Upload.State} state
211 * @param {Object} stateDetails
213 UP
.setState = function ( state
, stateDetails
) {
215 this.stateDetails
= stateDetails
;
219 * Gets the state of the upload.
221 * @return {mw.Upload.State}
223 UP
.getState = function () {
228 * Gets details of the current state.
232 UP
.getStateDetails = function () {
233 return this.stateDetails
;
237 * Get the imageinfo object for the finished upload.
238 * Only available once the upload is finished! Don't try to get it
241 * @return {Object|undefined}
243 UP
.getImageInfo = function () {
244 return this.imageinfo
;
248 * Upload the file directly.
250 * @return {jQuery.Promise}
252 UP
.upload = function () {
255 if ( !this.getFile() ) {
256 return $.Deferred().reject( 'No file to upload. Call setFile to add one.' );
259 if ( !this.getFilename() ) {
260 return $.Deferred().reject( 'No filename set. Call setFilename to add one.' );
263 this.setState( Upload
.State
.UPLOADING
);
265 return this.api
.upload( this.getFile(), {
266 watchlist
: ( this.getWatchlist() ) ? 1 : undefined,
267 comment
: this.getComment(),
268 filename
: this.getFilename(),
270 } ).then( function ( result
) {
271 upload
.setState( Upload
.State
.UPLOADED
);
272 upload
.imageinfo
= result
.upload
.imageinfo
;
274 }, function ( errorCode
, result
) {
275 if ( result
&& result
.upload
&& result
.upload
.warnings
) {
276 upload
.setState( Upload
.State
.WARNING
, result
);
278 upload
.setState( Upload
.State
.ERROR
, result
);
280 return $.Deferred().reject( errorCode
, result
);
285 * Upload the file to the stash to be completed later.
287 * @return {jQuery.Promise}
289 UP
.uploadToStash = function () {
292 if ( !this.getFile() ) {
293 return $.Deferred().reject( 'No file to upload. Call setFile to add one.' );
296 if ( !this.getFilename() ) {
297 this.setFilenameFromFile();
300 this.setState( Upload
.State
.UPLOADING
);
302 this.stashPromise
= this.api
.uploadToStash( this.getFile(), {
303 filename
: this.getFilename()
304 } ).then( function ( finishStash
) {
305 upload
.setState( Upload
.State
.STASHED
);
307 }, function ( errorCode
, result
) {
308 if ( result
&& result
.upload
&& result
.upload
.warnings
) {
309 upload
.setState( Upload
.State
.WARNING
, result
);
311 upload
.setState( Upload
.State
.ERROR
, result
);
313 return $.Deferred().reject( errorCode
, result
);
316 return this.stashPromise
;
320 * Finish a stash upload.
322 * @return {jQuery.Promise}
324 UP
.finishStashUpload = function () {
327 if ( !this.stashPromise
) {
328 return $.Deferred().reject( 'This upload has not been stashed, please upload it to the stash first.' );
331 return this.stashPromise
.then( function ( finishStash
) {
332 upload
.setState( Upload
.State
.UPLOADING
);
334 return finishStash( {
335 watchlist
: ( upload
.getWatchlist() ) ? 1 : undefined,
336 comment
: upload
.getComment(),
337 filename
: upload
.getFilename(),
338 text
: upload
.getText()
339 } ).then( function ( result
) {
340 upload
.setState( Upload
.State
.UPLOADED
);
341 upload
.imageinfo
= result
.upload
.imageinfo
;
343 }, function ( errorCode
, result
) {
344 if ( result
&& result
.upload
&& result
.upload
.warnings
) {
345 upload
.setState( Upload
.State
.WARNING
, result
);
347 upload
.setState( Upload
.State
.ERROR
, result
);
349 return $.Deferred().reject( errorCode
, result
);
355 * @enum mw.Upload.State
356 * State of uploads represented in simple terms.
359 /** Upload not yet started */
362 /** Upload finished, but there was a warning */
365 /** Upload finished, but there was an error */
368 /** Upload in progress */
371 /** Upload finished, but not published, call #finishStashUpload */
374 /** Upload finished and published */
379 }( mediaWiki
, jQuery
) );