]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/plupload/handlers.dev.js
Wordpress 3.3-scripts
[autoinstalls/wordpress.git] / wp-includes / js / plupload / handlers.dev.js
1 var topWin = window.dialogArguments || opener || parent || top, uploader, uploader_init;
2
3 function fileDialogStart() {
4         jQuery("#media-upload-error").empty();
5 }
6
7 // progress and success handlers for media multi uploads
8 function fileQueued(fileObj) {
9         // Get rid of unused form
10         jQuery('.media-blank').remove();
11
12         var items = jQuery('#media-items').children(), postid = post_id || 0;
13
14         // Collapse a single item
15         if ( items.length == 1 ) {
16                 items.removeClass('open').find('.slidetoggle').slideUp(200);
17         }
18         // Create a progress bar containing the filename
19         jQuery('#media-items').append('<div id="media-item-' + fileObj.id + '" class="media-item child-of-' + postid + '"><div class="progress"><div class="percent">0%</div><div class="bar"></div></div><div class="filename original"> ' + fileObj.name + '</div></div>');
20
21         // Disable submit
22         jQuery('#insert-gallery').prop('disabled', true);
23 }
24
25 function uploadStart() {
26         try {
27                 if ( typeof topWin.tb_remove != 'undefined' )
28                         topWin.jQuery('#TB_overlay').unbind('click', topWin.tb_remove);
29         } catch(e){}
30
31         return true;
32 }
33
34 function uploadProgress(up, file) {
35         var item = jQuery('#media-item-' + file.id);
36
37         jQuery('.bar', item).width( (200 * file.loaded) / file.size );
38         jQuery('.percent', item).html( file.percent + '%' );
39 }
40
41 // check to see if a large file failed to upload
42 function fileUploading(up, file) {
43         var hundredmb = 100 * 1024 * 1024, max = parseInt(up.settings.max_file_size, 10);
44
45         if ( max > hundredmb && file.size > hundredmb ) {
46                 setTimeout(function(){
47                         if ( file.status == 2 && file.loaded == 0 ) { // not uploading
48                                 wpFileError(file, pluploadL10n.big_upload_failed.replace('%1$s', '<a class="uploader-html" href="#">').replace('%2$s', '</a>'));
49
50                                 if ( up.current && up.current.file.id == file.id && up.current.xhr.abort )
51                                         up.current.xhr.abort();
52                         }
53                 }, 10000); // wait for 10 sec. for the file to start uploading
54         }
55 }
56
57 function updateMediaForm() {
58         var items = jQuery('#media-items').children();
59
60         // Just one file, no need for collapsible part
61         if ( items.length == 1 ) {
62                 items.addClass('open').find('.slidetoggle').show();
63                 jQuery('.insert-gallery').hide();
64         } else if ( items.length > 1 ) {
65                 items.removeClass('open');
66                 // Only show Gallery button when there are at least two files.
67                 jQuery('.insert-gallery').show();
68         }
69
70         // Only show Save buttons when there is at least one file.
71         if ( items.not('.media-blank').length > 0 )
72                 jQuery('.savebutton').show();
73         else
74                 jQuery('.savebutton').hide();
75 }
76
77 function uploadSuccess(fileObj, serverData) {
78         var item = jQuery('#media-item-' + fileObj.id);
79
80         // on success serverData should be numeric, fix bug in html4 runtime returning the serverData wrapped in a <pre> tag
81         serverData = serverData.replace(/^<pre>(\d+)<\/pre>$/, '$1');
82
83         // if async-upload returned an error message, place it in the media item div and return
84         if ( serverData.match('media-upload-error') ) {
85                 item.html(serverData);
86                 return;
87         } else {
88                 jQuery('.percent', item).html( pluploadL10n.crunching );
89         }
90
91         prepareMediaItem(fileObj, serverData);
92         updateMediaForm();
93
94         // Increment the counter.
95         if ( post_id && item.hasClass('child-of-' + post_id) )
96                 jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);
97 }
98
99 function setResize(arg) {
100         if ( arg ) {
101                 if ( uploader.features.jpgresize )
102                         uploader.settings['resize'] = { width: resize_width, height: resize_height, quality: 100 };
103                 else
104                         uploader.settings.multipart_params.image_resize = true;
105         } else {
106                 delete(uploader.settings.resize);
107                 delete(uploader.settings.multipart_params.image_resize);
108         }
109 }
110
111 function prepareMediaItem(fileObj, serverData) {
112         var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id);
113
114         try {
115                 if ( typeof topWin.tb_remove != 'undefined' )
116                         topWin.jQuery('#TB_overlay').click(topWin.tb_remove);
117         } catch(e){}
118
119         if ( isNaN(serverData) || !serverData ) { // Old style: Append the HTML returned by the server -- thumbnail and form inputs
120                 item.append(serverData);
121                 prepareMediaItemInit(fileObj);
122         } else { // New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
123                 item.load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
124         }
125 }
126
127 function prepareMediaItemInit(fileObj) {
128         var item = jQuery('#media-item-' + fileObj.id);
129         // Clone the thumbnail as a "pinkynail" -- a tiny image to the left of the filename
130         jQuery('.thumbnail', item).clone().attr('class', 'pinkynail toggle').prependTo(item);
131
132         // Replace the original filename with the new (unique) one assigned during upload
133         jQuery('.filename.original', item).replaceWith( jQuery('.filename.new', item) );
134
135         // Bind AJAX to the new Delete button
136         jQuery('a.delete', item).click(function(){
137                 // Tell the server to delete it. TODO: handle exceptions
138                 jQuery.ajax({
139                         url: 'admin-ajax.php',
140                         type: 'post',
141                         success: deleteSuccess,
142                         error: deleteError,
143                         id: fileObj.id,
144                         data: {
145                                 id : this.id.replace(/[^0-9]/g, ''),
146                                 action : 'trash-post',
147                                 _ajax_nonce : this.href.replace(/^.*wpnonce=/,'')
148                         }
149                 });
150                 return false;
151         });
152
153         // Bind AJAX to the new Undo button
154         jQuery('a.undo', item).click(function(){
155                 // Tell the server to untrash it. TODO: handle exceptions
156                 jQuery.ajax({
157                         url: 'admin-ajax.php',
158                         type: 'post',
159                         id: fileObj.id,
160                         data: {
161                                 id : this.id.replace(/[^0-9]/g,''),
162                                 action: 'untrash-post',
163                                 _ajax_nonce: this.href.replace(/^.*wpnonce=/,'')
164                         },
165                         success: function(data, textStatus){
166                                 var item = jQuery('#media-item-' + fileObj.id);
167
168                                 if ( type = jQuery('#type-of-' + fileObj.id).val() )
169                                         jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-0+1);
170
171                                 if ( post_id && item.hasClass('child-of-'+post_id) )
172                                         jQuery('#attachments-count').text(jQuery('#attachments-count').text()-0+1);
173
174                                 jQuery('.filename .trashnotice', item).remove();
175                                 jQuery('.filename .title', item).css('font-weight','normal');
176                                 jQuery('a.undo', item).addClass('hidden');
177                                 jQuery('.menu_order_input', item).show();
178                                 item.css( {backgroundColor:'#ceb'} ).animate( {backgroundColor: '#fff'}, { queue: false, duration: 500, complete: function(){ jQuery(this).css({backgroundColor:''}); } }).removeClass('undo');
179                         }
180                 });
181                 return false;
182         });
183
184         // Open this item if it says to start open (e.g. to display an error)
185         jQuery('#media-item-' + fileObj.id + '.startopen').removeClass('startopen').addClass('open').find('slidetoggle').fadeIn();
186 }
187
188 // generic error message
189 function wpQueueError(message) {
190         jQuery('#media-upload-error').show().html( '<div class="error"><p>' + message + '</p></div>' );
191 }
192
193 // file-specific error messages
194 function wpFileError(fileObj, message) {
195         itemAjaxError(fileObj.id, message);
196 }
197
198 function itemAjaxError(id, message) {
199         var item = jQuery('#media-item-' + id), filename = item.find('.filename').text(), last_err = item.data('last-err');
200
201         if ( last_err == id ) // prevent firing an error for the same file twice
202                 return;
203
204         item.html('<div class="error-div">'
205                                 + '<a class="dismiss" href="#">' + pluploadL10n.dismiss + '</a>'
206                                 + '<strong>' + pluploadL10n.error_uploading.replace('%s', jQuery.trim(filename)) + '</strong> '
207                                 + message
208                                 + '</div>').data('last-err', id);
209 }
210
211 function deleteSuccess(data, textStatus) {
212         if ( data == '-1' )
213                 return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
214
215         if ( data == '0' )
216                 return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
217
218         var id = this.id, item = jQuery('#media-item-' + id);
219
220         // Decrement the counters.
221         if ( type = jQuery('#type-of-' + id).val() )
222                 jQuery('#' + type + '-counter').text( jQuery('#' + type + '-counter').text() - 1 );
223
224         if ( post_id && item.hasClass('child-of-'+post_id) )
225                 jQuery('#attachments-count').text( jQuery('#attachments-count').text() - 1 );
226
227         if ( jQuery('form.type-form #media-items').children().length == 1 && jQuery('.hidden', '#media-items').length > 0 ) {
228                 jQuery('.toggle').toggle();
229                 jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
230         }
231
232         // Vanish it.
233         jQuery('.toggle', item).toggle();
234         jQuery('.slidetoggle', item).slideUp(200).siblings().removeClass('hidden');
235         item.css( {backgroundColor:'#faa'} ).animate( {backgroundColor:'#f4f4f4'}, {queue:false, duration:500} ).addClass('undo');
236
237         jQuery('.filename:empty', item).remove();
238         jQuery('.filename .title', item).css('font-weight','bold');
239         jQuery('.filename', item).append('<span class="trashnotice"> ' + pluploadL10n.deleted + ' </span>').siblings('a.toggle').hide();
240         jQuery('.filename', item).append( jQuery('a.undo', item).removeClass('hidden') );
241         jQuery('.menu_order_input', item).hide();
242
243         return;
244 }
245
246 function deleteError(X, textStatus, errorThrown) {
247         // TODO
248 }
249
250 function uploadComplete() {
251         jQuery('#insert-gallery').prop('disabled', false);
252 }
253
254 function switchUploader(s) {
255         if ( s ) {
256                 deleteUserSetting('uploader');
257                 jQuery('.media-upload-form').removeClass('html-uploader');
258
259                 if ( typeof(uploader) == 'object' )
260                         uploader.refresh();
261         } else {
262                 setUserSetting('uploader', '1'); // 1 == html uploader
263                 jQuery('.media-upload-form').addClass('html-uploader');
264         }
265 }
266
267 function dndHelper(s) {
268         var d = document.getElementById('dnd-helper');
269
270         if ( s ) {
271                 d.style.display = 'block';
272         } else {
273                 d.style.display = 'none';
274         }
275 }
276
277 function uploadError(fileObj, errorCode, message, uploader) {
278         var hundredmb = 100 * 1024 * 1024, max;
279
280         switch (errorCode) {
281                 case plupload.FAILED:
282                         wpFileError(fileObj, pluploadL10n.upload_failed);
283                         break;
284                 case plupload.FILE_EXTENSION_ERROR:
285                         wpFileError(fileObj, pluploadL10n.invalid_filetype);
286                         break;
287                 case plupload.FILE_SIZE_ERROR:
288                         uploadSizeError(uploader, fileObj);
289                         break;
290                 case plupload.IMAGE_FORMAT_ERROR:
291                         wpFileError(fileObj, pluploadL10n.not_an_image);
292                         break;
293                 case plupload.IMAGE_MEMORY_ERROR:
294                         wpFileError(fileObj, pluploadL10n.image_memory_exceeded);
295                         break;
296                 case plupload.IMAGE_DIMENSIONS_ERROR:
297                         wpFileError(fileObj, pluploadL10n.image_dimensions_exceeded);
298                         break;
299                 case plupload.GENERIC_ERROR:
300                         wpQueueError(pluploadL10n.upload_failed);
301                         break;
302                 case plupload.IO_ERROR:
303                         max = parseInt(uploader.settings.max_file_size, 10);
304
305                         if ( max > hundredmb && fileObj.size > hundredmb )
306                                 wpFileError(fileObj, pluploadL10n.big_upload_failed.replace('%1$s', '<a class="uploader-html" href="#">').replace('%2$s', '</a>'));
307                         else
308                                 wpQueueError(pluploadL10n.io_error);
309                         break;
310                 case plupload.HTTP_ERROR:
311                         wpQueueError(pluploadL10n.http_error);
312                         break;
313                 case plupload.INIT_ERROR:
314                         jQuery('.media-upload-form').addClass('html-uploader');
315                         break;
316                 case plupload.SECURITY_ERROR:
317                         wpQueueError(pluploadL10n.security_error);
318                         break;
319 /*              case plupload.UPLOAD_ERROR.UPLOAD_STOPPED:
320                 case plupload.UPLOAD_ERROR.FILE_CANCELLED:
321                         jQuery('#media-item-' + fileObj.id).remove();
322                         break;*/
323                 default:
324                         wpFileError(fileObj, pluploadL10n.default_error);
325         }
326 }
327
328 function uploadSizeError( up, file, over100mb ) {
329         var message;
330
331         if ( over100mb )
332                 message = pluploadL10n.big_upload_queued.replace('%s', file.name) + ' ' + pluploadL10n.big_upload_failed.replace('%1$s', '<a class="uploader-html" href="#">').replace('%2$s', '</a>');
333         else
334                 message = pluploadL10n.file_exceeds_size_limit.replace('%s', file.name);
335
336         jQuery('#media-items').append('<div id="media-item-' + file.id + '" class="media-item error"><p>' + message + '</p></div>');
337         up.removeFile(file);
338 }
339
340 jQuery(document).ready(function($){
341         $('.media-upload-form').bind('click.uploader', function(e) {
342                 var target = $(e.target), tr, c;
343
344                 if ( target.is('input[type="radio"]') ) { // remember the last used image size and alignment
345                         tr = target.closest('tr');
346
347                         if ( $(tr).hasClass('align') )
348                                 setUserSetting('align', target.val());
349                         else if ( $(tr).hasClass('image-size') )
350                                 setUserSetting('imgsize', target.val());
351
352                 } else if ( target.is('button.button') ) { // remember the last used image link url
353                         c = e.target.className || '';
354                         c = c.match(/url([^ '"]+)/);
355
356                         if ( c && c[1] ) {
357                                 setUserSetting('urlbutton', c[1]);
358                                 target.siblings('.urlfield').val( target.attr('title') );
359                         }
360                 } else if ( target.is('a.dismiss') ) {
361                         target.parents('.media-item').fadeOut(200, function(){
362                                 $(this).remove();
363                         });
364                 } else if ( target.is('.upload-flash-bypass a') || target.is('a.uploader-html') ) { // switch uploader to html4
365                         $('#media-items, p.submit, span.big-file-warning').css('display', 'none');
366                         switchUploader(0);
367                         return false;
368                 } else if ( target.is('.upload-html-bypass a') ) { // switch uploader to multi-file
369                         $('#media-items, p.submit, span.big-file-warning').css('display', '');
370                         switchUploader(1);
371                         return false;
372                 } else if ( target.is('a.describe-toggle-on') ) { // Show
373                         target.parent().addClass('open');
374                         target.siblings('.slidetoggle').fadeIn(250, function(){
375                                 var S = $(window).scrollTop(), H = $(window).height(), top = $(this).offset().top, h = $(this).height(), b, B;
376
377                                 if ( H && top && h ) {
378                                         b = top + h;
379                                         B = S + H;
380
381                                         if ( b > B ) {
382                                                 if ( b - B < top - S )
383                                                         window.scrollBy(0, (b - B) + 10);
384                                                 else
385                                                         window.scrollBy(0, top - S - 40);
386                                         }
387                                 }
388                         });
389                         return false;
390                 } else if ( target.is('a.describe-toggle-off') ) { // Hide
391                         target.siblings('.slidetoggle').fadeOut(250, function(){
392                                 target.parent().removeClass('open');
393                         });
394                         return false;
395                 }
396         });
397
398         // init and set the uploader
399         uploader_init = function() {
400                 uploader = new plupload.Uploader(wpUploaderInit);
401
402                 $('#image_resize').bind('change', function() {
403                         var arg = $(this).prop('checked');
404
405                         setResize( arg );
406
407                         if ( arg )
408                                 setUserSetting('upload_resize', '1');
409                         else
410                                 deleteUserSetting('upload_resize');
411                 });
412
413                 uploader.bind('Init', function(up) {
414                         var uploaddiv = $('#plupload-upload-ui');
415
416                         setResize( getUserSetting('upload_resize', false) );
417
418                         if ( up.features.dragdrop ) {
419                                 uploaddiv.addClass('drag-drop');
420                                 $('#drag-drop-area').bind('dragover.wp-uploader', function(){ // dragenter doesn't fire right :(
421                                         uploaddiv.addClass('drag-over');
422                                 }).bind('dragleave.wp-uploader, drop.wp-uploader', function(){
423                                         uploaddiv.removeClass('drag-over');
424                                 });
425                         } else {
426                                 uploaddiv.removeClass('drag-drop');
427                                 $('#drag-drop-area').unbind('.wp-uploader');
428                         }
429                 });
430
431                 uploader.init();
432
433                 uploader.bind('FilesAdded', function(up, files) {
434                         var hundredmb = 100 * 1024 * 1024, max = parseInt(up.settings.max_file_size, 10);
435
436                         $('#media-upload-error').html('');
437                         uploadStart();
438
439                         plupload.each(files, function(file){
440                                 if ( max > hundredmb && file.size > hundredmb && up.runtime != 'html5' )
441                                         uploadSizeError( up, file, true );
442                                 else
443                                         fileQueued(file);
444                         });
445
446                         up.refresh();
447                         up.start();
448                 });
449
450                 uploader.bind('BeforeUpload', function(up, file) {
451                         // something
452                 });
453
454                 uploader.bind('UploadFile', function(up, file) {
455                         fileUploading(up, file);
456                 });
457
458                 uploader.bind('UploadProgress', function(up, file) {
459                         uploadProgress(up, file);
460                 });
461
462                 uploader.bind('Error', function(up, err) {
463                         uploadError(err.file, err.code, err.message, up);
464                         up.refresh();
465                 });
466
467                 uploader.bind('FileUploaded', function(up, file, response) {
468                         uploadSuccess(file, response.response);
469                 });
470
471                 uploader.bind('UploadComplete', function(up, files) {
472                         uploadComplete();
473                 });
474         }
475
476         if ( typeof(wpUploaderInit) == 'object' )
477                 uploader_init();
478
479 });