197ea6fc6a96fdeeee5274a6d485295ec04c0af9
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
;
26 UP
= Upload
.prototype;
29 * Set the text of the file page, to be created on file upload.
30 * @param {string} text
32 UP
.setText = function ( text
) {
37 * Set the filename, to be finalized on upload.
38 * @param {string} filename
40 UP
.setFilename = function ( filename
) {
41 this.filename
= filename
;
45 * Sets the filename based on the filename as it was on the upload.
47 UP
.setFilenameFromFile = function () {
48 if ( this.file
.nodeType
&& this.file
.nodeType
=== Node
.ELEMENT_NODE
) {
49 // File input element, use getBasename to cut out the path
50 this.setFilename( this.getBasename( this.file
.value
) );
51 } else if ( this.file
.name
&& this.file
.lastModified
) {
52 // HTML5 FileAPI File object, but use getBasename to be safe
53 this.setFilename( this.getBasename( this.file
.name
) );
58 * Set the file to be uploaded.
59 * @param {HTMLInputElement|File} file
61 UP
.setFile = function ( file
) {
66 * Set whether the file should be watchlisted after upload.
67 * @param {boolean} watchlist
69 UP
.setWatchlist = function ( watchlist
) {
70 this.watchlist
= watchlist
;
74 * Set the edit comment for the upload.
75 * @param {string} comment
77 UP
.setComment = function ( comment
) {
78 this.comment
= comment
;
82 * Get the text of the file page, to be created on file upload.
85 UP
.getText = function () {
90 * Get the filename, to be finalized on upload.
93 UP
.getFilename = function () {
98 * Get the file being uploaded.
99 * @return {HTMLInputElement|File}
101 UP
.getFile = function () {
106 * Get the boolean for whether the file will be watchlisted after upload.
109 UP
.getWatchlist = function () {
110 return this.watchlist
;
114 * Get the current value of the edit comment for the upload.
117 UP
.getComment = function () {
122 * Gets the base filename from a path name.
123 * @param {string} path
126 UP
.getBasename = function ( path
) {
127 if ( path
=== undefined || path
=== null ) {
131 // Find the index of the last path separator in the
132 // path, and add 1. Then, take the entire string after that.
135 path
.lastIndexOf( '/' ),
136 path
.lastIndexOf( '\\' )
142 * Gets the state of the upload.
143 * @return {mw.Upload.State}
145 UP
.getState = function () {
150 * Upload the file directly.
151 * @return {jQuery.Promise}
153 UP
.upload = function () {
157 return $.Deferred().reject( 'No file to upload. Call setFile to add one.' );
160 if ( !this.filename
) {
161 return $.Deferred().reject( 'No filename set. Call setFilename to add one.' );
164 this.state
= Upload
.State
.UPLOADING
;
166 return this.api
.upload( this.file
, {
167 watchlist
: ( this.watchlist
=== true ) ? 1 : undefined,
168 comment
: this.comment
,
169 filename
: this.filename
,
171 } ).then( function ( result
) {
172 upload
.state
= Upload
.State
.UPLOADED
;
175 upload
.state
= Upload
.State
.ERROR
;
180 * Upload the file to the stash to be completed later.
181 * @return {jQuery.Promise}
183 UP
.uploadToStash = function () {
187 return $.Deferred().reject( 'No file to upload. Call setFile to add one.' );
190 if ( !this.filename
) {
191 this.setFilenameFromFile();
194 this.state
= Upload
.State
.UPLOADING
;
196 this.stashPromise
= this.api
.uploadToStash( this.file
, {
197 filename
: this.filename
198 } ).then( function ( finishStash
) {
199 upload
.state
= Upload
.State
.STASHED
;
202 upload
.state
= Upload
.State
.ERROR
;
205 return this.stashPromise
;
209 * Finish a stash upload.
210 * @return {jQuery.Promise}
212 UP
.finishStashUpload = function () {
215 if ( !this.stashPromise
) {
216 return $.Deferred().reject( 'This upload has not been stashed, please upload it to the stash first.' );
219 return this.stashPromise
.then( function ( finishStash
) {
220 upload
.state
= Upload
.State
.UPLOADING
;
222 return finishStash( {
223 watchlist
: ( upload
.watchlist
=== true ) ? 1 : undefined,
224 comment
: upload
.getComment(),
225 filename
: upload
.getFilename(),
226 text
: upload
.getText()
227 } ).then( function () {
228 upload
.state
= Upload
.State
.UPLOADED
;
230 upload
.state
= Upload
.State
.ERROR
;
236 * @enum mw.Upload.State
237 * State of uploads represented in simple terms.
240 /** Upload not yet started */
243 /** Upload finished, but there was a warning */
246 /** Upload finished, but there was an error */
249 /** Upload in progress */
252 /** Upload finished, but not published, call #finishStashUpload */
255 /** Upload finished and published */
260 }( mediaWiki
, jQuery
) );