PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

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