Advertisement
Igor150195

Untitled

Jun 16th, 2020
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. shop2.queue.amount = function() {
  2.  
  3.     var $document = $(document);
  4.  
  5.     function validate(input) {
  6.         var kind = input.data('kind'),
  7.             max = input.data('max'),
  8.             val = Number(input.val()),
  9.             amount = 0,
  10.             available,
  11.             amount_min = parseFloat(input.data('min')),
  12.             multiplicity = parseFloat(input.data('multiplicity'));
  13.  
  14.         if (kind && max > 0) {
  15.             amount = Number(input.val());
  16.  
  17.             if (amount > max) {
  18.                 available = max - amount + val;
  19.                 input.val(available);
  20.  
  21.                 available = available.toFixed(4) - 0;
  22.  
  23.                 shop2.msg(_s3Lang.JS_AVAILABLE_ONLY + ' ' + available, input);
  24.             }
  25.         }
  26.  
  27.         if (amount_min || multiplicity) {
  28.  
  29.             if (multiplicity) {
  30.                 var x = (val - amount_min) % multiplicity;
  31.  
  32.                 if (x < (multiplicity / 2)) {
  33.                     val -= x;
  34.                 } else {
  35.                     val += multiplicity - x;
  36.                 }
  37.  
  38.                 if (amount_min === 1 && multiplicity > 1) {
  39.                     val--;
  40.                 }
  41.  
  42.                 val = val.toFixed(4) - 0;
  43.  
  44.                 input.val(val);
  45.             }
  46.  
  47.             if (amount_min > 0) {
  48.                 if (amount_min && val <= amount_min) {
  49.                     input.val(amount_min);
  50.                 }
  51.             } else {
  52.                 if (val <= shop2.options.amountDefaultValue) {
  53.                     input.val(amountDefaultValue);
  54.                 }
  55.             }
  56.  
  57.         }
  58.  
  59.  
  60.     }
  61.  
  62.     $document.on('click', '.amount-minus', function() {
  63.         var $this = $(this),
  64.             text = $this.siblings('input:text'),
  65.             value = text.getVal(),
  66.             amount_min = parseFloat(text.data('min')),
  67.             multiplicity = parseFloat(text.data('multiplicity'));
  68.  
  69.         if (value) {
  70.             value = value[0];
  71.         }
  72.  
  73.         if (amount_min && value <= amount_min) {
  74.             return;
  75.         }
  76.  
  77.         value = checkAmount(value, amount_min, multiplicity, -1);
  78.  
  79.         if (amount_min > 0) {
  80.             if (value <= amount_min) {
  81.                 value = amount_min;
  82.             }
  83.         } else {
  84.             if (value <= shop2.options.amountDefaultValue) {
  85.                 value = shop2.options.amountDefaultValue;
  86.             }
  87.         }
  88.  
  89.  
  90.  
  91.         text.val(value);
  92.         text.trigger('change');
  93.     });
  94.     $document.on('click', '.amount-plus', function() {
  95.         var $this = $(this),
  96.             text = $this.siblings('input:text'),
  97.             value = text.getVal(),
  98.             amount_min = parseFloat(text.data('min')),
  99.             multiplicity = parseFloat(text.data('multiplicity'));
  100.  
  101.         if (value) {
  102.             value = value[0];
  103.         }
  104.  
  105.         value = checkAmount(value, amount_min, multiplicity, 1);
  106.         text.val(value);
  107.         text.trigger('change');
  108.     });
  109.  
  110.     function checkAmount(amount, amount_min, multiplicity, sign) {
  111.  
  112.         if (multiplicity > 0) {
  113.             amount += multiplicity * sign;
  114.         } else {
  115.             amount += shop2.options.amountDefaultInc * sign;
  116.         }
  117.  
  118.         amount = amount.toFixed(4) - 0;
  119.  
  120.         return amount
  121.     }
  122.  
  123.     $document.on('change', '.shop2-product-amount input:text', function() {
  124.         var $this = $(this);
  125.         validate($this);
  126.     });
  127.  
  128.     $document.keyFilter('.shop2-product-amount input:text', {
  129.         type: shop2.options.amountType
  130.     });
  131.  
  132. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement