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.state
= 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.
67 * @param {string} text
69 UP
.setText = function ( text
) {
74 * Set the filename, to be finalized on upload.
75 * @param {string} filename
77 UP
.setFilename = function ( filename
) {
78 this.filename
= filename
;
82 * Sets the filename based on the filename as it was on the upload.
84 UP
.setFilenameFromFile = function () {
85 if ( this.file
.nodeType
&& this.file
.nodeType
=== Node
.ELEMENT_NODE
) {
86 // File input element, use getBasename to cut out the path
87 this.setFilename( this.getBasename( this.file
.value
) );
88 } else if ( this.file
.name
&& this.file
.lastModified
) {
89 // HTML5 FileAPI File object, but use getBasename to be safe
90 this.setFilename( this.getBasename( this.file
.name
) );
95 * Set the file to be uploaded.
96 * @param {HTMLInputElement|File} file
98 UP
.setFile = function ( file
) {
103 * Set whether the file should be watchlisted after upload.
104 * @param {boolean} watchlist
106 UP
.setWatchlist = function ( watchlist
) {
107 this.watchlist
= watchlist
;
111 * Set the edit comment for the upload.
112 * @param {string} comment
114 UP
.setComment = function ( comment
) {
115 this.comment
= comment
;
119 * Get the text of the file page, to be created on file upload.
122 UP
.getText = function () {
127 * Get the filename, to be finalized on upload.
130 UP
.getFilename = function () {
131 return this.filename
;
135 * Get the file being uploaded.
136 * @return {HTMLInputElement|File}
138 UP
.getFile = function () {
143 * Get the boolean for whether the file will be watchlisted after upload.
146 UP
.getWatchlist = function () {
147 return this.watchlist
;
151 * Get the current value of the edit comment for the upload.
154 UP
.getComment = function () {
159 * Gets the base filename from a path name.
160 * @param {string} path
163 UP
.getBasename = function ( path
) {
164 if ( path
=== undefined || path
=== null ) {
168 // Find the index of the last path separator in the
169 // path, and add 1. Then, take the entire string after that.
172 path
.lastIndexOf( '/' ),
173 path
.lastIndexOf( '\\' )
179 * Gets the state of the upload.
180 * @return {mw.Upload.State}
182 UP
.getState = function () {
187 * Get the imageinfo object for the finished upload.
188 * Only available once the upload is finished! Don't try to get it
190 * @return {Object|undefined}
192 UP
.getImageInfo = function () {
193 return this.imageinfo
;
197 * Upload the file directly.
198 * @return {jQuery.Promise}
200 UP
.upload = function () {
204 return $.Deferred().reject( 'No file to upload. Call setFile to add one.' );
207 if ( !this.filename
) {
208 return $.Deferred().reject( 'No filename set. Call setFilename to add one.' );
211 this.state
= Upload
.State
.UPLOADING
;
213 return this.api
.upload( this.file
, {
214 watchlist
: ( this.watchlist
=== true ) ? 1 : undefined,
215 comment
: this.comment
,
216 filename
: this.filename
,
218 } ).then( function ( result
) {
219 upload
.state
= Upload
.State
.UPLOADED
;
220 upload
.imageinfo
= result
.upload
.imageinfo
;
223 upload
.state
= Upload
.State
.ERROR
;
228 * Upload the file to the stash to be completed later.
229 * @return {jQuery.Promise}
231 UP
.uploadToStash = function () {
235 return $.Deferred().reject( 'No file to upload. Call setFile to add one.' );
238 if ( !this.filename
) {
239 this.setFilenameFromFile();
242 this.state
= Upload
.State
.UPLOADING
;
244 this.stashPromise
= this.api
.uploadToStash( this.file
, {
245 filename
: this.filename
246 } ).then( function ( finishStash
) {
247 upload
.state
= Upload
.State
.STASHED
;
250 upload
.state
= Upload
.State
.ERROR
;
253 return this.stashPromise
;
257 * Finish a stash upload.
258 * @return {jQuery.Promise}
260 UP
.finishStashUpload = function () {
263 if ( !this.stashPromise
) {
264 return $.Deferred().reject( 'This upload has not been stashed, please upload it to the stash first.' );
267 return this.stashPromise
.then( function ( finishStash
) {
268 upload
.state
= Upload
.State
.UPLOADING
;
270 return finishStash( {
271 watchlist
: ( upload
.watchlist
=== true ) ? 1 : undefined,
272 comment
: upload
.getComment(),
273 filename
: upload
.getFilename(),
274 text
: upload
.getText()
275 } ).then( function () {
276 upload
.state
= Upload
.State
.UPLOADED
;
278 upload
.state
= Upload
.State
.ERROR
;
284 * @enum mw.Upload.State
285 * State of uploads represented in simple terms.
288 /** Upload not yet started */
291 /** Upload finished, but there was a warning */
294 /** Upload finished, but there was an error */
297 /** Upload in progress */
300 /** Upload finished, but not published, call #finishStashUpload */
303 /** Upload finished and published */
308 }( mediaWiki
, jQuery
) );