PageRenderTime 69ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/js/plupload/handlers.dev.js

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