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