web/wp-includes/js/swfupload/handlers.dev.js
changeset 136 bde1974c263b
child 194 32102edaa81b
equal deleted inserted replaced
135:53cff4b4a802 136:bde1974c263b
       
     1 
       
     2 function fileDialogStart() {
       
     3 	jQuery("#media-upload-error").empty();
       
     4 }
       
     5 
       
     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');
       
    15 	}
       
    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();
       
    20 
       
    21 	// Disable submit and enable cancel
       
    22 	jQuery('#insert-gallery').attr('disabled', 'disabled');
       
    23 	jQuery('#cancel-upload').attr('disabled', '');
       
    24 }
       
    25 
       
    26 function uploadStart(fileObj) {
       
    27 	return true;
       
    28 }
       
    29 
       
    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) + '%' );
       
    35 
       
    36 	if ( bytesDone == bytesTotal )
       
    37 		jQuery('.bar', item).html('<strong class="crunching">' + swfuploadL10n.crunching + '</strong>');
       
    38 }
       
    39 
       
    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();
       
    45 
       
    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);
       
    50 	}
       
    51 	// New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
       
    52 	else {
       
    53 		item.load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
       
    54 	}
       
    55 }
       
    56 
       
    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);
       
    61 
       
    62 	// Replace the original filename with the new (unique) one assigned during upload
       
    63 	jQuery('.filename.original', item).replaceWith( jQuery('.filename.new', item) );
       
    64 
       
    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;
       
    69 
       
    70 			if ( w && t && h ) {
       
    71                 b = t + h;
       
    72 
       
    73                 if ( b > w && (h + 48) < w )
       
    74                     window.scrollBy(0, b - w + 13);
       
    75                 else if ( b > w )
       
    76                     window.scrollTo(0, t - 36);
       
    77             }
       
    78 		});
       
    79 		jQuery(this).siblings('.toggle').andSelf().toggle();
       
    80 		jQuery(this).siblings('a.toggle').focus();
       
    81 		return false;
       
    82 	});
       
    83 
       
    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
       
    87 		jQuery.ajax({
       
    88 			url: 'admin-ajax.php',
       
    89 			type: 'post',
       
    90 			success: deleteSuccess,
       
    91 			error: deleteError,
       
    92 			id: fileObj.id,
       
    93 			data: {
       
    94 				id : this.id.replace(/[^0-9]/g, ''),
       
    95 				action : 'trash-post',
       
    96 				_ajax_nonce : this.href.replace(/^.*wpnonce=/,'')
       
    97 			}
       
    98 		});
       
    99 		return false;
       
   100 	});
       
   101 
       
   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
       
   105 		jQuery.ajax({
       
   106 			url: 'admin-ajax.php',
       
   107 			type: 'post',
       
   108 			id: fileObj.id,
       
   109 			data: {
       
   110 				id : this.id.replace(/[^0-9]/g,''),
       
   111 				action: 'untrash-post',
       
   112 				_ajax_nonce: this.href.replace(/^.*wpnonce=/,'')
       
   113 			},
       
   114 			success: function(data, textStatus){
       
   115 				var item = jQuery('#media-item-' + fileObj.id);
       
   116 
       
   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);
       
   121 
       
   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');
       
   127 			}
       
   128 		});
       
   129 		return false;
       
   130 	});
       
   131 
       
   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();
       
   134 }
       
   135 
       
   136 function itemAjaxError(id, html) {
       
   137 	var error = jQuery('#media-item-error' + id);
       
   138 
       
   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();})});
       
   141 }
       
   142 
       
   143 function deleteSuccess(data, textStatus) {
       
   144 	if ( data == '-1' )
       
   145 		return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
       
   146 	if ( data == '0' )
       
   147 		return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
       
   148 
       
   149 	var id = this.id, item = jQuery('#media-item-' + id);
       
   150 
       
   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 );
       
   156 
       
   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');
       
   160 	}
       
   161 
       
   162 	// Vanish it.
       
   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');
       
   166 
       
   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();
       
   172 
       
   173 	return;
       
   174 }
       
   175 
       
   176 function deleteError(X, textStatus, errorThrown) {
       
   177 	// TODO
       
   178 }
       
   179 
       
   180 function updateMediaForm() {
       
   181 	var one = jQuery('form.type-form #media-items').children(), items = jQuery('#media-items').children();
       
   182 
       
   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();
       
   186 	}
       
   187 
       
   188 	// Only show Save buttons when there is at least one file.
       
   189 	if ( items.not('.media-blank').length > 0 )
       
   190 		jQuery('.savebutton').show();
       
   191 	else
       
   192 		jQuery('.savebutton').hide();
       
   193 
       
   194 	// Only show Gallery button when there are at least two files.
       
   195 	if ( items.length > 1 )
       
   196 		jQuery('.insert-gallery').show();
       
   197 	else
       
   198 		jQuery('.insert-gallery').hide();
       
   199 }
       
   200 
       
   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);
       
   205 		return;
       
   206 	}
       
   207 
       
   208 	prepareMediaItem(fileObj, serverData);
       
   209 	updateMediaForm();
       
   210 
       
   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);
       
   214 }
       
   215 
       
   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', '');
       
   221 	}
       
   222 }
       
   223 
       
   224 
       
   225 // wp-specific error handlers
       
   226 
       
   227 // generic message
       
   228 function wpQueueError(message) {
       
   229 	jQuery('#media-upload-error').show().text(message);
       
   230 }
       
   231 
       
   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();})});
       
   236 }
       
   237 
       
   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);
       
   242 	}
       
   243 	else if ( error_code == SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT ) {
       
   244 		fileQueued(fileObj);
       
   245 		wpFileError(fileObj, swfuploadL10n.file_exceeds_size_limit);
       
   246 	}
       
   247 	else if ( error_code == SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE ) {
       
   248 		fileQueued(fileObj);
       
   249 		wpFileError(fileObj, swfuploadL10n.zero_byte_file);
       
   250 	}
       
   251 	else if ( error_code == SWFUpload.QUEUE_ERROR.INVALID_FILETYPE ) {
       
   252 		fileQueued(fileObj);
       
   253 		wpFileError(fileObj, swfuploadL10n.invalid_filetype);
       
   254 	}
       
   255 	else {
       
   256 		wpQueueError(swfuploadL10n.default_error);
       
   257 	}
       
   258 }
       
   259 
       
   260 function fileDialogComplete(num_files_queued) {
       
   261 	try {
       
   262 		if (num_files_queued > 0) {
       
   263 			this.startUpload();
       
   264 		}
       
   265 	} catch (ex) {
       
   266 		this.debug(ex);
       
   267 	}
       
   268 }
       
   269 
       
   270 function switchUploader(s) {
       
   271 	var f = document.getElementById(swfu.customSettings.swfupload_element_id), h = document.getElementById(swfu.customSettings.degraded_element_id);
       
   272 	if ( s ) {
       
   273 		f.style.display = 'block';
       
   274 		h.style.display = 'none';
       
   275 	} else {
       
   276 		f.style.display = 'none';
       
   277 		h.style.display = 'block';
       
   278 	}
       
   279 }
       
   280 
       
   281 function swfuploadPreLoad() {
       
   282 	if ( !uploaderMode ) {
       
   283 		switchUploader(1);
       
   284 	} else {
       
   285 		switchUploader(0);
       
   286 	}
       
   287 }
       
   288 
       
   289 function swfuploadLoadFailed() {
       
   290 	switchUploader(0);
       
   291 	jQuery('.upload-html-bypass').hide();
       
   292 }
       
   293 
       
   294 function uploadError(fileObj, errorCode, message) {
       
   295 
       
   296 	switch (errorCode) {
       
   297 		case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
       
   298 			wpFileError(fileObj, swfuploadL10n.missing_upload_url);
       
   299 			break;
       
   300 		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
       
   301 			wpFileError(fileObj, swfuploadL10n.upload_limit_exceeded);
       
   302 			break;
       
   303 		case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
       
   304 			wpQueueError(swfuploadL10n.http_error);
       
   305 			break;
       
   306 		case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
       
   307 			wpQueueError(swfuploadL10n.upload_failed);
       
   308 			break;
       
   309 		case SWFUpload.UPLOAD_ERROR.IO_ERROR:
       
   310 			wpQueueError(swfuploadL10n.io_error);
       
   311 			break;
       
   312 		case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
       
   313 			wpQueueError(swfuploadL10n.security_error);
       
   314 			break;
       
   315 		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
       
   316 		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
       
   317 			jQuery('#media-item-' + fileObj.id).remove();
       
   318 			break;
       
   319 		default:
       
   320 			wpFileError(fileObj, swfuploadL10n.default_error);
       
   321 	}
       
   322 }
       
   323 
       
   324 function cancelUpload() {
       
   325 	swfu.cancelQueue();
       
   326 }
       
   327 
       
   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');
       
   332 
       
   333 		if ( $(tr).hasClass('align') )
       
   334 			setUserSetting('align', $(this).val());
       
   335 		else if ( $(tr).hasClass('image-size') )
       
   336 			setUserSetting('imgsize', $(this).val());
       
   337 	});
       
   338 
       
   339 	$('button.button', '#media-items').live('click', function(){
       
   340 		var c = this.className || '';
       
   341 		c = c.match(/url([^ '"]+)/);
       
   342 		if ( c && c[1] ) {
       
   343 			setUserSetting('urlbutton', c[1]);
       
   344 			$(this).siblings('.urlfield').val( $(this).attr('title') );
       
   345 		}
       
   346 	});
       
   347 });