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 * Set the text of the file page, to be created on file upload.
68 * @param {string} text
70 UP.setText = function ( text ) {
75 * Set the filename, to be finalized on upload.
77 * @param {string} filename
79 UP.setFilename = function ( filename ) {
80 this.filename = filename;
84 * Sets the filename based on the filename as it was on the upload.
86 UP.setFilenameFromFile = function () {
87 var file = this.getFile();
91 if ( file.nodeType && file.nodeType === Node.ELEMENT_NODE ) {
92 // File input element, use getBasename to cut out the path
93 this.setFilename( this.getBasename( file.value ) );
94 } else if ( file.name ) {
95 // HTML5 FileAPI File object, but use getBasename to be safe
96 this.setFilename( this.getBasename( file.name ) );
98 // If we ever implement uploading files from clipboard, they might not have a name
99 this.setFilename( '?' );
104 * Set the file to be uploaded.
106 * @param {HTMLInputElement|File} file
108 UP.setFile = function ( file ) {
113 * Set whether the file should be watchlisted after upload.
115 * @param {boolean} watchlist
117 UP.setWatchlist = function ( watchlist ) {
118 this.watchlist = watchlist;
122 * Set the edit comment for the upload.
124 * @param {string} comment
126 UP.setComment = function ( comment ) {
127 this.comment = comment;
131 * Get the text of the file page, to be created on file upload.
135 UP.getText = function () {
140 * Get the filename, to be finalized on upload.
144 UP.getFilename = function () {
145 return this.filename;
149 * Get the file being uploaded.
151 * @return {HTMLInputElement|File}
153 UP.getFile = function () {
158 * Get the boolean for whether the file will be watchlisted after upload.
162 UP.getWatchlist = function () {
163 return this.watchlist;
167 * Get the current value of the edit comment for the upload.
171 UP.getComment = function () {
176 * Gets the base filename from a path name.
178 * @param {string} path
181 UP.getBasename = function ( path ) {
182 if ( path === undefined || path === null ) {
186 // Find the index of the last path separator in the
187 // path, and add 1. Then, take the entire string after that.
190 path.lastIndexOf( '/' ),
191 path.lastIndexOf( '\\' )
197 * Sets the state and state details (if any) of the upload.
199 * @param {mw.Upload.State} state
200 * @param {Object} stateDetails
202 UP.setState = function ( state, stateDetails ) {
204 this.stateDetails = stateDetails;
208 * Gets the state of the upload.
210 * @return {mw.Upload.State}
212 UP.getState = function () {
217 * Gets details of the current state.
221 UP.getStateDetails = function () {
222 return this.stateDetails;
226 * Get the imageinfo object for the finished upload.
227 * Only available once the upload is finished! Don't try to get it
230 * @return {Object|undefined}
232 UP.getImageInfo = function () {
233 return this.imageinfo;
237 * Upload the file directly.
239 * @return {jQuery.Promise}
241 UP.upload = function () {
244 if ( !this.getFile() ) {
245 return $.Deferred().reject( 'No file to upload. Call setFile to add one.' );
248 if ( !this.getFilename() ) {
249 return $.Deferred().reject( 'No filename set. Call setFilename to add one.' );
252 this.setState( Upload.State.UPLOADING );
254 return this.api.upload( this.getFile(), {
255 watchlist: ( this.getWatchlist() ) ? 1 : undefined,
256 comment: this.getComment(),
257 filename: this.getFilename(),
259 } ).then( function ( result ) {
260 upload.setState( Upload.State.UPLOADED );
261 upload.imageinfo = result.upload.imageinfo;
263 }, function ( errorCode, result ) {
264 if ( result && result.upload && result.upload.warnings ) {
265 upload.setState( Upload.State.WARNING, result );
267 upload.setState( Upload.State.ERROR, result );
269 return $.Deferred().reject( errorCode, result );
274 * Upload the file to the stash to be completed later.
276 * @return {jQuery.Promise}
278 UP.uploadToStash = function () {
281 if ( !this.getFile() ) {
282 return $.Deferred().reject( 'No file to upload. Call setFile to add one.' );
285 if ( !this.getFilename() ) {
286 this.setFilenameFromFile();
289 this.setState( Upload.State.UPLOADING );
291 this.stashPromise = this.api.uploadToStash( this.getFile(), {
292 filename: this.getFilename()
293 } ).then( function ( finishStash ) {
294 upload.setState( Upload.State.STASHED );
296 }, function ( errorCode, result ) {
297 if ( result && result.upload && result.upload.warnings ) {
298 upload.setState( Upload.State.WARNING, result );
300 upload.setState( Upload.State.ERROR, result );
302 return $.Deferred().reject( errorCode, result );
305 return this.stashPromise;
309 * Finish a stash upload.
311 * @return {jQuery.Promise}
313 UP.finishStashUpload = function () {
316 if ( !this.stashPromise ) {
317 return $.Deferred().reject( 'This upload has not been stashed, please upload it to the stash first.' );
320 return this.stashPromise.then( function ( finishStash ) {
321 upload.setState( Upload.State.UPLOADING );
323 return finishStash( {
324 bucket: upload.bucket, // Automatically ignored if undefined
325 watchlist: ( upload.getWatchlist() ) ? 1 : undefined,
326 comment: upload.getComment(),
327 filename: upload.getFilename(),
328 text: upload.getText()
329 } ).then( function ( result ) {
330 upload.setState( Upload.State.UPLOADED );
331 upload.imageinfo = result.upload.imageinfo;
333 }, function ( errorCode, result ) {
334 if ( result && result.upload && result.upload.warnings ) {
335 upload.setState( Upload.State.WARNING, result );
337 upload.setState( Upload.State.ERROR, result );
339 return $.Deferred().reject( errorCode, result );
345 * @enum mw.Upload.State
346 * State of uploads represented in simple terms.
349 /** Upload not yet started */
352 /** Upload finished, but there was a warning */
355 /** Upload finished, but there was an error */
358 /** Upload in progress */
361 /** Upload finished, but not published, call #finishStashUpload */
364 /** Upload finished and published */
369 }( mediaWiki, jQuery ) );