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(){
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>'));
50 if ( up.current && up.current.file.id == file.id && up.current.xhr.abort )
51 up.current.xhr.abort();
53 }, 10000); // wait for 10 sec. for the file to start uploading
57 function updateMediaForm() {
58 var items = jQuery('#media-items').children();
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();
70 // Only show Save buttons when there is at least one file.
71 if ( items.not('.media-blank').length > 0 )
72 jQuery('.savebutton').show();
74 jQuery('.savebutton').hide();
77 function uploadSuccess(fileObj, serverData) {
78 var item = jQuery('#media-item-' + fileObj.id);
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');
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);
88 jQuery('.percent', item).html( pluploadL10n.crunching );
91 prepareMediaItem(fileObj, serverData);
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);
99 function setResize(arg) {
101 if ( uploader.features.jpgresize )
102 uploader.settings['resize'] = { width: resize_width, height: resize_height, quality: 100 };
104 uploader.settings.multipart_params.image_resize = true;
106 delete(uploader.settings.resize);
107 delete(uploader.settings.multipart_params.image_resize);
111 function prepareMediaItem(fileObj, serverData) {
112 var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id);
115 if ( typeof topWin.tb_remove != 'undefined' )
116 topWin.jQuery('#TB_overlay').click(topWin.tb_remove);
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()});
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);
132 // Replace the original filename with the new (unique) one assigned during upload
133 jQuery('.filename.original', item).replaceWith( jQuery('.filename.new', item) );
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
139 url: 'admin-ajax.php',
141 success: deleteSuccess,
145 id : this.id.replace(/[^0-9]/g, ''),
146 action : 'trash-post',
147 _ajax_nonce : this.href.replace(/^.*wpnonce=/,'')
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
157 url: 'admin-ajax.php',
161 id : this.id.replace(/[^0-9]/g,''),
162 action: 'untrash-post',
163 _ajax_nonce: this.href.replace(/^.*wpnonce=/,'')
165 success: function(data, textStatus){
166 var item = jQuery('#media-item-' + fileObj.id);
168 if ( type = jQuery('#type-of-' + fileObj.id).val() )
169 jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-0+1);
171 if ( post_id && item.hasClass('child-of-'+post_id) )
172 jQuery('#attachments-count').text(jQuery('#attachments-count').text()-0+1);
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');
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();
188 // generic error message
189 function wpQueueError(message) {
190 jQuery('#media-upload-error').show().html( '<div class="error"><p>' + message + '</p></div>' );
193 // file-specific error messages
194 function wpFileError(fileObj, message) {
195 itemAjaxError(fileObj.id, message);
198 function itemAjaxError(id, message) {
199 var item = jQuery('#media-item-' + id), filename = item.find('.filename').text(), last_err = item.data('last-err');
201 if ( last_err == id ) // prevent firing an error for the same file twice
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> '
208 + '</div>').data('last-err', id);
211 function deleteSuccess(data, textStatus) {
213 return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
216 return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
218 var id = this.id, item = jQuery('#media-item-' + id);
220 // Decrement the counters.
221 if ( type = jQuery('#type-of-' + id).val() )
222 jQuery('#' + type + '-counter').text( jQuery('#' + type + '-counter').text() - 1 );
224 if ( post_id && item.hasClass('child-of-'+post_id) )
225 jQuery('#attachments-count').text( jQuery('#attachments-count').text() - 1 );
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');
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');
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();
246 function deleteError(X, textStatus, errorThrown) {
250 function uploadComplete() {
251 jQuery('#insert-gallery').prop('disabled', false);
254 function switchUploader(s) {
256 deleteUserSetting('uploader');
257 jQuery('.media-upload-form').removeClass('html-uploader');
259 if ( typeof(uploader) == 'object' )
262 setUserSetting('uploader', '1'); // 1 == html uploader
263 jQuery('.media-upload-form').addClass('html-uploader');
267 function dndHelper(s) {
268 var d = document.getElementById('dnd-helper');
271 d.style.display = 'block';
273 d.style.display = 'none';
277 function uploadError(fileObj, errorCode, message, uploader) {
278 var hundredmb = 100 * 1024 * 1024, max;
281 case plupload.FAILED:
282 wpFileError(fileObj, pluploadL10n.upload_failed);
284 case plupload.FILE_EXTENSION_ERROR:
285 wpFileError(fileObj, pluploadL10n.invalid_filetype);
287 case plupload.FILE_SIZE_ERROR:
288 uploadSizeError(uploader, fileObj);
290 case plupload.IMAGE_FORMAT_ERROR:
291 wpFileError(fileObj, pluploadL10n.not_an_image);
293 case plupload.IMAGE_MEMORY_ERROR:
294 wpFileError(fileObj, pluploadL10n.image_memory_exceeded);
296 case plupload.IMAGE_DIMENSIONS_ERROR:
297 wpFileError(fileObj, pluploadL10n.image_dimensions_exceeded);
299 case plupload.GENERIC_ERROR:
300 wpQueueError(pluploadL10n.upload_failed);
302 case plupload.IO_ERROR:
303 max = parseInt(uploader.settings.max_file_size, 10);
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>'));
308 wpQueueError(pluploadL10n.io_error);
310 case plupload.HTTP_ERROR:
311 wpQueueError(pluploadL10n.http_error);
313 case plupload.INIT_ERROR:
314 jQuery('.media-upload-form').addClass('html-uploader');
316 case plupload.SECURITY_ERROR:
317 wpQueueError(pluploadL10n.security_error);
319 /* case plupload.UPLOAD_ERROR.UPLOAD_STOPPED:
320 case plupload.UPLOAD_ERROR.FILE_CANCELLED:
321 jQuery('#media-item-' + fileObj.id).remove();
324 wpFileError(fileObj, pluploadL10n.default_error);
328 function uploadSizeError( up, file, 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>');
334 message = pluploadL10n.file_exceeds_size_limit.replace('%s', file.name);
336 jQuery('#media-items').append('<div id="media-item-' + file.id + '" class="media-item error"><p>' + message + '</p></div>');
340 jQuery(document).ready(function($){
341 $('.media-upload-form').bind('click.uploader', function(e) {
342 var target = $(e.target), tr, c;
344 if ( target.is('input[type="radio"]') ) { // remember the last used image size and alignment
345 tr = target.closest('tr');
347 if ( $(tr).hasClass('align') )
348 setUserSetting('align', target.val());
349 else if ( $(tr).hasClass('image-size') )
350 setUserSetting('imgsize', target.val());
352 } else if ( target.is('button.button') ) { // remember the last used image link url
353 c = e.target.className || '';
354 c = c.match(/url([^ '"]+)/);
357 setUserSetting('urlbutton', c[1]);
358 target.siblings('.urlfield').val( target.attr('title') );
360 } else if ( target.is('a.dismiss') ) {
361 target.parents('.media-item').fadeOut(200, function(){
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');
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', '');
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;
377 if ( H && top && h ) {
382 if ( b - B < top - S )
383 window.scrollBy(0, (b - B) + 10);
385 window.scrollBy(0, top - S - 40);
390 } else if ( target.is('a.describe-toggle-off') ) { // Hide
391 target.siblings('.slidetoggle').fadeOut(250, function(){
392 target.parent().removeClass('open');
398 // init and set the uploader
399 uploader_init = function() {
400 uploader = new plupload.Uploader(wpUploaderInit);
402 $('#image_resize').bind('change', function() {
403 var arg = $(this).prop('checked');
408 setUserSetting('upload_resize', '1');
410 deleteUserSetting('upload_resize');
413 uploader.bind('Init', function(up) {
414 var uploaddiv = $('#plupload-upload-ui');
416 setResize( getUserSetting('upload_resize', false) );
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');
426 uploaddiv.removeClass('drag-drop');
427 $('#drag-drop-area').unbind('.wp-uploader');
433 uploader.bind('FilesAdded', function(up, files) {
434 var hundredmb = 100 * 1024 * 1024, max = parseInt(up.settings.max_file_size, 10);
436 $('#media-upload-error').html('');
439 plupload.each(files, function(file){
440 if ( max > hundredmb && file.size > hundredmb && up.runtime != 'html5' )
441 uploadSizeError( up, file, true );
450 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(up, files) {
476 if ( typeof(wpUploaderInit) == 'object' )