PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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