1 var topWin = window.dialogArguments || opener || parent || top, uploader, uploader_init;
3 function fileDialogStart() {
4 jQuery("#media-upload-error").empty();
7 // progress and success handlers for media multi uploads
8 function fileQueued(fileObj) {
9 // Get rid of unused form
10 jQuery('.media-blank').remove();
12 var items = jQuery('#media-items').children(), postid = post_id || 0;
14 // Collapse a single item
15 if ( items.length == 1 ) {
16 items.removeClass('open').find('.slidetoggle').slideUp(200);
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>');
22 jQuery('#insert-gallery').prop('disabled', true);
25 function uploadStart() {
27 if ( typeof topWin.tb_remove != 'undefined' )
28 topWin.jQuery('#TB_overlay').unbind('click', topWin.tb_remove);
34 function uploadProgress(up, file) {
35 var item = jQuery('#media-item-' + file.id);
37 jQuery('.bar', item).width( (200 * file.loaded) / file.size );
38 jQuery('.percent', item).html( file.percent + '%' );
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);
45 if ( max > hundredmb && file.size > hundredmb ) {
46 setTimeout(function(){
49 if ( file.status < 3 && file.loaded == 0 ) { // not uploading
50 wpFileError(file, pluploadL10n.big_upload_failed.replace('%1$s', '<a class="uploader-html" href="#">').replace('%2$s', '</a>'));
51 up.stop(); // stops the whole queue
53 up.start(); // restart the queue
55 }, 10000); // wait for 10 sec. for the file to start uploading
59 function updateMediaForm() {
60 var items = jQuery('#media-items').children();
62 // Just one file, no need for collapsible part
63 if ( items.length == 1 ) {
64 items.addClass('open').find('.slidetoggle').show();
65 jQuery('.insert-gallery').hide();
66 } else if ( items.length > 1 ) {
67 items.removeClass('open');
68 // Only show Gallery button when there are at least two files.
69 jQuery('.insert-gallery').show();
72 // Only show Save buttons when there is at least one file.
73 if ( items.not('.media-blank').length > 0 )
74 jQuery('.savebutton').show();
76 jQuery('.savebutton').hide();
79 function uploadSuccess(fileObj, serverData) {
80 var item = jQuery('#media-item-' + fileObj.id);
82 // on success serverData should be numeric, fix bug in html4 runtime returning the serverData wrapped in a <pre> tag
83 serverData = serverData.replace(/^<pre>(\d+)<\/pre>$/, '$1');
85 // if async-upload returned an error message, place it in the media item div and return
86 if ( serverData.match('media-upload-error') ) {
87 item.html(serverData);
90 jQuery('.percent', item).html( pluploadL10n.crunching );
93 prepareMediaItem(fileObj, serverData);
96 // Increment the counter.
97 if ( post_id && item.hasClass('child-of-' + post_id) )
98 jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);
101 function setResize(arg) {
103 if ( uploader.features.jpgresize )
104 uploader.settings['resize'] = { width: resize_width, height: resize_height, quality: 100 };
106 uploader.settings.multipart_params.image_resize = true;
108 delete(uploader.settings.resize);
109 delete(uploader.settings.multipart_params.image_resize);
113 function prepareMediaItem(fileObj, serverData) {
114 var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id);
117 if ( typeof topWin.tb_remove != 'undefined' )
118 topWin.jQuery('#TB_overlay').click(topWin.tb_remove);
121 if ( isNaN(serverData) || !serverData ) { // Old style: Append the HTML returned by the server -- thumbnail and form inputs
122 item.append(serverData);
123 prepareMediaItemInit(fileObj);
124 } else { // New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
125 item.load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
129 function prepareMediaItemInit(fileObj) {
130 var item = jQuery('#media-item-' + fileObj.id);
131 // Clone the thumbnail as a "pinkynail" -- a tiny image to the left of the filename
132 jQuery('.thumbnail', item).clone().attr('class', 'pinkynail toggle').prependTo(item);
134 // Replace the original filename with the new (unique) one assigned during upload
135 jQuery('.filename.original', item).replaceWith( jQuery('.filename.new', item) );
137 // Bind AJAX to the new Delete button
138 jQuery('a.delete', item).click(function(){
139 // Tell the server to delete it. TODO: handle exceptions
141 url: 'admin-ajax.php',
143 success: deleteSuccess,
147 id : this.id.replace(/[^0-9]/g, ''),
148 action : 'trash-post',
149 _ajax_nonce : this.href.replace(/^.*wpnonce=/,'')
155 // Bind AJAX to the new Undo button
156 jQuery('a.undo', item).click(function(){
157 // Tell the server to untrash it. TODO: handle exceptions
159 url: 'admin-ajax.php',
163 id : this.id.replace(/[^0-9]/g,''),
164 action: 'untrash-post',
165 _ajax_nonce: this.href.replace(/^.*wpnonce=/,'')
167 success: function(data, textStatus){
168 var item = jQuery('#media-item-' + fileObj.id);
170 if ( type = jQuery('#type-of-' + fileObj.id).val() )
171 jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-0+1);
173 if ( post_id && item.hasClass('child-of-'+post_id) )
174 jQuery('#attachments-count').text(jQuery('#attachments-count').text()-0+1);
176 jQuery('.filename .trashnotice', item).remove();
177 jQuery('.filename .title', item).css('font-weight','normal');
178 jQuery('a.undo', item).addClass('hidden');
179 jQuery('.menu_order_input', item).show();
180 item.css( {backgroundColor:'#ceb'} ).animate( {backgroundColor: '#fff'}, { queue: false, duration: 500, complete: function(){ jQuery(this).css({backgroundColor:''}); } }).removeClass('undo');
186 // Open this item if it says to start open (e.g. to display an error)
187 jQuery('#media-item-' + fileObj.id + '.startopen').removeClass('startopen').addClass('open').find('slidetoggle').fadeIn();
190 // generic error message
191 function wpQueueError(message) {
192 jQuery('#media-upload-error').show().html( '<div class="error"><p>' + message + '</p></div>' );
195 // file-specific error messages
196 function wpFileError(fileObj, message) {
197 itemAjaxError(fileObj.id, message);
200 function itemAjaxError(id, message) {
201 var item = jQuery('#media-item-' + id), filename = item.find('.filename').text(), last_err = item.data('last-err');
203 if ( last_err == id ) // prevent firing an error for the same file twice
206 item.html('<div class="error-div">'
207 + '<a class="dismiss" href="#">' + pluploadL10n.dismiss + '</a>'
208 + '<strong>' + pluploadL10n.error_uploading.replace('%s', jQuery.trim(filename)) + '</strong> '
210 + '</div>').data('last-err', id);
213 function deleteSuccess(data, textStatus) {
215 return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
218 return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
220 var id = this.id, item = jQuery('#media-item-' + id);
222 // Decrement the counters.
223 if ( type = jQuery('#type-of-' + id).val() )
224 jQuery('#' + type + '-counter').text( jQuery('#' + type + '-counter').text() - 1 );
226 if ( post_id && item.hasClass('child-of-'+post_id) )
227 jQuery('#attachments-count').text( jQuery('#attachments-count').text() - 1 );
229 if ( jQuery('form.type-form #media-items').children().length == 1 && jQuery('.hidden', '#media-items').length > 0 ) {
230 jQuery('.toggle').toggle();
231 jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
235 jQuery('.toggle', item).toggle();
236 jQuery('.slidetoggle', item).slideUp(200).siblings().removeClass('hidden');
237 item.css( {backgroundColor:'#faa'} ).animate( {backgroundColor:'#f4f4f4'}, {queue:false, duration:500} ).addClass('undo');
239 jQuery('.filename:empty', item).remove();
240 jQuery('.filename .title', item).css('font-weight','bold');
241 jQuery('.filename', item).append('<span class="trashnotice"> ' + pluploadL10n.deleted + ' </span>').siblings('a.toggle').hide();
242 jQuery('.filename', item).append( jQuery('a.undo', item).removeClass('hidden') );
243 jQuery('.menu_order_input', item).hide();
248 function deleteError(X, textStatus, errorThrown) {
252 function uploadComplete() {
253 jQuery('#insert-gallery').prop('disabled', false);
256 function switchUploader(s) {
258 deleteUserSetting('uploader');
259 jQuery('.media-upload-form').removeClass('html-uploader');
261 if ( typeof(uploader) == 'object' )
264 setUserSetting('uploader', '1'); // 1 == html uploader
265 jQuery('.media-upload-form').addClass('html-uploader');
269 function dndHelper(s) {
270 var d = document.getElementById('dnd-helper');
273 d.style.display = 'block';
275 d.style.display = 'none';
279 function uploadError(fileObj, errorCode, message, uploader) {
280 var hundredmb = 100 * 1024 * 1024, max;
283 case plupload.FAILED:
284 wpFileError(fileObj, pluploadL10n.upload_failed);
286 case plupload.FILE_EXTENSION_ERROR:
287 wpFileError(fileObj, pluploadL10n.invalid_filetype);
289 case plupload.FILE_SIZE_ERROR:
290 uploadSizeError(uploader, fileObj);
292 case plupload.IMAGE_FORMAT_ERROR:
293 wpFileError(fileObj, pluploadL10n.not_an_image);
295 case plupload.IMAGE_MEMORY_ERROR:
296 wpFileError(fileObj, pluploadL10n.image_memory_exceeded);
298 case plupload.IMAGE_DIMENSIONS_ERROR:
299 wpFileError(fileObj, pluploadL10n.image_dimensions_exceeded);
301 case plupload.GENERIC_ERROR:
302 wpQueueError(pluploadL10n.upload_failed);
304 case plupload.IO_ERROR:
305 max = parseInt(uploader.settings.max_file_size, 10);
307 if ( max > hundredmb && fileObj.size > hundredmb )
308 wpFileError(fileObj, pluploadL10n.big_upload_failed.replace('%1$s', '<a class="uploader-html" href="#">').replace('%2$s', '</a>'));
310 wpQueueError(pluploadL10n.io_error);
312 case plupload.HTTP_ERROR:
313 wpQueueError(pluploadL10n.http_error);
315 case plupload.INIT_ERROR:
316 jQuery('.media-upload-form').addClass('html-uploader');
318 case plupload.SECURITY_ERROR:
319 wpQueueError(pluploadL10n.security_error);
321 /* case plupload.UPLOAD_ERROR.UPLOAD_STOPPED:
322 case plupload.UPLOAD_ERROR.FILE_CANCELLED:
323 jQuery('#media-item-' + fileObj.id).remove();
326 wpFileError(fileObj, pluploadL10n.default_error);
330 function uploadSizeError( up, file, over100mb ) {
334 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>');
336 message = pluploadL10n.file_exceeds_size_limit.replace('%s', file.name);
338 jQuery('#media-items').append('<div id="media-item-' + file.id + '" class="media-item error"><p>' + message + '</p></div>');
342 jQuery(document).ready(function($){
343 $('.media-upload-form').bind('click.uploader', function(e) {
344 var target = $(e.target), tr, c;
346 if ( target.is('input[type="radio"]') ) { // remember the last used image size and alignment
347 tr = target.closest('tr');
349 if ( $(tr).hasClass('align') )
350 setUserSetting('align', target.val());
351 else if ( $(tr).hasClass('image-size') )
352 setUserSetting('imgsize', target.val());
354 } else if ( target.is('button.button') ) { // remember the last used image link url
355 c = e.target.className || '';
356 c = c.match(/url([^ '"]+)/);
359 setUserSetting('urlbutton', c[1]);
360 target.siblings('.urlfield').val( target.attr('title') );
362 } else if ( target.is('a.dismiss') ) {
363 target.parents('.media-item').fadeOut(200, function(){
366 } else if ( target.is('.upload-flash-bypass a') || target.is('a.uploader-html') ) { // switch uploader to html4
367 $('#media-items, p.submit, span.big-file-warning').css('display', 'none');
370 } else if ( target.is('.upload-html-bypass a') ) { // switch uploader to multi-file
371 $('#media-items, p.submit, span.big-file-warning').css('display', '');
374 } else if ( target.is('a.describe-toggle-on') ) { // Show
375 target.parent().addClass('open');
376 target.siblings('.slidetoggle').fadeIn(250, function(){
377 var S = $(window).scrollTop(), H = $(window).height(), top = $(this).offset().top, h = $(this).height(), b, B;
379 if ( H && top && h ) {
384 if ( b - B < top - S )
385 window.scrollBy(0, (b - B) + 10);
387 window.scrollBy(0, top - S - 40);
392 } else if ( target.is('a.describe-toggle-off') ) { // Hide
393 target.siblings('.slidetoggle').fadeOut(250, function(){
394 target.parent().removeClass('open');
400 // init and set the uploader
401 uploader_init = function() {
402 uploader = new plupload.Uploader(wpUploaderInit);
404 $('#image_resize').bind('change', function() {
405 var arg = $(this).prop('checked');
410 setUserSetting('upload_resize', '1');
412 deleteUserSetting('upload_resize');
415 uploader.bind('Init', function(up) {
416 var uploaddiv = $('#plupload-upload-ui');
418 setResize( getUserSetting('upload_resize', false) );
420 if ( up.features.dragdrop ) {
421 uploaddiv.addClass('drag-drop');
422 $('#drag-drop-area').bind('dragover.wp-uploader', function(){ // dragenter doesn't fire right :(
423 uploaddiv.addClass('drag-over');
424 }).bind('dragleave.wp-uploader, drop.wp-uploader', function(){
425 uploaddiv.removeClass('drag-over');
428 uploaddiv.removeClass('drag-drop');
429 $('#drag-drop-area').unbind('.wp-uploader');
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) {
456 uploader.bind('UploadFile', function(up, file) {
457 fileUploading(up, file);
460 uploader.bind('UploadProgress', function(up, file) {
461 uploadProgress(up, file);
464 uploader.bind('Error', function(up, err) {
465 uploadError(err.file, err.code, err.message, up);
469 uploader.bind('FileUploaded', function(up, file, response) {
470 uploadSuccess(file, response.response);
473 uploader.bind('UploadComplete', function(up, files) {
478 if ( typeof(wpUploaderInit) == 'object' )