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('<div class="media-item">')
19 .attr( 'id', 'media-item-' + fileObj.id )
20 .addClass('child-of-' + post_id)
21 .append('<div class="progress"><div class="bar"></div></div>',
22 jQuery('<div class="filename original"><span class="percent"></span>').text( ' ' + fileObj.name ))
23 .appendTo( jQuery('#media-items' ) );
24 // Display the progress div
25 jQuery('.progress', '#media-item-' + fileObj.id).show();
27 // Disable submit and enable cancel
28 jQuery('#insert-gallery').prop('disabled', true);
29 jQuery('#cancel-upload').prop('disabled', false);
32 function uploadStart(fileObj) {
34 if ( typeof topWin.tb_remove != 'undefined' )
35 topWin.jQuery('#TB_overlay').unbind('click', topWin.tb_remove);
41 function uploadProgress(fileObj, bytesDone, bytesTotal) {
42 // Lengthen the progress bar
43 var w = jQuery('#media-items').width() - 2, item = jQuery('#media-item-' + fileObj.id);
44 jQuery('.bar', item).width( w * bytesDone / bytesTotal );
45 jQuery('.percent', item).html( Math.ceil(bytesDone / bytesTotal * 100) + '%' );
47 if ( bytesDone == bytesTotal )
48 jQuery('.bar', item).html('<strong class="crunching">' + swfuploadL10n.crunching + '</strong>');
51 function prepareMediaItem(fileObj, serverData) {
52 var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id);
53 // Move the progress bar to 100%
54 jQuery('.bar', item).remove();
55 jQuery('.progress', item).hide();
58 if ( typeof topWin.tb_remove != 'undefined' )
59 topWin.jQuery('#TB_overlay').click(topWin.tb_remove);
62 // Old style: Append the HTML returned by the server -- thumbnail and form inputs
63 if ( isNaN(serverData) || !serverData ) {
64 item.append(serverData);
65 prepareMediaItemInit(fileObj);
67 // New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
69 item.load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
73 function prepareMediaItemInit(fileObj) {
74 var item = jQuery('#media-item-' + fileObj.id);
75 // Clone the thumbnail as a "pinkynail" -- a tiny image to the left of the filename
76 jQuery('.thumbnail', item).clone().attr('class', 'pinkynail toggle').prependTo(item);
78 // Replace the original filename with the new (unique) one assigned during upload
79 jQuery('.filename.original', item).replaceWith( jQuery('.filename.new', item) );
81 // Also bind toggle to the links
82 jQuery('a.toggle', item).click(function(){
83 jQuery(this).siblings('.slidetoggle').slideToggle(350, function(){
84 var w = jQuery(window).height(), t = jQuery(this).offset().top, h = jQuery(this).height(), b;
89 if ( b > w && (h + 48) < w )
90 window.scrollBy(0, b - w + 13);
92 window.scrollTo(0, t - 36);
95 jQuery(this).siblings('.toggle').andSelf().toggle();
96 jQuery(this).siblings('a.toggle').focus();
100 // Bind AJAX to the new Delete button
101 jQuery('a.delete', item).click(function(){
102 // Tell the server to delete it. TODO: handle exceptions
106 success: deleteSuccess,
110 id : this.id.replace(/[^0-9]/g, ''),
111 action : 'trash-post',
112 _ajax_nonce : this.href.replace(/^.*wpnonce=/,'')
118 // Bind AJAX to the new Undo button
119 jQuery('a.undo', item).click(function(){
120 // Tell the server to untrash it. TODO: handle exceptions
126 id : this.id.replace(/[^0-9]/g,''),
127 action: 'untrash-post',
128 _ajax_nonce: this.href.replace(/^.*wpnonce=/,'')
130 success: function(data, textStatus){
131 var item = jQuery('#media-item-' + fileObj.id);
133 if ( type = jQuery('#type-of-' + fileObj.id).val() )
134 jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-0+1);
135 if ( item.hasClass('child-of-'+post_id) )
136 jQuery('#attachments-count').text(jQuery('#attachments-count').text()-0+1);
138 jQuery('.filename .trashnotice', item).remove();
139 jQuery('.filename .title', item).css('font-weight','normal');
140 jQuery('a.undo', item).addClass('hidden');
141 jQuery('a.describe-toggle-on, .menu_order_input', item).show();
142 item.css( {backgroundColor:'#ceb'} ).animate( {backgroundColor: '#fff'}, { queue: false, duration: 500, complete: function(){ jQuery(this).css({backgroundColor:''}); } }).removeClass('undo');
148 // Open this item if it says to start open (e.g. to display an error)
149 jQuery('#media-item-' + fileObj.id + '.startopen').removeClass('startopen').slideToggle(500).siblings('.toggle').toggle();
152 function itemAjaxError(id, html) {
153 var item = jQuery('#media-item-' + id);
154 var filename = jQuery('.filename', item).text();
156 item.html('<div class="error-div">'
157 + '<a class="dismiss" href="#">' + swfuploadL10n.dismiss + '</a>'
158 + '<strong>' + swfuploadL10n.error_uploading.replace('%s', filename) + '</strong><br />'
161 item.find('a.dismiss').click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})});
164 function deleteSuccess(data, textStatus) {
166 return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
168 return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
170 var id = this.id, item = jQuery('#media-item-' + id);
172 // Decrement the counters.
173 if ( type = jQuery('#type-of-' + id).val() )
174 jQuery('#' + type + '-counter').text( jQuery('#' + type + '-counter').text() - 1 );
175 if ( item.hasClass('child-of-'+post_id) )
176 jQuery('#attachments-count').text( jQuery('#attachments-count').text() - 1 );
178 if ( jQuery('form.type-form #media-items').children().length == 1 && jQuery('.hidden', '#media-items').length > 0 ) {
179 jQuery('.toggle').toggle();
180 jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
184 jQuery('.toggle', item).toggle();
185 jQuery('.slidetoggle', item).slideUp(200).siblings().removeClass('hidden');
186 item.css( {backgroundColor:'#faa'} ).animate( {backgroundColor:'#f4f4f4'}, {queue:false, duration:500} ).addClass('undo');
188 jQuery('.filename:empty', item).remove();
189 jQuery('.filename .title', item).css('font-weight','bold');
190 jQuery('.filename', item).append('<span class="trashnotice"> ' + swfuploadL10n.deleted + ' </span>').siblings('a.toggle').hide();
191 jQuery('.filename', item).append( jQuery('a.undo', item).removeClass('hidden') );
192 jQuery('.menu_order_input', item).hide();
197 function deleteError(X, textStatus, errorThrown) {
201 function updateMediaForm() {
202 var one = jQuery('form.type-form #media-items').children(), items = jQuery('#media-items').children();
204 // Just one file, no need for collapsible part
205 if ( one.length == 1 ) {
206 jQuery('.slidetoggle', one).slideDown(500).siblings().addClass('hidden').filter('.toggle').toggle();
209 // Only show Save buttons when there is at least one file.
210 if ( items.not('.media-blank').length > 0 )
211 jQuery('.savebutton').show();
213 jQuery('.savebutton').hide();
215 // Only show Gallery button when there are at least two files.
216 if ( items.length > 1 )
217 jQuery('.insert-gallery').show();
219 jQuery('.insert-gallery').hide();
222 function uploadSuccess(fileObj, serverData) {
223 // if async-upload returned an error message, place it in the media item div and return
224 if ( serverData.match('media-upload-error') ) {
225 jQuery('#media-item-' + fileObj.id).html(serverData);
229 prepareMediaItem(fileObj, serverData);
232 // Increment the counter.
233 if ( jQuery('#media-item-' + fileObj.id).hasClass('child-of-' + post_id) )
234 jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);
237 function uploadComplete(fileObj) {
238 // If no more uploads queued, enable the submit button
239 if ( swfu.getStats().files_queued == 0 ) {
240 jQuery('#cancel-upload').prop('disabled', true);
241 jQuery('#insert-gallery').prop('disabled', false);
246 // wp-specific error handlers
249 function wpQueueError(message) {
250 jQuery('#media-upload-error').show().text(message);
253 // file-specific message
254 function wpFileError(fileObj, message) {
255 var item = jQuery('#media-item-' + fileObj.id);
256 var filename = jQuery('.filename', item).text();
258 item.html('<div class="error-div">'
259 + '<a class="dismiss" href="#">' + swfuploadL10n.dismiss + '</a>'
260 + '<strong>' + swfuploadL10n.error_uploading.replace('%s', filename) + '</strong><br />'
263 item.find('a.dismiss').click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})});
266 function fileQueueError(fileObj, error_code, message) {
267 // Handle this error separately because we don't want to create a FileProgress element for it.
268 if ( error_code == SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED ) {
269 wpQueueError(swfuploadL10n.queue_limit_exceeded);
271 else if ( error_code == SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT ) {
273 wpFileError(fileObj, swfuploadL10n.file_exceeds_size_limit);
275 else if ( error_code == SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE ) {
277 wpFileError(fileObj, swfuploadL10n.zero_byte_file);
279 else if ( error_code == SWFUpload.QUEUE_ERROR.INVALID_FILETYPE ) {
281 wpFileError(fileObj, swfuploadL10n.invalid_filetype);
284 wpQueueError(swfuploadL10n.default_error);
288 function fileDialogComplete(num_files_queued) {
290 if (num_files_queued > 0) {
298 function switchUploader(s) {
299 var f = document.getElementById(swfu.customSettings.swfupload_element_id), h = document.getElementById(swfu.customSettings.degraded_element_id);
301 f.style.display = 'block';
302 h.style.display = 'none';
304 f.style.display = 'none';
305 h.style.display = 'block';
309 function swfuploadPreLoad() {
310 if ( !uploaderMode ) {
317 function swfuploadLoadFailed() {
319 jQuery('.upload-html-bypass').hide();
322 function uploadError(fileObj, errorCode, message) {
325 case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
326 wpFileError(fileObj, swfuploadL10n.missing_upload_url);
328 case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
329 wpFileError(fileObj, swfuploadL10n.upload_limit_exceeded);
331 case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
332 wpQueueError(swfuploadL10n.http_error);
334 case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
335 wpQueueError(swfuploadL10n.upload_failed);
337 case SWFUpload.UPLOAD_ERROR.IO_ERROR:
338 wpQueueError(swfuploadL10n.io_error);
340 case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
341 wpQueueError(swfuploadL10n.security_error);
343 case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
344 case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
345 jQuery('#media-item-' + fileObj.id).remove();
348 wpFileError(fileObj, swfuploadL10n.default_error);
352 function cancelUpload() {
356 // remember the last used image size, alignment and url
357 jQuery(document).ready(function($){
358 $('input[type="radio"]', '#media-items').live('click', function(){
359 var tr = $(this).closest('tr');
361 if ( $(tr).hasClass('align') )
362 setUserSetting('align', $(this).val());
363 else if ( $(tr).hasClass('image-size') )
364 setUserSetting('imgsize', $(this).val());
367 $('button.button', '#media-items').live('click', function(){
368 var c = this.className || '';
369 c = c.match(/url([^ '"]+)/);
371 setUserSetting('urlbutton', c[1]);
372 $(this).siblings('.urlfield').val( $(this).attr('title') );