|
1 var topWin = window.dialogArguments || opener || parent || top, uploader, uploader_init; |
|
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 |
|
12 var items = jQuery('#media-items').children(), postid = post_id || 0; |
|
13 |
|
14 // Collapse a single item |
|
15 if ( items.length == 1 ) { |
|
16 items.removeClass('open').find('.slidetoggle').slideUp(200); |
|
17 } |
|
18 // Create a progress bar containing the filename |
|
19 jQuery('#media-items').append('<div id="media-item-' + fileObj.id + '" class="media-item child-of-' + postid + '"><div class="progress"><div class="percent">0%</div><div class="bar"></div></div><div class="filename original"> ' + fileObj.name + '</div></div>'); |
|
20 |
|
21 // Disable submit |
|
22 jQuery('#insert-gallery').prop('disabled', true); |
|
23 } |
|
24 |
|
25 function uploadStart() { |
|
26 try { |
|
27 if ( typeof topWin.tb_remove != 'undefined' ) |
|
28 topWin.jQuery('#TB_overlay').unbind('click', topWin.tb_remove); |
|
29 } catch(e){} |
|
30 |
|
31 return true; |
|
32 } |
|
33 |
|
34 function uploadProgress(up, file) { |
|
35 var item = jQuery('#media-item-' + file.id); |
|
36 |
|
37 jQuery('.bar', item).width( (200 * file.loaded) / file.size ); |
|
38 jQuery('.percent', item).html( file.percent + '%' ); |
|
39 } |
|
40 |
|
41 // check to see if a large file failed to upload |
|
42 function fileUploading(up, file) { |
|
43 var hundredmb = 100 * 1024 * 1024, max = parseInt(up.settings.max_file_size, 10); |
|
44 |
|
45 if ( max > hundredmb && file.size > hundredmb ) { |
|
46 setTimeout(function(){ |
|
47 var done; |
|
48 |
|
49 if ( file.status < 3 && file.loaded == 0 ) { // not uploading |
|
50 wpFileError(file, pluploadL10n.big_upload_failed.replace('%1$s', '<a class="uploader-html" href="#">').replace('%2$s', '</a>')); |
|
51 up.stop(); // stops the whole queue |
|
52 up.removeFile(file); |
|
53 up.start(); // restart the queue |
|
54 } |
|
55 }, 10000); // wait for 10 sec. for the file to start uploading |
|
56 } |
|
57 } |
|
58 |
|
59 function updateMediaForm() { |
|
60 var items = jQuery('#media-items').children(); |
|
61 |
|
62 // Just one file, no need for collapsible part |
|
63 if ( items.length == 1 ) { |
|
64 items.addClass('open').find('.slidetoggle').show(); |
|
65 jQuery('.insert-gallery').hide(); |
|
66 } else if ( items.length > 1 ) { |
|
67 items.removeClass('open'); |
|
68 // Only show Gallery button when there are at least two files. |
|
69 jQuery('.insert-gallery').show(); |
|
70 } |
|
71 |
|
72 // Only show Save buttons when there is at least one file. |
|
73 if ( items.not('.media-blank').length > 0 ) |
|
74 jQuery('.savebutton').show(); |
|
75 else |
|
76 jQuery('.savebutton').hide(); |
|
77 } |
|
78 |
|
79 function uploadSuccess(fileObj, serverData) { |
|
80 var item = jQuery('#media-item-' + fileObj.id); |
|
81 |
|
82 // on success serverData should be numeric, fix bug in html4 runtime returning the serverData wrapped in a <pre> tag |
|
83 serverData = serverData.replace(/^<pre>(\d+)<\/pre>$/, '$1'); |
|
84 |
|
85 // if async-upload returned an error message, place it in the media item div and return |
|
86 if ( serverData.match(/media-upload-error|error-div/) ) { |
|
87 item.html(serverData); |
|
88 return; |
|
89 } else { |
|
90 jQuery('.percent', item).html( pluploadL10n.crunching ); |
|
91 } |
|
92 |
|
93 prepareMediaItem(fileObj, serverData); |
|
94 updateMediaForm(); |
|
95 |
|
96 // Increment the counter. |
|
97 if ( post_id && item.hasClass('child-of-' + post_id) ) |
|
98 jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1); |
|
99 } |
|
100 |
|
101 function setResize(arg) { |
|
102 if ( arg ) { |
|
103 if ( uploader.features.jpgresize ) |
|
104 uploader.settings['resize'] = { width: resize_width, height: resize_height, quality: 100 }; |
|
105 else |
|
106 uploader.settings.multipart_params.image_resize = true; |
|
107 } else { |
|
108 delete(uploader.settings.resize); |
|
109 delete(uploader.settings.multipart_params.image_resize); |
|
110 } |
|
111 } |
|
112 |
|
113 function prepareMediaItem(fileObj, serverData) { |
|
114 var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id); |
|
115 |
|
116 try { |
|
117 if ( typeof topWin.tb_remove != 'undefined' ) |
|
118 topWin.jQuery('#TB_overlay').click(topWin.tb_remove); |
|
119 } catch(e){} |
|
120 |
|
121 if ( isNaN(serverData) || !serverData ) { // Old style: Append the HTML returned by the server -- thumbnail and form inputs |
|
122 item.append(serverData); |
|
123 prepareMediaItemInit(fileObj); |
|
124 } else { // New style: server data is just the attachment ID, fetch the thumbnail and form html from the server |
|
125 item.load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()}); |
|
126 } |
|
127 } |
|
128 |
|
129 function prepareMediaItemInit(fileObj) { |
|
130 var item = jQuery('#media-item-' + fileObj.id); |
|
131 // Clone the thumbnail as a "pinkynail" -- a tiny image to the left of the filename |
|
132 jQuery('.thumbnail', item).clone().attr('class', 'pinkynail toggle').prependTo(item); |
|
133 |
|
134 // Replace the original filename with the new (unique) one assigned during upload |
|
135 jQuery('.filename.original', item).replaceWith( jQuery('.filename.new', item) ); |
|
136 |
|
137 // Bind AJAX to the new Delete button |
|
138 jQuery('a.delete', item).click(function(){ |
|
139 // Tell the server to delete it. TODO: handle exceptions |
|
140 jQuery.ajax({ |
|
141 url: ajaxurl, |
|
142 type: 'post', |
|
143 success: deleteSuccess, |
|
144 error: deleteError, |
|
145 id: fileObj.id, |
|
146 data: { |
|
147 id : this.id.replace(/[^0-9]/g, ''), |
|
148 action : 'trash-post', |
|
149 _ajax_nonce : this.href.replace(/^.*wpnonce=/,'') |
|
150 } |
|
151 }); |
|
152 return false; |
|
153 }); |
|
154 |
|
155 // Bind AJAX to the new Undo button |
|
156 jQuery('a.undo', item).click(function(){ |
|
157 // Tell the server to untrash it. TODO: handle exceptions |
|
158 jQuery.ajax({ |
|
159 url: ajaxurl, |
|
160 type: 'post', |
|
161 id: fileObj.id, |
|
162 data: { |
|
163 id : this.id.replace(/[^0-9]/g,''), |
|
164 action: 'untrash-post', |
|
165 _ajax_nonce: this.href.replace(/^.*wpnonce=/,'') |
|
166 }, |
|
167 success: function(data, textStatus){ |
|
168 var item = jQuery('#media-item-' + fileObj.id); |
|
169 |
|
170 if ( type = jQuery('#type-of-' + fileObj.id).val() ) |
|
171 jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-0+1); |
|
172 |
|
173 if ( post_id && item.hasClass('child-of-'+post_id) ) |
|
174 jQuery('#attachments-count').text(jQuery('#attachments-count').text()-0+1); |
|
175 |
|
176 jQuery('.filename .trashnotice', item).remove(); |
|
177 jQuery('.filename .title', item).css('font-weight','normal'); |
|
178 jQuery('a.undo', item).addClass('hidden'); |
|
179 jQuery('.menu_order_input', item).show(); |
|
180 item.css( {backgroundColor:'#ceb'} ).animate( {backgroundColor: '#fff'}, { queue: false, duration: 500, complete: function(){ jQuery(this).css({backgroundColor:''}); } }).removeClass('undo'); |
|
181 } |
|
182 }); |
|
183 return false; |
|
184 }); |
|
185 |
|
186 // Open this item if it says to start open (e.g. to display an error) |
|
187 jQuery('#media-item-' + fileObj.id + '.startopen').removeClass('startopen').addClass('open').find('slidetoggle').fadeIn(); |
|
188 } |
|
189 |
|
190 // generic error message |
|
191 function wpQueueError(message) { |
|
192 jQuery('#media-upload-error').show().html( '<div class="error"><p>' + message + '</p></div>' ); |
|
193 } |
|
194 |
|
195 // file-specific error messages |
|
196 function wpFileError(fileObj, message) { |
|
197 itemAjaxError(fileObj.id, message); |
|
198 } |
|
199 |
|
200 function itemAjaxError(id, message) { |
|
201 var item = jQuery('#media-item-' + id), filename = item.find('.filename').text(), last_err = item.data('last-err'); |
|
202 |
|
203 if ( last_err == id ) // prevent firing an error for the same file twice |
|
204 return; |
|
205 |
|
206 item.html('<div class="error-div">' |
|
207 + '<a class="dismiss" href="#">' + pluploadL10n.dismiss + '</a>' |
|
208 + '<strong>' + pluploadL10n.error_uploading.replace('%s', jQuery.trim(filename)) + '</strong> ' |
|
209 + message |
|
210 + '</div>').data('last-err', id); |
|
211 } |
|
212 |
|
213 function deleteSuccess(data, textStatus) { |
|
214 if ( data == '-1' ) |
|
215 return itemAjaxError(this.id, 'You do not have permission. Has your session expired?'); |
|
216 |
|
217 if ( data == '0' ) |
|
218 return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?'); |
|
219 |
|
220 var id = this.id, item = jQuery('#media-item-' + id); |
|
221 |
|
222 // Decrement the counters. |
|
223 if ( type = jQuery('#type-of-' + id).val() ) |
|
224 jQuery('#' + type + '-counter').text( jQuery('#' + type + '-counter').text() - 1 ); |
|
225 |
|
226 if ( post_id && item.hasClass('child-of-'+post_id) ) |
|
227 jQuery('#attachments-count').text( jQuery('#attachments-count').text() - 1 ); |
|
228 |
|
229 if ( jQuery('form.type-form #media-items').children().length == 1 && jQuery('.hidden', '#media-items').length > 0 ) { |
|
230 jQuery('.toggle').toggle(); |
|
231 jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden'); |
|
232 } |
|
233 |
|
234 // Vanish it. |
|
235 jQuery('.toggle', item).toggle(); |
|
236 jQuery('.slidetoggle', item).slideUp(200).siblings().removeClass('hidden'); |
|
237 item.css( {backgroundColor:'#faa'} ).animate( {backgroundColor:'#f4f4f4'}, {queue:false, duration:500} ).addClass('undo'); |
|
238 |
|
239 jQuery('.filename:empty', item).remove(); |
|
240 jQuery('.filename .title', item).css('font-weight','bold'); |
|
241 jQuery('.filename', item).append('<span class="trashnotice"> ' + pluploadL10n.deleted + ' </span>').siblings('a.toggle').hide(); |
|
242 jQuery('.filename', item).append( jQuery('a.undo', item).removeClass('hidden') ); |
|
243 jQuery('.menu_order_input', item).hide(); |
|
244 |
|
245 return; |
|
246 } |
|
247 |
|
248 function deleteError(X, textStatus, errorThrown) { |
|
249 // TODO |
|
250 } |
|
251 |
|
252 function uploadComplete() { |
|
253 jQuery('#insert-gallery').prop('disabled', false); |
|
254 } |
|
255 |
|
256 function switchUploader(s) { |
|
257 if ( s ) { |
|
258 deleteUserSetting('uploader'); |
|
259 jQuery('.media-upload-form').removeClass('html-uploader'); |
|
260 |
|
261 if ( typeof(uploader) == 'object' ) |
|
262 uploader.refresh(); |
|
263 } else { |
|
264 setUserSetting('uploader', '1'); // 1 == html uploader |
|
265 jQuery('.media-upload-form').addClass('html-uploader'); |
|
266 } |
|
267 } |
|
268 |
|
269 function dndHelper(s) { |
|
270 var d = document.getElementById('dnd-helper'); |
|
271 |
|
272 if ( s ) { |
|
273 d.style.display = 'block'; |
|
274 } else { |
|
275 d.style.display = 'none'; |
|
276 } |
|
277 } |
|
278 |
|
279 function uploadError(fileObj, errorCode, message, uploader) { |
|
280 var hundredmb = 100 * 1024 * 1024, max; |
|
281 |
|
282 switch (errorCode) { |
|
283 case plupload.FAILED: |
|
284 wpFileError(fileObj, pluploadL10n.upload_failed); |
|
285 break; |
|
286 case plupload.FILE_EXTENSION_ERROR: |
|
287 wpFileError(fileObj, pluploadL10n.invalid_filetype); |
|
288 break; |
|
289 case plupload.FILE_SIZE_ERROR: |
|
290 uploadSizeError(uploader, fileObj); |
|
291 break; |
|
292 case plupload.IMAGE_FORMAT_ERROR: |
|
293 wpFileError(fileObj, pluploadL10n.not_an_image); |
|
294 break; |
|
295 case plupload.IMAGE_MEMORY_ERROR: |
|
296 wpFileError(fileObj, pluploadL10n.image_memory_exceeded); |
|
297 break; |
|
298 case plupload.IMAGE_DIMENSIONS_ERROR: |
|
299 wpFileError(fileObj, pluploadL10n.image_dimensions_exceeded); |
|
300 break; |
|
301 case plupload.GENERIC_ERROR: |
|
302 wpQueueError(pluploadL10n.upload_failed); |
|
303 break; |
|
304 case plupload.IO_ERROR: |
|
305 max = parseInt(uploader.settings.max_file_size, 10); |
|
306 |
|
307 if ( max > hundredmb && fileObj.size > hundredmb ) |
|
308 wpFileError(fileObj, pluploadL10n.big_upload_failed.replace('%1$s', '<a class="uploader-html" href="#">').replace('%2$s', '</a>')); |
|
309 else |
|
310 wpQueueError(pluploadL10n.io_error); |
|
311 break; |
|
312 case plupload.HTTP_ERROR: |
|
313 wpQueueError(pluploadL10n.http_error); |
|
314 break; |
|
315 case plupload.INIT_ERROR: |
|
316 jQuery('.media-upload-form').addClass('html-uploader'); |
|
317 break; |
|
318 case plupload.SECURITY_ERROR: |
|
319 wpQueueError(pluploadL10n.security_error); |
|
320 break; |
|
321 /* case plupload.UPLOAD_ERROR.UPLOAD_STOPPED: |
|
322 case plupload.UPLOAD_ERROR.FILE_CANCELLED: |
|
323 jQuery('#media-item-' + fileObj.id).remove(); |
|
324 break;*/ |
|
325 default: |
|
326 wpFileError(fileObj, pluploadL10n.default_error); |
|
327 } |
|
328 } |
|
329 |
|
330 function uploadSizeError( up, file, over100mb ) { |
|
331 var message; |
|
332 |
|
333 if ( over100mb ) |
|
334 message = pluploadL10n.big_upload_queued.replace('%s', file.name) + ' ' + pluploadL10n.big_upload_failed.replace('%1$s', '<a class="uploader-html" href="#">').replace('%2$s', '</a>'); |
|
335 else |
|
336 message = pluploadL10n.file_exceeds_size_limit.replace('%s', file.name); |
|
337 |
|
338 jQuery('#media-items').append('<div id="media-item-' + file.id + '" class="media-item error"><p>' + message + '</p></div>'); |
|
339 up.removeFile(file); |
|
340 } |
|
341 |
|
342 jQuery(document).ready(function($){ |
|
343 $('.media-upload-form').bind('click.uploader', function(e) { |
|
344 var target = $(e.target), tr, c; |
|
345 |
|
346 if ( target.is('input[type="radio"]') ) { // remember the last used image size and alignment |
|
347 tr = target.closest('tr'); |
|
348 |
|
349 if ( tr.hasClass('align') ) |
|
350 setUserSetting('align', target.val()); |
|
351 else if ( tr.hasClass('image-size') ) |
|
352 setUserSetting('imgsize', target.val()); |
|
353 |
|
354 } else if ( target.is('button.button') ) { // remember the last used image link url |
|
355 c = e.target.className || ''; |
|
356 c = c.match(/url([^ '"]+)/); |
|
357 |
|
358 if ( c && c[1] ) { |
|
359 setUserSetting('urlbutton', c[1]); |
|
360 target.siblings('.urlfield').val( target.data('link-url') ); |
|
361 } |
|
362 } else if ( target.is('a.dismiss') ) { |
|
363 target.parents('.media-item').fadeOut(200, function(){ |
|
364 $(this).remove(); |
|
365 }); |
|
366 } else if ( target.is('.upload-flash-bypass a') || target.is('a.uploader-html') ) { // switch uploader to html4 |
|
367 $('#media-items, p.submit, span.big-file-warning').css('display', 'none'); |
|
368 switchUploader(0); |
|
369 e.preventDefault(); |
|
370 } else if ( target.is('.upload-html-bypass a') ) { // switch uploader to multi-file |
|
371 $('#media-items, p.submit, span.big-file-warning').css('display', ''); |
|
372 switchUploader(1); |
|
373 e.preventDefault(); |
|
374 } else if ( target.is('a.describe-toggle-on') ) { // Show |
|
375 target.parent().addClass('open'); |
|
376 target.siblings('.slidetoggle').fadeIn(250, function(){ |
|
377 var S = $(window).scrollTop(), H = $(window).height(), top = $(this).offset().top, h = $(this).height(), b, B; |
|
378 |
|
379 if ( H && top && h ) { |
|
380 b = top + h; |
|
381 B = S + H; |
|
382 |
|
383 if ( b > B ) { |
|
384 if ( b - B < top - S ) |
|
385 window.scrollBy(0, (b - B) + 10); |
|
386 else |
|
387 window.scrollBy(0, top - S - 40); |
|
388 } |
|
389 } |
|
390 }); |
|
391 e.preventDefault(); |
|
392 } else if ( target.is('a.describe-toggle-off') ) { // Hide |
|
393 target.siblings('.slidetoggle').fadeOut(250, function(){ |
|
394 target.parent().removeClass('open'); |
|
395 }); |
|
396 e.preventDefault(); |
|
397 } |
|
398 }); |
|
399 |
|
400 // init and set the uploader |
|
401 uploader_init = function() { |
|
402 uploader = new plupload.Uploader(wpUploaderInit); |
|
403 |
|
404 $('#image_resize').bind('change', function() { |
|
405 var arg = $(this).prop('checked'); |
|
406 |
|
407 setResize( arg ); |
|
408 |
|
409 if ( arg ) |
|
410 setUserSetting('upload_resize', '1'); |
|
411 else |
|
412 deleteUserSetting('upload_resize'); |
|
413 }); |
|
414 |
|
415 uploader.bind('Init', function(up) { |
|
416 var uploaddiv = $('#plupload-upload-ui'); |
|
417 |
|
418 setResize( getUserSetting('upload_resize', false) ); |
|
419 |
|
420 if ( up.features.dragdrop && ! $(document.body).hasClass('mobile') ) { |
|
421 uploaddiv.addClass('drag-drop'); |
|
422 $('#drag-drop-area').bind('dragover.wp-uploader', function(){ // dragenter doesn't fire right :( |
|
423 uploaddiv.addClass('drag-over'); |
|
424 }).bind('dragleave.wp-uploader, drop.wp-uploader', function(){ |
|
425 uploaddiv.removeClass('drag-over'); |
|
426 }); |
|
427 } else { |
|
428 uploaddiv.removeClass('drag-drop'); |
|
429 $('#drag-drop-area').unbind('.wp-uploader'); |
|
430 } |
|
431 }); |
|
432 |
|
433 uploader.init(); |
|
434 |
|
435 uploader.bind('FilesAdded', function(up, files) { |
|
436 var hundredmb = 100 * 1024 * 1024, max = parseInt(up.settings.max_file_size, 10); |
|
437 |
|
438 $('#media-upload-error').html(''); |
|
439 uploadStart(); |
|
440 |
|
441 plupload.each(files, function(file){ |
|
442 if ( max > hundredmb && file.size > hundredmb && up.runtime != 'html5' ) |
|
443 uploadSizeError( up, file, true ); |
|
444 else |
|
445 fileQueued(file); |
|
446 }); |
|
447 |
|
448 up.refresh(); |
|
449 up.start(); |
|
450 }); |
|
451 |
|
452 uploader.bind('BeforeUpload', function(up, file) { |
|
453 // something |
|
454 }); |
|
455 |
|
456 uploader.bind('UploadFile', function(up, file) { |
|
457 fileUploading(up, file); |
|
458 }); |
|
459 |
|
460 uploader.bind('UploadProgress', function(up, file) { |
|
461 uploadProgress(up, file); |
|
462 }); |
|
463 |
|
464 uploader.bind('Error', function(up, err) { |
|
465 uploadError(err.file, err.code, err.message, up); |
|
466 up.refresh(); |
|
467 }); |
|
468 |
|
469 uploader.bind('FileUploaded', function(up, file, response) { |
|
470 uploadSuccess(file, response.response); |
|
471 }); |
|
472 |
|
473 uploader.bind('UploadComplete', function(up, files) { |
|
474 uploadComplete(); |
|
475 }); |
|
476 } |
|
477 |
|
478 if ( typeof(wpUploaderInit) == 'object' ) |
|
479 uploader_init(); |
|
480 |
|
481 }); |