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

/wp-content/plugins/wp-e-commerce/wpsc-admin/js/wpsc-swfupload-handlers.js

https://gitlab.com/endomorphosis/reservationtelco
JavaScript | 348 lines | 246 code | 55 blank | 47 comment | 74 complexity | 42446a957c7fd3eff6b411ba47c96e4e 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. image_count = jQuery("ul#gallery_list li.gallery_image div a img.previewimage").size();
  132. if(image_count < 1) {
  133. replace_existing = 1;
  134. }
  135. //console.log(jQuery("ul#gallery_list li.gallery_image div a img.previewimage"));
  136. //console.log(image_count);
  137. if(replace_existing == 1) {
  138. jQuery("ul#gallery_list").html(output_html);
  139. input_set = jQuery.makeArray(jQuery("#gallery_list li:not(.ui-sortable-helper) input.image-id"));
  140. set = new Array();
  141. for( var i in input_set) {
  142. set[i] = jQuery(input_set[i]).val();
  143. }
  144. //console.log(set);
  145. img_id = jQuery('#gallery_image_'+set[0]).parent('li').attr('id');
  146. jQuery('#gallery_image_'+set[0]).children('img.deleteButton').remove();
  147. jQuery('#gallery_image_'+set[0]).append("<a class='editButton'>Edit <img src='"+WPSC_URL+"/images/pencil.png' alt ='' /></a>");
  148. // jQuery('#gallery_image_'+set[0]).parent('li').attr('id', 0);
  149. for(i=1;i<set.length;i++) {
  150. jQuery('#gallery_image_'+set[i]).children('a.editButton').remove();
  151. jQuery('#gallery_image_'+set[i]).append("<img alt='-' class='deleteButton' src='"+WPSC_URL+"/images/cross.png'/>");
  152. element_id = jQuery('#gallery_image_'+set[i]).parent('li').attr('id');
  153. if(element_id == 0) {
  154. // jQuery('#gallery_image_'+set[i]).parent('li').attr('id', img_id);
  155. }
  156. }
  157. order = set.join(',');
  158. product_id = jQuery('#product_id').val();
  159. postVars = "product_id="+product_id+"&order="+order;
  160. jQuery.post( 'index.php?wpsc_admin_action=rearrange_images', postVars, function(returned_data) {
  161. eval(returned_data);
  162. jQuery('#gallery_image_'+image_id).children('a.editButton').remove();
  163. jQuery('#gallery_image_'+image_id).children('div.image_settings_box').remove();
  164. jQuery('#gallery_image_'+image_id).append(image_menu);
  165. });
  166. } else {
  167. jQuery("ul#gallery_list").append(output_html);
  168. ///jQuery("#gallery_list").trigger( 'update' );
  169. }
  170. }
  171. //jQuery('#media-item-' + fileObj.id + ' .progress').show();
  172. //window.setInterval(function() {
  173. jQuery("#media-item-" + fileObj.id + "").fadeOut("normal");
  174. //}, 5000);
  175. //prepareMediaItem(fileObj, serverData);
  176. //updateMediaForm();
  177. }
  178. function wpsc_uploadComplete(fileObj) {
  179. // If no more uploads queued, enable the submit button
  180. if ( swfu.getStats().files_queued == 0 )
  181. jQuery('#insert-gallery').attr('disabled', '');
  182. }
  183. // wp-specific error handlers
  184. // generic message
  185. function wpsc_wpQueueError(message) {
  186. jQuery('#media-upload-error').show().text(message);
  187. }
  188. // file-specific message
  189. function wpsc_wpFileError(fileObj, message) {
  190. 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();
  191. jQuery('#dismiss-' + fileObj.id).click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})});
  192. }
  193. function wpsc_fileQueueError(fileObj, error_code, message) {
  194. // Handle this error separately because we don't want to create a FileProgress element for it.
  195. if ( error_code == SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED ) {
  196. wpQueueError(swfuploadL10n.queue_limit_exceeded);
  197. }
  198. else if ( error_code == SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT ) {
  199. fileQueued(fileObj);
  200. wpFileError(fileObj, swfuploadL10n.file_exceeds_size_limit);
  201. }
  202. else if ( error_code == SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE ) {
  203. fileQueued(fileObj);
  204. wpFileError(fileObj, swfuploadL10n.zero_byte_file);
  205. }
  206. else if ( error_code == SWFUpload.QUEUE_ERROR.INVALID_FILETYPE ) {
  207. fileQueued(fileObj);
  208. wpFileError(fileObj, swfuploadL10n.invalid_filetype);
  209. }
  210. else {
  211. wpQueueError(swfuploadL10n.default_error);
  212. }
  213. }
  214. function wpsc_fileDialogComplete(num_files_queued) {
  215. try {
  216. if (num_files_queued > 0) {
  217. this.startUpload();
  218. }
  219. } catch (ex) {
  220. this.debug(ex);
  221. }
  222. }
  223. function wpsc_swfuploadPreLoad() {
  224. var swfupload_element = jQuery('#'+swfu.customSettings.swfupload_element_id).get(0);
  225. jQuery('#' + swfu.customSettings.degraded_element_id).hide();
  226. // Doing this directly because jQuery().show() seems to have timing problems
  227. if ( swfupload_element && ! swfupload_element.style.display )
  228. swfupload_element.style.display = 'block';
  229. }
  230. function wpsc_swfuploadLoadFailed() {
  231. jQuery('#' + swfu.customSettings.swfupload_element_id).hide();
  232. jQuery('#' + swfu.customSettings.degraded_element_id).show();
  233. }
  234. function wpsc_uploadError(fileObj, error_code, message) {
  235. // first the file specific error
  236. if ( error_code == SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL ) {
  237. wpFileError(fileObj, swfuploadL10n.missing_upload_url);
  238. }
  239. else if ( error_code == SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED ) {
  240. wpFileError(fileObj, swfuploadL10n.upload_limit_exceeded);
  241. }
  242. else {
  243. wpFileError(fileObj, swfuploadL10n.default_error);
  244. }
  245. // now the general upload status
  246. if ( error_code == SWFUpload.UPLOAD_ERROR.HTTP_ERROR ) {
  247. wpQueueError(swfuploadL10n.http_error);
  248. }
  249. else if ( error_code == SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED ) {
  250. wpQueueError(swfuploadL10n.upload_failed);
  251. }
  252. else if ( error_code == SWFUpload.UPLOAD_ERROR.IO_ERROR ) {
  253. wpQueueError(swfuploadL10n.io_error);
  254. }
  255. else if ( error_code == SWFUpload.UPLOAD_ERROR.SECURITY_ERROR ) {
  256. wpQueueError(swfuploadL10n.security_error);
  257. }
  258. else if ( error_code == SWFUpload.UPLOAD_ERROR.FILE_CANCELLED ) {
  259. wpQueueError(swfuploadL10n.security_error);
  260. }
  261. }
  262. // remember the last used image size, alignment and url
  263. var storeState;
  264. (function($){
  265. storeState = function(){
  266. var align = getUserSetting('align') || '', imgsize = getUserSetting('imgsize') || '';
  267. $('tr.align input[type="radio"]').click(function(){
  268. setUserSetting('align', $(this).val());
  269. }).filter(function(){
  270. if ( $(this).val() == align )
  271. return true;
  272. return false;
  273. }).attr('checked','checked');
  274. $('tr.image-size input[type="radio"]').click(function(){
  275. setUserSetting('imgsize', $(this).val());
  276. }).filter(function(){
  277. if ( $(this).attr('disabled') || $(this).val() != imgsize )
  278. return false;
  279. return true;
  280. }).attr('checked','checked');
  281. $('tr.url button').click(function(){
  282. var c = this.className || '';
  283. c = c.replace(/.*?(url[^ '"]+).*/, '$1');
  284. if (c) setUserSetting('urlbutton', c);
  285. $(this).siblings('.urlfield').val( $(this).attr('title') );
  286. });
  287. $('tr.url .urlfield').each(function(){
  288. var b = getUserSetting('urlbutton');
  289. $(this).val( $(this).siblings('button.'+b).attr('title') );
  290. });
  291. }
  292. })(jQuery);