1 var topWin = window.dialogArguments || opener || parent || top;
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();
11 // Collapse a single item
12 if ( jQuery('form.type-form #media-items').children().length == 1 && jQuery('.hidden', '#media-items').length > 0 ) {
13 jQuery('.describe-toggle-on').show();
14 jQuery('.describe-toggle-off').hide();
15 jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
17 // Create a progress bar containing the filename
18 jQuery('#media-items').append('<div id="media-item-' + fileObj.id + '" class="media-item child-of-' + post_id + '"><div class="progress"><div class="bar"></div></div><div class="filename original"><span class="percent"></span> ' + fileObj.name + '</div></div>');
19 // Display the progress div
20 jQuery('.progress', '#media-item-' + fileObj.id).show();
22 // Disable submit and enable cancel
23 jQuery('#insert-gallery').prop('disabled', true);
24 jQuery('#cancel-upload').prop('disabled', false);
27 function uploadStart(fileObj) {
29 if ( typeof topWin.tb_remove != 'undefined' )
30 topWin.jQuery('#TB_overlay').unbind('click', topWin.tb_remove);
36 function uploadProgress(fileObj, bytesDone, bytesTotal) {
37 // Lengthen the progress bar
38 var w = jQuery('#media-items').width() - 2, item = jQuery('#media-item-' + fileObj.id);
39 jQuery('.bar', item).width( w * bytesDone / bytesTotal );
40 jQuery('.percent', item).html( Math.ceil(bytesDone / bytesTotal * 100) + '%' );
42 if ( bytesDone == bytesTotal )
43 jQuery('.bar', item).html('<strong class="crunching">' + swfuploadL10n.crunching + '</strong>');
46 function prepareMediaItem(fileObj, serverData) {
47 var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id);
48 // Move the progress bar to 100%
49 jQuery('.bar', item).remove();
50 jQuery('.progress', item).hide();
53 if ( typeof topWin.tb_remove != 'undefined' )
54 topWin.jQuery('#TB_overlay').click(topWin.tb_remove);
57 // Old style: Append the HTML returned by the server -- thumbnail and form inputs
58 if ( isNaN(serverData) || !serverData ) {
59 item.append(serverData);
60 prepareMediaItemInit(fileObj);
62 // New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
64 item.load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
68 function prepareMediaItemInit(fileObj) {
69 var item = jQuery('#media-item-' + fileObj.id);
70 // Clone the thumbnail as a "pinkynail" -- a tiny image to the left of the filename
71 jQuery('.thumbnail', item).clone().attr('class', 'pinkynail toggle').prependTo(item);
73 // Replace the original filename with the new (unique) one assigned during upload
74 jQuery('.filename.original', item).replaceWith( jQuery('.filename.new', item) );
76 // Also bind toggle to the links
77 jQuery('a.toggle', item).click(function(){
78 jQuery(this).siblings('.slidetoggle').slideToggle(350, function(){
79 var w = jQuery(window).height(), t = jQuery(this).offset().top, h = jQuery(this).height(), b;
84 if ( b > w && (h + 48) < w )
85 window.scrollBy(0, b - w + 13);
87 window.scrollTo(0, t - 36);
90 jQuery(this).siblings('.toggle').andSelf().toggle();
91 jQuery(this).siblings('a.toggle').focus();
95 // Bind AJAX to the new Delete button
96 jQuery('a.delete', item).click(function(){
97 // Tell the server to delete it. TODO: handle exceptions
99 url: 'admin-ajax.php',
101 success: deleteSuccess,
105 id : this.id.replace(/[^0-9]/g, ''),
106 action : 'trash-post',
107 _ajax_nonce : this.href.replace(/^.*wpnonce=/,'')
113 // Bind AJAX to the new Undo button
114 jQuery('a.undo', item).click(function(){
115 // Tell the server to untrash it. TODO: handle exceptions
117 url: 'admin-ajax.php',
121 id : this.id.replace(/[^0-9]/g,''),
122 action: 'untrash-post',
123 _ajax_nonce: this.href.replace(/^.*wpnonce=/,'')
125 success: function(data, textStatus){
126 var item = jQuery('#media-item-' + fileObj.id);
128 if ( type = jQuery('#type-of-' + fileObj.id).val() )
129 jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-0+1);
130 if ( item.hasClass('child-of-'+post_id) )
131 jQuery('#attachments-count').text(jQuery('#attachments-count').text()-0+1);
133 jQuery('.filename .trashnotice', item).remove();
134 jQuery('.filename .title', item).css('font-weight','normal');
135 jQuery('a.undo', item).addClass('hidden');
136 jQuery('a.describe-toggle-on, .menu_order_input', item).show();
137 item.css( {backgroundColor:'#ceb'} ).animate( {backgroundColor: '#fff'}, { queue: false, duration: 500, complete: function(){ jQuery(this).css({backgroundColor:''}); } }).removeClass('undo');
143 // Open this item if it says to start open (e.g. to display an error)
144 jQuery('#media-item-' + fileObj.id + '.startopen').removeClass('startopen').slideToggle(500).siblings('.toggle').toggle();
147 function itemAjaxError(id, html) {
148 var item = jQuery('#media-item-' + id);
149 var filename = jQuery('.filename', item).text();
151 item.html('<div class="error-div">'
152 + '<a class="dismiss" href="#">' + swfuploadL10n.dismiss + '</a>'
153 + '<strong>' + swfuploadL10n.error_uploading.replace('%s', filename) + '</strong><br />'
156 item.find('a.dismiss').click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})});
159 function deleteSuccess(data, textStatus) {
161 return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
163 return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
165 var id = this.id, item = jQuery('#media-item-' + id);
167 // Decrement the counters.
168 if ( type = jQuery('#type-of-' + id).val() )
169 jQuery('#' + type + '-counter').text( jQuery('#' + type + '-counter').text() - 1 );
170 if ( item.hasClass('child-of-'+post_id) )
171 jQuery('#attachments-count').text( jQuery('#attachments-count').text() - 1 );
173 if ( jQuery('form.type-form #media-items').children().length == 1 && jQuery('.hidden', '#media-items').length > 0 ) {
174 jQuery('.toggle').toggle();
175 jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
179 jQuery('.toggle', item).toggle();
180 jQuery('.slidetoggle', item).slideUp(200).siblings().removeClass('hidden');
181 item.css( {backgroundColor:'#faa'} ).animate( {backgroundColor:'#f4f4f4'}, {queue:false, duration:500} ).addClass('undo');
183 jQuery('.filename:empty', item).remove();
184 jQuery('.filename .title', item).css('font-weight','bold');
185 jQuery('.filename', item).append('<span class="trashnotice"> ' + swfuploadL10n.deleted + ' </span>').siblings('a.toggle').hide();
186 jQuery('.filename', item).append( jQuery('a.undo', item).removeClass('hidden') );
187 jQuery('.menu_order_input', item).hide();
192 function deleteError(X, textStatus, errorThrown) {
196 function updateMediaForm() {
197 var one = jQuery('form.type-form #media-items').children(), items = jQuery('#media-items').children();
199 // Just one file, no need for collapsible part
200 if ( one.length == 1 ) {
201 jQuery('.slidetoggle', one).slideDown(500).siblings().addClass('hidden').filter('.toggle').toggle();
204 // Only show Save buttons when there is at least one file.
205 if ( items.not('.media-blank').length > 0 )
206 jQuery('.savebutton').show();
208 jQuery('.savebutton').hide();
210 // Only show Gallery button when there are at least two files.
211 if ( items.length > 1 )
212 jQuery('.insert-gallery').show();
214 jQuery('.insert-gallery').hide();
217 function uploadSuccess(fileObj, serverData) {
218 // if async-upload returned an error message, place it in the media item div and return
219 if ( serverData.match('media-upload-error') ) {
220 jQuery('#media-item-' + fileObj.id).html(serverData);
224 prepareMediaItem(fileObj, serverData);
227 // Increment the counter.
228 if ( jQuery('#media-item-' + fileObj.id).hasClass('child-of-' + post_id) )
229 jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);
232 function uploadComplete(fileObj) {
233 // If no more uploads queued, enable the submit button
234 if ( swfu.getStats().files_queued == 0 ) {
235 jQuery('#cancel-upload').prop('disabled', true);
236 jQuery('#insert-gallery').prop('disabled', false);
241 // wp-specific error handlers
244 function wpQueueError(message) {
245 jQuery('#media-upload-error').show().text(message);
248 // file-specific message
249 function wpFileError(fileObj, message) {
250 var item = jQuery('#media-item-' + fileObj.id);
251 var filename = jQuery('.filename', item).text();
253 item.html('<div class="error-div">'
254 + '<a class="dismiss" href="#">' + swfuploadL10n.dismiss + '</a>'
255 + '<strong>' + swfuploadL10n.error_uploading.replace('%s', filename) + '</strong><br />'
258 item.find('a.dismiss').click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})});
261 function fileQueueError(fileObj, error_code, message) {
262 // Handle this error separately because we don't want to create a FileProgress element for it.
263 if ( error_code == SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED ) {
264 wpQueueError(swfuploadL10n.queue_limit_exceeded);
266 else if ( error_code == SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT ) {
268 wpFileError(fileObj, swfuploadL10n.file_exceeds_size_limit);
270 else if ( error_code == SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE ) {
272 wpFileError(fileObj, swfuploadL10n.zero_byte_file);
274 else if ( error_code == SWFUpload.QUEUE_ERROR.INVALID_FILETYPE ) {
276 wpFileError(fileObj, swfuploadL10n.invalid_filetype);
279 wpQueueError(swfuploadL10n.default_error);
283 function fileDialogComplete(num_files_queued) {
285 if (num_files_queued > 0) {
293 function switchUploader(s) {
294 var f = document.getElementById(swfu.customSettings.swfupload_element_id), h = document.getElementById(swfu.customSettings.degraded_element_id);
296 f.style.display = 'block';
297 h.style.display = 'none';
299 f.style.display = 'none';
300 h.style.display = 'block';
304 function swfuploadPreLoad() {
305 if ( !uploaderMode ) {
312 function swfuploadLoadFailed() {
314 jQuery('.upload-html-bypass').hide();
317 function uploadError(fileObj, errorCode, message) {
320 case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
321 wpFileError(fileObj, swfuploadL10n.missing_upload_url);
323 case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
324 wpFileError(fileObj, swfuploadL10n.upload_limit_exceeded);
326 case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
327 wpQueueError(swfuploadL10n.http_error);
329 case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
330 wpQueueError(swfuploadL10n.upload_failed);
332 case SWFUpload.UPLOAD_ERROR.IO_ERROR:
333 wpQueueError(swfuploadL10n.io_error);
335 case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
336 wpQueueError(swfuploadL10n.security_error);
338 case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
339 case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
340 jQuery('#media-item-' + fileObj.id).remove();
343 wpFileError(fileObj, swfuploadL10n.default_error);
347 function cancelUpload() {
351 // remember the last used image size, alignment and url
352 jQuery(document).ready(function($){
353 $('input[type="radio"]', '#media-items').live('click', function(){
354 var tr = $(this).closest('tr');
356 if ( $(tr).hasClass('align') )
357 setUserSetting('align', $(this).val());
358 else if ( $(tr).hasClass('image-size') )
359 setUserSetting('imgsize', $(this).val());
362 $('button.button', '#media-items').live('click', function(){
363 var c = this.className || '';
364 c = c.match(/url([^ '"]+)/);
366 setUserSetting('urlbutton', c[1]);
367 $(this).siblings('.urlfield').val( $(this).attr('title') );