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 // If the uploader has neither a browse button nor a dropzone, bail.
77 if ( ! ( this.browser && this.browser.length ) && ! ( this.dropzone && this.dropzone.length ) )
80 this.uploader = new plupload.Uploader( this.plupload );
83 // Set default params and remove this.params alias.
84 this.param( this.params || {} );
89 this.supports.dragdrop = this.uploader.features.dragdrop && ! Uploader.browser.mobile;
91 // Generate drag/drop helper classes.
92 (function( dropzone, supported ) {
99 dropzone.toggleClass( 'supports-drag-drop', !! supported );
102 return dropzone.unbind('.wp-uploader');
104 // 'dragenter' doesn't fire correctly,
105 // simulate it with a limited 'dragover'
106 dropzone.bind( 'dragover.wp-uploader', function(){
110 dropzone.addClass('drag-over');
114 dropzone.bind('dragleave.wp-uploader, drop.wp-uploader', function(){
116 dropzone.removeClass('drag-over');
118 }( this.dropzone, this.supports.dragdrop ));
120 if ( this.browser ) {
121 this.browser.on( 'mouseenter', this.refresh );
123 this.uploader.disableBrowse( true );
124 // If HTML5 mode, hide the auto-created file container.
125 $('#' + this.uploader.id + '_html5_container').hide();
128 this.uploader.bind( 'UploadProgress', this.progress );
130 this.uploader.bind( 'FileUploaded', function( up, file, response ) {
132 response = JSON.parse( response.response );
134 return self.error( pluploadL10n.default_error, e );
137 if ( ! response || ! response.type || ! response.data )
138 return self.error( pluploadL10n.default_error );
140 if ( 'error' === response.type )
141 return self.error( response.data.message, response.data );
143 if ( 'success' === response.type )
144 return self.success( response.data );
148 this.uploader.bind( 'Error', function( up, error ) {
149 var message = pluploadL10n.default_error,
152 // Check for plupload errors.
153 for ( key in Uploader.errorMap ) {
154 if ( error.code === plupload[ key ] ) {
155 message = Uploader.errorMap[ key ];
160 self.error( message, error );
164 this.uploader.bind( 'FilesAdded', function( up, files ) {
165 $.each( files, function() {
176 // Adds the 'defaults' and 'browser' properties.
177 $.extend( Uploader, _wpPluploadSettings );
181 Uploader.errorMap = {
182 'FAILED': pluploadL10n.upload_failed,
183 'FILE_EXTENSION_ERROR': pluploadL10n.invalid_filetype,
184 // 'FILE_SIZE_ERROR': '',
185 'IMAGE_FORMAT_ERROR': pluploadL10n.not_an_image,
186 'IMAGE_MEMORY_ERROR': pluploadL10n.image_memory_exceeded,
187 'IMAGE_DIMENSIONS_ERROR': pluploadL10n.image_dimensions_exceeded,
188 'GENERIC_ERROR': pluploadL10n.upload_failed,
189 'IO_ERROR': pluploadL10n.io_error,
190 'HTTP_ERROR': pluploadL10n.http_error,
191 'SECURITY_ERROR': pluploadL10n.security_error
194 $.extend( Uploader.prototype, {
196 * Acts as a shortcut to extending the uploader's multipart_params object.
199 * Returns the value of the key.
201 * param( key, value )
202 * Sets the value of a key.
205 * Sets values for a map of data.
207 param: function( key, value ) {
208 if ( arguments.length === 1 && typeof key === 'string' )
209 return this.uploader.settings.multipart_params[ key ];
211 if ( arguments.length > 1 ) {
212 this.uploader.settings.multipart_params[ key ] = value;
214 $.extend( this.uploader.settings.multipart_params, key );
219 error: function() {},
220 success: function() {},
221 added: function() {},
222 progress: function() {},
223 complete: function() {},
224 refresh: function() {
225 this.uploader.refresh();
229 exports.Uploader = Uploader;