mp-wp_genesis 1 function fileDialogStart() {
mp-wp_genesis 2 jQuery("#media-upload-error").empty();
mp-wp_genesis 3 }
mp-wp_genesis 4
mp-wp_genesis 5 // progress and success handlers for media multi uploads
mp-wp_genesis 6 function fileQueued(fileObj) {
mp-wp_genesis 7 // Get rid of unused form
mp-wp_genesis 8 jQuery('.media-blank').remove();
mp-wp_genesis 9 // Collapse a single item
mp-wp_genesis 10 if ( jQuery('.type-form #media-items>*').length == 1 && jQuery('#media-items .hidden').length > 0 ) {
mp-wp_genesis 11 jQuery('.toggle').toggle();
mp-wp_genesis 12 jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
mp-wp_genesis 13 }
mp-wp_genesis 14 // Create a progress bar containing the filename
mp-wp_genesis 15 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">' + fileObj.name + '</div></div>');
mp-wp_genesis 16 // Display the progress div
mp-wp_genesis 17 jQuery('#media-item-' + fileObj.id + ' .progress').show();
mp-wp_genesis 18
mp-wp_genesis 19 // Disable the submit button
mp-wp_genesis 20 jQuery('#insert-gallery').attr('disabled', 'disabled');
mp-wp_genesis 21 }
mp-wp_genesis 22
mp-wp_genesis 23 function uploadStart(fileObj) { return true; }
mp-wp_genesis 24
mp-wp_genesis 25 function uploadProgress(fileObj, bytesDone, bytesTotal) {
mp-wp_genesis 26 // Lengthen the progress bar
mp-wp_genesis 27 jQuery('#media-item-' + fileObj.id + ' .bar').width(620*bytesDone/bytesTotal);
mp-wp_genesis 28
mp-wp_genesis 29 if ( bytesDone == bytesTotal )
mp-wp_genesis 30 jQuery('#media-item-' + fileObj.id + ' .bar').html('<strong class="crunching">' + swfuploadL10n.crunching + '</strong>');
mp-wp_genesis 31 }
mp-wp_genesis 32
mp-wp_genesis 33 function prepareMediaItem(fileObj, serverData) {
mp-wp_genesis 34 // Move the progress bar to 100%
mp-wp_genesis 35 jQuery('#media-item-' + fileObj.id + ' .bar').remove();
mp-wp_genesis 36 jQuery('#media-item-' + fileObj.id + ' .progress').hide();
mp-wp_genesis 37
mp-wp_genesis 38 var f = ( typeof shortform == 'undefined' ) ? 1 : 2;
mp-wp_genesis 39 // Old style: Append the HTML returned by the server -- thumbnail and form inputs
mp-wp_genesis 40 if ( isNaN(serverData) || !serverData ) {
mp-wp_genesis 41 jQuery('#media-item-' + fileObj.id).append(serverData);
mp-wp_genesis 42 prepareMediaItemInit(fileObj);
mp-wp_genesis 43 }
mp-wp_genesis 44 // New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
mp-wp_genesis 45 else {
mp-wp_genesis 46 jQuery('#media-item-' + fileObj.id).load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
mp-wp_genesis 47 }
mp-wp_genesis 48 }
mp-wp_genesis 49
mp-wp_genesis 50 function prepareMediaItemInit(fileObj) {
mp-wp_genesis 51
mp-wp_genesis 52 // Clone the thumbnail as a "pinkynail" -- a tiny image to the left of the filename
mp-wp_genesis 53 jQuery('#media-item-' + fileObj.id + ' .thumbnail').clone().attr('className', 'pinkynail toggle').prependTo('#media-item-' + fileObj.id);
mp-wp_genesis 54
mp-wp_genesis 55 // Replace the original filename with the new (unique) one assigned during upload
mp-wp_genesis 56 jQuery('#media-item-' + fileObj.id + ' .filename.original').replaceWith(jQuery('#media-item-' + fileObj.id + ' .filename.new'));
mp-wp_genesis 57
mp-wp_genesis 58 // Also bind toggle to the links
mp-wp_genesis 59 jQuery('#media-item-' + fileObj.id + ' a.toggle').bind('click', function(){jQuery(this).siblings('.slidetoggle').slideToggle(150, function(){var o=jQuery(this).offset();window.scrollTo(0,o.top-36);});jQuery(this).parent().eq(0).children('.toggle').toggle();jQuery(this).siblings('a.toggle').focus();return false;});
mp-wp_genesis 60
mp-wp_genesis 61 // Bind AJAX to the new Delete button
mp-wp_genesis 62 jQuery('#media-item-' + fileObj.id + ' a.delete').bind('click',function(){
mp-wp_genesis 63 // Tell the server to delete it. TODO: handle exceptions
mp-wp_genesis 64 jQuery.ajax({url:'admin-ajax.php',type:'post',success:deleteSuccess,error:deleteError,id:fileObj.id,data:{
mp-wp_genesis 65 id : this.id.replace(/[^0-9]/g,''),
mp-wp_genesis 66 action : 'delete-post',
mp-wp_genesis 67 _ajax_nonce : this.href.replace(/^.*wpnonce=/,'')}
mp-wp_genesis 68 });
mp-wp_genesis 69 return false;
mp-wp_genesis 70 });
mp-wp_genesis 71
mp-wp_genesis 72 // Open this item if it says to start open (e.g. to display an error)
mp-wp_genesis 73 jQuery('#media-item-' + fileObj.id + '.startopen')
mp-wp_genesis 74 .removeClass('startopen')
mp-wp_genesis 75 .slideToggle(500)
mp-wp_genesis 76 .parent().eq(0).children('.toggle').toggle();
mp-wp_genesis 77 }
mp-wp_genesis 78
mp-wp_genesis 79 function itemAjaxError(id, html) {
mp-wp_genesis 80 var error = jQuery('#media-item-error' + id);
mp-wp_genesis 81
mp-wp_genesis 82 error.html('<div class="file-error"><button type="button" id="dismiss-'+id+'" class="button dismiss">'+swfuploadL10n.dismiss+'</button>'+html+'</div>');
mp-wp_genesis 83 jQuery('#dismiss-'+id).click(function(){jQuery(this).parents('.file-error').slideUp(200, function(){jQuery(this).empty();})});
mp-wp_genesis 84 }
mp-wp_genesis 85
mp-wp_genesis 86 function deleteSuccess(data, textStatus) {
mp-wp_genesis 87 if ( data == '-1' )
mp-wp_genesis 88 return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
mp-wp_genesis 89 if ( data == '0' )
mp-wp_genesis 90 return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
mp-wp_genesis 91
mp-wp_genesis 92 var item = jQuery('#media-item-' + this.id);
mp-wp_genesis 93
mp-wp_genesis 94 // Decrement the counters.
mp-wp_genesis 95 if ( type = jQuery('#type-of-' + this.id).val() )
mp-wp_genesis 96 jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-1);
mp-wp_genesis 97 if ( item.hasClass('child-of-'+post_id) )
mp-wp_genesis 98 jQuery('#attachments-count').text(jQuery('#attachments-count').text()-1);
mp-wp_genesis 99
mp-wp_genesis 100 if ( jQuery('.type-form #media-items>*').length == 1 && jQuery('#media-items .hidden').length > 0 ) {
mp-wp_genesis 101 jQuery('.toggle').toggle();
mp-wp_genesis 102 jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
mp-wp_genesis 103 }
mp-wp_genesis 104
mp-wp_genesis 105 // Vanish it.
mp-wp_genesis 106 jQuery('#media-item-' + this.id + ' .filename:empty').remove();
mp-wp_genesis 107 jQuery('#media-item-' + this.id + ' .filename').append(' <span class="file-error">'+swfuploadL10n.deleted+'</span>').siblings('a.toggle').remove();
mp-wp_genesis 108 jQuery('#media-item-' + this.id).children('.describe').css({backgroundColor:'#fff'}).end()
mp-wp_genesis 109 .animate({backgroundColor:'#ffc0c0'}, {queue:false,duration:50})
mp-wp_genesis 110 .animate({minHeight:0,height:36}, 400, null, function(){jQuery(this).children('.describe').remove()})
mp-wp_genesis 111 .animate({backgroundColor:'#fff'}, 400)
mp-wp_genesis 112 .animate({height:0}, 800, null, function(){jQuery(this).remove();updateMediaForm();});
mp-wp_genesis 113
mp-wp_genesis 114 return;
mp-wp_genesis 115 }
mp-wp_genesis 116
mp-wp_genesis 117 function deleteError(X, textStatus, errorThrown) {
mp-wp_genesis 118 // TODO
mp-wp_genesis 119 }
mp-wp_genesis 120
mp-wp_genesis 121 function updateMediaForm() {
mp-wp_genesis 122 storeState();
mp-wp_genesis 123 // Just one file, no need for collapsible part
mp-wp_genesis 124 if ( jQuery('.type-form #media-items>*').length == 1 ) {
mp-wp_genesis 125 jQuery('#media-items .slidetoggle').slideDown(500).parent().eq(0).children('.toggle').toggle();
mp-wp_genesis 126 jQuery('.type-form .slidetoggle').siblings().addClass('hidden');
mp-wp_genesis 127 }
mp-wp_genesis 128
mp-wp_genesis 129 // Only show Save buttons when there is at least one file.
mp-wp_genesis 130 if ( jQuery('#media-items>*').not('.media-blank').length > 0 )
mp-wp_genesis 131 jQuery('.savebutton').show();
mp-wp_genesis 132 else
mp-wp_genesis 133 jQuery('.savebutton').hide();
mp-wp_genesis 134
mp-wp_genesis 135 // Only show Gallery button when there are at least two files.
mp-wp_genesis 136 if ( jQuery('#media-items>*').length > 1 )
mp-wp_genesis 137 jQuery('.insert-gallery').show();
mp-wp_genesis 138 else
mp-wp_genesis 139 jQuery('.insert-gallery').hide();
mp-wp_genesis 140 }
mp-wp_genesis 141
mp-wp_genesis 142 function uploadSuccess(fileObj, serverData) {
mp-wp_genesis 143 // if async-upload returned an error message, place it in the media item div and return
mp-wp_genesis 144 if ( serverData.match('media-upload-error') ) {
mp-wp_genesis 145 jQuery('#media-item-' + fileObj.id).html(serverData);
mp-wp_genesis 146 return;
mp-wp_genesis 147 }
mp-wp_genesis 148
mp-wp_genesis 149 prepareMediaItem(fileObj, serverData);
mp-wp_genesis 150 updateMediaForm();
mp-wp_genesis 151
mp-wp_genesis 152 // Increment the counter.
mp-wp_genesis 153 if ( jQuery('#media-item-' + fileObj.id).hasClass('child-of-' + post_id) )
mp-wp_genesis 154 jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);
mp-wp_genesis 155 }
mp-wp_genesis 156
mp-wp_genesis 157 function uploadComplete(fileObj) {
mp-wp_genesis 158 // If no more uploads queued, enable the submit button
mp-wp_genesis 159 if ( swfu.getStats().files_queued == 0 )
mp-wp_genesis 160 jQuery('#insert-gallery').attr('disabled', '');
mp-wp_genesis 161 }
mp-wp_genesis 162
mp-wp_genesis 163
mp-wp_genesis 164 // wp-specific error handlers
mp-wp_genesis 165
mp-wp_genesis 166 // generic message
mp-wp_genesis 167 function wpQueueError(message) {
mp-wp_genesis 168 jQuery('#media-upload-error').show().text(message);
mp-wp_genesis 169 }
mp-wp_genesis 170
mp-wp_genesis 171 // file-specific message
mp-wp_genesis 172 function wpFileError(fileObj, message) {
mp-wp_genesis 173 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();
mp-wp_genesis 174 jQuery('#dismiss-' + fileObj.id).click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})});
mp-wp_genesis 175 }
mp-wp_genesis 176
mp-wp_genesis 177 function fileQueueError(fileObj, error_code, message) {
mp-wp_genesis 178 // Handle this error separately because we don't want to create a FileProgress element for it.
mp-wp_genesis 179 if ( error_code == SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED ) {
mp-wp_genesis 180 wpQueueError(swfuploadL10n.queue_limit_exceeded);
mp-wp_genesis 181 }
mp-wp_genesis 182 else if ( error_code == SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT ) {
mp-wp_genesis 183 fileQueued(fileObj);
mp-wp_genesis 184 wpFileError(fileObj, swfuploadL10n.file_exceeds_size_limit);
mp-wp_genesis 185 }
mp-wp_genesis 186 else if ( error_code == SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE ) {
mp-wp_genesis 187 fileQueued(fileObj);
mp-wp_genesis 188 wpFileError(fileObj, swfuploadL10n.zero_byte_file);
mp-wp_genesis 189 }
mp-wp_genesis 190 else if ( error_code == SWFUpload.QUEUE_ERROR.INVALID_FILETYPE ) {
mp-wp_genesis 191 fileQueued(fileObj);
mp-wp_genesis 192 wpFileError(fileObj, swfuploadL10n.invalid_filetype);
mp-wp_genesis 193 }
mp-wp_genesis 194 else {
mp-wp_genesis 195 wpQueueError(swfuploadL10n.default_error);
mp-wp_genesis 196 }
mp-wp_genesis 197 }
mp-wp_genesis 198
mp-wp_genesis 199 function fileDialogComplete(num_files_queued) {
mp-wp_genesis 200 try {
mp-wp_genesis 201 if (num_files_queued > 0) {
mp-wp_genesis 202 this.startUpload();
mp-wp_genesis 203 }
mp-wp_genesis 204 } catch (ex) {
mp-wp_genesis 205 this.debug(ex);
mp-wp_genesis 206 }
mp-wp_genesis 207 }
mp-wp_genesis 208
mp-wp_genesis 209 function swfuploadPreLoad() {
mp-wp_genesis 210 var swfupload_element = jQuery('#'+swfu.customSettings.swfupload_element_id).get(0);
mp-wp_genesis 211 jQuery('#' + swfu.customSettings.degraded_element_id).hide();
mp-wp_genesis 212 // Doing this directly because jQuery().show() seems to have timing problems
mp-wp_genesis 213 if ( swfupload_element && ! swfupload_element.style.display )
mp-wp_genesis 214 swfupload_element.style.display = 'block';
mp-wp_genesis 215 }
mp-wp_genesis 216
mp-wp_genesis 217 function swfuploadLoadFailed() {
mp-wp_genesis 218 jQuery('#' + swfu.customSettings.swfupload_element_id).hide();
mp-wp_genesis 219 jQuery('#' + swfu.customSettings.degraded_element_id).show();
mp-wp_genesis 220 }
mp-wp_genesis 221
mp-wp_genesis 222 function uploadError(fileObj, error_code, message) {
mp-wp_genesis 223 // first the file specific error
mp-wp_genesis 224 if ( error_code == SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL ) {
mp-wp_genesis 225 wpFileError(fileObj, swfuploadL10n.missing_upload_url);
mp-wp_genesis 226 }
mp-wp_genesis 227 else if ( error_code == SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED ) {
mp-wp_genesis 228 wpFileError(fileObj, swfuploadL10n.upload_limit_exceeded);
mp-wp_genesis 229 }
mp-wp_genesis 230 else {
mp-wp_genesis 231 wpFileError(fileObj, swfuploadL10n.default_error);
mp-wp_genesis 232 }
mp-wp_genesis 233
mp-wp_genesis 234 // now the general upload status
mp-wp_genesis 235 if ( error_code == SWFUpload.UPLOAD_ERROR.HTTP_ERROR ) {
mp-wp_genesis 236 wpQueueError(swfuploadL10n.http_error);
mp-wp_genesis 237 }
mp-wp_genesis 238 else if ( error_code == SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED ) {
mp-wp_genesis 239 wpQueueError(swfuploadL10n.upload_failed);
mp-wp_genesis 240 }
mp-wp_genesis 241 else if ( error_code == SWFUpload.UPLOAD_ERROR.IO_ERROR ) {
mp-wp_genesis 242 wpQueueError(swfuploadL10n.io_error);
mp-wp_genesis 243 }
mp-wp_genesis 244 else if ( error_code == SWFUpload.UPLOAD_ERROR.SECURITY_ERROR ) {
mp-wp_genesis 245 wpQueueError(swfuploadL10n.security_error);
mp-wp_genesis 246 }
mp-wp_genesis 247 else if ( error_code == SWFUpload.UPLOAD_ERROR.FILE_CANCELLED ) {
mp-wp_genesis 248 wpQueueError(swfuploadL10n.security_error);
mp-wp_genesis 249 }
mp-wp_genesis 250 }
mp-wp_genesis 251
mp-wp_genesis 252 // remember the last used image size, alignment and url
mp-wp_genesis 253 var storeState;
mp-wp_genesis 254 (function($){
mp-wp_genesis 255
mp-wp_genesis 256 storeState = function(){
mp-wp_genesis 257 var align = getUserSetting('align') || '', imgsize = getUserSetting('imgsize') || '';
mp-wp_genesis 258
mp-wp_genesis 259 $('tr.align input[type="radio"]').click(function(){
mp-wp_genesis 260 setUserSetting('align', $(this).val());
mp-wp_genesis 261 }).filter(function(){
mp-wp_genesis 262 if ( $(this).val() == align )
mp-wp_genesis 263 return true;
mp-wp_genesis 264 return false;
mp-wp_genesis 265 }).attr('checked','checked');
mp-wp_genesis 266
mp-wp_genesis 267 $('tr.image-size input[type="radio"]').click(function(){
mp-wp_genesis 268 setUserSetting('imgsize', $(this).val());
mp-wp_genesis 269 }).filter(function(){
mp-wp_genesis 270 if ( $(this).attr('disabled') || $(this).val() != imgsize )
mp-wp_genesis 271 return false;
mp-wp_genesis 272 return true;
mp-wp_genesis 273 }).attr('checked','checked');
mp-wp_genesis 274
mp-wp_genesis 275 $('tr.url button').click(function(){
mp-wp_genesis 276 var c = this.className || '';
mp-wp_genesis 277 c = c.replace(/.*?(url[^ '"]+).*/, '$1');
mp-wp_genesis 278 if (c) setUserSetting('urlbutton', c);
mp-wp_genesis 279 $(this).siblings('.urlfield').val( $(this).attr('title') );
mp-wp_genesis 280 });
mp-wp_genesis 281
mp-wp_genesis 282 $('tr.url .urlfield').each(function(){
mp-wp_genesis 283 var b = getUserSetting('urlbutton');
mp-wp_genesis 284 $(this).val( $(this).siblings('button.'+b).attr('title') );
mp-wp_genesis 285 });
mp-wp_genesis 286 }
mp-wp_genesis 287 })(jQuery);