wp/wp-includes/js/plupload/handlers.js
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
       
     1 /* global plupload, pluploadL10n, ajaxurl, post_id, wpUploaderInit, deleteUserSetting, setUserSetting, getUserSetting, shortform */
     1 var topWin = window.dialogArguments || opener || parent || top, uploader, uploader_init;
     2 var topWin = window.dialogArguments || opener || parent || top, uploader, uploader_init;
     2 
       
     3 function fileDialogStart() {
       
     4 	jQuery("#media-upload-error").empty();
       
     5 }
       
     6 
     3 
     7 // progress and success handlers for media multi uploads
     4 // progress and success handlers for media multi uploads
     8 function fileQueued(fileObj) {
     5 function fileQueued(fileObj) {
     9 	// Get rid of unused form
     6 	// Get rid of unused form
    10 	jQuery('.media-blank').remove();
     7 	jQuery('.media-blank').remove();
    42 	jQuery('.bar', item).width( (200 * file.loaded) / file.size );
    39 	jQuery('.bar', item).width( (200 * file.loaded) / file.size );
    43 	jQuery('.percent', item).html( file.percent + '%' );
    40 	jQuery('.percent', item).html( file.percent + '%' );
    44 }
    41 }
    45 
    42 
    46 // check to see if a large file failed to upload
    43 // check to see if a large file failed to upload
    47 function fileUploading(up, file) {
    44 function fileUploading( up, file ) {
    48 	var hundredmb = 100 * 1024 * 1024, max = parseInt(up.settings.max_file_size, 10);
    45 	var hundredmb = 100 * 1024 * 1024,
       
    46 		max = parseInt( up.settings.max_file_size, 10 );
    49 
    47 
    50 	if ( max > hundredmb && file.size > hundredmb ) {
    48 	if ( max > hundredmb && file.size > hundredmb ) {
    51 		setTimeout(function(){
    49 		setTimeout( function() {
    52 			var done;
    50 			if ( file.status < 3 && file.loaded === 0 ) { // not uploading
    53 
    51 				wpFileError( file, pluploadL10n.big_upload_failed.replace( '%1$s', '<a class="uploader-html" href="#">' ).replace( '%2$s', '</a>' ) );
    54 			if ( file.status < 3 && file.loaded == 0 ) { // not uploading
       
    55 				wpFileError(file, pluploadL10n.big_upload_failed.replace('%1$s', '<a class="uploader-html" href="#">').replace('%2$s', '</a>'));
       
    56 				up.stop(); // stops the whole queue
    52 				up.stop(); // stops the whole queue
    57 				up.removeFile(file);
    53 				up.removeFile( file );
    58 				up.start(); // restart the queue
    54 				up.start(); // restart the queue
    59 			}
    55 			}
    60 		}, 10000); // wait for 10 sec. for the file to start uploading
    56 		}, 10000 ); // wait for 10 sec. for the file to start uploading
    61 	}
    57 	}
    62 }
    58 }
    63 
    59 
    64 function updateMediaForm() {
    60 function updateMediaForm() {
    65 	var items = jQuery('#media-items').children();
    61 	var items = jQuery('#media-items').children();
    68 	if ( items.length == 1 ) {
    64 	if ( items.length == 1 ) {
    69 		items.addClass('open').find('.slidetoggle').show();
    65 		items.addClass('open').find('.slidetoggle').show();
    70 		jQuery('.insert-gallery').hide();
    66 		jQuery('.insert-gallery').hide();
    71 	} else if ( items.length > 1 ) {
    67 	} else if ( items.length > 1 ) {
    72 		items.removeClass('open');
    68 		items.removeClass('open');
    73 		// Only show Gallery button when there are at least two files.
    69 		// Only show Gallery/Playlist buttons when there are at least two files.
    74 		jQuery('.insert-gallery').show();
    70 		jQuery('.insert-gallery').show();
    75 	}
    71 	}
    76 
    72 
    77 	// Only show Save buttons when there is at least one file.
    73 	// Only show Save buttons when there is at least one file.
    78 	if ( items.not('.media-blank').length > 0 )
    74 	if ( items.not('.media-blank').length > 0 )
   101 	// Increment the counter.
    97 	// Increment the counter.
   102 	if ( post_id && item.hasClass('child-of-' + post_id) )
    98 	if ( post_id && item.hasClass('child-of-' + post_id) )
   103 		jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);
    99 		jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);
   104 }
   100 }
   105 
   101 
   106 function setResize(arg) {
   102 function setResize( arg ) {
   107 	if ( arg ) {
   103 	if ( arg ) {
   108 		if ( uploader.features.jpgresize )
   104 		if ( window.resize_width && window.resize_height ) {
   109 			uploader.settings['resize'] = { width: resize_width, height: resize_height, quality: 100 };
   105 			uploader.settings.resize = {
   110 		else
   106 				enabled: true,
       
   107 				width: window.resize_width,
       
   108 				height: window.resize_height,
       
   109 				quality: 100
       
   110 			};
       
   111 		} else {
   111 			uploader.settings.multipart_params.image_resize = true;
   112 			uploader.settings.multipart_params.image_resize = true;
       
   113 		}
   112 	} else {
   114 	} else {
   113 		delete(uploader.settings.resize);
   115 		delete( uploader.settings.multipart_params.image_resize );
   114 		delete(uploader.settings.multipart_params.image_resize);
       
   115 	}
   116 	}
   116 }
   117 }
   117 
   118 
   118 function prepareMediaItem(fileObj, serverData) {
   119 function prepareMediaItem(fileObj, serverData) {
   119 	var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id);
   120 	var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id);
   127 
   128 
   128 	if ( isNaN(serverData) || !serverData ) { // Old style: Append the HTML returned by the server -- thumbnail and form inputs
   129 	if ( isNaN(serverData) || !serverData ) { // Old style: Append the HTML returned by the server -- thumbnail and form inputs
   129 		item.append(serverData);
   130 		item.append(serverData);
   130 		prepareMediaItemInit(fileObj);
   131 		prepareMediaItemInit(fileObj);
   131 	} else { // New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
   132 	} else { // New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
   132 		item.load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
   133 		item.load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm();});
   133 	}
   134 	}
   134 }
   135 }
   135 
   136 
   136 function prepareMediaItemInit(fileObj) {
   137 function prepareMediaItemInit(fileObj) {
   137 	var item = jQuery('#media-item-' + fileObj.id);
   138 	var item = jQuery('#media-item-' + fileObj.id);
   169 			data: {
   170 			data: {
   170 				id : this.id.replace(/[^0-9]/g,''),
   171 				id : this.id.replace(/[^0-9]/g,''),
   171 				action: 'untrash-post',
   172 				action: 'untrash-post',
   172 				_ajax_nonce: this.href.replace(/^.*wpnonce=/,'')
   173 				_ajax_nonce: this.href.replace(/^.*wpnonce=/,'')
   173 			},
   174 			},
   174 			success: function(data, textStatus){
   175 			success: function( ){
   175 				var item = jQuery('#media-item-' + fileObj.id);
   176 				var type,
       
   177 					item = jQuery('#media-item-' + fileObj.id);
   176 
   178 
   177 				if ( type = jQuery('#type-of-' + fileObj.id).val() )
   179 				if ( type = jQuery('#type-of-' + fileObj.id).val() )
   178 					jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-0+1);
   180 					jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-0+1);
   179 
   181 
   180 				if ( post_id && item.hasClass('child-of-'+post_id) )
   182 				if ( post_id && item.hasClass('child-of-'+post_id) )
   208 	var item = jQuery('#media-item-' + id), filename = item.find('.filename').text(), last_err = item.data('last-err');
   210 	var item = jQuery('#media-item-' + id), filename = item.find('.filename').text(), last_err = item.data('last-err');
   209 
   211 
   210 	if ( last_err == id ) // prevent firing an error for the same file twice
   212 	if ( last_err == id ) // prevent firing an error for the same file twice
   211 		return;
   213 		return;
   212 
   214 
   213 	item.html('<div class="error-div">'
   215 	item.html('<div class="error-div">' +
   214 				+ '<a class="dismiss" href="#">' + pluploadL10n.dismiss + '</a>'
   216 				'<a class="dismiss" href="#">' + pluploadL10n.dismiss + '</a>' +
   215 				+ '<strong>' + pluploadL10n.error_uploading.replace('%s', jQuery.trim(filename)) + '</strong> '
   217 				'<strong>' + pluploadL10n.error_uploading.replace('%s', jQuery.trim(filename)) + '</strong> ' +
   216 				+ message
   218 				message +
   217 				+ '</div>').data('last-err', id);
   219 				'</div>').data('last-err', id);
   218 }
   220 }
   219 
   221 
   220 function deleteSuccess(data, textStatus) {
   222 function deleteSuccess(data) {
       
   223 	var type, id, item;
   221 	if ( data == '-1' )
   224 	if ( data == '-1' )
   222 		return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
   225 		return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
   223 
   226 
   224 	if ( data == '0' )
   227 	if ( data == '0' )
   225 		return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
   228 		return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
   226 
   229 
   227 	var id = this.id, item = jQuery('#media-item-' + id);
   230 	id = this.id;
       
   231 	item = jQuery('#media-item-' + id);
   228 
   232 
   229 	// Decrement the counters.
   233 	// Decrement the counters.
   230 	if ( type = jQuery('#type-of-' + id).val() )
   234 	if ( type = jQuery('#type-of-' + id).val() )
   231 		jQuery('#' + type + '-counter').text( jQuery('#' + type + '-counter').text() - 1 );
   235 		jQuery('#' + type + '-counter').text( jQuery('#' + type + '-counter').text() - 1 );
   232 
   236 
   250 	jQuery('.menu_order_input', item).hide();
   254 	jQuery('.menu_order_input', item).hide();
   251 
   255 
   252 	return;
   256 	return;
   253 }
   257 }
   254 
   258 
   255 function deleteError(X, textStatus, errorThrown) {
   259 function deleteError() {
   256 	// TODO
   260 	// TODO
   257 }
   261 }
   258 
   262 
   259 function uploadComplete() {
   263 function uploadComplete() {
   260 	jQuery('#insert-gallery').prop('disabled', false);
   264 	jQuery('#insert-gallery').prop('disabled', false);
   271 		setUserSetting('uploader', '1'); // 1 == html uploader
   275 		setUserSetting('uploader', '1'); // 1 == html uploader
   272 		jQuery('.media-upload-form').addClass('html-uploader');
   276 		jQuery('.media-upload-form').addClass('html-uploader');
   273 	}
   277 	}
   274 }
   278 }
   275 
   279 
   276 function dndHelper(s) {
       
   277 	var d = document.getElementById('dnd-helper');
       
   278 
       
   279 	if ( s ) {
       
   280 		d.style.display = 'block';
       
   281 	} else {
       
   282 		d.style.display = 'none';
       
   283 	}
       
   284 }
       
   285 
       
   286 function uploadError(fileObj, errorCode, message, uploader) {
   280 function uploadError(fileObj, errorCode, message, uploader) {
   287 	var hundredmb = 100 * 1024 * 1024, max;
   281 	var hundredmb = 100 * 1024 * 1024, max;
   288 
   282 
   289 	switch (errorCode) {
   283 	switch (errorCode) {
   290 		case plupload.FAILED:
   284 		case plupload.FAILED:
   307 			break;
   301 			break;
   308 		case plupload.GENERIC_ERROR:
   302 		case plupload.GENERIC_ERROR:
   309 			wpQueueError(pluploadL10n.upload_failed);
   303 			wpQueueError(pluploadL10n.upload_failed);
   310 			break;
   304 			break;
   311 		case plupload.IO_ERROR:
   305 		case plupload.IO_ERROR:
   312 			max = parseInt(uploader.settings.max_file_size, 10);
   306 			max = parseInt( uploader.settings.filters.max_file_size, 10 );
   313 
   307 
   314 			if ( max > hundredmb && fileObj.size > hundredmb )
   308 			if ( max > hundredmb && fileObj.size > hundredmb )
   315 				wpFileError(fileObj, pluploadL10n.big_upload_failed.replace('%1$s', '<a class="uploader-html" href="#">').replace('%2$s', '</a>'));
   309 				wpFileError( fileObj, pluploadL10n.big_upload_failed.replace('%1$s', '<a class="uploader-html" href="#">').replace('%2$s', '</a>') );
   316 			else
   310 			else
   317 				wpQueueError(pluploadL10n.io_error);
   311 				wpQueueError(pluploadL10n.io_error);
   318 			break;
   312 			break;
   319 		case plupload.HTTP_ERROR:
   313 		case plupload.HTTP_ERROR:
   320 			wpQueueError(pluploadL10n.http_error);
   314 			wpQueueError(pluploadL10n.http_error);
   404 		}
   398 		}
   405 	});
   399 	});
   406 
   400 
   407 	// init and set the uploader
   401 	// init and set the uploader
   408 	uploader_init = function() {
   402 	uploader_init = function() {
       
   403 		var isIE = navigator.userAgent.indexOf('Trident/') != -1 || navigator.userAgent.indexOf('MSIE ') != -1;
       
   404 
       
   405 		// Make sure flash sends cookies (seems in IE it does whitout switching to urlstream mode)
       
   406 		if ( ! isIE && 'flash' === plupload.predictRuntime( wpUploaderInit ) &&
       
   407 			( ! wpUploaderInit.required_features || ! wpUploaderInit.required_features.hasOwnProperty( 'send_binary_string' ) ) ) {
       
   408 
       
   409 			wpUploaderInit.required_features = wpUploaderInit.required_features || {};
       
   410 			wpUploaderInit.required_features.send_binary_string = true;
       
   411 		}
       
   412 
   409 		uploader = new plupload.Uploader(wpUploaderInit);
   413 		uploader = new plupload.Uploader(wpUploaderInit);
   410 
   414 
   411 		$('#image_resize').bind('change', function() {
   415 		$('#image_resize').bind('change', function() {
   412 			var arg = $(this).prop('checked');
   416 			var arg = $(this).prop('checked');
   413 
   417 
   434 			} else {
   438 			} else {
   435 				uploaddiv.removeClass('drag-drop');
   439 				uploaddiv.removeClass('drag-drop');
   436 				$('#drag-drop-area').unbind('.wp-uploader');
   440 				$('#drag-drop-area').unbind('.wp-uploader');
   437 			}
   441 			}
   438 
   442 
   439 			if ( up.runtime == 'html4' )
   443 			if ( up.runtime === 'html4' ) {
   440 				$('.upload-flash-bypass').hide();
   444 				$('.upload-flash-bypass').hide();
       
   445 			}
   441 		});
   446 		});
   442 
   447 
   443 		uploader.init();
   448 		uploader.init();
   444 
   449 
   445 		uploader.bind('FilesAdded', function(up, files) {
   450 		uploader.bind('FilesAdded', function( up, files ) {
   446 			var hundredmb = 100 * 1024 * 1024, max = parseInt(up.settings.max_file_size, 10);
   451 			$('#media-upload-error').empty();
   447 
       
   448 			$('#media-upload-error').html('');
       
   449 			uploadStart();
   452 			uploadStart();
   450 
   453 
   451 			plupload.each(files, function(file){
   454 			plupload.each( files, function( file ) {
   452 				if ( max > hundredmb && file.size > hundredmb && up.runtime != 'html5' )
   455 				fileQueued( file );
   453 					uploadSizeError( up, file, true );
       
   454 				else
       
   455 					fileQueued(file);
       
   456 			});
   456 			});
   457 
   457 
   458 			up.refresh();
   458 			up.refresh();
   459 			up.start();
   459 			up.start();
   460 		});
       
   461 
       
   462 		uploader.bind('BeforeUpload', function(up, file) {
       
   463 			// something
       
   464 		});
   460 		});
   465 
   461 
   466 		uploader.bind('UploadFile', function(up, file) {
   462 		uploader.bind('UploadFile', function(up, file) {
   467 			fileUploading(up, file);
   463 			fileUploading(up, file);
   468 		});
   464 		});
   478 
   474 
   479 		uploader.bind('FileUploaded', function(up, file, response) {
   475 		uploader.bind('FileUploaded', function(up, file, response) {
   480 			uploadSuccess(file, response.response);
   476 			uploadSuccess(file, response.response);
   481 		});
   477 		});
   482 
   478 
   483 		uploader.bind('UploadComplete', function(up, files) {
   479 		uploader.bind('UploadComplete', function() {
   484 			uploadComplete();
   480 			uploadComplete();
   485 		});
   481 		});
   486 	}
   482 	};
   487 
   483 
   488 	if ( typeof(wpUploaderInit) == 'object' )
   484 	if ( typeof(wpUploaderInit) == 'object' ) {
   489 		uploader_init();
   485 		uploader_init();
       
   486 	}
   490 
   487 
   491 });
   488 });