Advertisement
arcab

common.js

Apr 9th, 2020
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getURLVar(key) {
  2.     var value = [];
  3.  
  4.     var query = String(document.location).split('?');
  5.  
  6.     if (query[1]) {
  7.         var part = query[1].split('&');
  8.  
  9.         for (i = 0; i < part.length; i++) {
  10.             var data = part[i].split('=');
  11.  
  12.             if (data[0] && data[1]) {
  13.                 value[data[0]] = data[1];
  14.             }
  15.         }
  16.  
  17.         if (value[key]) {
  18.             return value[key];
  19.         } else {
  20.             return '';
  21.         }
  22.     }
  23. }
  24.  
  25. $(document).ready(function() {
  26.     // Highlight any found errors
  27.     $('.text-danger').each(function() {
  28.         var element = $(this).parent().parent();
  29.  
  30.         if (element.hasClass('form-group')) {
  31.             element.addClass('has-error');
  32.         }
  33.     });
  34.     $( '.combo-product>div' ).click( function() {
  35.         window.location.href = $( this ).find( 'a' ).attr( 'href' );
  36.     });
  37.     // Currency
  38.     $('#form-currency .currency-select').on('click', function(e) {
  39.         e.preventDefault();
  40.  
  41.         $('#form-currency input[name=\'code\']').val($(this).attr('name'));
  42.  
  43.         $('#form-currency').submit();
  44.     });
  45.  
  46.     // Language
  47.     $('#form-language .language-select').on('click', function(e) {
  48.         e.preventDefault();
  49.  
  50.         $('#form-language input[name=\'code\']').val($(this).attr('name'));
  51.        
  52.         $('#form-language').submit();
  53.     });
  54.  
  55.     /* Search */
  56.     $('#search input[name=\'search\']').parent().find('button').on('click', function() {
  57.         var url = $('base').attr('href') + 'index.php?route=product/search';
  58.  
  59.         var value = $('header #search input[name=\'search\']').val();
  60.  
  61.         if (value) {
  62.             url += '&search=' + encodeURIComponent(value);
  63.         }
  64.  
  65.         location = url;
  66.     });
  67.  
  68.     $('.input-plus-minus .btn-minus').on('click', function(e) {
  69.        
  70.         var v =  $( '.input-plus-minus input' ).val()-1;
  71.         if( v < 0 ) {
  72.             v = 0;
  73.         }
  74.         $( '.input-plus-minus input' ).val( v );
  75.        
  76.     });
  77.     $('.input-plus-minus .btn-plus').on('click', function(e) {
  78.         $( '.input-plus-minus input' ).val( parseInt( $( '.input-plus-minus input' ).val() )+1);
  79.        
  80.     });
  81.    
  82.    
  83.     $('#search input[name=\'search\']').on('keydown', function(e) {
  84.         if (e.keyCode == 13) {
  85.             $('header #search input[name=\'search\']').parent().find('button').trigger('click');
  86.         }
  87.     });
  88.  
  89.     // Menu
  90.     $('#menu .dropdown-menu').each(function() {
  91.         var menu = $('#menu').offset();
  92.         var dropdown = $(this).parent().offset();
  93.  
  94.         var i = (dropdown.left + $(this).outerWidth()) - (menu.left + $('#menu').outerWidth());
  95.  
  96.         if (i > 0) {
  97.             $(this).css('margin-left', '-' + (i + 10) + 'px');
  98.         }
  99.     });
  100.  
  101.     // Product List
  102.     $('#list-view').click(function() {
  103.         $('#content .product-grid > .clearfix').remove();
  104.  
  105.         $('#content .row > .product-grid').attr('class', 'product-layout product-list col-xs-12');
  106.         $('#grid-view').removeClass('active');
  107.         $('#list-view').addClass('active');
  108.  
  109.         localStorage.setItem('display', 'list');
  110.     });
  111.  
  112.     // Product Grid
  113.     $('#grid-view').click(function() {
  114.         // What a shame bootstrap does not take into account dynamically loaded columns
  115.         var cols = $('#column-right, #column-left').length;
  116.  
  117.         if (cols == 2) {
  118.             //$('#content .product-list').attr('class', 'product-layout product-grid col-lg-6 col-md-6 col-sm-12 col-xs-12');
  119.         } else if (cols == 1) {
  120.             //$('#content .product-list').attr('class', 'product-layout product-grid col-lg-4 col-md-4 col-sm-6 col-xs-12');
  121.         } else {
  122.             //$('#content .product-list').attr('class', 'product-layout product-grid col-lg-4 col-md-4 col-sm-6 col-xs-12');
  123.         }
  124.  
  125.         $('#list-view').removeClass('active');
  126.         $('#grid-view').addClass('active');
  127.  
  128.         localStorage.setItem('display', 'grid');
  129.     });
  130.  
  131.     if (localStorage.getItem('display') == 'list') {
  132.         $('#list-view').trigger('click');
  133.         $('#list-view').addClass('active');
  134.     } else {
  135.         $('#grid-view').trigger('click');
  136.         $('#grid-view').addClass('active');
  137.     }
  138.  
  139.     // Checkout
  140.     $(document).on('keydown', '#collapse-checkout-option input[name=\'email\'], #collapse-checkout-option input[name=\'password\']', function(e) {
  141.         if (e.keyCode == 13) {
  142.             $('#collapse-checkout-option #button-login').trigger('click');
  143.         }
  144.     });
  145.  
  146.     // tooltips on hover
  147.     $('[data-toggle=\'tooltip\']').tooltip({container: 'body'});
  148.  
  149.     // Makes tooltips work on ajax generated content
  150.     $(document).ajaxStop(function() {
  151.         $('[data-toggle=\'tooltip\']').tooltip({container: 'body'});
  152.     });
  153. });
  154.  
  155. // Cart add remove functions
  156. var cart = {
  157.     'add': function(product_id, quantity) {
  158.         $.ajax({
  159.             url: 'index.php?route=checkout/cart/add',
  160.             type: 'post',
  161.             data: 'product_id=' + product_id + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1),
  162.             dataType: 'json',
  163.             beforeSend: function() {
  164.                 $('#cart > button').button('loading');
  165.             },
  166.             complete: function() {
  167.                 $('#cart > button').button('reset');
  168.             },
  169.             success: function(json) {
  170.                 $('.alert-dismissible, .text-danger').remove();
  171.  
  172.                 if (json['redirect']) {
  173.                     location = json['redirect'];
  174.                 }
  175.  
  176.                 if (json['success']) {
  177.                     $('#content').parent().before('<div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
  178.  
  179.                     // Need to set timeout otherwise it wont update the total
  180.                     setTimeout(function () {
  181.                         $('#cart > button').html('<i class="fa fa-shopping-cart"></i><span id="cart-total"> ' + json['total'] + '</span>');
  182.                     }, 100);
  183.  
  184.                     $('html, body').animate({ scrollTop: 0 }, 'slow');
  185.  
  186.                     $('#cart > ul').load('index.php?route=common/cart/info ul li');
  187.  
  188.                     //Event snippet for Add to cart - Remarketing conversion page In your html page, add the snippet and call gtag_report_conversion when someone clicks on the chosen link or button. -->
  189.                     gtag('event', 'conversion', { 'send_to': 'AW-655736475/I6MzCJ_k4ssBEJv91rgC', 'value': product_id, 'currency': 'EUR' });
  190.                    
  191.                 }
  192.             },
  193.             error: function(xhr, ajaxOptions, thrownError) {
  194.                 alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  195.             }
  196.         });
  197.     },
  198.     'update': function(key, quantity) {
  199.         $.ajax({
  200.             url: 'index.php?route=checkout/cart/edit',
  201.             type: 'post',
  202.             data: 'key=' + key + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1),
  203.             dataType: 'json',
  204.             beforeSend: function() {
  205.                 $('#cart > button').button('loading');
  206.             },
  207.             complete: function() {
  208.                 $('#cart > button').button('reset');
  209.             },
  210.             success: function(json) {
  211.                 // Need to set timeout otherwise it wont update the total
  212.                 setTimeout(function () {
  213.                     $('#cart > button').html('<i class="fa fa-shopping-cart"></i><span id="cart-total"> ' + json['total'] + '</span>');
  214.                 }, 100);
  215.  
  216.                 if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {
  217.                     location = 'index.php?route=checkout/cart';
  218.                 } else {
  219.                     $('#cart > ul').load('index.php?route=common/cart/info ul li');
  220.                 }
  221.             },
  222.             error: function(xhr, ajaxOptions, thrownError) {
  223.                 alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  224.             }
  225.         });
  226.     },
  227.     'logremove': function( id, name, quantity, price ) {
  228.        
  229.         dataLayer.push({
  230.             'event': 'removeFromCart',
  231.             'ecommerce': {
  232.                 'remove': {
  233.                     'products': [{
  234.                         'name': name,
  235.                         'id': id,
  236.                         'price': price,
  237.                         'quantity': quantity
  238.                     }]
  239.                 }
  240.             }
  241.         });
  242.     },
  243.     'remove': function(key) {
  244.         $.ajax({
  245.             url: 'index.php?route=checkout/cart/remove',
  246.             type: 'post',
  247.             data: 'key=' + key,
  248.             dataType: 'json',
  249.             beforeSend: function() {
  250.                 $('#cart > button').button('loading');
  251.             },
  252.             complete: function() {
  253.                 $('#cart > button').button('reset');
  254.             },
  255.             success: function(json) {
  256.                 // Need to set timeout otherwise it wont update the total
  257.                
  258.                 setTimeout(function () {
  259.                     $('#cart > button').html('<i class="fa fa-shopping-cart"></i><span id="cart-total"> ' + json['total'] + '</span>');
  260.                 }, 100);
  261.  
  262.                 if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {
  263.                     location = 'index.php?route=checkout/cart';
  264.                 } else {
  265.                     $('#cart > ul').load('index.php?route=common/cart/info ul li');
  266.                 }
  267.             },
  268.             error: function(xhr, ajaxOptions, thrownError) {
  269.                 alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  270.             }
  271.         });
  272.     }
  273. }
  274.  
  275. var voucher = {
  276.     'add': function() {
  277.  
  278.     },
  279.     'remove': function(key) {
  280.         $.ajax({
  281.             url: 'index.php?route=checkout/cart/remove',
  282.             type: 'post',
  283.             data: 'key=' + key,
  284.             dataType: 'json',
  285.             beforeSend: function() {
  286.                 $('#cart > button').button('loading');
  287.             },
  288.             complete: function() {
  289.                 $('#cart > button').button('reset');
  290.             },
  291.             success: function(json) {
  292.                 // Need to set timeout otherwise it wont update the total
  293.                 setTimeout(function () {
  294.                     $('#cart > button').html('<i class="fa fa-shopping-cart"></i><span id="cart-total"> ' + json['total'] + '</span>');
  295.                 }, 100);
  296.  
  297.                 if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {
  298.                     location = 'index.php?route=checkout/cart';
  299.                 } else {
  300.                     $('#cart > ul').load('index.php?route=common/cart/info ul li');
  301.                 }
  302.             },
  303.             error: function(xhr, ajaxOptions, thrownError) {
  304.                 alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  305.             }
  306.         });
  307.     }
  308. }
  309.  
  310. var wishlist = {
  311.     'add': function(product_id) {
  312.         $.ajax({
  313.             url: 'index.php?route=account/wishlist/add',
  314.             type: 'post',
  315.             data: 'product_id=' + product_id,
  316.             dataType: 'json',
  317.             success: function(json) {
  318.                 $('.alert-dismissible').remove();
  319.  
  320.                 if (json['redirect']) {
  321.                     location = json['redirect'];
  322.                 }
  323.  
  324.                 if (json['success']) {
  325.                     $('#content').parent().before('<div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
  326.                 }
  327.  
  328.                 $('#wishlist-total span').html(json['total']);
  329.                 $('#wishlist-total').attr('title', json['total']);
  330.  
  331.                 $('html, body').animate({ scrollTop: 0 }, 'slow');
  332.             },
  333.             error: function(xhr, ajaxOptions, thrownError) {
  334.                 alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  335.             }
  336.         });
  337.     },
  338.     'remove': function() {
  339.  
  340.     }
  341. }
  342.  
  343. var compare = {
  344.     'add': function(product_id) {
  345.         $.ajax({
  346.             url: 'index.php?route=product/compare/add',
  347.             type: 'post',
  348.             data: 'product_id=' + product_id,
  349.             dataType: 'json',
  350.             success: function(json) {
  351.                 $('.alert-dismissible').remove();
  352.  
  353.                 if (json['success']) {
  354.                     $('#content').parent().before('<div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
  355.  
  356.                     $('#compare-total').html(json['total']);
  357.  
  358.                     $('html, body').animate({ scrollTop: 0 }, 'slow');
  359.                 }
  360.             },
  361.             error: function(xhr, ajaxOptions, thrownError) {
  362.                 alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  363.             }
  364.         });
  365.     },
  366.     'remove': function() {
  367.  
  368.     }
  369. }
  370.  
  371. /* Agree to Terms */
  372. $(document).delegate('.agree', 'click', function(e) {
  373.     e.preventDefault();
  374.  
  375.     $('#modal-agree').remove();
  376.  
  377.     var element = this;
  378.  
  379.     $.ajax({
  380.         url: $(element).attr('href'),
  381.         type: 'get',
  382.         dataType: 'html',
  383.         success: function(data) {
  384.             html  = '<div id="modal-agree" class="modal">';
  385.             html += '  <div class="modal-dialog">';
  386.             html += '    <div class="modal-content">';
  387.             html += '      <div class="modal-header">';
  388.             html += '        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>';
  389.             html += '        <h4 class="modal-title">' + $(element).text() + '</h4>';
  390.             html += '      </div>';
  391.             html += '      <div class="modal-body">' + data + '</div>';
  392.             html += '    </div>';
  393.             html += '  </div>';
  394.             html += '</div>';
  395.  
  396.             $('body').append(html);
  397.  
  398.             $('#modal-agree').modal('show');
  399.         }
  400.     });
  401. });
  402.  
  403. // Autocomplete */
  404. (function($) {
  405.     $.fn.autocomplete = function(option) {
  406.         return this.each(function() {
  407.             this.timer = null;
  408.             this.items = new Array();
  409.  
  410.             $.extend(this, option);
  411.  
  412.             $(this).attr('autocomplete', 'off');
  413.  
  414.             // Focus
  415.             $(this).on('focus', function() {
  416.                 this.request();
  417.             });
  418.  
  419.             // Blur
  420.             $(this).on('blur', function() {
  421.                 setTimeout(function(object) {
  422.                     object.hide();
  423.                 }, 200, this);
  424.             });
  425.  
  426.             // Keydown
  427.             $(this).on('keydown', function(event) {
  428.                 switch(event.keyCode) {
  429.                     case 27: // escape
  430.                         this.hide();
  431.                         break;
  432.                     default:
  433.                         this.request();
  434.                         break;
  435.                 }
  436.             });
  437.  
  438.             // Click
  439.             this.click = function(event) {
  440.                 event.preventDefault();
  441.  
  442.                 value = $(event.target).parent().attr('data-value');
  443.  
  444.                 if (value && this.items[value]) {
  445.                     this.select(this.items[value]);
  446.                 }
  447.             }
  448.  
  449.             // Show
  450.             this.show = function() {
  451.                 var pos = $(this).position();
  452.  
  453.                 $(this).siblings('ul.dropdown-menu').css({
  454.                     top: pos.top + $(this).outerHeight(),
  455.                     left: pos.left
  456.                 });
  457.  
  458.                 $(this).siblings('ul.dropdown-menu').show();
  459.             }
  460.  
  461.             // Hide
  462.             this.hide = function() {
  463.                 $(this).siblings('ul.dropdown-menu').hide();
  464.             }
  465.  
  466.             // Request
  467.             this.request = function() {
  468.                 clearTimeout(this.timer);
  469.  
  470.                 this.timer = setTimeout(function(object) {
  471.                     object.source($(object).val(), $.proxy(object.response, object));
  472.                 }, 200, this);
  473.             }
  474.  
  475.             // Response
  476.             this.response = function(json) {
  477.                 html = '';
  478.  
  479.                 if (json.length) {
  480.                     for (i = 0; i < json.length; i++) {
  481.                         this.items[json[i]['value']] = json[i];
  482.                     }
  483.  
  484.                     for (i = 0; i < json.length; i++) {
  485.                         if (!json[i]['category']) {
  486.                             html += '<li data-value="' + json[i]['value'] + '"><a href="#">' + json[i]['label'] + '</a></li>';
  487.                         }
  488.                     }
  489.  
  490.                     // Get all the ones with a categories
  491.                     var category = new Array();
  492.  
  493.                     for (i = 0; i < json.length; i++) {
  494.                         if (json[i]['category']) {
  495.                             if (!category[json[i]['category']]) {
  496.                                 category[json[i]['category']] = new Array();
  497.                                 category[json[i]['category']]['name'] = json[i]['category'];
  498.                                 category[json[i]['category']]['item'] = new Array();
  499.                             }
  500.  
  501.                             category[json[i]['category']]['item'].push(json[i]);
  502.                         }
  503.                     }
  504.  
  505.                     for (i in category) {
  506.                         html += '<li class="dropdown-header">' + category[i]['name'] + '</li>';
  507.  
  508.                         for (j = 0; j < category[i]['item'].length; j++) {
  509.                             html += '<li data-value="' + category[i]['item'][j]['value'] + '"><a href="#">&nbsp;&nbsp;&nbsp;' + category[i]['item'][j]['label'] + '</a></li>';
  510.                         }
  511.                     }
  512.                 }
  513.  
  514.                 if (html) {
  515.                     this.show();
  516.                 } else {
  517.                     this.hide();
  518.                 }
  519.  
  520.                 $(this).siblings('ul.dropdown-menu').html(html);
  521.             }
  522.  
  523.             $(this).after('<ul class="dropdown-menu"></ul>');
  524.             $(this).siblings('ul.dropdown-menu').delegate('a', 'click', $.proxy(this.click, this));
  525.  
  526.         });
  527.     }
  528. })(window.jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement