Advertisement
Guest User

Untitled

a guest
May 8th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 KB | None | 0 0
  1. // AjaxQ jQuery Plugin
  2. // Copyright (c) 2012 Foliotek Inc.
  3. // MIT License
  4. // https://github.com/Foliotek/ajaxq
  5. (function($){var queues={};$.ajaxq=function(qname,opts){if(typeof opts==="undefined"){throw ("AjaxQ: queue name is not provided")}var deferred=$.Deferred(),promise=deferred.promise();promise.success=promise.done;promise.error=promise.fail;promise.complete=promise.always;var clonedOptions=$.extend(true,{},opts);enqueue(function(){var jqXHR=$.ajax.apply(window,[clonedOptions]).always(dequeue);jqXHR.done(function(){deferred.resolve.apply(this,arguments)});jqXHR.fail(function(){deferred.reject.apply(this,arguments)})});return promise;function enqueue(cb){if(!queues[qname]){queues[qname]=[];cb()}else{queues[qname].push(cb)}}function dequeue(){if(!queues[qname]){return}var nextCallback=queues[qname].shift();if(nextCallback){nextCallback()}else{delete queues[qname]}}};$.each(["getq","postq"],function(i,method){$[method]=function(qname,url,data,callback,type){if($.isFunction(data)){type=type||callback;callback=data;data=undefined}return $.ajaxq(qname,{type:method==="postq"?"post":"get",url:url,data:data,success:callback,dataType:type})}});var isQueueRunning=function(qname){return queues.hasOwnProperty(qname)};var isAnyQueueRunning=function(){for(var i in queues){if(isQueueRunning(i)){return true}}return false};$.ajaxq.isRunning=function(qname){if(qname){return isQueueRunning(qname)}else{return isAnyQueueRunning()}};$.ajaxq.clear=function(qname){if(!qname){for(var i in queues){if(queues.hasOwnProperty(i)){delete queues[i]}}}else{if(queues[qname]){delete queues[qname]}}}})(jQuery);
  6.  
  7. /**** MarketPress Ajax JS *********/
  8. jQuery(document).ready(function($) {
  9. //empty cart
  10. function mp_empty_cart() {
  11. $(document).on('click', 'a.mp_empty_cart', function(e) {
  12. e.preventDefault();
  13.  
  14. var answer = confirm(MP_Ajax.emptyCartMsg);
  15. if (answer) {
  16. $(this).html('<img src="'+MP_Ajax.imgUrl+'" />');
  17. $.post(MP_Ajax.ajaxUrl, {action: 'mp-update-cart', empty_cart: 1}, function(data) {
  18. $("div.mp_cart_widget_content").html(data);
  19. });
  20. }
  21. return false;
  22. });
  23. }
  24.  
  25. //add item to cart
  26. function mp_cart_listeners() {
  27. $(document).on('submit', '.mp_buy_form:has(input[name="action"])', function(e) {
  28. e.preventDefault();
  29.  
  30. var $formElm = $(this),
  31. tempHtml = $formElm.html(),
  32. serializedForm = $formElm.serialize();
  33.  
  34. //product_id=268&variation=0&action=mp-update-cart
  35.  
  36. $formElm.html('<img src="' + MP_Ajax.imgUrl + '" alt="' + MP_Ajax.addingMsg + '" />');
  37.  
  38. // we use the AjaxQ plugin here because we need to queue multiple add-to-cart requests http://wp.mu/96f
  39. $.ajaxq('addtocart', {
  40. "data" : serializedForm,
  41. "dataType" : "html",
  42. "type" : "POST",
  43. "url" : MP_Ajax.ajaxUrl,
  44. })
  45.  
  46. //callback when item is successfully added to cart
  47. .success(function(data){
  48. var result = data.split('||', 2);
  49. if (result[0] == 'error') {
  50. alert(result[1]);
  51. $formElm.html(tempHtml);
  52. } else {
  53. $formElm.html('<span class="mp_adding_to_cart">' + MP_Ajax.successMsg + '</span>');
  54. $("div.mp_cart_widget_content").html(result[1]);
  55. if (result[0] > 0) {
  56. $formElm.fadeOut(2000, function(){
  57. $formElm.html(tempHtml).fadeIn('fast');
  58. });
  59. } else {
  60. $formElm.fadeOut(2000, function(){
  61. $formElm.html('<span class="mp_no_stock">' + MP_Ajax.outMsg + '</span>').fadeIn('fast');
  62. });
  63. }
  64. }
  65. })
  66.  
  67. //callback when an error occurs while adding an item to the cart
  68. .error(function(){
  69. alert(MP_Ajax.addToCartErrorMsg);
  70. });
  71.  
  72.  
  73. $.ajaxq('addtocart', {
  74. "data" : 'product_id=PRODUCT_ID&variation=0&action=mp-update-cart',
  75. "dataType" : "html",
  76. "type" : "POST",
  77. "url" : MP_Ajax.ajaxUrl,
  78. })
  79.  
  80. //callback when item is successfully added to cart
  81. .success(function(data){
  82. var result = data.split('||', 2);
  83. if (result[0] == 'error') {
  84. alert(result[1]);
  85. $formElm.html(tempHtml);
  86. } else {
  87. $formElm.html('<span class="mp_adding_to_cart">' + MP_Ajax.successMsg + '</span>');
  88. $("div.mp_cart_widget_content").html(result[1]);
  89. if (result[0] > 0) {
  90. $formElm.fadeOut(2000, function(){
  91. $formElm.html(tempHtml).fadeIn('fast');
  92. });
  93. } else {
  94. $formElm.fadeOut(2000, function(){
  95. $formElm.html('<span class="mp_no_stock">' + MP_Ajax.outMsg + '</span>').fadeIn('fast');
  96. });
  97. }
  98. }
  99. })
  100.  
  101. //callback when an error occurs while adding an item to the cart
  102. .error(function(){
  103. alert(MP_Ajax.addToCartErrorMsg);
  104. });
  105. });
  106. }
  107.  
  108. //general store listeners (e.g. pagination, etc)
  109. function mp_store_listeners(){
  110. // on next/prev link click, get page number and update products
  111. $(document).on('click', '#mp_product_nav a', function(e){
  112. e.preventDefault();
  113.  
  114. var hrefParts = $(this).attr('href').split('#'),
  115. qs = parse_query(hrefParts[1]);
  116. get_and_insert_products($('.mp_product_list_refine').serialize() + '&page=' + qs['page']);
  117. });
  118. }
  119.  
  120. // get products via ajax, insert into DOM with new pagination links
  121. function get_and_insert_products(query_string){
  122. ajax_loading(true);
  123. $.post(MP_Ajax.ajaxUrl, 'action=get_products_list&' + query_string, function(data) {
  124. var qs = parse_query(query_string),
  125. hash = 'order=' + $('select[name="order"]').val() + '&page=' + qs['page'];
  126.  
  127. ajax_loading(false);
  128. $('#mp_product_nav').remove();
  129. $('#mp_product_list').first().replaceWith(data.products);
  130. location.hash = hash;
  131.  
  132. // scroll to top of list
  133. var pos = $('a[name="mp-product-list-top"]').offset();
  134. $('body,html').animate({ scrollTop: pos.top - 20 });
  135. });
  136. }
  137.  
  138. // parse querystring into variables
  139. function parse_query(query_string){
  140. var vars = [],
  141. pairs = query_string.split('&');
  142.  
  143. for ( i = 0; i < pairs.length; i++ ) {
  144. var tmp = pairs[i].split('=');
  145. vars[tmp[0]] = tmp[1];
  146. }
  147.  
  148. return vars;
  149. }
  150.  
  151. // if the page has been loaded from a bookmark set the current state for select elements
  152. function update_dropdown_state(query_string){
  153. var query = JSON.parse('{"' + decodeURI(query_string.replace(/&/g, "\",\"").replace(/=/g,"\":\"")) + '"}');
  154. for(name in query){
  155. $('select[name="'+name+'"]').val(query[name]);
  156. }
  157. }
  158.  
  159. // show loading ui when waiting for ajax response
  160. function ajax_loading(loading){
  161. $('#mp_product_list .mp_ajax_loading').remove();
  162. if(loading){
  163. $('#mp_product_list').prepend('<div class="mp_ajax_loading"></div>');
  164. }
  165. }
  166.  
  167. // Ajax for products view.
  168. function mp_ajax_products_list(){
  169. $('.mp_product_list_refine').show(); // hide for non JS users
  170.  
  171. // if hash tag contains a state, update view
  172. // hash tags are used to store the current state in these situations:
  173. // a) when the user views a product and then clicks back
  174. // b) viewing the URL from a bookmark
  175. if( /filter-term|order|page/.test(location.hash) ){
  176. var query_string = location.hash.replace('#', '');
  177.  
  178. if ( MP_Ajax.productCategory != '' )
  179. query_string += '&product_category=' + MP_Ajax.productCategory;
  180.  
  181. var $perPage = $('.mp_list_filter').find('[name="per_page"]');
  182. if ( $perPage.length )
  183. query_string += '&per_page=' + $perPage.val();
  184.  
  185. get_and_insert_products(query_string);
  186. update_dropdown_state(query_string);
  187. }
  188.  
  189. $(".mp_list_filter select").not('#product-category').change(function(){
  190. get_and_insert_products( $('.mp_product_list_refine').serialize() );
  191. });
  192.  
  193. $('#product-category').change(function(){
  194. window.location.href = MP_Ajax.links[$(this).val()] + location.hash;
  195. });
  196. }
  197.  
  198. //add listeners
  199. mp_empty_cart();
  200. mp_cart_listeners();
  201. mp_store_listeners();
  202.  
  203. if( MP_Ajax.showFilters == 1 ){
  204. mp_ajax_products_list();
  205. }
  206.  
  207. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement