1 if ( typeof wp === 'undefined' )
4 (function( exports, $ ) {
7 if ( typeof _wpPluploadSettings === 'undefined' )
11 * An object that helps create a WordPress uploader using plupload.
13 * @param options - object - The options passed to the new plupload instance.
14 * Requires the following parameters:
15 * - container - The id of uploader container.
16 * - browser - The id of button to trigger the file select.
17 * - dropzone - The id of file drop target.
18 * - plupload - An object of parameters to pass to the plupload instance.
19 * - params - An object of parameters to pass to $_POST when uploading the file.
20 * Extends this.plupload.multipart_params under the hood.
22 * @param attributes - object - Attributes and methods for this specific instance.
24 Uploader = function( options ) {
27 container: 'container',
28 browser: 'browse_button',
29 dropzone: 'drop_element'
34 upload: Uploader.browser.supported
37 this.supported = this.supports.upload;
39 if ( ! this.supported )
42 // Use deep extend to ensure that multipart_params and other objects are cloned.
43 this.plupload = $.extend( true, { multipart_params: {} }, Uploader.defaults );
44 this.container = document.body; // Set default container.
46 // Extend the instance with options
48 // Use deep extend to allow options.plupload to override individual
49 // default plupload keys.
50 $.extend( true, this, options );
52 // Proxy all methods so this always refers to the current instance.
54 if ( $.isFunction( this[ key ] ) )
55 this[ key ] = $.proxy( this[ key ], this );
58 // Ensure all elements are jQuery elements and have id attributes
59 // Then set the proper plupload arguments to the ids.
60 for ( key in elements ) {
64 this[ key ] = $( this[ key ] ).first();
66 if ( ! this[ key ].length ) {
71 if ( ! this[ key ].prop('id') )
72 this[ key ].prop( 'id', '__wp-uploader-id-' + Uploader.uuid++ );
73 this.plupload[ elements[ key ] ] = this[ key ].prop('id');
76 this.uploader = new plupload.Uploader( this.plupload );
79 // Set default params and remove this.params alias.
80 this.param( this.params || {} );
85 this.supports.dragdrop = this.uploader.features.dragdrop && ! Uploader.browser.mobile;
87 // Generate drag/drop helper classes.
88 (function( dropzone, supported ) {
95 dropzone.toggleClass( 'supports-drag-drop', !! supported );
98 return dropzone.unbind('.wp-uploader');
100 // 'dragenter' doesn't fire correctly,
101 // simulate it with a limited 'dragover'
102 dropzone.bind( 'dragover.wp-uploader', function(){
106 dropzone.addClass('drag-over');
110 dropzone.bind('dragleave.wp-uploader, drop.wp-uploader', function(){
112 dropzone.removeClass('drag-over');
114 }( this.dropzone, this.supports.dragdrop ));
116 this.browser.on( 'mouseenter', this.refresh );
118 this.uploader.bind( 'UploadProgress', this.progress );
120 this.uploader.bind( 'FileUploaded', function( up, file, response ) {
122 response = JSON.parse( response.response );
124 return self.error( pluploadL10n.default_error, e );
127 if ( ! response || ! response.type || ! response.data )
128 return self.error( pluploadL10n.default_error );
130 if ( 'error' === response.type )
131 return self.error( response.data.message, response.data );
133 if ( 'success' === response.type )
134 return self.success( response.data );
138 this.uploader.bind( 'Error', function( up, error ) {
139 var message = pluploadL10n.default_error,
142 // Check for plupload errors.
143 for ( key in Uploader.errorMap ) {
144 if ( error.code === plupload[ key ] ) {
145 message = Uploader.errorMap[ key ];
150 self.error( message, error );
154 this.uploader.bind( 'FilesAdded', function( up, files ) {
155 $.each( files, function() {
166 // Adds the 'defaults' and 'browser' properties.
167 $.extend( Uploader, _wpPluploadSettings );
171 Uploader.errorMap = {
172 'FAILED': pluploadL10n.upload_failed,
173 'FILE_EXTENSION_ERROR': pluploadL10n.invalid_filetype,
174 // 'FILE_SIZE_ERROR': '',
175 'IMAGE_FORMAT_ERROR': pluploadL10n.not_an_image,
176 'IMAGE_MEMORY_ERROR': pluploadL10n.image_memory_exceeded,
177 'IMAGE_DIMENSIONS_ERROR': pluploadL10n.image_dimensions_exceeded,
178 'GENERIC_ERROR': pluploadL10n.upload_failed,
179 'IO_ERROR': pluploadL10n.io_error,
180 'HTTP_ERROR': pluploadL10n.http_error,
181 'SECURITY_ERROR': pluploadL10n.security_error
184 $.extend( Uploader.prototype, {
186 * Acts as a shortcut to extending the uploader's multipart_params object.
189 * Returns the value of the key.
191 * param( key, value )
192 * Sets the value of a key.
195 * Sets values for a map of data.
197 param: function( key, value ) {
198 if ( arguments.length === 1 && typeof key === 'string' )
199 return this.uploader.settings.multipart_params[ key ];
201 if ( arguments.length > 1 ) {
202 this.uploader.settings.multipart_params[ key ] = value;
204 $.extend( this.uploader.settings.multipart_params, key );
209 error: function() {},
210 success: function() {},
211 added: function() {},
212 progress: function() {},
213 complete: function() {},
214 refresh: function() {
215 this.uploader.refresh();
219 exports.Uploader = Uploader;