PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/js/script.js

https://github.com/ThemesWpFr/jigoshop
JavaScript | 528 lines | 510 code | 13 blank | 5 comment | 14 complexity | fb71d9902562dc4b2554b794e2a297e4 MD5 | raw file
Possible License(s): GPL-3.0
  1. jQuery.fn.animateHighlight = function(highlightColor, duration) {
  2. var highlightBg = highlightColor || "#FFFF9C";
  3. var animateMs = duration || 1500;
  4. var originalBg = this.css("backgroundColor");
  5. this.stop().css("background-color", highlightBg).animate({backgroundColor: originalBg}, animateMs);
  6. };
  7. jQuery(function() {
  8. // Lightbox
  9. if (jigoshop_params.load_fancybox) {
  10. jQuery('a.zoom').fancybox({
  11. 'transitionIn' : 'elastic',
  12. 'transitionOut' : 'elastic',
  13. 'speedIn' : 600,
  14. 'speedOut' : 200,
  15. 'overlayShow' : true
  16. });
  17. }
  18. // Star ratings
  19. jQuery('#rating').hide().before('<p class="stars"><span><a class="star-1" href="#">1</a><a class="star-2" href="#">2</a><a class="star-3" href="#">3</a><a class="star-4" href="#">4</a><a class="star-5" href="#">5</a></span></p>');
  20. jQuery('p.stars a').click(function(){
  21. jQuery('#rating').val(jQuery(this).text());
  22. jQuery('p.stars a').removeClass('active');
  23. jQuery(this).addClass('active');
  24. return false;
  25. });
  26. // Price slider
  27. var min_price = parseInt(jQuery('.price_slider_amount #min_price').val());
  28. var max_price = parseInt(jQuery('.price_slider_amount #max_price').val());
  29. if (jigoshop_params.min_price) {
  30. current_min_price = jigoshop_params.min_price;
  31. } else {
  32. current_min_price = min_price;
  33. }
  34. if (jigoshop_params.max_price) {
  35. current_max_price = jigoshop_params.max_price;
  36. } else {
  37. current_max_price = max_price;
  38. }
  39. jQuery('.price_slider').slider({
  40. range: true,
  41. min: min_price,
  42. max: max_price,
  43. values: [ current_min_price, current_max_price ],
  44. create : function( event, ui ) {
  45. jQuery( ".price_slider_amount span" ).html( jigoshop_params.currency_symbol + current_min_price + " - " + jigoshop_params.currency_symbol + current_max_price );
  46. jQuery( ".price_slider_amount #min_price" ).val(current_min_price);
  47. jQuery( ".price_slider_amount #max_price" ).val(current_max_price);
  48. },
  49. slide: function( event, ui ) {
  50. jQuery( ".price_slider_amount span" ).html( jigoshop_params.currency_symbol + ui.values[ 0 ] + " - " + jigoshop_params.currency_symbol + ui.values[ 1 ] );
  51. jQuery( "input#min_price" ).val(ui.values[ 0 ]);
  52. jQuery( "input#max_price" ).val(ui.values[ 1 ]);
  53. }
  54. });
  55. // Quantity buttons
  56. jQuery("div.quantity, td.quantity").append('<input type="button" value="+" id="add1" class="plus" />').prepend('<input type="button" value="-" id="minus1" class="minus" />');
  57. jQuery(".plus").click(function()
  58. {
  59. var currentVal = parseInt(jQuery(this).prev(".qty").val());
  60. if (!currentVal || currentVal=="" || currentVal == "NaN") currentVal = 0;
  61. jQuery(this).prev(".qty").val(currentVal + 1);
  62. });
  63. jQuery(".minus").click(function()
  64. {
  65. var currentVal = parseInt(jQuery(this).next(".qty").val());
  66. if (currentVal == "NaN") currentVal = 0;
  67. if (currentVal > 0)
  68. {
  69. jQuery(this).next(".qty").val(currentVal - 1);
  70. }
  71. });
  72. /* states */
  73. var states_json = jigoshop_params.countries.replace(/&quot;/g, '"');
  74. var states = jQuery.parseJSON( states_json );
  75. jQuery('select.country_to_state').change(function(){
  76. var country = jQuery(this).val();
  77. var state_box = jQuery('#' + jQuery(this).attr('rel'));
  78. var input_name = jQuery(state_box).attr('name');
  79. var input_id = jQuery(state_box).attr('id');
  80. if (states[country]) {
  81. var options = '';
  82. var state = states[country];
  83. var state_selected = jigoshop_params.billing_state;
  84. if (input_name == 'calc_shipping_state') {
  85. state_selected = jQuery('#calc_shipping_state').val();
  86. }
  87. else if ( input_name == 'billing-state' ) {
  88. state_selected = jigoshop_params.billing_state;
  89. }
  90. else {
  91. state_selected = jigoshop_params.shipping_state;
  92. }
  93. for(var index in state) {
  94. if (state_selected == index) {
  95. options = options + '<option value="' + index + '" selected="selected">' + state[index] + '</option>';
  96. } else {
  97. options = options + '<option value="' + index + '">' + state[index] + '</option>';
  98. }
  99. }
  100. if (jQuery(state_box).is('input')) {
  101. // Change for select
  102. jQuery(state_box).prev().append(' <span class="required">*</span>');
  103. jQuery(state_box).replaceWith('<select name="' + input_name + '" id="' + input_id + '"><option value="">' + jigoshop_params.select_state_text + '</option></select>');
  104. state_box = jQuery('#' + jQuery(this).attr('rel'));
  105. }
  106. jQuery(state_box).html(options);
  107. } else {
  108. if (jQuery(state_box).is('select')) {
  109. jQuery(state_box).prev().find('span.required').remove();
  110. jQuery(state_box).replaceWith('<input class="input-text" type="text" placeholder="' + jigoshop_params.state_text + '" name="' + input_name + '" id="' + input_id + '" />');
  111. state_box = jQuery('#' + jQuery(this).attr('rel'));
  112. }
  113. }
  114. }).change();
  115. /* Tabs */
  116. jQuery('#tabs .panel:not(#tabs .panel)').hide();
  117. jQuery('div#tabs ul.tabs li > a').click(function(){
  118. var href = jQuery(this).attr('href');
  119. jQuery('#tabs li').removeClass('active');
  120. jQuery('div.panel').hide();
  121. jQuery('div' + href).show();
  122. jQuery(this).parent().addClass('active');
  123. jQuery.cookie('current_tab', href);
  124. return false;
  125. });
  126. if (jQuery('#tabs li.active').size()==0) {
  127. jQuery('#tabs li:first a').click();
  128. } else {
  129. jQuery('#tabs li.active a').click();
  130. }
  131. /* Shipping calculator */
  132. jQuery('.shipping-calculator-form').hide();
  133. jQuery('.shipping-calculator-button').click(function() {
  134. jQuery('.shipping-calculator-form').slideToggle('slow', function() {
  135. // Animation complete.
  136. });
  137. });
  138. // Stop anchors moving the viewport
  139. jQuery(".shipping-calculator-button").click(function() {return false;});
  140. jQuery("input[name=shipping_rates]").click(function(){
  141. var dataString = 'shipping_rates=' + jQuery(this).val();
  142. var cart_url = jQuery("input[name=cart-url]").val();
  143. jQuery('.cart_totals_table').block({message: null, overlayCSS: {background: '#fff url(' + jigoshop_params.assets_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
  144. jQuery.ajax({
  145. type: "POST",
  146. url: cart_url,
  147. data: dataString,
  148. success: function(ret) {
  149. var jqObj = jQuery(ret);
  150. jQuery('.cart_totals_table').replaceWith(jqObj.find('.cart_totals_table'));
  151. jQuery('.cart_totals_table').unblock();
  152. }
  153. });
  154. });
  155. /*################# VARIATIONS ###################*/
  156. //check if two arrays of attributes match
  157. function variations_match(attrs1, attrs2) {
  158. var match = true;
  159. for(name in attrs1) {
  160. var val1 = attrs1[name].toLowerCase();
  161. if ( typeof( attrs2[name] ) == 'undefined' ) {
  162. var val2 = 'undefined';
  163. } else {
  164. var val2 = attrs2[name].toLowerCase();
  165. }
  166. if(val1.length != 0 && val2.length != 0 && val1 != val2) {
  167. match = false;
  168. }
  169. }
  170. return match;
  171. }
  172. //search for matching variations for given set of attributes
  173. function find_matching_variations(attributes) {
  174. var matching = [];
  175. for(i = 0; i < product_variations.length; i++) {
  176. var variation = product_variations[i];
  177. if(variations_match(variation.attributes, attributes)) {
  178. matching.push(variation);
  179. }
  180. }
  181. return matching;
  182. }
  183. //disable option fields that are unavaiable for current set of attributes
  184. function update_variation_values(variations) {
  185. // Loop through selects and disable/enable options based on selections
  186. jQuery('.variations select').each(function( index, el ){
  187. current_attr_select = jQuery(el);
  188. // Disable all
  189. current_attr_select.find('option:gt(0)').attr('disabled', 'disabled');
  190. // Get name
  191. var current_attr_name = current_attr_select.attr('name');
  192. // Loop through variations
  193. for(num in variations) {
  194. var attributes = variations[num].attributes;
  195. for(attr_name in attributes) {
  196. var attr_val = attributes[attr_name];
  197. if(attr_name == current_attr_name) {
  198. if (attr_val) {
  199. current_attr_select.find('option[value="'+attr_val+'"]').removeAttr('disabled');
  200. } else {
  201. current_attr_select.find('option').removeAttr('disabled');
  202. }
  203. }
  204. }
  205. }
  206. // completely re-enable the previous select so 'Choose an option' isn't required to change selections
  207. current_attr_select.parent().prev().find('select').find('option:gt(0)').removeAttr('disabled');
  208. });
  209. }
  210. //show single variation details (price, stock, image)
  211. function show_variation(variation) {
  212. var img = jQuery('div.images img:eq(0)');
  213. var link = jQuery('div.images a.zoom:eq(0)');
  214. var o_src = jQuery(img).attr('original-src');
  215. var o_link = jQuery(link).attr('original-href');
  216. var variation_image = variation.image_src;
  217. var variation_link = variation.image_link;
  218. jQuery('.single_variation').html( variation.price_html + variation.availability_html );
  219. if (!o_src) {
  220. jQuery(img).attr('original-src', jQuery(img).attr('src'));
  221. }
  222. if (!o_link) {
  223. jQuery(link).attr('original-href', jQuery(link).attr('href'));
  224. }
  225. if (variation_image && variation_image.length > 1) {
  226. jQuery(img).attr('src', variation_image);
  227. jQuery(link).attr('href', variation_link);
  228. } else {
  229. jQuery(img).attr('src', o_src);
  230. jQuery(link).attr('href', o_link);
  231. }
  232. jQuery('.product_meta .sku').remove();
  233. jQuery('.product_meta').append(variation.sku);
  234. jQuery('.shop_attributes').find('.weight').remove();
  235. if ( variation.a_weight ) {
  236. jQuery('.shop_attributes').append(variation.a_weight);
  237. }
  238. jQuery('.shop_attributes').find('.length').remove();
  239. if ( variation.a_length ) {
  240. jQuery('.shop_attributes').append(variation.a_length);
  241. }
  242. jQuery('.shop_attributes').find('.width').remove();
  243. if ( variation.a_width ) {
  244. jQuery('.shop_attributes').append(variation.a_width);
  245. }
  246. jQuery('.shop_attributes').find('.height').remove();
  247. if ( variation.a_height ) {
  248. jQuery('.shop_attributes').append(variation.a_height);
  249. }
  250. if ( ! variation.in_stock ) {
  251. jQuery('.single_variation').slideDown();
  252. } else {
  253. jQuery('.variations_button, .single_variation').slideDown();
  254. }
  255. }
  256. //when one of attributes is changed - check everything to show only valid options
  257. function check_variations() {
  258. jQuery('form input[name=variation_id]').val('');
  259. jQuery('.single_variation').text('');
  260. jQuery('.variations_button, .single_variation').slideUp();
  261. jQuery('.product_meta .sku').remove();
  262. jQuery('.shop_attributes').find('.weight').remove();
  263. jQuery('.shop_attributes').find('.length').remove();
  264. jQuery('.shop_attributes').find('.width').remove();
  265. jQuery('.shop_attributes').find('.height').remove();
  266. var all_set = true;
  267. var current_attributes = {};
  268. jQuery('.variations select').each(function(){
  269. if (jQuery(this).val().length == 0) {
  270. all_set = false;
  271. }
  272. current_attributes[jQuery(this).attr('name')] = jQuery(this).val();
  273. });
  274. var matching_variations = find_matching_variations(current_attributes);
  275. if(all_set) {
  276. var variation = matching_variations.pop();
  277. jQuery('form input[name=variation_id]').val(variation.variation_id);
  278. show_variation(variation);
  279. } else {
  280. update_variation_values(matching_variations);
  281. }
  282. }
  283. jQuery('.variations select').change(function(){
  284. //make sure that only selects before this one, and one after this are enabled
  285. var num = jQuery(this).data('num');
  286. if(jQuery(this).val().length > 0) {
  287. num += 1;
  288. }
  289. var selects = jQuery('.variations select');
  290. selects.filter(':lt('+num+')').removeAttr('disabled');
  291. selects.filter(':eq('+num+')').removeAttr('disabled').val('');
  292. selects.filter(':gt('+num+')').attr('disabled', 'disabled').val('');
  293. check_variations(jQuery(this));
  294. });
  295. //disable all but first select field
  296. jQuery('.variations select:gt(0)').attr('disabled', 'disabled');
  297. //numerate all selects
  298. jQuery.each(jQuery('.variations select'), function(i, item){
  299. jQuery(item).data('num', i);
  300. });
  301. });
  302. if ( jigoshop_params.is_checkout ) {
  303. var updateTimer;
  304. var jqxhr;
  305. function update_checkout() {
  306. if (jqxhr) jqxhr.abort();
  307. var method = jQuery('#shipping_method').val();
  308. var coupon = jQuery('#coupon_code').val();
  309. var payment_method= jQuery('input[name=payment_method]:checked').val();
  310. var country = jQuery('#billing-country').val();
  311. var state = jQuery('#billing-state').val();
  312. var postcode = jQuery('input#billing-postcode').val();
  313. if (jQuery('#shiptobilling input').is(':checked') || jQuery('#shiptobilling input').size()==0) {
  314. var s_country = jQuery('#billing-country').val();
  315. var s_state = jQuery('#billing-state').val();
  316. var s_postcode= jQuery('input#billing-postcode').val();
  317. } else {
  318. var s_country = jQuery('#shipping-country').val();
  319. var s_state = jQuery('#shipping-state').val();
  320. var s_postcode= jQuery('input#shipping-postcode').val();
  321. }
  322. jQuery('#order_methods, #order_review').block({message: null, overlayCSS: {background: '#fff url(' + jigoshop_params.assets_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
  323. var data = {
  324. action: 'jigoshop_update_order_review',
  325. security: jigoshop_params.update_order_review_nonce,
  326. shipping_method: method,
  327. country: country,
  328. state: state,
  329. postcode: postcode,
  330. s_country: s_country,
  331. s_state: s_state,
  332. s_postcode: s_postcode,
  333. payment_method: payment_method,
  334. coupon_code: coupon,
  335. post_data: jQuery('form.checkout').serialize()
  336. };
  337. jqxhr = jQuery.ajax({
  338. type: 'POST',
  339. url: jigoshop_params.ajax_url,
  340. data: data,
  341. success: function( response ) {
  342. /* Prevent stacking of errors. */
  343. jQuery('.jigoshop_error, .jigoshop_message').remove();
  344. jQuery('#order_methods, #order_review').remove();
  345. jQuery('#order_review_heading').after(response);
  346. jQuery('#order_review input[name=payment_method]:checked').click();
  347. }
  348. });
  349. }
  350. jQuery(function(){
  351. jQuery('p.password').hide();
  352. jQuery('input.show_password').change(function(){
  353. jQuery('p.password').slideToggle();
  354. });
  355. jQuery('div.shipping-address').hide();
  356. jQuery('#shiptobilling input').change(function(){
  357. jQuery('div.shipping-address').hide();
  358. if (!jQuery(this).is(':checked')) {
  359. jQuery('div.shipping-address').slideDown();
  360. }
  361. }).change();
  362. if (jigoshop_params.option_guest_checkout=='yes') {
  363. jQuery('div.create-account').hide();
  364. jQuery('input#createaccount').change(function(){
  365. jQuery('div.create-account').hide();
  366. if (jQuery(this).is(':checked')) {
  367. jQuery('div.create-account').slideDown();
  368. }
  369. }).change();
  370. }
  371. jQuery('.payment_methods input.input-radio').live('click', function(){
  372. jQuery('div.payment_box').hide();
  373. if (jQuery(this).is(':checked')) {
  374. jQuery('div.payment_box.' + jQuery(this).attr('ID')).slideDown();
  375. }
  376. });
  377. jQuery('#order_review input[name=payment_method]:checked').click();
  378. jQuery('form.login').hide();
  379. jQuery('a.showlogin').click(function(e){
  380. e.preventDefault();
  381. jQuery('form.login').slideToggle();
  382. });
  383. /* Update totals */
  384. jQuery('#shipping_method').live('change', function(){
  385. clearTimeout(updateTimer);
  386. update_checkout();
  387. }).change();
  388. jQuery('#coupon_code').live('change', function(e){
  389. clearTimeout(updateTimer);
  390. update_checkout();
  391. }).change();
  392. jQuery('input#billing-country, input#billing-state, #billing-postcode, input#shipping-country, input#shipping-state, #shipping-postcode').live('change', function(){
  393. clearTimeout(updateTimer);
  394. update_checkout();
  395. });
  396. jQuery('select#billing-country, select#billing-state, select#shipping-country, select#shipping-state, #shiptobilling input').live('change', function(){
  397. clearTimeout(updateTimer);
  398. update_checkout();
  399. });
  400. /* AJAX Form Submission */
  401. jQuery('form.checkout').submit(function(){
  402. var form = this;
  403. jQuery(form).block({message: null, overlayCSS: {background: '#fff url(' + jigoshop_params.assets_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
  404. jQuery.ajax({
  405. type: 'POST',
  406. url: jigoshop_params.checkout_url,
  407. data: jQuery(form).serialize(),
  408. success: function( code ) {
  409. jQuery('.jigoshop_error, .jigoshop_message').remove();
  410. try {
  411. success = jQuery.parseJSON( code );
  412. window.location = decodeURI(success.redirect);
  413. }
  414. catch(err) {
  415. jQuery(form).prepend( code );
  416. jQuery(form).unblock();
  417. jQuery.scrollTo(jQuery(form).parent(), {easing:'swing'});
  418. }
  419. },
  420. dataType: "html"
  421. });
  422. return false;
  423. });
  424. });
  425. }
  426. //message fade in
  427. jQuery(document).ready(function(){
  428. jQuery('.jigoshop_error, .jigoshop_message').css('opacity', 0);
  429. setTimeout(function(){jQuery('.jigoshop_error, .jigoshop_message').animate({opacity:1}, 1500);},100);
  430. });