]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/plupload/wp-plupload.dev.js
WordPress 3.4
[autoinstalls/wordpress.git] / wp-includes / js / plupload / wp-plupload.dev.js
1 if ( typeof wp === 'undefined' )
2         var wp = {};
3
4 (function( exports, $ ) {
5         var Uploader;
6
7         if ( typeof _wpPluploadSettings === 'undefined' )
8                 return;
9
10         /*
11          * An object that helps create a WordPress uploader using plupload.
12          *
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.
21          *
22          * @param attributes - object - Attributes and methods for this specific instance.
23          */
24         Uploader = function( options ) {
25                 var self = this,
26                         elements = {
27                                 container: 'container',
28                                 browser:   'browse_button',
29                                 dropzone:  'drop_element'
30                         },
31                         key;
32
33                 this.supports = {
34                         upload: Uploader.browser.supported
35                 };
36
37                 this.supported = this.supports.upload;
38
39                 if ( ! this.supported )
40                         return;
41
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.
45
46                 // Extend the instance with options
47                 //
48                 // Use deep extend to allow options.plupload to override individual
49                 // default plupload keys.
50                 $.extend( true, this, options );
51
52                 // Proxy all methods so this always refers to the current instance.
53                 for ( key in this ) {
54                         if ( $.isFunction( this[ key ] ) )
55                                 this[ key ] = $.proxy( this[ key ], this );
56                 }
57
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 ) {
61                         if ( ! this[ key ] )
62                                 continue;
63
64                         this[ key ] = $( this[ key ] ).first();
65
66                         if ( ! this[ key ].length ) {
67                                 delete this[ key ];
68                                 continue;
69                         }
70
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');
74                 }
75
76                 this.uploader = new plupload.Uploader( this.plupload );
77                 delete this.plupload;
78
79                 // Set default params and remove this.params alias.
80                 this.param( this.params || {} );
81                 delete this.params;
82
83                 this.uploader.init();
84
85                 this.supports.dragdrop = this.uploader.features.dragdrop && ! Uploader.browser.mobile;
86
87                 // Generate drag/drop helper classes.
88                 (function( dropzone, supported ) {
89                         var sensitivity = 50,
90                                 active;
91
92                         if ( ! dropzone )
93                                 return;
94
95                         dropzone.toggleClass( 'supports-drag-drop', !! supported );
96
97                         if ( ! supported )
98                                 return dropzone.unbind('.wp-uploader');
99
100                         // 'dragenter' doesn't fire correctly,
101                         // simulate it with a limited 'dragover'
102                         dropzone.bind( 'dragover.wp-uploader', function(){
103                                 if ( active )
104                                         return;
105
106                                 dropzone.addClass('drag-over');
107                                 active = true;
108                         });
109
110                         dropzone.bind('dragleave.wp-uploader, drop.wp-uploader', function(){
111                                 active = false;
112                                 dropzone.removeClass('drag-over');
113                         });
114                 }( this.dropzone, this.supports.dragdrop ));
115
116                 this.browser.on( 'mouseenter', this.refresh );
117
118                 this.uploader.bind( 'UploadProgress', this.progress );
119
120                 this.uploader.bind( 'FileUploaded', function( up, file, response ) {
121                         try {
122                                 response = JSON.parse( response.response );
123                         } catch ( e ) {
124                                 return self.error( pluploadL10n.default_error, e );
125                         }
126
127                         if ( ! response || ! response.type || ! response.data )
128                                 return self.error( pluploadL10n.default_error );
129
130                         if ( 'error' === response.type )
131                                 return self.error( response.data.message, response.data );
132
133                         if ( 'success' === response.type )
134                                 return self.success( response.data );
135
136                 });
137
138                 this.uploader.bind( 'Error', function( up, error ) {
139                         var message = pluploadL10n.default_error,
140                                 key;
141
142                         // Check for plupload errors.
143                         for ( key in Uploader.errorMap ) {
144                                 if ( error.code === plupload[ key ] ) {
145                                         message = Uploader.errorMap[ key ];
146                                         break;
147                                 }
148                         }
149
150                         self.error( message, error );
151                         up.refresh();
152                 });
153
154                 this.uploader.bind( 'FilesAdded', function( up, files ) {
155                         $.each( files, function() {
156                                 self.added( this );
157                         });
158
159                         up.refresh();
160                         up.start();
161                 });
162
163                 this.init();
164         };
165
166         // Adds the 'defaults' and 'browser' properties.
167         $.extend( Uploader, _wpPluploadSettings );
168
169         Uploader.uuid = 0;
170
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
182         };
183
184         $.extend( Uploader.prototype, {
185                 /**
186                  * Acts as a shortcut to extending the uploader's multipart_params object.
187                  *
188                  * param( key )
189                  *    Returns the value of the key.
190                  *
191                  * param( key, value )
192                  *    Sets the value of a key.
193                  *
194                  * param( map )
195                  *    Sets values for a map of data.
196                  */
197                 param: function( key, value ) {
198                         if ( arguments.length === 1 && typeof key === 'string' )
199                                 return this.uploader.settings.multipart_params[ key ];
200
201                         if ( arguments.length > 1 ) {
202                                 this.uploader.settings.multipart_params[ key ] = value;
203                         } else {
204                                 $.extend( this.uploader.settings.multipart_params, key );
205                         }
206                 },
207
208                 init:     function() {},
209                 error:    function() {},
210                 success:  function() {},
211                 added:    function() {},
212                 progress: function() {},
213                 complete: function() {},
214                 refresh:  function() {
215                         this.uploader.refresh();
216                 }
217         });
218
219         exports.Uploader = Uploader;
220 })( wp, jQuery );