PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/core/js/handlers.dev.js

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