1 window.wp = window.wp || {};
3 (function( exports, $ ) {
6 if ( typeof _wpPluploadSettings === 'undefined' )
10 * An object that helps create a WordPress uploader using plupload.
12 * @param options - object - The options passed to the new plupload instance.
13 * Accepts the following parameters:
14 * - container - The id of uploader container.
15 * - browser - The id of button to trigger the file select.
16 * - dropzone - The id of file drop target.
17 * - plupload - An object of parameters to pass to the plupload instance.
18 * - params - An object of parameters to pass to $_POST when uploading the file.
19 * Extends this.plupload.multipart_params under the hood.
21 * @param attributes - object - Attributes and methods for this specific instance.
23 Uploader = function( options ) {
26 container: 'container',
27 browser: 'browse_button',
28 dropzone: 'drop_element'
33 upload: Uploader.browser.supported
36 this.supported = this.supports.upload;
38 if ( ! this.supported )
41 // Use deep extend to ensure that multipart_params and other objects are cloned.
42 this.plupload = $.extend( true, { multipart_params: {} }, Uploader.defaults );
43 this.container = document.body; // Set default container.
45 // Extend the instance with options
47 // Use deep extend to allow options.plupload to override individual
48 // default plupload keys.
49 $.extend( true, this, options );
51 // Proxy all methods so this always refers to the current instance.
53 if ( $.isFunction( this[ key ] ) )
54 this[ key ] = $.proxy( this[ key ], this );
57 // Ensure all elements are jQuery elements and have id attributes
58 // Then set the proper plupload arguments to the ids.
59 for ( key in elements ) {
63 this[ key ] = $( this[ key ] ).first();
65 if ( ! this[ key ].length ) {
70 if ( ! this[ key ].prop('id') )
71 this[ key ].prop( 'id', '__wp-uploader-id-' + Uploader.uuid++ );
72 this.plupload[ elements[ key ] ] = this[ key ].prop('id');
75 // If the uploader has neither a browse button nor a dropzone, bail.
76 if ( ! ( this.browser && this.browser.length ) && ! ( this.dropzone && this.dropzone.length ) )
79 this.uploader = new plupload.Uploader( this.plupload );
82 // Set default params and remove this.params alias.
83 this.param( this.params || {} );
86 error = function( message, data, file ) {
87 if ( file.attachment )
88 file.attachment.destroy();
90 Uploader.errors.unshift({
91 message: message || pluploadL10n.default_error,
96 self.error( message, data, file );
101 this.supports.dragdrop = this.uploader.features.dragdrop && ! Uploader.browser.mobile;
103 // Generate drag/drop helper classes.
104 (function( dropzone, supported ) {
110 dropzone.toggleClass( 'supports-drag-drop', !! supported );
113 return dropzone.unbind('.wp-uploader');
115 // 'dragenter' doesn't fire correctly,
116 // simulate it with a limited 'dragover'
117 dropzone.bind( 'dragover.wp-uploader', function(){
119 clearTimeout( timer );
124 dropzone.trigger('dropzone:enter').addClass('drag-over');
128 dropzone.bind('dragleave.wp-uploader, drop.wp-uploader', function(){
129 // Using an instant timer prevents the drag-over class from
130 // being quickly removed and re-added when elements inside the
131 // dropzone are repositioned.
133 // See http://core.trac.wordpress.org/ticket/21705
134 timer = setTimeout( function() {
136 dropzone.trigger('dropzone:leave').removeClass('drag-over');
139 }( this.dropzone, this.supports.dragdrop ));
141 if ( this.browser ) {
142 this.browser.on( 'mouseenter', this.refresh );
144 this.uploader.disableBrowse( true );
145 // If HTML5 mode, hide the auto-created file container.
146 $('#' + this.uploader.id + '_html5_container').hide();
149 this.uploader.bind( 'FilesAdded', function( up, files ) {
150 _.each( files, function( file ) {
151 var attributes, image;
153 // Ignore failed uploads.
154 if ( plupload.FAILED === file.status )
157 // Generate attributes for a new `Attachment` model.
158 attributes = _.extend({
164 uploadedTo: wp.media.model.settings.post.id
165 }, _.pick( file, 'loaded', 'size', 'percent' ) );
167 // Handle early mime type scanning for images.
168 image = /(?:jpe?g|png|gif)$/i.exec( file.name );
170 // Did we find an image?
172 attributes.type = 'image';
174 // `jpeg`, `png` and `gif` are valid subtypes.
175 // `jpg` is not, so map it to `jpeg`.
176 attributes.subtype = ( 'jpg' === image[0] ) ? 'jpeg' : image[0];
179 // Create the `Attachment`.
180 file.attachment = wp.media.model.Attachment.create( attributes );
182 Uploader.queue.add( file.attachment );
184 self.added( file.attachment );
191 this.uploader.bind( 'UploadProgress', function( up, file ) {
192 file.attachment.set( _.pick( file, 'loaded', 'percent' ) );
193 self.progress( file.attachment );
196 this.uploader.bind( 'FileUploaded', function( up, file, response ) {
200 response = JSON.parse( response.response );
202 return error( pluploadL10n.default_error, e, file );
205 if ( ! _.isObject( response ) || _.isUndefined( response.success ) )
206 return error( pluploadL10n.default_error, null, file );
207 else if ( ! response.success )
208 return error( response.data && response.data.message, response.data, file );
210 _.each(['file','loaded','size','percent'], function( key ) {
211 file.attachment.unset( key );
214 file.attachment.set( _.extend( response.data, { uploading: false }) );
215 wp.media.model.Attachment.get( response.data.id, file.attachment );
217 complete = Uploader.queue.all( function( attachment ) {
218 return ! attachment.get('uploading');
222 Uploader.queue.reset();
224 self.success( file.attachment );
227 this.uploader.bind( 'Error', function( up, pluploadError ) {
228 var message = pluploadL10n.default_error,
231 // Check for plupload errors.
232 for ( key in Uploader.errorMap ) {
233 if ( pluploadError.code === plupload[ key ] ) {
234 message = Uploader.errorMap[ key ];
235 if ( _.isFunction( message ) )
236 message = message( pluploadError.file, pluploadError );
241 error( message, pluploadError, pluploadError.file );
248 // Adds the 'defaults' and 'browser' properties.
249 $.extend( Uploader, _wpPluploadSettings );
253 Uploader.errorMap = {
254 'FAILED': pluploadL10n.upload_failed,
255 'FILE_EXTENSION_ERROR': pluploadL10n.invalid_filetype,
256 'IMAGE_FORMAT_ERROR': pluploadL10n.not_an_image,
257 'IMAGE_MEMORY_ERROR': pluploadL10n.image_memory_exceeded,
258 'IMAGE_DIMENSIONS_ERROR': pluploadL10n.image_dimensions_exceeded,
259 'GENERIC_ERROR': pluploadL10n.upload_failed,
260 'IO_ERROR': pluploadL10n.io_error,
261 'HTTP_ERROR': pluploadL10n.http_error,
262 'SECURITY_ERROR': pluploadL10n.security_error,
264 'FILE_SIZE_ERROR': function( file ) {
265 return pluploadL10n.file_exceeds_size_limit.replace('%s', file.name);
269 $.extend( Uploader.prototype, {
271 * Acts as a shortcut to extending the uploader's multipart_params object.
274 * Returns the value of the key.
276 * param( key, value )
277 * Sets the value of a key.
280 * Sets values for a map of data.
282 param: function( key, value ) {
283 if ( arguments.length === 1 && typeof key === 'string' )
284 return this.uploader.settings.multipart_params[ key ];
286 if ( arguments.length > 1 ) {
287 this.uploader.settings.multipart_params[ key ] = value;
289 $.extend( this.uploader.settings.multipart_params, key );
294 error: function() {},
295 success: function() {},
296 added: function() {},
297 progress: function() {},
298 complete: function() {},
299 refresh: function() {
300 var node, attached, container, id;
302 if ( this.browser ) {
303 node = this.browser[0];
305 // Check if the browser node is in the DOM.
307 if ( node === document.body ) {
311 node = node.parentNode;
314 // If the browser node is not attached to the DOM, use a
315 // temporary container to house it, as the browser button
316 // shims require the button to exist in the DOM at all times.
318 id = 'wp-uploader-browser-' + this.uploader.id;
320 container = $( '#' + id );
321 if ( ! container.length ) {
322 container = $('<div class="wp-uploader-browser" />').css({
328 }).attr( 'id', 'wp-uploader-browser-' + this.uploader.id ).appendTo('body');
331 container.append( this.browser );
335 this.uploader.refresh();
339 Uploader.queue = new wp.media.model.Attachments( [], { query: false });
340 Uploader.errors = new Backbone.Collection();
342 exports.Uploader = Uploader;