2 function fileDialogStart() {
3 jQuery("#media-upload-error").empty();
6 // progress and success handlers for media multi uploads
7 function fileQueued(fileObj) {
8 // Get rid of unused form
9 jQuery('.media-blank').remove();
10 // Collapse a single item
11 if ( jQuery('form.type-form #media-items').children().length == 1 && jQuery('.hidden', '#media-items').length > 0 ) {
12 jQuery('.describe-toggle-on').show();
13 jQuery('.describe-toggle-off').hide();
14 jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
16 // Create a progress bar containing the filename
17 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>');
18 // Display the progress div
19 jQuery('.progress', '#media-item-' + fileObj.id).show();
21 // Disable submit and enable cancel
22 jQuery('#insert-gallery').attr('disabled', 'disabled');
23 jQuery('#cancel-upload').attr('disabled', '');
26 function uploadStart(fileObj) {
30 function uploadProgress(fileObj, bytesDone, bytesTotal) {
31 // Lengthen the progress bar
32 var w = jQuery('#media-items').width() - 2, item = jQuery('#media-item-' + fileObj.id);
33 jQuery('.bar', item).width( w * bytesDone / bytesTotal );
34 jQuery('.percent', item).html( Math.ceil(bytesDone / bytesTotal * 100) + '%' );
36 if ( bytesDone == bytesTotal )
37 jQuery('.bar', item).html('<strong class="crunching">' + swfuploadL10n.crunching + '</strong>');
40 function prepareMediaItem(fileObj, serverData) {
41 var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id);
42 // Move the progress bar to 100%
43 jQuery('.bar', item).remove();
44 jQuery('.progress', item).hide();
46 // Old style: Append the HTML returned by the server -- thumbnail and form inputs
47 if ( isNaN(serverData) || !serverData ) {
48 item.append(serverData);
49 prepareMediaItemInit(fileObj);
51 // New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
53 item.load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
57 function prepareMediaItemInit(fileObj) {
58 var item = jQuery('#media-item-' + fileObj.id);
59 // Clone the thumbnail as a "pinkynail" -- a tiny image to the left of the filename
60 jQuery('.thumbnail', item).clone().attr('className', 'pinkynail toggle').prependTo(item);
62 // Replace the original filename with the new (unique) one assigned during upload
63 jQuery('.filename.original', item).replaceWith( jQuery('.filename.new', item) );
65 // Also bind toggle to the links
66 jQuery('a.toggle', item).click(function(){
67 jQuery(this).siblings('.slidetoggle').slideToggle(350, function(){
68 var w = jQuery(window).height(), t = jQuery(this).offset().top, h = jQuery(this).height(), b;
73 if ( b > w && (h + 48) < w )
74 window.scrollBy(0, b - w + 13);
76 window.scrollTo(0, t - 36);
79 jQuery(this).siblings('.toggle').andSelf().toggle();
80 jQuery(this).siblings('a.toggle').focus();
84 // Bind AJAX to the new Delete button
85 jQuery('a.delete', item).click(function(){
86 // Tell the server to delete it. TODO: handle exceptions
88 url: 'admin-ajax.php',
90 success: deleteSuccess,
94 id : this.id.replace(/[^0-9]/g, ''),
95 action : 'trash-post',
96 _ajax_nonce : this.href.replace(/^.*wpnonce=/,'')
102 // Bind AJAX to the new Undo button
103 jQuery('a.undo', item).click(function(){
104 // Tell the server to untrash it. TODO: handle exceptions
106 url: 'admin-ajax.php',
110 id : this.id.replace(/[^0-9]/g,''),
111 action: 'untrash-post',
112 _ajax_nonce: this.href.replace(/^.*wpnonce=/,'')
114 success: function(data, textStatus){
115 var item = jQuery('#media-item-' + fileObj.id);
117 if ( type = jQuery('#type-of-' + fileObj.id).val() )
118 jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-0+1);
119 if ( item.hasClass('child-of-'+post_id) )
120 jQuery('#attachments-count').text(jQuery('#attachments-count').text()-0+1);
122 jQuery('.filename .trashnotice', item).remove();
123 jQuery('.filename .title', item).css('font-weight','normal');
124 jQuery('a.undo', item).addClass('hidden');
125 jQuery('a.describe-toggle-on, .menu_order_input', item).show();
126 item.css( {backgroundColor:'#ceb'} ).animate( {backgroundColor: '#fff'}, { queue: false, duration: 500, complete: function(){ jQuery(this).css({backgroundColor:''}); } }).removeClass('undo');
132 // Open this item if it says to start open (e.g. to display an error)
133 jQuery('#media-item-' + fileObj.id + '.startopen').removeClass('startopen').slideToggle(500).siblings('.toggle').toggle();
136 function itemAjaxError(id, html) {
137 var error = jQuery('#media-item-error' + id);
139 error.html('<div class="file-error"><button type="button" id="dismiss-'+id+'" class="button dismiss">'+swfuploadL10n.dismiss+'</button>'+html+'</div>');
140 jQuery('#dismiss-'+id).click(function(){jQuery(this).parents('.file-error').slideUp(200, function(){jQuery(this).empty();})});
143 function deleteSuccess(data, textStatus) {
145 return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
147 return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
149 var id = this.id, item = jQuery('#media-item-' + id);
151 // Decrement the counters.
152 if ( type = jQuery('#type-of-' + id).val() )
153 jQuery('#' + type + '-counter').text( jQuery('#' + type + '-counter').text() - 1 );
154 if ( item.hasClass('child-of-'+post_id) )
155 jQuery('#attachments-count').text( jQuery('#attachments-count').text() - 1 );
157 if ( jQuery('form.type-form #media-items').children().length == 1 && jQuery('.hidden', '#media-items').length > 0 ) {
158 jQuery('.toggle').toggle();
159 jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
163 jQuery('.toggle', item).toggle();
164 jQuery('.slidetoggle', item).slideUp(200).siblings().removeClass('hidden');
165 item.css( {backgroundColor:'#faa'} ).animate( {backgroundColor:'#f4f4f4'}, {queue:false, duration:500} ).addClass('undo');
167 jQuery('.filename:empty', item).remove();
168 jQuery('.filename .title', item).css('font-weight','bold');
169 jQuery('.filename', item).append('<span class="trashnotice"> ' + swfuploadL10n.deleted + ' </span>').siblings('a.toggle').hide();
170 jQuery('.filename', item).append( jQuery('a.undo', item).removeClass('hidden') );
171 jQuery('.menu_order_input', item).hide();
176 function deleteError(X, textStatus, errorThrown) {
180 function updateMediaForm() {
181 var one = jQuery('form.type-form #media-items').children(), items = jQuery('#media-items').children();
183 // Just one file, no need for collapsible part
184 if ( one.length == 1 ) {
185 jQuery('.slidetoggle', one).slideDown(500).siblings().addClass('hidden').filter('.toggle').toggle();
188 // Only show Save buttons when there is at least one file.
189 if ( items.not('.media-blank').length > 0 )
190 jQuery('.savebutton').show();
192 jQuery('.savebutton').hide();
194 // Only show Gallery button when there are at least two files.
195 if ( items.length > 1 )
196 jQuery('.insert-gallery').show();
198 jQuery('.insert-gallery').hide();
201 function uploadSuccess(fileObj, serverData) {
202 // if async-upload returned an error message, place it in the media item div and return
203 if ( serverData.match('media-upload-error') ) {
204 jQuery('#media-item-' + fileObj.id).html(serverData);
208 prepareMediaItem(fileObj, serverData);
211 // Increment the counter.
212 if ( jQuery('#media-item-' + fileObj.id).hasClass('child-of-' + post_id) )
213 jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);
216 function uploadComplete(fileObj) {
217 // If no more uploads queued, enable the submit button
218 if ( swfu.getStats().files_queued == 0 ) {
219 jQuery('#cancel-upload').attr('disabled', 'disabled');
220 jQuery('#insert-gallery').attr('disabled', '');
225 // wp-specific error handlers
228 function wpQueueError(message) {
229 jQuery('#media-upload-error').show().text(message);
232 // file-specific message
233 function wpFileError(fileObj, message) {
234 jQuery('#media-item-' + fileObj.id + ' .filename').after('<div class="file-error"><button type="button" id="dismiss-' + fileObj.id + '" class="button dismiss">'+swfuploadL10n.dismiss+'</button>'+message+'</div>').siblings('.toggle').remove();
235 jQuery('#dismiss-' + fileObj.id).click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})});
238 function fileQueueError(fileObj, error_code, message) {
239 // Handle this error separately because we don't want to create a FileProgress element for it.
240 if ( error_code == SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED ) {
241 wpQueueError(swfuploadL10n.queue_limit_exceeded);
243 else if ( error_code == SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT ) {
245 wpFileError(fileObj, swfuploadL10n.file_exceeds_size_limit);
247 else if ( error_code == SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE ) {
249 wpFileError(fileObj, swfuploadL10n.zero_byte_file);
251 else if ( error_code == SWFUpload.QUEUE_ERROR.INVALID_FILETYPE ) {
253 wpFileError(fileObj, swfuploadL10n.invalid_filetype);
256 wpQueueError(swfuploadL10n.default_error);
260 function fileDialogComplete(num_files_queued) {
262 if (num_files_queued > 0) {
270 function switchUploader(s) {
271 var f = document.getElementById(swfu.customSettings.swfupload_element_id), h = document.getElementById(swfu.customSettings.degraded_element_id);
273 f.style.display = 'block';
274 h.style.display = 'none';
276 f.style.display = 'none';
277 h.style.display = 'block';
281 function swfuploadPreLoad() {
282 if ( !uploaderMode ) {
289 function swfuploadLoadFailed() {
291 jQuery('.upload-html-bypass').hide();
294 function uploadError(fileObj, errorCode, message) {
297 case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
298 wpFileError(fileObj, swfuploadL10n.missing_upload_url);
300 case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
301 wpFileError(fileObj, swfuploadL10n.upload_limit_exceeded);
303 case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
304 wpQueueError(swfuploadL10n.http_error);
306 case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
307 wpQueueError(swfuploadL10n.upload_failed);
309 case SWFUpload.UPLOAD_ERROR.IO_ERROR:
310 wpQueueError(swfuploadL10n.io_error);
312 case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
313 wpQueueError(swfuploadL10n.security_error);
315 case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
316 case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
317 jQuery('#media-item-' + fileObj.id).remove();
320 wpFileError(fileObj, swfuploadL10n.default_error);
324 function cancelUpload() {
328 // remember the last used image size, alignment and url
329 jQuery(document).ready(function($){
330 $('input[type="radio"]', '#media-items').live('click', function(){
331 var tr = $(this).closest('tr');
333 if ( $(tr).hasClass('align') )
334 setUserSetting('align', $(this).val());
335 else if ( $(tr).hasClass('image-size') )
336 setUserSetting('imgsize', $(this).val());
339 $('button.button', '#media-items').live('click', function(){
340 var c = this.className || '';
341 c = c.match(/url([^ '"]+)/);
343 setUserSetting('urlbutton', c[1]);
344 $(this).siblings('.urlfield').val( $(this).attr('title') );