1 /* global plupload, pluploadL10n, ajaxurl, post_id, wpUploaderInit, deleteUserSetting, setUserSetting, getUserSetting */
2 /* global resize_width, resize_height, shortform */
3 var topWin = window.dialogArguments || opener || parent || top, uploader, uploader_init;
5 // progress and success handlers for media multi uploads
6 function fileQueued(fileObj) {
7 // Get rid of unused form
8 jQuery('.media-blank').remove();
10 var items = jQuery('#media-items').children(), postid = post_id || 0;
12 // Collapse a single item
13 if ( items.length == 1 ) {
14 items.removeClass('open').find('.slidetoggle').slideUp(200);
16 // Create a progress bar containing the filename
17 jQuery('<div class="media-item">')
18 .attr( 'id', 'media-item-' + fileObj.id )
19 .addClass('child-of-' + postid)
20 .append('<div class="progress"><div class="percent">0%</div><div class="bar"></div></div>',
21 jQuery('<div class="filename original">').text( ' ' + fileObj.name ))
22 .appendTo( jQuery('#media-items' ) );
25 jQuery('#insert-gallery').prop('disabled', true);
28 function uploadStart() {
30 if ( typeof topWin.tb_remove != 'undefined' )
31 topWin.jQuery('#TB_overlay').unbind('click', topWin.tb_remove);
37 function uploadProgress(up, file) {
38 var item = jQuery('#media-item-' + file.id);
40 jQuery('.bar', item).width( (200 * file.loaded) / file.size );
41 jQuery('.percent', item).html( file.percent + '%' );
44 // check to see if a large file failed to upload
45 function fileUploading(up, file) {
46 var hundredmb = 100 * 1024 * 1024, max = parseInt(up.settings.max_file_size, 10);
48 if ( max > hundredmb && file.size > hundredmb ) {
49 setTimeout(function(){
51 if ( file.status < 3 && file.loaded === 0 ) { // not uploading
52 wpFileError(file, pluploadL10n.big_upload_failed.replace('%1$s', '<a class="uploader-html" href="#">').replace('%2$s', '</a>'));
53 up.stop(); // stops the whole queue
55 up.start(); // restart the queue
57 }, 10000); // wait for 10 sec. for the file to start uploading
61 function updateMediaForm() {
62 var items = jQuery('#media-items').children();
64 // Just one file, no need for collapsible part
65 if ( items.length == 1 ) {
66 items.addClass('open').find('.slidetoggle').show();
67 jQuery('.insert-gallery').hide();
68 } else if ( items.length > 1 ) {
69 items.removeClass('open');
70 // Only show Gallery button when there are at least two files.
71 jQuery('.insert-gallery').show();
74 // Only show Save buttons when there is at least one file.
75 if ( items.not('.media-blank').length > 0 )
76 jQuery('.savebutton').show();
78 jQuery('.savebutton').hide();
81 function uploadSuccess(fileObj, serverData) {
82 var item = jQuery('#media-item-' + fileObj.id);
84 // on success serverData should be numeric, fix bug in html4 runtime returning the serverData wrapped in a <pre> tag
85 serverData = serverData.replace(/^<pre>(\d+)<\/pre>$/, '$1');
87 // if async-upload returned an error message, place it in the media item div and return
88 if ( serverData.match(/media-upload-error|error-div/) ) {
89 item.html(serverData);
92 jQuery('.percent', item).html( pluploadL10n.crunching );
95 prepareMediaItem(fileObj, serverData);
98 // Increment the counter.
99 if ( post_id && item.hasClass('child-of-' + post_id) )
100 jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);
103 function setResize(arg) {
105 if ( uploader.features.jpgresize )
106 uploader.settings.resize = { width: resize_width, height: resize_height, quality: 100 };
108 uploader.settings.multipart_params.image_resize = true;
110 delete(uploader.settings.resize);
111 delete(uploader.settings.multipart_params.image_resize);
115 function prepareMediaItem(fileObj, serverData) {
116 var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id);
117 if ( f == 2 && shortform > 2 )
121 if ( typeof topWin.tb_remove != 'undefined' )
122 topWin.jQuery('#TB_overlay').click(topWin.tb_remove);
125 if ( isNaN(serverData) || !serverData ) { // Old style: Append the HTML returned by the server -- thumbnail and form inputs
126 item.append(serverData);
127 prepareMediaItemInit(fileObj);
128 } else { // New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
129 item.load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm();});
133 function prepareMediaItemInit(fileObj) {
134 var item = jQuery('#media-item-' + fileObj.id);
135 // Clone the thumbnail as a "pinkynail" -- a tiny image to the left of the filename
136 jQuery('.thumbnail', item).clone().attr('class', 'pinkynail toggle').prependTo(item);
138 // Replace the original filename with the new (unique) one assigned during upload
139 jQuery('.filename.original', item).replaceWith( jQuery('.filename.new', item) );
141 // Bind AJAX to the new Delete button
142 jQuery('a.delete', item).click(function(){
143 // Tell the server to delete it. TODO: handle exceptions
147 success: deleteSuccess,
151 id : this.id.replace(/[^0-9]/g, ''),
152 action : 'trash-post',
153 _ajax_nonce : this.href.replace(/^.*wpnonce=/,'')
159 // Bind AJAX to the new Undo button
160 jQuery('a.undo', item).click(function(){
161 // Tell the server to untrash it. TODO: handle exceptions
167 id : this.id.replace(/[^0-9]/g,''),
168 action: 'untrash-post',
169 _ajax_nonce: this.href.replace(/^.*wpnonce=/,'')
171 success: function( ){
173 item = jQuery('#media-item-' + fileObj.id);
175 if ( type = jQuery('#type-of-' + fileObj.id).val() )
176 jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-0+1);
178 if ( post_id && item.hasClass('child-of-'+post_id) )
179 jQuery('#attachments-count').text(jQuery('#attachments-count').text()-0+1);
181 jQuery('.filename .trashnotice', item).remove();
182 jQuery('.filename .title', item).css('font-weight','normal');
183 jQuery('a.undo', item).addClass('hidden');
184 jQuery('.menu_order_input', item).show();
185 item.css( {backgroundColor:'#ceb'} ).animate( {backgroundColor: '#fff'}, { queue: false, duration: 500, complete: function(){ jQuery(this).css({backgroundColor:''}); } }).removeClass('undo');
191 // Open this item if it says to start open (e.g. to display an error)
192 jQuery('#media-item-' + fileObj.id + '.startopen').removeClass('startopen').addClass('open').find('slidetoggle').fadeIn();
195 // generic error message
196 function wpQueueError(message) {
197 jQuery('#media-upload-error').show().html( '<div class="error"><p>' + message + '</p></div>' );
200 // file-specific error messages
201 function wpFileError(fileObj, message) {
202 itemAjaxError(fileObj.id, message);
205 function itemAjaxError(id, message) {
206 var item = jQuery('#media-item-' + id), filename = item.find('.filename').text(), last_err = item.data('last-err');
208 if ( last_err == id ) // prevent firing an error for the same file twice
211 item.html('<div class="error-div">' +
212 '<a class="dismiss" href="#">' + pluploadL10n.dismiss + '</a>' +
213 '<strong>' + pluploadL10n.error_uploading.replace('%s', jQuery.trim(filename)) + '</strong> ' +
215 '</div>').data('last-err', id);
218 function deleteSuccess(data) {
221 return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
224 return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
227 item = jQuery('#media-item-' + id);
229 // Decrement the counters.
230 if ( type = jQuery('#type-of-' + id).val() )
231 jQuery('#' + type + '-counter').text( jQuery('#' + type + '-counter').text() - 1 );
233 if ( post_id && item.hasClass('child-of-'+post_id) )
234 jQuery('#attachments-count').text( jQuery('#attachments-count').text() - 1 );
236 if ( jQuery('form.type-form #media-items').children().length == 1 && jQuery('.hidden', '#media-items').length > 0 ) {
237 jQuery('.toggle').toggle();
238 jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
242 jQuery('.toggle', item).toggle();
243 jQuery('.slidetoggle', item).slideUp(200).siblings().removeClass('hidden');
244 item.css( {backgroundColor:'#faa'} ).animate( {backgroundColor:'#f4f4f4'}, {queue:false, duration:500} ).addClass('undo');
246 jQuery('.filename:empty', item).remove();
247 jQuery('.filename .title', item).css('font-weight','bold');
248 jQuery('.filename', item).append('<span class="trashnotice"> ' + pluploadL10n.deleted + ' </span>').siblings('a.toggle').hide();
249 jQuery('.filename', item).append( jQuery('a.undo', item).removeClass('hidden') );
250 jQuery('.menu_order_input', item).hide();
255 function deleteError() {
259 function uploadComplete() {
260 jQuery('#insert-gallery').prop('disabled', false);
263 function switchUploader(s) {
265 deleteUserSetting('uploader');
266 jQuery('.media-upload-form').removeClass('html-uploader');
268 if ( typeof(uploader) == 'object' )
271 setUserSetting('uploader', '1'); // 1 == html uploader
272 jQuery('.media-upload-form').addClass('html-uploader');
276 function uploadError(fileObj, errorCode, message, uploader) {
277 var hundredmb = 100 * 1024 * 1024, max;
280 case plupload.FAILED:
281 wpFileError(fileObj, pluploadL10n.upload_failed);
283 case plupload.FILE_EXTENSION_ERROR:
284 wpFileError(fileObj, pluploadL10n.invalid_filetype);
286 case plupload.FILE_SIZE_ERROR:
287 uploadSizeError(uploader, fileObj);
289 case plupload.IMAGE_FORMAT_ERROR:
290 wpFileError(fileObj, pluploadL10n.not_an_image);
292 case plupload.IMAGE_MEMORY_ERROR:
293 wpFileError(fileObj, pluploadL10n.image_memory_exceeded);
295 case plupload.IMAGE_DIMENSIONS_ERROR:
296 wpFileError(fileObj, pluploadL10n.image_dimensions_exceeded);
298 case plupload.GENERIC_ERROR:
299 wpQueueError(pluploadL10n.upload_failed);
301 case plupload.IO_ERROR:
302 max = parseInt(uploader.settings.max_file_size, 10);
304 if ( max > hundredmb && fileObj.size > hundredmb )
305 wpFileError(fileObj, pluploadL10n.big_upload_failed.replace('%1$s', '<a class="uploader-html" href="#">').replace('%2$s', '</a>'));
307 wpQueueError(pluploadL10n.io_error);
309 case plupload.HTTP_ERROR:
310 wpQueueError(pluploadL10n.http_error);
312 case plupload.INIT_ERROR:
313 jQuery('.media-upload-form').addClass('html-uploader');
315 case plupload.SECURITY_ERROR:
316 wpQueueError(pluploadL10n.security_error);
318 /* case plupload.UPLOAD_ERROR.UPLOAD_STOPPED:
319 case plupload.UPLOAD_ERROR.FILE_CANCELLED:
320 jQuery('#media-item-' + fileObj.id).remove();
323 wpFileError(fileObj, pluploadL10n.default_error);
327 function uploadSizeError( up, file, over100mb ) {
331 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 message = pluploadL10n.file_exceeds_size_limit.replace('%s', file.name);
335 jQuery('#media-items').append('<div id="media-item-' + file.id + '" class="media-item error"><p>' + message + '</p></div>');
339 jQuery(document).ready(function($){
340 $('.media-upload-form').bind('click.uploader', function(e) {
341 var target = $(e.target), tr, c;
343 if ( target.is('input[type="radio"]') ) { // remember the last used image size and alignment
344 tr = target.closest('tr');
346 if ( tr.hasClass('align') )
347 setUserSetting('align', target.val());
348 else if ( tr.hasClass('image-size') )
349 setUserSetting('imgsize', target.val());
351 } else if ( target.is('button.button') ) { // remember the last used image link url
352 c = e.target.className || '';
353 c = c.match(/url([^ '"]+)/);
356 setUserSetting('urlbutton', c[1]);
357 target.siblings('.urlfield').val( target.data('link-url') );
359 } else if ( target.is('a.dismiss') ) {
360 target.parents('.media-item').fadeOut(200, function(){
363 } else if ( target.is('.upload-flash-bypass a') || target.is('a.uploader-html') ) { // switch uploader to html4
364 $('#media-items, p.submit, span.big-file-warning').css('display', 'none');
367 } else if ( target.is('.upload-html-bypass a') ) { // switch uploader to multi-file
368 $('#media-items, p.submit, span.big-file-warning').css('display', '');
371 } else if ( target.is('a.describe-toggle-on') ) { // Show
372 target.parent().addClass('open');
373 target.siblings('.slidetoggle').fadeIn(250, function(){
374 var S = $(window).scrollTop(), H = $(window).height(), top = $(this).offset().top, h = $(this).height(), b, B;
376 if ( H && top && h ) {
381 if ( b - B < top - S )
382 window.scrollBy(0, (b - B) + 10);
384 window.scrollBy(0, top - S - 40);
389 } else if ( target.is('a.describe-toggle-off') ) { // Hide
390 target.siblings('.slidetoggle').fadeOut(250, function(){
391 target.parent().removeClass('open');
397 // init and set the uploader
398 uploader_init = function() {
399 uploader = new plupload.Uploader(wpUploaderInit);
401 $('#image_resize').bind('change', function() {
402 var arg = $(this).prop('checked');
407 setUserSetting('upload_resize', '1');
409 deleteUserSetting('upload_resize');
412 uploader.bind('Init', function(up) {
413 var uploaddiv = $('#plupload-upload-ui');
415 setResize( getUserSetting('upload_resize', false) );
417 if ( up.features.dragdrop && ! $(document.body).hasClass('mobile') ) {
418 uploaddiv.addClass('drag-drop');
419 $('#drag-drop-area').bind('dragover.wp-uploader', function(){ // dragenter doesn't fire right :(
420 uploaddiv.addClass('drag-over');
421 }).bind('dragleave.wp-uploader, drop.wp-uploader', function(){
422 uploaddiv.removeClass('drag-over');
425 uploaddiv.removeClass('drag-drop');
426 $('#drag-drop-area').unbind('.wp-uploader');
429 if ( up.runtime == 'html4' )
430 $('.upload-flash-bypass').hide();
435 uploader.bind('FilesAdded', function(up, files) {
436 var hundredmb = 100 * 1024 * 1024, max = parseInt(up.settings.max_file_size, 10);
438 $('#media-upload-error').html('');
441 plupload.each(files, function(file){
442 if ( max > hundredmb && file.size > hundredmb && up.runtime != 'html5' )
443 uploadSizeError( up, file, true );
452 // uploader.bind('BeforeUpload', function(up, file) {});
454 uploader.bind('UploadFile', function(up, file) {
455 fileUploading(up, file);
458 uploader.bind('UploadProgress', function(up, file) {
459 uploadProgress(up, file);
462 uploader.bind('Error', function(up, err) {
463 uploadError(err.file, err.code, err.message, up);
467 uploader.bind('FileUploaded', function(up, file, response) {
468 uploadSuccess(file, response.response);
471 uploader.bind('UploadComplete', function() {
476 if ( typeof(wpUploaderInit) == 'object' ) {