]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/swfupload/handlers.js
WordPress 4.5.1-scripts
[autoinstalls/wordpress.git] / wp-includes / js / swfupload / handlers.js
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 buttons 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
223 function uploadSuccess(fileObj, serverData) {
224         // if async-upload returned an error message, place it in the media item div and return
225         if ( serverData.match('media-upload-error') ) {
226                 jQuery('#media-item-' + fileObj.id).html(serverData);
227                 return;
228         }
229
230         prepareMediaItem(fileObj, serverData);
231         updateMediaForm();
232
233         // Increment the counter.
234         if ( jQuery('#media-item-' + fileObj.id).hasClass('child-of-' + post_id) )
235                 jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);
236 }
237
238 function uploadComplete(fileObj) {
239         // If no more uploads queued, enable the submit button
240         if ( swfu.getStats().files_queued == 0 ) {
241                 jQuery('#cancel-upload').prop('disabled', true);
242                 jQuery('#insert-gallery').prop('disabled', false);
243         }
244 }
245
246
247 // wp-specific error handlers
248
249 // generic message
250 function wpQueueError(message) {
251         jQuery('#media-upload-error').show().text(message);
252 }
253
254 // file-specific message
255 function wpFileError(fileObj, message) {
256         var item = jQuery('#media-item-' + fileObj.id);
257         var filename = jQuery('.filename', item).text();
258
259         item.html('<div class="error-div">'
260                                 + '<a class="dismiss" href="#">' + swfuploadL10n.dismiss + '</a>'
261                                 + '<strong>' + swfuploadL10n.error_uploading.replace('%s', filename) + '</strong><br />'
262                                 + message
263                                 + '</div>');
264         item.find('a.dismiss').click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})});
265 }
266
267 function fileQueueError(fileObj, error_code, message)  {
268         // Handle this error separately because we don't want to create a FileProgress element for it.
269         if ( error_code == SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED ) {
270                 wpQueueError(swfuploadL10n.queue_limit_exceeded);
271         }
272         else if ( error_code == SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT ) {
273                 fileQueued(fileObj);
274                 wpFileError(fileObj, swfuploadL10n.file_exceeds_size_limit);
275         }
276         else if ( error_code == SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE ) {
277                 fileQueued(fileObj);
278                 wpFileError(fileObj, swfuploadL10n.zero_byte_file);
279         }
280         else if ( error_code == SWFUpload.QUEUE_ERROR.INVALID_FILETYPE ) {
281                 fileQueued(fileObj);
282                 wpFileError(fileObj, swfuploadL10n.invalid_filetype);
283         }
284         else {
285                 wpQueueError(swfuploadL10n.default_error);
286         }
287 }
288
289 function fileDialogComplete(num_files_queued) {
290         try {
291                 if (num_files_queued > 0) {
292                         this.startUpload();
293                 }
294         } catch (ex) {
295                 this.debug(ex);
296         }
297 }
298
299 function switchUploader(s) {
300         var f = document.getElementById(swfu.customSettings.swfupload_element_id), h = document.getElementById(swfu.customSettings.degraded_element_id);
301         if ( s ) {
302                 f.style.display = 'block';
303                 h.style.display = 'none';
304         } else {
305                 f.style.display = 'none';
306                 h.style.display = 'block';
307         }
308 }
309
310 function swfuploadPreLoad() {
311         if ( !uploaderMode ) {
312                 switchUploader(1);
313         } else {
314                 switchUploader(0);
315         }
316 }
317
318 function swfuploadLoadFailed() {
319         switchUploader(0);
320         jQuery('.upload-html-bypass').hide();
321 }
322
323 function uploadError(fileObj, errorCode, message) {
324
325         switch (errorCode) {
326                 case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
327                         wpFileError(fileObj, swfuploadL10n.missing_upload_url);
328                         break;
329                 case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
330                         wpFileError(fileObj, swfuploadL10n.upload_limit_exceeded);
331                         break;
332                 case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
333                         wpQueueError(swfuploadL10n.http_error);
334                         break;
335                 case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
336                         wpQueueError(swfuploadL10n.upload_failed);
337                         break;
338                 case SWFUpload.UPLOAD_ERROR.IO_ERROR:
339                         wpQueueError(swfuploadL10n.io_error);
340                         break;
341                 case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
342                         wpQueueError(swfuploadL10n.security_error);
343                         break;
344                 case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
345                 case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
346                         jQuery('#media-item-' + fileObj.id).remove();
347                         break;
348                 default:
349                         wpFileError(fileObj, swfuploadL10n.default_error);
350         }
351 }
352
353 function cancelUpload() {
354         swfu.cancelQueue();
355 }
356
357 // remember the last used image size, alignment and url
358 jQuery(document).ready(function($){
359         $('input[type="radio"]', '#media-items').live('click', function(){
360                 var tr = $(this).closest('tr');
361
362                 if ( $(tr).hasClass('align') )
363                         setUserSetting('align', $(this).val());
364                 else if ( $(tr).hasClass('image-size') )
365                         setUserSetting('imgsize', $(this).val());
366         });
367
368         $('button.button', '#media-items').live('click', function(){
369                 var c = this.className || '';
370                 c = c.match(/url([^ '"]+)/);
371                 if ( c && c[1] ) {
372                         setUserSetting('urlbutton', c[1]);
373                         $(this).siblings('.urlfield').val( $(this).attr('title') );
374                 }
375         });
376 });