web/wp-includes/js/swfupload/handlers.dev.js
branchwordpress
changeset 132 4d4862461b8d
parent 109 03b0d1493584
equal deleted inserted replaced
131:a4642baaf829 132:4d4862461b8d
     6 // progress and success handlers for media multi uploads
     6 // progress and success handlers for media multi uploads
     7 function fileQueued(fileObj) {
     7 function fileQueued(fileObj) {
     8 	// Get rid of unused form
     8 	// Get rid of unused form
     9 	jQuery('.media-blank').remove();
     9 	jQuery('.media-blank').remove();
    10 	// Collapse a single item
    10 	// Collapse a single item
    11 	if ( jQuery('.type-form #media-items>*').length == 1 && jQuery('#media-items .hidden').length > 0 ) {
    11 	if ( jQuery('form.type-form #media-items').children().length == 1 && jQuery('.hidden', '#media-items').length > 0 ) {
    12 		jQuery('.describe-toggle-on').show();
    12 		jQuery('.describe-toggle-on').show();
    13 		jQuery('.describe-toggle-off').hide();
    13 		jQuery('.describe-toggle-off').hide();
    14 		jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
    14 		jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
    15 	}
    15 	}
    16 	// Create a progress bar containing the filename
    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>');
    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
    18 	// Display the progress div
    19 	jQuery('#media-item-' + fileObj.id + ' .progress').show();
    19 	jQuery('.progress', '#media-item-' + fileObj.id).show();
    20 
    20 
    21 	// Disable submit and enable cancel
    21 	// Disable submit and enable cancel
    22 	jQuery('#insert-gallery').attr('disabled', 'disabled');
    22 	jQuery('#insert-gallery').attr('disabled', 'disabled');
    23 	jQuery('#cancel-upload').attr('disabled', '');
    23 	jQuery('#cancel-upload').attr('disabled', '');
    24 }
    24 }
    27 	return true;
    27 	return true;
    28 }
    28 }
    29 
    29 
    30 function uploadProgress(fileObj, bytesDone, bytesTotal) {
    30 function uploadProgress(fileObj, bytesDone, bytesTotal) {
    31 	// Lengthen the progress bar
    31 	// Lengthen the progress bar
    32 	var w = jQuery('#media-items').width() - 2;
    32 	var w = jQuery('#media-items').width() - 2, item = jQuery('#media-item-' + fileObj.id);
    33 	jQuery('#media-item-' + fileObj.id + ' .bar').width( w * bytesDone / bytesTotal );
    33 	jQuery('.bar', item).width( w * bytesDone / bytesTotal );
    34 	jQuery('#media-item-' + fileObj.id + ' .percent').html( Math.ceil(bytesDone / bytesTotal * 100) + '%' );
    34 	jQuery('.percent', item).html( Math.ceil(bytesDone / bytesTotal * 100) + '%' );
    35 
    35 
    36 	if ( bytesDone == bytesTotal )
    36 	if ( bytesDone == bytesTotal )
    37 		jQuery('#media-item-' + fileObj.id + ' .bar').html('<strong class="crunching">' + swfuploadL10n.crunching + '</strong>');
    37 		jQuery('.bar', item).html('<strong class="crunching">' + swfuploadL10n.crunching + '</strong>');
    38 }
    38 }
    39 
    39 
    40 function prepareMediaItem(fileObj, serverData) {
    40 function prepareMediaItem(fileObj, serverData) {
       
    41 	var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id);
    41 	// Move the progress bar to 100%
    42 	// Move the progress bar to 100%
    42 	jQuery('#media-item-' + fileObj.id + ' .bar').remove();
    43 	jQuery('.bar', item).remove();
    43 	jQuery('#media-item-' + fileObj.id + ' .progress').hide();
    44 	jQuery('.progress', item).hide();
    44 
    45 
    45 	var f = ( typeof shortform == 'undefined' ) ? 1 : 2;
       
    46 	// Old style: Append the HTML returned by the server -- thumbnail and form inputs
    46 	// Old style: Append the HTML returned by the server -- thumbnail and form inputs
    47 	if ( isNaN(serverData) || !serverData ) {
    47 	if ( isNaN(serverData) || !serverData ) {
    48 		jQuery('#media-item-' + fileObj.id).append(serverData);
    48 		item.append(serverData);
    49 		prepareMediaItemInit(fileObj);
    49 		prepareMediaItemInit(fileObj);
    50 	}
    50 	}
    51 	// New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
    51 	// New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
    52 	else {
    52 	else {
    53 		jQuery('#media-item-' + fileObj.id).load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
    53 		item.load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
    54 	}
    54 	}
    55 }
    55 }
    56 		
    56 
    57 function prepareMediaItemInit(fileObj) {
    57 function prepareMediaItemInit(fileObj) {
    58 
    58 	var item = jQuery('#media-item-' + fileObj.id);
    59 	// Clone the thumbnail as a "pinkynail" -- a tiny image to the left of the filename
    59 	// Clone the thumbnail as a "pinkynail" -- a tiny image to the left of the filename
    60 	jQuery('#media-item-' + fileObj.id + ' .thumbnail').clone().attr('className', 'pinkynail toggle').prependTo('#media-item-' + fileObj.id);
    60 	jQuery('.thumbnail', item).clone().attr('className', 'pinkynail toggle').prependTo(item);
    61 
    61 
    62 	// Replace the original filename with the new (unique) one assigned during upload
    62 	// Replace the original filename with the new (unique) one assigned during upload
    63 	jQuery('#media-item-' + fileObj.id + ' .filename.original').replaceWith(jQuery('#media-item-' + fileObj.id + ' .filename.new'));
    63 	jQuery('.filename.original', item).replaceWith( jQuery('.filename.new', item) );
    64 
    64 
    65 	// Also bind toggle to the links
    65 	// Also bind toggle to the links
    66 	jQuery('#media-item-' + fileObj.id + ' a.toggle').click(function(){
    66 	jQuery('a.toggle', item).click(function(){
    67 		jQuery(this).siblings('.slidetoggle').slideToggle(150, function(){
    67 		jQuery(this).siblings('.slidetoggle').slideToggle(350, function(){
    68 			var o = jQuery(this).offset();
    68 			var w = jQuery(window).height(), t = jQuery(this).offset().top, h = jQuery(this).height(), b;
    69 			window.scrollTo(0, o.top-36);
    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             }
    70 		});
    78 		});
    71 		jQuery(this).parent().children('.toggle').toggle();
    79 		jQuery(this).siblings('.toggle').andSelf().toggle();
    72 		jQuery(this).siblings('a.toggle').focus();
    80 		jQuery(this).siblings('a.toggle').focus();
    73 		return false;
    81 		return false;
    74 	});
    82 	});
    75 
    83 
    76 	// Bind AJAX to the new Delete button
    84 	// Bind AJAX to the new Delete button
    77 	jQuery('#media-item-' + fileObj.id + ' a.delete').click(function(){
    85 	jQuery('a.delete', item).click(function(){
    78 		// Tell the server to delete it. TODO: handle exceptions
    86 		// Tell the server to delete it. TODO: handle exceptions
    79 		jQuery.ajax({url:'admin-ajax.php',type:'post',success:deleteSuccess,error:deleteError,id:fileObj.id,data:{
    87 		jQuery.ajax({
    80 			id : this.id.replace(/[^0-9]/g,''),
    88 			url: 'admin-ajax.php',
    81 			action : 'delete-post',
    89 			type: 'post',
    82 			_ajax_nonce : this.href.replace(/^.*wpnonce=/,'')}
    90 			success: deleteSuccess,
    83 			});
    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 		});
    84 		return false;
    99 		return false;
    85 	});
   100 	});
    86 
   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 
    87 	// Open this item if it says to start open (e.g. to display an error)
   132 	// Open this item if it says to start open (e.g. to display an error)
    88 	jQuery('#media-item-' + fileObj.id + '.startopen').removeClass('startopen').slideToggle(500).parent().children('.toggle').toggle();
   133 	jQuery('#media-item-' + fileObj.id + '.startopen').removeClass('startopen').slideToggle(500).siblings('.toggle').toggle();
    89 }
   134 }
    90 
   135 
    91 function itemAjaxError(id, html) {
   136 function itemAjaxError(id, html) {
    92 	var error = jQuery('#media-item-error' + id);
   137 	var error = jQuery('#media-item-error' + id);
    93 
   138 
    99 	if ( data == '-1' )
   144 	if ( data == '-1' )
   100 		return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
   145 		return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
   101 	if ( data == '0' )
   146 	if ( data == '0' )
   102 		return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
   147 		return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
   103 
   148 
   104 	var item = jQuery('#media-item-' + this.id);
   149 	var id = this.id, item = jQuery('#media-item-' + id);
   105 
   150 
   106 	// Decrement the counters.
   151 	// Decrement the counters.
   107 	if ( type = jQuery('#type-of-' + this.id).val() )
   152 	if ( type = jQuery('#type-of-' + id).val() )
   108 		jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-1);
   153 		jQuery('#' + type + '-counter').text( jQuery('#' + type + '-counter').text() - 1 );
   109 	if ( item.hasClass('child-of-'+post_id) )
   154 	if ( item.hasClass('child-of-'+post_id) )
   110 		jQuery('#attachments-count').text(jQuery('#attachments-count').text()-1);
   155 		jQuery('#attachments-count').text( jQuery('#attachments-count').text() - 1 );
   111 
   156 
   112 	if ( jQuery('.type-form #media-items>*').length == 1 && jQuery('#media-items .hidden').length > 0 ) {
   157 	if ( jQuery('form.type-form #media-items').children().length == 1 && jQuery('.hidden', '#media-items').length > 0 ) {
   113 		jQuery('.toggle').toggle();
   158 		jQuery('.toggle').toggle();
   114 		jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
   159 		jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
   115 	}
   160 	}
   116 
   161 
   117 	// Vanish it.
   162 	// Vanish it.
   118 	jQuery('#media-item-' + this.id + ' .filename:empty').remove();
   163 	jQuery('.toggle', item).toggle();
   119 	jQuery('#media-item-' + this.id + ' .filename').append(' <span class="file-error">'+swfuploadL10n.deleted+'</span>').siblings('a.toggle').remove();
   164 	jQuery('.slidetoggle', item).slideUp(200).siblings().removeClass('hidden');
   120 	jQuery('#media-item-' + this.id).children('.describe').css({backgroundColor:'#fff'}).end()
   165 	item.css( {backgroundColor:'#faa'} ).animate( {backgroundColor:'#f4f4f4'}, {queue:false, duration:500} ).addClass('undo');
   121 			.animate({backgroundColor:'#ffc0c0'}, {queue:false,duration:50})
   166 
   122 			.animate({minHeight:0,height:36}, 400, null, function(){jQuery(this).children('.describe').remove()})
   167 	jQuery('.filename:empty', item).remove();
   123 			.animate({backgroundColor:'#fff'}, 400)
   168 	jQuery('.filename .title', item).css('font-weight','bold');
   124 			.animate({height:0}, 800, null, function(){jQuery(this).remove();updateMediaForm();});
   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();
   125 
   172 
   126 	return;
   173 	return;
   127 }
   174 }
   128 
   175 
   129 function deleteError(X, textStatus, errorThrown) {
   176 function deleteError(X, textStatus, errorThrown) {
   130 	// TODO
   177 	// TODO
   131 }
   178 }
   132 
   179 
   133 function updateMediaForm() {
   180 function updateMediaForm() {
   134 	storeState();
   181 	var one = jQuery('form.type-form #media-items').children(), items = jQuery('#media-items').children();
       
   182 
   135 	// Just one file, no need for collapsible part
   183 	// Just one file, no need for collapsible part
   136 	if ( jQuery('.type-form #media-items>*').length == 1 ) {
   184 	if ( one.length == 1 ) {
   137 		jQuery('#media-items .slidetoggle').slideDown(500).parent().eq(0).children('.toggle').toggle();
   185 		jQuery('.slidetoggle', one).slideDown(500).siblings().addClass('hidden').filter('.toggle').toggle();
   138 		jQuery('.type-form .slidetoggle').siblings().addClass('hidden');
       
   139 	}
   186 	}
   140 
   187 
   141 	// Only show Save buttons when there is at least one file.
   188 	// Only show Save buttons when there is at least one file.
   142 	if ( jQuery('#media-items>*').not('.media-blank').length > 0 )
   189 	if ( items.not('.media-blank').length > 0 )
   143 		jQuery('.savebutton').show();
   190 		jQuery('.savebutton').show();
   144 	else
   191 	else
   145 		jQuery('.savebutton').hide();
   192 		jQuery('.savebutton').hide();
   146 
   193 
   147 	// Only show Gallery button when there are at least two files.
   194 	// Only show Gallery button when there are at least two files.
   148 	if ( jQuery('#media-items>*').length > 1 )
   195 	if ( items.length > 1 )
   149 		jQuery('.insert-gallery').show();
   196 		jQuery('.insert-gallery').show();
   150 	else
   197 	else
   151 		jQuery('.insert-gallery').hide();
   198 		jQuery('.insert-gallery').hide();
   152 }
   199 }
   153 
   200 
   243 	switchUploader(0);
   290 	switchUploader(0);
   244 	jQuery('.upload-html-bypass').hide();
   291 	jQuery('.upload-html-bypass').hide();
   245 }
   292 }
   246 
   293 
   247 function uploadError(fileObj, errorCode, message) {
   294 function uploadError(fileObj, errorCode, message) {
   248 	
   295 
   249 	switch (errorCode) {
   296 	switch (errorCode) {
   250 		case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
   297 		case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
   251 			wpFileError(fileObj, swfuploadL10n.missing_upload_url);
   298 			wpFileError(fileObj, swfuploadL10n.missing_upload_url);
   252 			break;
   299 			break;
   253 		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
   300 		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
   277 function cancelUpload() {
   324 function cancelUpload() {
   278 	swfu.cancelQueue();
   325 	swfu.cancelQueue();
   279 }
   326 }
   280 
   327 
   281 // remember the last used image size, alignment and url
   328 // remember the last used image size, alignment and url
   282 var storeState;
   329 jQuery(document).ready(function($){
   283 (function($){
   330 	$('input[type="radio"]', '#media-items').live('click', function(){
   284 
   331 		var tr = $(this).closest('tr');
   285 storeState = function(){
   332 
   286 	var align = getUserSetting('align') || '', imgsize = getUserSetting('imgsize') || '';
   333 		if ( $(tr).hasClass('align') )
   287 
   334 			setUserSetting('align', $(this).val());
   288 	$('tr.align input[type="radio"]').click(function(){
   335 		else if ( $(tr).hasClass('image-size') )
   289 		setUserSetting('align', $(this).val());
   336 			setUserSetting('imgsize', $(this).val());
   290 	}).filter(function(){
   337 	});
   291 		if ( $(this).val() == align )
   338 
   292 			return true;
   339 	$('button.button', '#media-items').live('click', function(){
   293 		return false;
       
   294 	}).attr('checked','checked');
       
   295 
       
   296 	$('tr.image-size input[type="radio"]').click(function(){
       
   297 		setUserSetting('imgsize', $(this).val());
       
   298 	}).filter(function(){
       
   299 		if ( $(this).attr('disabled') || $(this).val() != imgsize )
       
   300 			return false;
       
   301 		return true;
       
   302 	}).attr('checked','checked');
       
   303 
       
   304 	$('tr.url button').click(function(){
       
   305 		var c = this.className || '';
   340 		var c = this.className || '';
   306 		c = c.replace(/.*?(url[^ '"]+).*/, '$1');
   341 		c = c.match(/url([^ '"]+)/);
   307 		if (c) setUserSetting('urlbutton', c);
   342 		if ( c && c[1] ) {
   308 		$(this).siblings('.urlfield').val( $(this).attr('title') );
   343 			setUserSetting('urlbutton', c[1]);
   309 	});
   344 			$(this).siblings('.urlfield').val( $(this).attr('title') );
   310 
   345 		}
   311 	$('tr.url .urlfield').each(function(){
   346 	});
   312 		var b = getUserSetting('urlbutton');
   347 });
   313 		$(this).val( $(this).siblings('button.'+b).attr('title') );
       
   314 	});
       
   315 }
       
   316 })(jQuery);