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

/tags/3.7.5 RC 2/wpsc-admin/js/wpsc-swfupload-handlers.js

https://github.com/evadne/wp-e-commerce
JavaScript | 342 lines | 242 code | 55 blank | 45 comment | 73 complexity | f4d00a5fdb07f7ce48ff64694dc6db21 MD5 | raw file
  1. /**
  2. * This is a nearly exact copy of the corresponding wordpress file, we needed to copy and modify it for our use of swfupoader as the wordpress handler code is specific to posts
  3. */
  4. function wpsc_fileDialogStart() {
  5. jQuery("#media-upload-error").empty();
  6. }
  7. // progress and success handlers for media multi uploads
  8. function wpsc_fileQueued(fileObj) {
  9. // Create a progress bar containing the filename
  10. jQuery('#media-items').append('<div id="media-item-' + fileObj.id + '" ><div class="progress"><div class="bar"></div></div></div>');
  11. // Display the progress div
  12. jQuery('#media-item-' + fileObj.id + ' .progress').show();
  13. // Disable the submit button
  14. //jQuery('#insert-gallery').attr('disabled', 'disabled');
  15. }
  16. function wpsc_uploadStart(fileObj) { return true; }
  17. function wpsc_uploadProgress(fileObj, bytesDone, bytesTotal) {
  18. // Lengthen the progress bar
  19. jQuery('#media-item-' + fileObj.id + ' .bar').width(500*bytesDone/bytesTotal);
  20. if ( bytesDone == bytesTotal ) {
  21. jQuery('#media-item-' + fileObj.id + ' .bar').html('<strong class="crunching"></strong>');
  22. }
  23. }
  24. function wpsc_prepareMediaItem(fileObj, serverData) {
  25. // Move the progress bar to 100%
  26. jQuery('#media-item-' + fileObj.id + ' .bar').remove();
  27. jQuery('#media-item-' + fileObj.id + ' .progress').hide();
  28. var f = ( typeof shortform == 'undefined' ) ? 1 : 2;
  29. // Old style: Append the HTML returned by the server -- thumbnail and form inputs
  30. if ( isNaN(serverData) || !serverData ) {
  31. jQuery('#media-item-' + fileObj.id).append(serverData);
  32. prepareMediaItemInit(fileObj);
  33. }
  34. // New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
  35. else {
  36. jQuery('#media-item-' + fileObj.id).load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
  37. }
  38. }
  39. function wpsc_prepareMediaItemInit(fileObj) {
  40. // Clone the thumbnail as a "pinkynail" -- a tiny image to the left of the filename
  41. jQuery('#media-item-' + fileObj.id + ' .thumbnail').clone().attr('className', 'pinkynail toggle').prependTo('#media-item-' + fileObj.id);
  42. // Replace the original filename with the new (unique) one assigned during upload
  43. jQuery('#media-item-' + fileObj.id + ' .filename.original').replaceWith(jQuery('#media-item-' + fileObj.id + ' .filename.new'));
  44. // Also bind toggle to the links
  45. jQuery('#media-item-' + fileObj.id + ' a.toggle').bind('click', function(){jQuery(this).siblings('.slidetoggle').slideToggle(150, function(){var o=jQuery(this).offset();window.scrollTo(0,o.top-36);});jQuery(this).parent().eq(0).children('.toggle').toggle();jQuery(this).siblings('a.toggle').focus();return false;});
  46. // Bind AJAX to the new Delete button
  47. jQuery('#media-item-' + fileObj.id + ' a.delete').bind('click',function(){
  48. // Tell the server to delete it. TODO: handle exceptions
  49. jQuery.ajax({url:'admin-ajax.php',type:'post',success:deleteSuccess,error:deleteError,id:fileObj.id,data:{
  50. id : this.id.replace(/[^0-9]/g,''),
  51. action : 'delete-post',
  52. _ajax_nonce : this.href.replace(/^.*wpnonce=/,'')}
  53. });
  54. return false;
  55. });
  56. // Open this item if it says to start open (e.g. to display an error)
  57. jQuery('#media-item-' + fileObj.id + '.startopen')
  58. .removeClass('startopen')
  59. .slideToggle(500)
  60. .parent().eq(0).children('.toggle').toggle();
  61. }
  62. function wpsc_itemAjaxError(id, html) {
  63. var error = jQuery('#media-item-error' + id);
  64. error.html('<div class="file-error"><button type="button" id="dismiss-'+id+'" class="button dismiss">'+swfuploadL10n.dismiss+'</button>'+html+'</div>');
  65. jQuery('#dismiss-'+id).click(function(){jQuery(this).parents('.file-error').slideUp(200, function(){jQuery(this).empty();})});
  66. }
  67. function wpsc_deleteSuccess(data, textStatus) {
  68. if ( data == '-1' )
  69. return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
  70. if ( data == '0' )
  71. return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
  72. var item = jQuery('#media-item-' + this.id);
  73. // Decrement the counters.
  74. if ( type = jQuery('#type-of-' + this.id).val() )
  75. jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-1);
  76. if ( jQuery('.type-form #media-items>*').length == 1 && jQuery('#media-items .hidden').length > 0 ) {
  77. jQuery('.toggle').toggle();
  78. jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
  79. }
  80. // Vanish it.
  81. jQuery('#media-item-' + this.id + ' .filename:empty').remove();
  82. jQuery('#media-item-' + this.id + ' .filename').append(' <span class="file-error">'+swfuploadL10n.deleted+'</span>').siblings('a.toggle').remove();
  83. jQuery('#media-item-' + this.id).children('.describe').css({backgroundColor:'#fff'}).end()
  84. .animate({backgroundColor:'#ffc0c0'}, {queue:false,duration:50})
  85. .animate({minHeight:0,height:36}, 400, null, function(){jQuery(this).children('.describe').remove()})
  86. .animate({backgroundColor:'#fff'}, 400)
  87. .animate({height:0}, 800, null, function(){jQuery(this).remove();updateMediaForm();});
  88. return;
  89. }
  90. function wpsc_deleteError(X, textStatus, errorThrown) {
  91. // TODO
  92. }
  93. function wpsc_updateMediaForm() {
  94. storeState();
  95. // Just one file, no need for collapsible part
  96. if ( jQuery('.type-form #media-items>*').length == 1 ) {
  97. jQuery('#media-items .slidetoggle').slideDown(500).parent().eq(0).children('.toggle').toggle();
  98. jQuery('.type-form .slidetoggle').siblings().addClass('hidden');
  99. }
  100. // Only show Save buttons when there is at least one file.
  101. if ( jQuery('#media-items>*').not('.media-blank').length > 0 )
  102. jQuery('.savebutton').show();
  103. else
  104. jQuery('.savebutton').hide();
  105. // Only show Gallery button when there are at least two files.
  106. if ( jQuery('#media-items>*').length > 1 )
  107. jQuery('.insert-gallery').show();
  108. else
  109. jQuery('.insert-gallery').hide();
  110. }
  111. function wpsc_uploadSuccess(fileObj, serverData) {
  112. // if async-upload returned an error message, place it in the media item div and return
  113. if ( serverData.match('media-upload-error') ) {
  114. jQuery('#media-item-' + fileObj.id).html(serverData);
  115. return;
  116. }
  117. //console.log(fileObj);
  118. //console.log(serverData);
  119. eval(serverData);
  120. if(upload_status == 1 ) {
  121. output_html = "";
  122. output_html +="<li class='gallery_image' id='product_image_"+image_id+"'>\n";
  123. output_html += " <input type='hidden' value='"+image_id+"' name='gallery_image_id[]' class='image-id'/>\n";
  124. output_html += " <div id='gallery_image_"+image_id+"' class='previewimage'>\n";
  125. output_html += " <a class='thickbox' rel='product_extra_image_"+image_id+"' href='admin.php?wpsc_admin_action=crop_image&amp;imagename="+image_src+"&amp;imgheight=480&amp;imgwidth=600&amp;product_id=103&amp;width=640&amp;height=342' id='extra_preview_link_"+image_id+"'>\n";
  126. output_html += " <img title='Preview' alt='Preview' src='"+WPSC_IMAGE_URL+image_src+"' class='previewimage'/>\n";
  127. output_html += " </a>\n";
  128. output_html += " <img src='"+WPSC_URL+"/images/cross.png' class='deleteButton' alt='-' style='display: none;'/>\n";
  129. output_html += " </div>\n";
  130. output_html += "</li>\n";
  131. if(replace_existing == 1) {
  132. jQuery("ul#gallery_list").html(output_html);
  133. input_set = jQuery.makeArray(jQuery("#gallery_list li:not(.ui-sortable-helper) input.image-id"));
  134. set = new Array();
  135. for( var i in input_set) {
  136. set[i] = jQuery(input_set[i]).val();
  137. }
  138. //console.log(set);
  139. img_id = jQuery('#gallery_image_'+set[0]).parent('li').attr('id');
  140. jQuery('#gallery_image_'+set[0]).children('img.deleteButton').remove();
  141. jQuery('#gallery_image_'+set[0]).append("<a class='editButton'>Edit <img src='"+WPSC_URL+"/images/pencil.png' alt ='' /></a>");
  142. // jQuery('#gallery_image_'+set[0]).parent('li').attr('id', 0);
  143. for(i=1;i<set.length;i++) {
  144. jQuery('#gallery_image_'+set[i]).children('a.editButton').remove();
  145. jQuery('#gallery_image_'+set[i]).append("<img alt='-' class='deleteButton' src='"+WPSC_URL+"/images/cross.png'/>");
  146. element_id = jQuery('#gallery_image_'+set[i]).parent('li').attr('id');
  147. if(element_id == 0) {
  148. // jQuery('#gallery_image_'+set[i]).parent('li').attr('id', img_id);
  149. }
  150. }
  151. order = set.join(',');
  152. product_id = jQuery('#product_id').val();
  153. postVars = "product_id="+product_id+"&order="+order;
  154. jQuery.post( 'index.php?wpsc_admin_action=rearrange_images', postVars, function(returned_data) {
  155. eval(returned_data);
  156. jQuery('#gallery_image_'+image_id).children('a.editButton').remove();
  157. jQuery('#gallery_image_'+image_id).children('div.image_settings_box').remove();
  158. jQuery('#gallery_image_'+image_id).append(image_menu);
  159. });
  160. } else {
  161. jQuery("ul#gallery_list").append(output_html);
  162. ///jQuery("#gallery_list").trigger( 'update' );
  163. }
  164. }
  165. //jQuery('#media-item-' + fileObj.id + ' .progress').show();
  166. //window.setInterval(function() {
  167. jQuery("#media-item-" + fileObj.id + "").fadeOut("normal");
  168. //}, 5000);
  169. //prepareMediaItem(fileObj, serverData);
  170. //updateMediaForm();
  171. }
  172. function wpsc_uploadComplete(fileObj) {
  173. // If no more uploads queued, enable the submit button
  174. if ( swfu.getStats().files_queued == 0 )
  175. jQuery('#insert-gallery').attr('disabled', '');
  176. }
  177. // wp-specific error handlers
  178. // generic message
  179. function wpsc_wpQueueError(message) {
  180. jQuery('#media-upload-error').show().text(message);
  181. }
  182. // file-specific message
  183. function wpsc_wpFileError(fileObj, message) {
  184. 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();
  185. jQuery('#dismiss-' + fileObj.id).click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})});
  186. }
  187. function wpsc_fileQueueError(fileObj, error_code, message) {
  188. // Handle this error separately because we don't want to create a FileProgress element for it.
  189. if ( error_code == SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED ) {
  190. wpQueueError(swfuploadL10n.queue_limit_exceeded);
  191. }
  192. else if ( error_code == SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT ) {
  193. fileQueued(fileObj);
  194. wpFileError(fileObj, swfuploadL10n.file_exceeds_size_limit);
  195. }
  196. else if ( error_code == SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE ) {
  197. fileQueued(fileObj);
  198. wpFileError(fileObj, swfuploadL10n.zero_byte_file);
  199. }
  200. else if ( error_code == SWFUpload.QUEUE_ERROR.INVALID_FILETYPE ) {
  201. fileQueued(fileObj);
  202. wpFileError(fileObj, swfuploadL10n.invalid_filetype);
  203. }
  204. else {
  205. wpQueueError(swfuploadL10n.default_error);
  206. }
  207. }
  208. function wpsc_fileDialogComplete(num_files_queued) {
  209. try {
  210. if (num_files_queued > 0) {
  211. this.startUpload();
  212. }
  213. } catch (ex) {
  214. this.debug(ex);
  215. }
  216. }
  217. function wpsc_swfuploadPreLoad() {
  218. var swfupload_element = jQuery('#'+swfu.customSettings.swfupload_element_id).get(0);
  219. jQuery('#' + swfu.customSettings.degraded_element_id).hide();
  220. // Doing this directly because jQuery().show() seems to have timing problems
  221. if ( swfupload_element && ! swfupload_element.style.display )
  222. swfupload_element.style.display = 'block';
  223. }
  224. function wpsc_swfuploadLoadFailed() {
  225. jQuery('#' + swfu.customSettings.swfupload_element_id).hide();
  226. jQuery('#' + swfu.customSettings.degraded_element_id).show();
  227. }
  228. function wpsc_uploadError(fileObj, error_code, message) {
  229. // first the file specific error
  230. if ( error_code == SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL ) {
  231. wpFileError(fileObj, swfuploadL10n.missing_upload_url);
  232. }
  233. else if ( error_code == SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED ) {
  234. wpFileError(fileObj, swfuploadL10n.upload_limit_exceeded);
  235. }
  236. else {
  237. wpFileError(fileObj, swfuploadL10n.default_error);
  238. }
  239. // now the general upload status
  240. if ( error_code == SWFUpload.UPLOAD_ERROR.HTTP_ERROR ) {
  241. wpQueueError(swfuploadL10n.http_error);
  242. }
  243. else if ( error_code == SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED ) {
  244. wpQueueError(swfuploadL10n.upload_failed);
  245. }
  246. else if ( error_code == SWFUpload.UPLOAD_ERROR.IO_ERROR ) {
  247. wpQueueError(swfuploadL10n.io_error);
  248. }
  249. else if ( error_code == SWFUpload.UPLOAD_ERROR.SECURITY_ERROR ) {
  250. wpQueueError(swfuploadL10n.security_error);
  251. }
  252. else if ( error_code == SWFUpload.UPLOAD_ERROR.FILE_CANCELLED ) {
  253. wpQueueError(swfuploadL10n.security_error);
  254. }
  255. }
  256. // remember the last used image size, alignment and url
  257. var storeState;
  258. (function($){
  259. storeState = function(){
  260. var align = getUserSetting('align') || '', imgsize = getUserSetting('imgsize') || '';
  261. $('tr.align input[type="radio"]').click(function(){
  262. setUserSetting('align', $(this).val());
  263. }).filter(function(){
  264. if ( $(this).val() == align )
  265. return true;
  266. return false;
  267. }).attr('checked','checked');
  268. $('tr.image-size input[type="radio"]').click(function(){
  269. setUserSetting('imgsize', $(this).val());
  270. }).filter(function(){
  271. if ( $(this).attr('disabled') || $(this).val() != imgsize )
  272. return false;
  273. return true;
  274. }).attr('checked','checked');
  275. $('tr.url button').click(function(){
  276. var c = this.className || '';
  277. c = c.replace(/.*?(url[^ '"]+).*/, '$1');
  278. if (c) setUserSetting('urlbutton', c);
  279. $(this).siblings('.urlfield').val( $(this).attr('title') );
  280. });
  281. $('tr.url .urlfield').each(function(){
  282. var b = getUserSetting('urlbutton');
  283. $(this).val( $(this).siblings('button.'+b).attr('title') );
  284. });
  285. }
  286. })(jQuery);