Igor150195

Обновили кратность.

Apr 21st, 2020
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 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(2) - 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(2) - 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.         // Если пользователь сделает некорректный ввод числа, то цифра должна изменяться в числовом окне в соответствии с кратным числом
  111.         // (система должна автоматически изменить его на ближайшее или на минимальное к указанному),
  112.    
  113.         function checkAmount(amount, amount_min, multiplicity, sign) {
  114.  
  115.             if (multiplicity > 0) {
  116.                 amount += multiplicity * sign;
  117.             } else {
  118.                 amount += shop2.options.amountDefaultInc * sign;
  119.             }
  120.            
  121.             amount = amount.toFixed(2) - 0;
  122.  
  123.             return amount
  124.         }
  125.    
  126.         $document.on('change', '.shop2-product-amount input:text', function() {
  127.             var $this = $(this);
  128.             validate($this);
  129.         });
  130.        
  131.         $document.keyFilter('.shop2-product-amount input:text', {
  132.             type: shop2.options.amountType
  133.         });
  134.  
  135.     },
Add Comment
Please, Sign In to add comment