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