PageRenderTime 24ms CodeModel.GetById 6ms app.highlight 14ms RepoModel.GetById 0ms app.codeStats 1ms

/Web/wp-includes/js/swfupload/handlers.dev.js

https://bitbucket.org/jimjenkins5/blog
JavaScript | 370 lines | 284 code | 54 blank | 32 comment | 64 complexity | 793ff9921f096583ba52f8606ec79f4c MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-2.1
  1var topWin = window.dialogArguments || opener || parent || top;
  2
  3function fileDialogStart() {
  4	jQuery("#media-upload-error").empty();
  5}
  6
  7// progress and success handlers for media multi uploads
  8function 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('#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>');
 19	// Display the progress div
 20	jQuery('.progress', '#media-item-' + fileObj.id).show();
 21
 22	// Disable submit and enable cancel
 23	jQuery('#insert-gallery').prop('disabled', true);
 24	jQuery('#cancel-upload').prop('disabled', false);
 25}
 26
 27function uploadStart(fileObj) {
 28	try {
 29		if ( typeof topWin.tb_remove != 'undefined' )
 30			topWin.jQuery('#TB_overlay').unbind('click', topWin.tb_remove); 
 31	} catch(e){}
 32
 33	return true;
 34}
 35
 36function uploadProgress(fileObj, bytesDone, bytesTotal) {
 37	// Lengthen the progress bar
 38	var w = jQuery('#media-items').width() - 2, item = jQuery('#media-item-' + fileObj.id);
 39	jQuery('.bar', item).width( w * bytesDone / bytesTotal );
 40	jQuery('.percent', item).html( Math.ceil(bytesDone / bytesTotal * 100) + '%' );
 41
 42	if ( bytesDone == bytesTotal )
 43		jQuery('.bar', item).html('<strong class="crunching">' + swfuploadL10n.crunching + '</strong>');
 44}
 45
 46function prepareMediaItem(fileObj, serverData) {
 47	var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id);
 48	// Move the progress bar to 100%
 49	jQuery('.bar', item).remove();
 50	jQuery('.progress', item).hide();
 51
 52	try {
 53		if ( typeof topWin.tb_remove != 'undefined' )
 54			topWin.jQuery('#TB_overlay').click(topWin.tb_remove);
 55	} catch(e){}
 56
 57	// Old style: Append the HTML returned by the server -- thumbnail and form inputs
 58	if ( isNaN(serverData) || !serverData ) {
 59		item.append(serverData);
 60		prepareMediaItemInit(fileObj);
 61	}
 62	// New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
 63	else {
 64		item.load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
 65	}
 66}
 67
 68function prepareMediaItemInit(fileObj) {
 69	var item = jQuery('#media-item-' + fileObj.id);
 70	// Clone the thumbnail as a "pinkynail" -- a tiny image to the left of the filename
 71	jQuery('.thumbnail', item).clone().attr('class', 'pinkynail toggle').prependTo(item);
 72
 73	// Replace the original filename with the new (unique) one assigned during upload
 74	jQuery('.filename.original', item).replaceWith( jQuery('.filename.new', item) );
 75
 76	// Also bind toggle to the links
 77	jQuery('a.toggle', item).click(function(){
 78		jQuery(this).siblings('.slidetoggle').slideToggle(350, function(){
 79			var w = jQuery(window).height(), t = jQuery(this).offset().top, h = jQuery(this).height(), b;
 80
 81			if ( w && t && h ) {
 82                b = t + h;
 83
 84                if ( b > w && (h + 48) < w )
 85                    window.scrollBy(0, b - w + 13);
 86                else if ( b > w )
 87                    window.scrollTo(0, t - 36);
 88            }
 89		});
 90		jQuery(this).siblings('.toggle').andSelf().toggle();
 91		jQuery(this).siblings('a.toggle').focus();
 92		return false;
 93	});
 94
 95	// Bind AJAX to the new Delete button
 96	jQuery('a.delete', item).click(function(){
 97		// Tell the server to delete it. TODO: handle exceptions
 98		jQuery.ajax({
 99			url: ajaxurl,
100			type: 'post',
101			success: deleteSuccess,
102			error: deleteError,
103			id: fileObj.id,
104			data: {
105				id : this.id.replace(/[^0-9]/g, ''),
106				action : 'trash-post',
107				_ajax_nonce : this.href.replace(/^.*wpnonce=/,'')
108			}
109		});
110		return false;
111	});
112
113	// Bind AJAX to the new Undo button
114	jQuery('a.undo', item).click(function(){
115		// Tell the server to untrash it. TODO: handle exceptions
116		jQuery.ajax({
117			url: ajaxurl,
118			type: 'post',
119			id: fileObj.id,
120			data: {
121				id : this.id.replace(/[^0-9]/g,''),
122				action: 'untrash-post',
123				_ajax_nonce: this.href.replace(/^.*wpnonce=/,'')
124			},
125			success: function(data, textStatus){
126				var item = jQuery('#media-item-' + fileObj.id);
127
128				if ( type = jQuery('#type-of-' + fileObj.id).val() )
129					jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-0+1);
130				if ( item.hasClass('child-of-'+post_id) )
131					jQuery('#attachments-count').text(jQuery('#attachments-count').text()-0+1);
132
133				jQuery('.filename .trashnotice', item).remove();
134				jQuery('.filename .title', item).css('font-weight','normal');
135				jQuery('a.undo', item).addClass('hidden');
136				jQuery('a.describe-toggle-on, .menu_order_input', item).show();
137				item.css( {backgroundColor:'#ceb'} ).animate( {backgroundColor: '#fff'}, { queue: false, duration: 500, complete: function(){ jQuery(this).css({backgroundColor:''}); } }).removeClass('undo');
138			}
139		});
140		return false;
141	});
142
143	// Open this item if it says to start open (e.g. to display an error)
144	jQuery('#media-item-' + fileObj.id + '.startopen').removeClass('startopen').slideToggle(500).siblings('.toggle').toggle();
145}
146
147function itemAjaxError(id, html) {
148	var item = jQuery('#media-item-' + id);
149	var filename = jQuery('.filename', item).text();
150
151	item.html('<div class="error-div">'
152				+ '<a class="dismiss" href="#">' + swfuploadL10n.dismiss + '</a>'
153				+ '<strong>' + swfuploadL10n.error_uploading.replace('%s', filename) + '</strong><br />'
154				+ html
155				+ '</div>');
156	item.find('a.dismiss').click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})});
157}
158
159function deleteSuccess(data, textStatus) {
160	if ( data == '-1' )
161		return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
162	if ( data == '0' )
163		return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
164
165	var id = this.id, item = jQuery('#media-item-' + id);
166
167	// Decrement the counters.
168	if ( type = jQuery('#type-of-' + id).val() )
169		jQuery('#' + type + '-counter').text( jQuery('#' + type + '-counter').text() - 1 );
170	if ( item.hasClass('child-of-'+post_id) )
171		jQuery('#attachments-count').text( jQuery('#attachments-count').text() - 1 );
172
173	if ( jQuery('form.type-form #media-items').children().length == 1 && jQuery('.hidden', '#media-items').length > 0 ) {
174		jQuery('.toggle').toggle();
175		jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
176	}
177
178	// Vanish it.
179	jQuery('.toggle', item).toggle();
180	jQuery('.slidetoggle', item).slideUp(200).siblings().removeClass('hidden');
181	item.css( {backgroundColor:'#faa'} ).animate( {backgroundColor:'#f4f4f4'}, {queue:false, duration:500} ).addClass('undo');
182
183	jQuery('.filename:empty', item).remove();
184	jQuery('.filename .title', item).css('font-weight','bold');
185	jQuery('.filename', item).append('<span class="trashnotice"> ' + swfuploadL10n.deleted + ' </span>').siblings('a.toggle').hide();
186	jQuery('.filename', item).append( jQuery('a.undo', item).removeClass('hidden') );
187	jQuery('.menu_order_input', item).hide();
188
189	return;
190}
191
192function deleteError(X, textStatus, errorThrown) {
193	// TODO
194}
195
196function updateMediaForm() {
197	var one = jQuery('form.type-form #media-items').children(), items = jQuery('#media-items').children();
198
199	// Just one file, no need for collapsible part
200	if ( one.length == 1 ) {
201		jQuery('.slidetoggle', one).slideDown(500).siblings().addClass('hidden').filter('.toggle').toggle();
202	}
203
204	// Only show Save buttons when there is at least one file.
205	if ( items.not('.media-blank').length > 0 )
206		jQuery('.savebutton').show();
207	else
208		jQuery('.savebutton').hide();
209
210	// Only show Gallery button when there are at least two files.
211	if ( items.length > 1 )
212		jQuery('.insert-gallery').show();
213	else
214		jQuery('.insert-gallery').hide();
215}
216
217function uploadSuccess(fileObj, serverData) {
218	// if async-upload returned an error message, place it in the media item div and return
219	if ( serverData.match('media-upload-error') ) {
220		jQuery('#media-item-' + fileObj.id).html(serverData);
221		return;
222	}
223
224	prepareMediaItem(fileObj, serverData);
225	updateMediaForm();
226
227	// Increment the counter.
228	if ( jQuery('#media-item-' + fileObj.id).hasClass('child-of-' + post_id) )
229		jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);
230}
231
232function uploadComplete(fileObj) {
233	// If no more uploads queued, enable the submit button
234	if ( swfu.getStats().files_queued == 0 ) {
235		jQuery('#cancel-upload').prop('disabled', true);
236		jQuery('#insert-gallery').prop('disabled', false);
237	}
238}
239
240
241// wp-specific error handlers
242
243// generic message
244function wpQueueError(message) {
245	jQuery('#media-upload-error').show().text(message);
246}
247
248// file-specific message
249function wpFileError(fileObj, message) {
250	var item = jQuery('#media-item-' + fileObj.id);
251	var filename = jQuery('.filename', item).text();
252
253	item.html('<div class="error-div">'
254				+ '<a class="dismiss" href="#">' + swfuploadL10n.dismiss + '</a>'
255				+ '<strong>' + swfuploadL10n.error_uploading.replace('%s', filename) + '</strong><br />'
256				+ message
257				+ '</div>');
258	item.find('a.dismiss').click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})});
259}
260
261function fileQueueError(fileObj, error_code, message)  {
262	// Handle this error separately because we don't want to create a FileProgress element for it.
263	if ( error_code == SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED ) {
264		wpQueueError(swfuploadL10n.queue_limit_exceeded);
265	}
266	else if ( error_code == SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT ) {
267		fileQueued(fileObj);
268		wpFileError(fileObj, swfuploadL10n.file_exceeds_size_limit);
269	}
270	else if ( error_code == SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE ) {
271		fileQueued(fileObj);
272		wpFileError(fileObj, swfuploadL10n.zero_byte_file);
273	}
274	else if ( error_code == SWFUpload.QUEUE_ERROR.INVALID_FILETYPE ) {
275		fileQueued(fileObj);
276		wpFileError(fileObj, swfuploadL10n.invalid_filetype);
277	}
278	else {
279		wpQueueError(swfuploadL10n.default_error);
280	}
281}
282
283function fileDialogComplete(num_files_queued) {
284	try {
285		if (num_files_queued > 0) {
286			this.startUpload();
287		}
288	} catch (ex) {
289		this.debug(ex);
290	}
291}
292
293function switchUploader(s) {
294	var f = document.getElementById(swfu.customSettings.swfupload_element_id), h = document.getElementById(swfu.customSettings.degraded_element_id);
295	if ( s ) {
296		f.style.display = 'block';
297		h.style.display = 'none';
298	} else {
299		f.style.display = 'none';
300		h.style.display = 'block';
301	}
302}
303
304function swfuploadPreLoad() {
305	if ( !uploaderMode ) {
306		switchUploader(1);
307	} else {
308		switchUploader(0);
309	}
310}
311
312function swfuploadLoadFailed() {
313	switchUploader(0);
314	jQuery('.upload-html-bypass').hide();
315}
316
317function uploadError(fileObj, errorCode, message) {
318
319	switch (errorCode) {
320		case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
321			wpFileError(fileObj, swfuploadL10n.missing_upload_url);
322			break;
323		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
324			wpFileError(fileObj, swfuploadL10n.upload_limit_exceeded);
325			break;
326		case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
327			wpQueueError(swfuploadL10n.http_error);
328			break;
329		case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
330			wpQueueError(swfuploadL10n.upload_failed);
331			break;
332		case SWFUpload.UPLOAD_ERROR.IO_ERROR:
333			wpQueueError(swfuploadL10n.io_error);
334			break;
335		case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
336			wpQueueError(swfuploadL10n.security_error);
337			break;
338		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
339		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
340			jQuery('#media-item-' + fileObj.id).remove();
341			break;
342		default:
343			wpFileError(fileObj, swfuploadL10n.default_error);
344	}
345}
346
347function cancelUpload() {
348	swfu.cancelQueue();
349}
350
351// remember the last used image size, alignment and url
352jQuery(document).ready(function($){
353	$('input[type="radio"]', '#media-items').live('click', function(){
354		var tr = $(this).closest('tr');
355
356		if ( $(tr).hasClass('align') )
357			setUserSetting('align', $(this).val());
358		else if ( $(tr).hasClass('image-size') )
359			setUserSetting('imgsize', $(this).val());
360	});
361
362	$('button.button', '#media-items').live('click', function(){
363		var c = this.className || '';
364		c = c.match(/url([^ '"]+)/);
365		if ( c && c[1] ) {
366			setUserSetting('urlbutton', c[1]);
367			$(this).siblings('.urlfield').val( $(this).attr('title') );
368		}
369	});
370});