b8425456a31110e103d93fba1b74aced99640da5
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.
13 * @param {Object} apiconfig Passed to the constructor of mw.Api.
15 function Upload( apiconfig
) {
16 this.api
= new mw
.Api( apiconfig
);
18 this.watchlist
= false;
23 this.state
= Upload
.State
.NEW
;
25 this.imageinfo
= undefined;
28 UP
= Upload
.prototype;
31 * Set the text of the file page, to be created on file upload.
32 * @param {string} text
34 UP
.setText = function ( text
) {
39 * Set the filename, to be finalized on upload.
40 * @param {string} filename
42 UP
.setFilename = function ( filename
) {
43 this.filename
= filename
;
47 * Sets the filename based on the filename as it was on the upload.
49 UP
.setFilenameFromFile = function () {
50 if ( this.file
.nodeType
&& this.file
.nodeType
=== Node
.ELEMENT_NODE
) {
51 // File input element, use getBasename to cut out the path
52 this.setFilename( this.getBasename( this.file
.value
) );
53 } else if ( this.file
.name
&& this.file
.lastModified
) {
54 // HTML5 FileAPI File object, but use getBasename to be safe
55 this.setFilename( this.getBasename( this.file
.name
) );
60 * Set the file to be uploaded.
61 * @param {HTMLInputElement|File} file
63 UP
.setFile = function ( file
) {
68 * Set whether the file should be watchlisted after upload.
69 * @param {boolean} watchlist
71 UP
.setWatchlist = function ( watchlist
) {
72 this.watchlist
= watchlist
;
76 * Set the edit comment for the upload.
77 * @param {string} comment
79 UP
.setComment = function ( comment
) {
80 this.comment
= comment
;
84 * Get the text of the file page, to be created on file upload.
87 UP
.getText = function () {
92 * Get the filename, to be finalized on upload.
95 UP
.getFilename = function () {
100 * Get the file being uploaded.
101 * @return {HTMLInputElement|File}
103 UP
.getFile = function () {
108 * Get the boolean for whether the file will be watchlisted after upload.
111 UP
.getWatchlist = function () {
112 return this.watchlist
;
116 * Get the current value of the edit comment for the upload.
119 UP
.getComment = function () {
124 * Gets the base filename from a path name.
125 * @param {string} path
128 UP
.getBasename = function ( path
) {
129 if ( path
=== undefined || path
=== null ) {
133 // Find the index of the last path separator in the
134 // path, and add 1. Then, take the entire string after that.
137 path
.lastIndexOf( '/' ),
138 path
.lastIndexOf( '\\' )
144 * Gets the state of the upload.
145 * @return {mw.Upload.State}
147 UP
.getState = function () {
152 * Get the imageinfo object for the finished upload.
153 * Only available once the upload is finished! Don't try to get it
155 * @return {Object|undefined}
157 UP
.getImageInfo = function () {
158 return this.imageinfo
;
162 * Upload the file directly.
163 * @return {jQuery.Promise}
165 UP
.upload = function () {
169 return $.Deferred().reject( 'No file to upload. Call setFile to add one.' );
172 if ( !this.filename
) {
173 return $.Deferred().reject( 'No filename set. Call setFilename to add one.' );
176 this.state
= Upload
.State
.UPLOADING
;
178 return this.api
.upload( this.file
, {
179 watchlist
: ( this.watchlist
=== true ) ? 1 : undefined,
180 comment
: this.comment
,
181 filename
: this.filename
,
183 } ).then( function ( result
) {
184 upload
.state
= Upload
.State
.UPLOADED
;
185 upload
.imageinfo
= result
.upload
.imageinfo
;
188 upload
.state
= Upload
.State
.ERROR
;
193 * Upload the file to the stash to be completed later.
194 * @return {jQuery.Promise}
196 UP
.uploadToStash = function () {
200 return $.Deferred().reject( 'No file to upload. Call setFile to add one.' );
203 if ( !this.filename
) {
204 this.setFilenameFromFile();
207 this.state
= Upload
.State
.UPLOADING
;
209 this.stashPromise
= this.api
.uploadToStash( this.file
, {
210 filename
: this.filename
211 } ).then( function ( finishStash
) {
212 upload
.state
= Upload
.State
.STASHED
;
215 upload
.state
= Upload
.State
.ERROR
;
218 return this.stashPromise
;
222 * Finish a stash upload.
223 * @return {jQuery.Promise}
225 UP
.finishStashUpload = function () {
228 if ( !this.stashPromise
) {
229 return $.Deferred().reject( 'This upload has not been stashed, please upload it to the stash first.' );
232 return this.stashPromise
.then( function ( finishStash
) {
233 upload
.state
= Upload
.State
.UPLOADING
;
235 return finishStash( {
236 watchlist
: ( upload
.watchlist
=== true ) ? 1 : undefined,
237 comment
: upload
.getComment(),
238 filename
: upload
.getFilename(),
239 text
: upload
.getText()
240 } ).then( function () {
241 upload
.state
= Upload
.State
.UPLOADED
;
243 upload
.state
= Upload
.State
.ERROR
;
249 * @enum mw.Upload.State
250 * State of uploads represented in simple terms.
253 /** Upload not yet started */
256 /** Upload finished, but there was a warning */
259 /** Upload finished, but there was an error */
262 /** Upload in progress */
265 /** Upload finished, but not published, call #finishStashUpload */
268 /** Upload finished and published */
273 }( mediaWiki
, jQuery
) );