|
1 var topWin = window.dialogArguments || opener || parent || top; |
|
2 |
|
3 function fileDialogStart() { |
|
4 jQuery("#media-upload-error").empty(); |
|
5 } |
|
6 |
|
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'); |
|
16 } |
|
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(); |
|
26 |
|
27 // Disable submit and enable cancel |
|
28 jQuery('#insert-gallery').prop('disabled', true); |
|
29 jQuery('#cancel-upload').prop('disabled', false); |
|
30 } |
|
31 |
|
32 function uploadStart(fileObj) { |
|
33 try { |
|
34 if ( typeof topWin.tb_remove != 'undefined' ) |
|
35 topWin.jQuery('#TB_overlay').unbind('click', topWin.tb_remove); |
|
36 } catch(e){} |
|
37 |
|
38 return true; |
|
39 } |
|
40 |
|
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) + '%' ); |
|
46 |
|
47 if ( bytesDone == bytesTotal ) |
|
48 jQuery('.bar', item).html('<strong class="crunching">' + swfuploadL10n.crunching + '</strong>'); |
|
49 } |
|
50 |
|
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(); |
|
56 |
|
57 try { |
|
58 if ( typeof topWin.tb_remove != 'undefined' ) |
|
59 topWin.jQuery('#TB_overlay').click(topWin.tb_remove); |
|
60 } catch(e){} |
|
61 |
|
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); |
|
66 } |
|
67 // New style: server data is just the attachment ID, fetch the thumbnail and form html from the server |
|
68 else { |
|
69 item.load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()}); |
|
70 } |
|
71 } |
|
72 |
|
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); |
|
77 |
|
78 // Replace the original filename with the new (unique) one assigned during upload |
|
79 jQuery('.filename.original', item).replaceWith( jQuery('.filename.new', item) ); |
|
80 |
|
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; |
|
85 |
|
86 if ( w && t && h ) { |
|
87 b = t + h; |
|
88 |
|
89 if ( b > w && (h + 48) < w ) |
|
90 window.scrollBy(0, b - w + 13); |
|
91 else if ( b > w ) |
|
92 window.scrollTo(0, t - 36); |
|
93 } |
|
94 }); |
|
95 jQuery(this).siblings('.toggle').andSelf().toggle(); |
|
96 jQuery(this).siblings('a.toggle').focus(); |
|
97 return false; |
|
98 }); |
|
99 |
|
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 |
|
103 jQuery.ajax({ |
|
104 url: ajaxurl, |
|
105 type: 'post', |
|
106 success: deleteSuccess, |
|
107 error: deleteError, |
|
108 id: fileObj.id, |
|
109 data: { |
|
110 id : this.id.replace(/[^0-9]/g, ''), |
|
111 action : 'trash-post', |
|
112 _ajax_nonce : this.href.replace(/^.*wpnonce=/,'') |
|
113 } |
|
114 }); |
|
115 return false; |
|
116 }); |
|
117 |
|
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 |
|
121 jQuery.ajax({ |
|
122 url: ajaxurl, |
|
123 type: 'post', |
|
124 id: fileObj.id, |
|
125 data: { |
|
126 id : this.id.replace(/[^0-9]/g,''), |
|
127 action: 'untrash-post', |
|
128 _ajax_nonce: this.href.replace(/^.*wpnonce=/,'') |
|
129 }, |
|
130 success: function(data, textStatus){ |
|
131 var item = jQuery('#media-item-' + fileObj.id); |
|
132 |
|
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); |
|
137 |
|
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'); |
|
143 } |
|
144 }); |
|
145 return false; |
|
146 }); |
|
147 |
|
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(); |
|
150 } |
|
151 |
|
152 function itemAjaxError(id, html) { |
|
153 var item = jQuery('#media-item-' + id); |
|
154 var filename = jQuery('.filename', item).text(); |
|
155 |
|
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 />' |
|
159 + html |
|
160 + '</div>'); |
|
161 item.find('a.dismiss').click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})}); |
|
162 } |
|
163 |
|
164 function deleteSuccess(data, textStatus) { |
|
165 if ( data == '-1' ) |
|
166 return itemAjaxError(this.id, 'You do not have permission. Has your session expired?'); |
|
167 if ( data == '0' ) |
|
168 return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?'); |
|
169 |
|
170 var id = this.id, item = jQuery('#media-item-' + id); |
|
171 |
|
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 ); |
|
177 |
|
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'); |
|
181 } |
|
182 |
|
183 // Vanish it. |
|
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'); |
|
187 |
|
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(); |
|
193 |
|
194 return; |
|
195 } |
|
196 |
|
197 function deleteError(X, textStatus, errorThrown) { |
|
198 // TODO |
|
199 } |
|
200 |
|
201 function updateMediaForm() { |
|
202 var one = jQuery('form.type-form #media-items').children(), items = jQuery('#media-items').children(); |
|
203 |
|
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(); |
|
207 } |
|
208 |
|
209 // Only show Save buttons when there is at least one file. |
|
210 if ( items.not('.media-blank').length > 0 ) |
|
211 jQuery('.savebutton').show(); |
|
212 else |
|
213 jQuery('.savebutton').hide(); |
|
214 |
|
215 // Only show Gallery button when there are at least two files. |
|
216 if ( items.length > 1 ) |
|
217 jQuery('.insert-gallery').show(); |
|
218 else |
|
219 jQuery('.insert-gallery').hide(); |
|
220 } |
|
221 |
|
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); |
|
226 return; |
|
227 } |
|
228 |
|
229 prepareMediaItem(fileObj, serverData); |
|
230 updateMediaForm(); |
|
231 |
|
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); |
|
235 } |
|
236 |
|
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); |
|
242 } |
|
243 } |
|
244 |
|
245 |
|
246 // wp-specific error handlers |
|
247 |
|
248 // generic message |
|
249 function wpQueueError(message) { |
|
250 jQuery('#media-upload-error').show().text(message); |
|
251 } |
|
252 |
|
253 // file-specific message |
|
254 function wpFileError(fileObj, message) { |
|
255 var item = jQuery('#media-item-' + fileObj.id); |
|
256 var filename = jQuery('.filename', item).text(); |
|
257 |
|
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 />' |
|
261 + message |
|
262 + '</div>'); |
|
263 item.find('a.dismiss').click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})}); |
|
264 } |
|
265 |
|
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); |
|
270 } |
|
271 else if ( error_code == SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT ) { |
|
272 fileQueued(fileObj); |
|
273 wpFileError(fileObj, swfuploadL10n.file_exceeds_size_limit); |
|
274 } |
|
275 else if ( error_code == SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE ) { |
|
276 fileQueued(fileObj); |
|
277 wpFileError(fileObj, swfuploadL10n.zero_byte_file); |
|
278 } |
|
279 else if ( error_code == SWFUpload.QUEUE_ERROR.INVALID_FILETYPE ) { |
|
280 fileQueued(fileObj); |
|
281 wpFileError(fileObj, swfuploadL10n.invalid_filetype); |
|
282 } |
|
283 else { |
|
284 wpQueueError(swfuploadL10n.default_error); |
|
285 } |
|
286 } |
|
287 |
|
288 function fileDialogComplete(num_files_queued) { |
|
289 try { |
|
290 if (num_files_queued > 0) { |
|
291 this.startUpload(); |
|
292 } |
|
293 } catch (ex) { |
|
294 this.debug(ex); |
|
295 } |
|
296 } |
|
297 |
|
298 function switchUploader(s) { |
|
299 var f = document.getElementById(swfu.customSettings.swfupload_element_id), h = document.getElementById(swfu.customSettings.degraded_element_id); |
|
300 if ( s ) { |
|
301 f.style.display = 'block'; |
|
302 h.style.display = 'none'; |
|
303 } else { |
|
304 f.style.display = 'none'; |
|
305 h.style.display = 'block'; |
|
306 } |
|
307 } |
|
308 |
|
309 function swfuploadPreLoad() { |
|
310 if ( !uploaderMode ) { |
|
311 switchUploader(1); |
|
312 } else { |
|
313 switchUploader(0); |
|
314 } |
|
315 } |
|
316 |
|
317 function swfuploadLoadFailed() { |
|
318 switchUploader(0); |
|
319 jQuery('.upload-html-bypass').hide(); |
|
320 } |
|
321 |
|
322 function uploadError(fileObj, errorCode, message) { |
|
323 |
|
324 switch (errorCode) { |
|
325 case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL: |
|
326 wpFileError(fileObj, swfuploadL10n.missing_upload_url); |
|
327 break; |
|
328 case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED: |
|
329 wpFileError(fileObj, swfuploadL10n.upload_limit_exceeded); |
|
330 break; |
|
331 case SWFUpload.UPLOAD_ERROR.HTTP_ERROR: |
|
332 wpQueueError(swfuploadL10n.http_error); |
|
333 break; |
|
334 case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED: |
|
335 wpQueueError(swfuploadL10n.upload_failed); |
|
336 break; |
|
337 case SWFUpload.UPLOAD_ERROR.IO_ERROR: |
|
338 wpQueueError(swfuploadL10n.io_error); |
|
339 break; |
|
340 case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR: |
|
341 wpQueueError(swfuploadL10n.security_error); |
|
342 break; |
|
343 case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED: |
|
344 case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED: |
|
345 jQuery('#media-item-' + fileObj.id).remove(); |
|
346 break; |
|
347 default: |
|
348 wpFileError(fileObj, swfuploadL10n.default_error); |
|
349 } |
|
350 } |
|
351 |
|
352 function cancelUpload() { |
|
353 swfu.cancelQueue(); |
|
354 } |
|
355 |
|
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'); |
|
360 |
|
361 if ( $(tr).hasClass('align') ) |
|
362 setUserSetting('align', $(this).val()); |
|
363 else if ( $(tr).hasClass('image-size') ) |
|
364 setUserSetting('imgsize', $(this).val()); |
|
365 }); |
|
366 |
|
367 $('button.button', '#media-items').live('click', function(){ |
|
368 var c = this.className || ''; |
|
369 c = c.match(/url([^ '"]+)/); |
|
370 if ( c && c[1] ) { |
|
371 setUserSetting('urlbutton', c[1]); |
|
372 $(this).siblings('.urlfield').val( $(this).attr('title') ); |
|
373 } |
|
374 }); |
|
375 }); |