Blame view

frontend/web/js/product.js 3.84 KB
ec957aa4   Volodymyr   product modal js
1
2
  var phoneInput = document.querySelector('#cardProduct [type="tel"]');
  var counter = document.querySelector('#cardProduct .product-counter__input');
79137940   Volodymyr   fix product modal
3
  var price = 0;
e582a8c8   Volodymyr   feedback message ...
4
5
6
7
8
9
10
11
12
  $('#order-product').click(function (e){
      e.preventDefault();
      var message='Сума: ';
      message += $('#cardProduct .product-counter__value').text();
      message +='\nКод товара: ' + $('#product-code').val();
      message +='\nКількість: ' + counter.querySelector('input').value;
      console.log(message + phoneInput.value);
      var phone = '+38' + phoneInput.value;
      if(phone.length===17){
f3a5cf05   Volodymyr   feedback message ...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
          $.ajax({
              type: "POST",
              url: '/site/feedback',
              data:
                  {'Feedback': {
                          'phone': phone,
                          'message': message,
                          'topic': 'product',
                          'name': phone,
                          'returnUrl': '/'
                      }
                  },
              success: function(data) {
                  if (data.success) {
                      success();
                  } else {
                      alert(data.error)
                  }
              },
              error: function() {
                  alert('Achtung!');
              }
          });
e582a8c8   Volodymyr   feedback message ...
36
37
38
          console.log('good')
      }
  })
ec957aa4   Volodymyr   product modal js
39
40
41
  if (phoneInput) {
      phoneInput.addEventListener('input', phoneHandleInput);
  }
7445c339   Volodymyr   product modal js
42
  $('#cardProduct').on('show.bs.modal', function (e) {
79137940   Volodymyr   fix product modal
43
44
45
46
47
48
      var button = $(e.relatedTarget);
      price = button.data('price');
      $('#cardProduct .product-counter__value').data('price', price);
      var itemPrice = counter.parentElement.querySelector('.product-counter__value');
      itemPrice.innerHTML = `${price} ${itemPrice.dataset.currency}`;
      counter.querySelector('input').value = 1;
e582a8c8   Volodymyr   feedback message ...
49
50
51
      var sku = button.data('sku');
      $('#product-code').val(sku);
      $('#cardProduct .product-code').text('Код товара ' + sku);
7445c339   Volodymyr   product modal js
52
  })
e582a8c8   Volodymyr   feedback message ...
53
  
79137940   Volodymyr   fix product modal
54
55
  if(counter){
    counterFunc(counter);
ec957aa4   Volodymyr   product modal js
56
57
  }
  
ec957aa4   Volodymyr   product modal js
58
59
60
61
62
63
64
  function phoneHandleInput(e) {
      e.target.value = phoneMasks(e.target.value);
  }
  
  function phoneMasks(phone) {
      return phone.replace(/\D/g, '')
          .replace(/^(\d)/, '($1')
e582a8c8   Volodymyr   feedback message ...
65
          .replace(/^(\(\d{3})(\d)/, '$1)$2')
ec957aa4   Volodymyr   product modal js
66
67
68
69
70
71
          .replace(/(\d{3})(\d{1,7})/, '$1-$2')
          .replace(/(-\d{2})(\d{1,7})/, '$1-$2')
          .replace(/(-\d{2})\d+?$/, '$1');
  }
  
  function counterFunc(counter) {
79137940   Volodymyr   fix product modal
72
73
      var itemPrice = counter.parentElement.querySelector('.product-counter__value');
      itemPrice.innerHTML = `${price} ${itemPrice.dataset.currency}`;
ec957aa4   Volodymyr   product modal js
74
75
76
77
78
79
80
81
82
83
84
85
86
  
      counter.querySelector('input').value = 1;
      counter.addEventListener('input', counterHandleInput);
      counter.addEventListener('change', counterHandleChange);
      counter.querySelector('.product-counter__increment').addEventListener('click', increment);
      counter.querySelector('.product-counter__decrement').addEventListener('click', decrement);
  
      function counterHandleInput(e) {
          e.target.value = e.target.value.replace(/\D/g, '');
          updateSummary();
      }
  
      function counterHandleChange(e) {
79137940   Volodymyr   fix product modal
87
          var value = e.target.value.replace(/\D/g, '');
ec957aa4   Volodymyr   product modal js
88
89
90
91
92
93
94
95
          if (value == 0) {
              e.target.value = 1;
          }
          updateSummary();
      }
  
      function increment(e) {
          e.preventDefault();
79137940   Volodymyr   fix product modal
96
          var value = Number(counter.querySelector('input').value);
ec957aa4   Volodymyr   product modal js
97
          counter.querySelector('input').value = value + 1;
ec957aa4   Volodymyr   product modal js
98
99
100
101
102
          updateSummary();
      }
  
      function decrement(e) {
          e.preventDefault();
79137940   Volodymyr   fix product modal
103
          var value = Number(counter.querySelector('input').value);
ec957aa4   Volodymyr   product modal js
104
105
106
107
108
109
110
          if (value > 1) {
              counter.querySelector('input').value = value - 1;
              updateSummary();
          }
      }
  
      function updateSummary() {
79137940   Volodymyr   fix product modal
111
          var amount = counter.querySelector('input').value;
ec957aa4   Volodymyr   product modal js
112
          if (amount >= 1) {
79137940   Volodymyr   fix product modal
113
114
115
              var oldSum = Number(price);
              var newSum = String(oldSum * amount);
              itemPrice.innerHTML = `${newSum} ${itemPrice.dataset.currency}`;
ec957aa4   Volodymyr   product modal js
116
117
118
          }
      }
  }