author | ymh <ymh.work@gmail.com> |
Tue, 22 Oct 2019 16:11:46 +0200 | |
changeset 15 | 3d4e9c994f10 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
5 | 1 |
/* global pluploadL10n, plupload, _wpPluploadSettings */ |
2 |
||
9 | 3 |
/** |
4 |
* @namespace wp |
|
5 |
*/ |
|
0 | 6 |
window.wp = window.wp || {}; |
7 |
||
5 | 8 |
( function( exports, $ ) { |
0 | 9 |
var Uploader; |
10 |
||
5 | 11 |
if ( typeof _wpPluploadSettings === 'undefined' ) { |
0 | 12 |
return; |
5 | 13 |
} |
0 | 14 |
|
5 | 15 |
/** |
16 |
* A WordPress uploader. |
|
0 | 17 |
* |
5 | 18 |
* The Plupload library provides cross-browser uploader UI integration. |
19 |
* This object bridges the Plupload API to integrate uploads into the |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
20 |
* WordPress back end and the WordPress media experience. |
0 | 21 |
* |
9 | 22 |
* @class |
23 |
* @memberOf wp |
|
24 |
* @alias wp.Uploader |
|
25 |
* |
|
5 | 26 |
* @param {object} options The options passed to the new plupload instance. |
27 |
* @param {object} options.container The id of uploader container. |
|
28 |
* @param {object} options.browser The id of button to trigger the file select. |
|
29 |
* @param {object} options.dropzone The id of file drop target. |
|
30 |
* @param {object} options.plupload An object of parameters to pass to the plupload instance. |
|
31 |
* @param {object} options.params An object of parameters to pass to $_POST when uploading the file. |
|
32 |
* Extends this.plupload.multipart_params under the hood. |
|
0 | 33 |
*/ |
34 |
Uploader = function( options ) { |
|
35 |
var self = this, |
|
5 | 36 |
isIE = navigator.userAgent.indexOf('Trident/') != -1 || navigator.userAgent.indexOf('MSIE ') != -1, |
0 | 37 |
elements = { |
38 |
container: 'container', |
|
39 |
browser: 'browse_button', |
|
40 |
dropzone: 'drop_element' |
|
41 |
}, |
|
42 |
key, error; |
|
43 |
||
44 |
this.supports = { |
|
45 |
upload: Uploader.browser.supported |
|
46 |
}; |
|
47 |
||
48 |
this.supported = this.supports.upload; |
|
49 |
||
5 | 50 |
if ( ! this.supported ) { |
0 | 51 |
return; |
5 | 52 |
} |
0 | 53 |
|
5 | 54 |
// Arguments to send to pluplad.Uploader(). |
0 | 55 |
// Use deep extend to ensure that multipart_params and other objects are cloned. |
56 |
this.plupload = $.extend( true, { multipart_params: {} }, Uploader.defaults ); |
|
57 |
this.container = document.body; // Set default container. |
|
58 |
||
5 | 59 |
// Extend the instance with options. |
0 | 60 |
// |
61 |
// Use deep extend to allow options.plupload to override individual |
|
62 |
// default plupload keys. |
|
63 |
$.extend( true, this, options ); |
|
64 |
||
65 |
// Proxy all methods so this always refers to the current instance. |
|
66 |
for ( key in this ) { |
|
5 | 67 |
if ( $.isFunction( this[ key ] ) ) { |
0 | 68 |
this[ key ] = $.proxy( this[ key ], this ); |
5 | 69 |
} |
0 | 70 |
} |
71 |
||
5 | 72 |
// Ensure all elements are jQuery elements and have id attributes, |
73 |
// then set the proper plupload arguments to the ids. |
|
0 | 74 |
for ( key in elements ) { |
5 | 75 |
if ( ! this[ key ] ) { |
0 | 76 |
continue; |
5 | 77 |
} |
0 | 78 |
|
79 |
this[ key ] = $( this[ key ] ).first(); |
|
80 |
||
81 |
if ( ! this[ key ].length ) { |
|
82 |
delete this[ key ]; |
|
83 |
continue; |
|
84 |
} |
|
85 |
||
5 | 86 |
if ( ! this[ key ].prop('id') ) { |
0 | 87 |
this[ key ].prop( 'id', '__wp-uploader-id-' + Uploader.uuid++ ); |
5 | 88 |
} |
89 |
||
0 | 90 |
this.plupload[ elements[ key ] ] = this[ key ].prop('id'); |
91 |
} |
|
92 |
||
93 |
// If the uploader has neither a browse button nor a dropzone, bail. |
|
5 | 94 |
if ( ! ( this.browser && this.browser.length ) && ! ( this.dropzone && this.dropzone.length ) ) { |
0 | 95 |
return; |
5 | 96 |
} |
0 | 97 |
|
5 | 98 |
// Make sure flash sends cookies (seems in IE it does without switching to urlstream mode) |
99 |
if ( ! isIE && 'flash' === plupload.predictRuntime( this.plupload ) && |
|
100 |
( ! this.plupload.required_features || ! this.plupload.required_features.hasOwnProperty( 'send_binary_string' ) ) ) { |
|
101 |
||
102 |
this.plupload.required_features = this.plupload.required_features || {}; |
|
103 |
this.plupload.required_features.send_binary_string = true; |
|
104 |
} |
|
105 |
||
106 |
// Initialize the plupload instance. |
|
0 | 107 |
this.uploader = new plupload.Uploader( this.plupload ); |
108 |
delete this.plupload; |
|
109 |
||
110 |
// Set default params and remove this.params alias. |
|
111 |
this.param( this.params || {} ); |
|
112 |
delete this.params; |
|
113 |
||
5 | 114 |
/** |
115 |
* Custom error callback. |
|
116 |
* |
|
117 |
* Add a new error to the errors collection, so other modules can track |
|
118 |
* and display errors. @see wp.Uploader.errors. |
|
119 |
* |
|
120 |
* @param {string} message |
|
121 |
* @param {object} data |
|
122 |
* @param {plupload.File} file File that was uploaded. |
|
123 |
*/ |
|
0 | 124 |
error = function( message, data, file ) { |
5 | 125 |
if ( file.attachment ) { |
0 | 126 |
file.attachment.destroy(); |
5 | 127 |
} |
0 | 128 |
|
129 |
Uploader.errors.unshift({ |
|
130 |
message: message || pluploadL10n.default_error, |
|
131 |
data: data, |
|
132 |
file: file |
|
133 |
}); |
|
134 |
||
135 |
self.error( message, data, file ); |
|
136 |
}; |
|
137 |
||
5 | 138 |
/** |
139 |
* After the Uploader has been initialized, initialize some behaviors for the dropzone. |
|
140 |
* |
|
141 |
* @param {plupload.Uploader} uploader Uploader instance. |
|
142 |
*/ |
|
143 |
this.uploader.bind( 'init', function( uploader ) { |
|
144 |
var timer, active, dragdrop, |
|
145 |
dropzone = self.dropzone; |
|
0 | 146 |
|
5 | 147 |
dragdrop = self.supports.dragdrop = uploader.features.dragdrop && ! Uploader.browser.mobile; |
0 | 148 |
|
5 | 149 |
// Generate drag/drop helper classes. |
150 |
if ( ! dropzone ) { |
|
0 | 151 |
return; |
5 | 152 |
} |
0 | 153 |
|
5 | 154 |
dropzone.toggleClass( 'supports-drag-drop', !! dragdrop ); |
0 | 155 |
|
5 | 156 |
if ( ! dragdrop ) { |
0 | 157 |
return dropzone.unbind('.wp-uploader'); |
5 | 158 |
} |
0 | 159 |
|
5 | 160 |
// 'dragenter' doesn't fire correctly, simulate it with a limited 'dragover'. |
161 |
dropzone.bind( 'dragover.wp-uploader', function() { |
|
162 |
if ( timer ) { |
|
0 | 163 |
clearTimeout( timer ); |
5 | 164 |
} |
0 | 165 |
|
5 | 166 |
if ( active ) { |
0 | 167 |
return; |
5 | 168 |
} |
0 | 169 |
|
170 |
dropzone.trigger('dropzone:enter').addClass('drag-over'); |
|
171 |
active = true; |
|
172 |
}); |
|
173 |
||
5 | 174 |
dropzone.bind('dragleave.wp-uploader, drop.wp-uploader', function() { |
0 | 175 |
// Using an instant timer prevents the drag-over class from |
176 |
// being quickly removed and re-added when elements inside the |
|
177 |
// dropzone are repositioned. |
|
178 |
// |
|
5 | 179 |
// @see https://core.trac.wordpress.org/ticket/21705 |
0 | 180 |
timer = setTimeout( function() { |
181 |
active = false; |
|
182 |
dropzone.trigger('dropzone:leave').removeClass('drag-over'); |
|
183 |
}, 0 ); |
|
184 |
}); |
|
5 | 185 |
|
186 |
self.ready = true; |
|
187 |
$(self).trigger( 'uploader:ready' ); |
|
188 |
}); |
|
189 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
190 |
this.uploader.bind( 'postinit', function( up ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
up.refresh(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
self.init(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
194 |
|
5 | 195 |
this.uploader.init(); |
0 | 196 |
|
197 |
if ( this.browser ) { |
|
198 |
this.browser.on( 'mouseenter', this.refresh ); |
|
199 |
} else { |
|
200 |
this.uploader.disableBrowse( true ); |
|
201 |
// If HTML5 mode, hide the auto-created file container. |
|
202 |
$('#' + this.uploader.id + '_html5_container').hide(); |
|
203 |
} |
|
204 |
||
5 | 205 |
/** |
206 |
* After files were filtered and added to the queue, create a model for each. |
|
207 |
* |
|
208 |
* @param {plupload.Uploader} uploader Uploader instance. |
|
209 |
* @param {Array} files Array of file objects that were added to queue by the user. |
|
210 |
*/ |
|
0 | 211 |
this.uploader.bind( 'FilesAdded', function( up, files ) { |
212 |
_.each( files, function( file ) { |
|
213 |
var attributes, image; |
|
214 |
||
215 |
// Ignore failed uploads. |
|
5 | 216 |
if ( plupload.FAILED === file.status ) { |
0 | 217 |
return; |
5 | 218 |
} |
0 | 219 |
|
220 |
// Generate attributes for a new `Attachment` model. |
|
221 |
attributes = _.extend({ |
|
222 |
file: file, |
|
223 |
uploading: true, |
|
224 |
date: new Date(), |
|
225 |
filename: file.name, |
|
226 |
menuOrder: 0, |
|
227 |
uploadedTo: wp.media.model.settings.post.id |
|
228 |
}, _.pick( file, 'loaded', 'size', 'percent' ) ); |
|
229 |
||
230 |
// Handle early mime type scanning for images. |
|
231 |
image = /(?:jpe?g|png|gif)$/i.exec( file.name ); |
|
232 |
||
5 | 233 |
// For images set the model's type and subtype attributes. |
0 | 234 |
if ( image ) { |
235 |
attributes.type = 'image'; |
|
236 |
||
237 |
// `jpeg`, `png` and `gif` are valid subtypes. |
|
238 |
// `jpg` is not, so map it to `jpeg`. |
|
239 |
attributes.subtype = ( 'jpg' === image[0] ) ? 'jpeg' : image[0]; |
|
240 |
} |
|
241 |
||
5 | 242 |
// Create a model for the attachment, and add it to the Upload queue collection |
243 |
// so listeners to the upload queue can track and display upload progress. |
|
0 | 244 |
file.attachment = wp.media.model.Attachment.create( attributes ); |
245 |
Uploader.queue.add( file.attachment ); |
|
246 |
||
247 |
self.added( file.attachment ); |
|
248 |
}); |
|
249 |
||
250 |
up.refresh(); |
|
251 |
up.start(); |
|
252 |
}); |
|
253 |
||
254 |
this.uploader.bind( 'UploadProgress', function( up, file ) { |
|
255 |
file.attachment.set( _.pick( file, 'loaded', 'percent' ) ); |
|
256 |
self.progress( file.attachment ); |
|
257 |
}); |
|
258 |
||
5 | 259 |
/** |
260 |
* After a file is successfully uploaded, update its model. |
|
261 |
* |
|
262 |
* @param {plupload.Uploader} uploader Uploader instance. |
|
263 |
* @param {plupload.File} file File that was uploaded. |
|
264 |
* @param {Object} response Object with response properties. |
|
265 |
* @return {mixed} |
|
266 |
*/ |
|
0 | 267 |
this.uploader.bind( 'FileUploaded', function( up, file, response ) { |
268 |
var complete; |
|
269 |
||
270 |
try { |
|
271 |
response = JSON.parse( response.response ); |
|
272 |
} catch ( e ) { |
|
273 |
return error( pluploadL10n.default_error, e, file ); |
|
274 |
} |
|
275 |
||
276 |
if ( ! _.isObject( response ) || _.isUndefined( response.success ) ) |
|
277 |
return error( pluploadL10n.default_error, null, file ); |
|
278 |
else if ( ! response.success ) |
|
279 |
return error( response.data && response.data.message, response.data, file ); |
|
280 |
||
281 |
_.each(['file','loaded','size','percent'], function( key ) { |
|
282 |
file.attachment.unset( key ); |
|
283 |
}); |
|
284 |
||
285 |
file.attachment.set( _.extend( response.data, { uploading: false }) ); |
|
286 |
wp.media.model.Attachment.get( response.data.id, file.attachment ); |
|
287 |
||
288 |
complete = Uploader.queue.all( function( attachment ) { |
|
289 |
return ! attachment.get('uploading'); |
|
290 |
}); |
|
291 |
||
292 |
if ( complete ) |
|
293 |
Uploader.queue.reset(); |
|
294 |
||
295 |
self.success( file.attachment ); |
|
296 |
}); |
|
297 |
||
5 | 298 |
/** |
299 |
* When plupload surfaces an error, send it to the error handler. |
|
300 |
* |
|
301 |
* @param {plupload.Uploader} uploader Uploader instance. |
|
302 |
* @param {Object} error Contains code, message and sometimes file and other details. |
|
303 |
*/ |
|
0 | 304 |
this.uploader.bind( 'Error', function( up, pluploadError ) { |
305 |
var message = pluploadL10n.default_error, |
|
306 |
key; |
|
307 |
||
308 |
// Check for plupload errors. |
|
309 |
for ( key in Uploader.errorMap ) { |
|
310 |
if ( pluploadError.code === plupload[ key ] ) { |
|
311 |
message = Uploader.errorMap[ key ]; |
|
5 | 312 |
|
313 |
if ( _.isFunction( message ) ) { |
|
0 | 314 |
message = message( pluploadError.file, pluploadError ); |
5 | 315 |
} |
316 |
||
0 | 317 |
break; |
318 |
} |
|
319 |
} |
|
320 |
||
321 |
error( message, pluploadError, pluploadError.file ); |
|
322 |
up.refresh(); |
|
323 |
}); |
|
324 |
||
325 |
}; |
|
326 |
||
327 |
// Adds the 'defaults' and 'browser' properties. |
|
328 |
$.extend( Uploader, _wpPluploadSettings ); |
|
329 |
||
330 |
Uploader.uuid = 0; |
|
331 |
||
5 | 332 |
// Map Plupload error codes to user friendly error messages. |
0 | 333 |
Uploader.errorMap = { |
334 |
'FAILED': pluploadL10n.upload_failed, |
|
335 |
'FILE_EXTENSION_ERROR': pluploadL10n.invalid_filetype, |
|
336 |
'IMAGE_FORMAT_ERROR': pluploadL10n.not_an_image, |
|
337 |
'IMAGE_MEMORY_ERROR': pluploadL10n.image_memory_exceeded, |
|
338 |
'IMAGE_DIMENSIONS_ERROR': pluploadL10n.image_dimensions_exceeded, |
|
339 |
'GENERIC_ERROR': pluploadL10n.upload_failed, |
|
340 |
'IO_ERROR': pluploadL10n.io_error, |
|
341 |
'HTTP_ERROR': pluploadL10n.http_error, |
|
342 |
'SECURITY_ERROR': pluploadL10n.security_error, |
|
343 |
||
344 |
'FILE_SIZE_ERROR': function( file ) { |
|
345 |
return pluploadL10n.file_exceeds_size_limit.replace('%s', file.name); |
|
346 |
} |
|
347 |
}; |
|
348 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
$.extend( Uploader.prototype, /** @lends wp.Uploader.prototype */{ |
0 | 350 |
/** |
351 |
* Acts as a shortcut to extending the uploader's multipart_params object. |
|
352 |
* |
|
353 |
* param( key ) |
|
354 |
* Returns the value of the key. |
|
355 |
* |
|
356 |
* param( key, value ) |
|
357 |
* Sets the value of a key. |
|
358 |
* |
|
359 |
* param( map ) |
|
360 |
* Sets values for a map of data. |
|
361 |
*/ |
|
362 |
param: function( key, value ) { |
|
5 | 363 |
if ( arguments.length === 1 && typeof key === 'string' ) { |
0 | 364 |
return this.uploader.settings.multipart_params[ key ]; |
5 | 365 |
} |
0 | 366 |
|
367 |
if ( arguments.length > 1 ) { |
|
368 |
this.uploader.settings.multipart_params[ key ] = value; |
|
369 |
} else { |
|
370 |
$.extend( this.uploader.settings.multipart_params, key ); |
|
371 |
} |
|
372 |
}, |
|
373 |
||
5 | 374 |
/** |
375 |
* Make a few internal event callbacks available on the wp.Uploader object |
|
376 |
* to change the Uploader internals if absolutely necessary. |
|
377 |
*/ |
|
0 | 378 |
init: function() {}, |
379 |
error: function() {}, |
|
380 |
success: function() {}, |
|
381 |
added: function() {}, |
|
382 |
progress: function() {}, |
|
383 |
complete: function() {}, |
|
384 |
refresh: function() { |
|
385 |
var node, attached, container, id; |
|
386 |
||
387 |
if ( this.browser ) { |
|
388 |
node = this.browser[0]; |
|
389 |
||
390 |
// Check if the browser node is in the DOM. |
|
391 |
while ( node ) { |
|
392 |
if ( node === document.body ) { |
|
393 |
attached = true; |
|
394 |
break; |
|
395 |
} |
|
396 |
node = node.parentNode; |
|
397 |
} |
|
398 |
||
399 |
// If the browser node is not attached to the DOM, use a |
|
400 |
// temporary container to house it, as the browser button |
|
401 |
// shims require the button to exist in the DOM at all times. |
|
402 |
if ( ! attached ) { |
|
403 |
id = 'wp-uploader-browser-' + this.uploader.id; |
|
404 |
||
405 |
container = $( '#' + id ); |
|
406 |
if ( ! container.length ) { |
|
407 |
container = $('<div class="wp-uploader-browser" />').css({ |
|
408 |
position: 'fixed', |
|
409 |
top: '-1000px', |
|
410 |
left: '-1000px', |
|
411 |
height: 0, |
|
412 |
width: 0 |
|
413 |
}).attr( 'id', 'wp-uploader-browser-' + this.uploader.id ).appendTo('body'); |
|
414 |
} |
|
415 |
||
416 |
container.append( this.browser ); |
|
417 |
} |
|
418 |
} |
|
419 |
||
420 |
this.uploader.refresh(); |
|
421 |
} |
|
422 |
}); |
|
423 |
||
5 | 424 |
// Create a collection of attachments in the upload queue, |
425 |
// so that other modules can track and display upload progress. |
|
0 | 426 |
Uploader.queue = new wp.media.model.Attachments( [], { query: false }); |
5 | 427 |
|
428 |
// Create a collection to collect errors incurred while attempting upload. |
|
0 | 429 |
Uploader.errors = new Backbone.Collection(); |
430 |
||
431 |
exports.Uploader = Uploader; |
|
432 |
})( wp, jQuery ); |