1 /* global pluploadL10n, plupload, _wpPluploadSettings */
3 window.wp = window.wp || {};
5 ( function( exports, $ ) {
8 if ( typeof _wpPluploadSettings === 'undefined' ) {
13 * A WordPress uploader.
15 * The Plupload library provides cross-browser uploader UI integration.
16 * This object bridges the Plupload API to integrate uploads into the
17 * WordPress back-end and the WordPress media experience.
19 * @param {object} options The options passed to the new plupload instance.
20 * @param {object} options.container The id of uploader container.
21 * @param {object} options.browser The id of button to trigger the file select.
22 * @param {object} options.dropzone The id of file drop target.
23 * @param {object} options.plupload An object of parameters to pass to the plupload instance.
24 * @param {object} options.params An object of parameters to pass to $_POST when uploading the file.
25 * Extends this.plupload.multipart_params under the hood.
27 Uploader = function( options ) {
29 isIE = navigator.userAgent.indexOf('Trident/') != -1 || navigator.userAgent.indexOf('MSIE ') != -1,
31 container: 'container',
32 browser: 'browse_button',
33 dropzone: 'drop_element'
38 upload: Uploader.browser.supported
41 this.supported = this.supports.upload;
43 if ( ! this.supported ) {
47 // Arguments to send to pluplad.Uploader().
48 // Use deep extend to ensure that multipart_params and other objects are cloned.
49 this.plupload = $.extend( true, { multipart_params: {} }, Uploader.defaults );
50 this.container = document.body; // Set default container.
52 // Extend the instance with options.
54 // Use deep extend to allow options.plupload to override individual
55 // default plupload keys.
56 $.extend( true, this, options );
58 // Proxy all methods so this always refers to the current instance.
60 if ( $.isFunction( this[ key ] ) ) {
61 this[ key ] = $.proxy( this[ key ], this );
65 // Ensure all elements are jQuery elements and have id attributes,
66 // then set the proper plupload arguments to the ids.
67 for ( key in elements ) {
68 if ( ! this[ key ] ) {
72 this[ key ] = $( this[ key ] ).first();
74 if ( ! this[ key ].length ) {
79 if ( ! this[ key ].prop('id') ) {
80 this[ key ].prop( 'id', '__wp-uploader-id-' + Uploader.uuid++ );
83 this.plupload[ elements[ key ] ] = this[ key ].prop('id');
86 // If the uploader has neither a browse button nor a dropzone, bail.
87 if ( ! ( this.browser && this.browser.length ) && ! ( this.dropzone && this.dropzone.length ) ) {
91 // Make sure flash sends cookies (seems in IE it does without switching to urlstream mode)
92 if ( ! isIE && 'flash' === plupload.predictRuntime( this.plupload ) &&
93 ( ! this.plupload.required_features || ! this.plupload.required_features.hasOwnProperty( 'send_binary_string' ) ) ) {
95 this.plupload.required_features = this.plupload.required_features || {};
96 this.plupload.required_features.send_binary_string = true;
99 // Initialize the plupload instance.
100 this.uploader = new plupload.Uploader( this.plupload );
101 delete this.plupload;
103 // Set default params and remove this.params alias.
104 this.param( this.params || {} );
108 * Custom error callback.
110 * Add a new error to the errors collection, so other modules can track
111 * and display errors. @see wp.Uploader.errors.
113 * @param {string} message
114 * @param {object} data
115 * @param {plupload.File} file File that was uploaded.
117 error = function( message, data, file ) {
118 if ( file.attachment ) {
119 file.attachment.destroy();
122 Uploader.errors.unshift({
123 message: message || pluploadL10n.default_error,
128 self.error( message, data, file );
132 * After the Uploader has been initialized, initialize some behaviors for the dropzone.
134 * @param {plupload.Uploader} uploader Uploader instance.
136 this.uploader.bind( 'init', function( uploader ) {
137 var timer, active, dragdrop,
138 dropzone = self.dropzone;
140 dragdrop = self.supports.dragdrop = uploader.features.dragdrop && ! Uploader.browser.mobile;
142 // Generate drag/drop helper classes.
147 dropzone.toggleClass( 'supports-drag-drop', !! dragdrop );
150 return dropzone.unbind('.wp-uploader');
153 // 'dragenter' doesn't fire correctly, simulate it with a limited 'dragover'.
154 dropzone.bind( 'dragover.wp-uploader', function() {
156 clearTimeout( timer );
163 dropzone.trigger('dropzone:enter').addClass('drag-over');
167 dropzone.bind('dragleave.wp-uploader, drop.wp-uploader', function() {
168 // Using an instant timer prevents the drag-over class from
169 // being quickly removed and re-added when elements inside the
170 // dropzone are repositioned.
172 // @see https://core.trac.wordpress.org/ticket/21705
173 timer = setTimeout( function() {
175 dropzone.trigger('dropzone:leave').removeClass('drag-over');
180 $(self).trigger( 'uploader:ready' );
183 this.uploader.init();
185 if ( this.browser ) {
186 this.browser.on( 'mouseenter', this.refresh );
188 this.uploader.disableBrowse( true );
189 // If HTML5 mode, hide the auto-created file container.
190 $('#' + this.uploader.id + '_html5_container').hide();
194 * After files were filtered and added to the queue, create a model for each.
197 * @param {plupload.Uploader} uploader Uploader instance.
198 * @param {Array} files Array of file objects that were added to queue by the user.
200 this.uploader.bind( 'FilesAdded', function( up, files ) {
201 _.each( files, function( file ) {
202 var attributes, image;
204 // Ignore failed uploads.
205 if ( plupload.FAILED === file.status ) {
209 // Generate attributes for a new `Attachment` model.
210 attributes = _.extend({
216 uploadedTo: wp.media.model.settings.post.id
217 }, _.pick( file, 'loaded', 'size', 'percent' ) );
219 // Handle early mime type scanning for images.
220 image = /(?:jpe?g|png|gif)$/i.exec( file.name );
222 // For images set the model's type and subtype attributes.
224 attributes.type = 'image';
226 // `jpeg`, `png` and `gif` are valid subtypes.
227 // `jpg` is not, so map it to `jpeg`.
228 attributes.subtype = ( 'jpg' === image[0] ) ? 'jpeg' : image[0];
231 // Create a model for the attachment, and add it to the Upload queue collection
232 // so listeners to the upload queue can track and display upload progress.
233 file.attachment = wp.media.model.Attachment.create( attributes );
234 Uploader.queue.add( file.attachment );
236 self.added( file.attachment );
243 this.uploader.bind( 'UploadProgress', function( up, file ) {
244 file.attachment.set( _.pick( file, 'loaded', 'percent' ) );
245 self.progress( file.attachment );
249 * After a file is successfully uploaded, update its model.
251 * @param {plupload.Uploader} uploader Uploader instance.
252 * @param {plupload.File} file File that was uploaded.
253 * @param {Object} response Object with response properties.
256 this.uploader.bind( 'FileUploaded', function( up, file, response ) {
260 response = JSON.parse( response.response );
262 return error( pluploadL10n.default_error, e, file );
265 if ( ! _.isObject( response ) || _.isUndefined( response.success ) )
266 return error( pluploadL10n.default_error, null, file );
267 else if ( ! response.success )
268 return error( response.data && response.data.message, response.data, file );
270 _.each(['file','loaded','size','percent'], function( key ) {
271 file.attachment.unset( key );
274 file.attachment.set( _.extend( response.data, { uploading: false }) );
275 wp.media.model.Attachment.get( response.data.id, file.attachment );
277 complete = Uploader.queue.all( function( attachment ) {
278 return ! attachment.get('uploading');
282 Uploader.queue.reset();
284 self.success( file.attachment );
288 * When plupload surfaces an error, send it to the error handler.
290 * @param {plupload.Uploader} uploader Uploader instance.
291 * @param {Object} error Contains code, message and sometimes file and other details.
293 this.uploader.bind( 'Error', function( up, pluploadError ) {
294 var message = pluploadL10n.default_error,
297 // Check for plupload errors.
298 for ( key in Uploader.errorMap ) {
299 if ( pluploadError.code === plupload[ key ] ) {
300 message = Uploader.errorMap[ key ];
302 if ( _.isFunction( message ) ) {
303 message = message( pluploadError.file, pluploadError );
310 error( message, pluploadError, pluploadError.file );
314 this.uploader.bind( 'PostInit', function() {
319 // Adds the 'defaults' and 'browser' properties.
320 $.extend( Uploader, _wpPluploadSettings );
324 // Map Plupload error codes to user friendly error messages.
325 Uploader.errorMap = {
326 'FAILED': pluploadL10n.upload_failed,
327 'FILE_EXTENSION_ERROR': pluploadL10n.invalid_filetype,
328 'IMAGE_FORMAT_ERROR': pluploadL10n.not_an_image,
329 'IMAGE_MEMORY_ERROR': pluploadL10n.image_memory_exceeded,
330 'IMAGE_DIMENSIONS_ERROR': pluploadL10n.image_dimensions_exceeded,
331 'GENERIC_ERROR': pluploadL10n.upload_failed,
332 'IO_ERROR': pluploadL10n.io_error,
333 'HTTP_ERROR': pluploadL10n.http_error,
334 'SECURITY_ERROR': pluploadL10n.security_error,
336 'FILE_SIZE_ERROR': function( file ) {
337 return pluploadL10n.file_exceeds_size_limit.replace('%s', file.name);
341 $.extend( Uploader.prototype, {
343 * Acts as a shortcut to extending the uploader's multipart_params object.
346 * Returns the value of the key.
348 * param( key, value )
349 * Sets the value of a key.
352 * Sets values for a map of data.
354 param: function( key, value ) {
355 if ( arguments.length === 1 && typeof key === 'string' ) {
356 return this.uploader.settings.multipart_params[ key ];
359 if ( arguments.length > 1 ) {
360 this.uploader.settings.multipart_params[ key ] = value;
362 $.extend( this.uploader.settings.multipart_params, key );
367 * Make a few internal event callbacks available on the wp.Uploader object
368 * to change the Uploader internals if absolutely necessary.
371 error: function() {},
372 success: function() {},
373 added: function() {},
374 progress: function() {},
375 complete: function() {},
376 refresh: function() {
377 var node, attached, container, id;
379 if ( this.browser ) {
380 node = this.browser[0];
382 // Check if the browser node is in the DOM.
384 if ( node === document.body ) {
388 node = node.parentNode;
391 // If the browser node is not attached to the DOM, use a
392 // temporary container to house it, as the browser button
393 // shims require the button to exist in the DOM at all times.
395 id = 'wp-uploader-browser-' + this.uploader.id;
397 container = $( '#' + id );
398 if ( ! container.length ) {
399 container = $('<div class="wp-uploader-browser" />').css({
405 }).attr( 'id', 'wp-uploader-browser-' + this.uploader.id ).appendTo('body');
408 container.append( this.browser );
412 this.uploader.refresh();
416 // Create a collection of attachments in the upload queue,
417 // so that other modules can track and display upload progress.
418 Uploader.queue = new wp.media.model.Attachments( [], { query: false });
420 // Create a collection to collect errors incurred while attempting upload.
421 Uploader.errors = new Backbone.Collection();
423 exports.Uploader = Uploader;