Test Failed
Push — master ( 84d0c3...657dff )
by Alexey
05:26
created

system/modules/Ecommerce/static/js/cart.js   A

Complexity

Total Complexity 33
Complexity/F 2.36

Size

Lines of Code 138
Function Count 14

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 49
dl 0
loc 138
rs 9.3999
c 0
b 0
f 0
wmc 33
mnd 4
bc 28
fnc 14
bpm 2
cpm 2.3571
noi 1

3 Functions

Rating   Name   Duplication   Size   Complexity  
A cart.js ➔ ??? 0 53 1
B inji.onLoad 0 78 1
A inji.Ecommerce.toggleFav 0 5 1
1
/**
2
 * Ecommerce Classes
3
 */
4
inji.Ecommerce = {
5
  Cart: new function () {
6
    this.addItem = function (itemOfferPriceId, count) {
7
      inji.Server.request({
8
        url: 'ecommerce/cart/add',
9
        data: {
10
          itemOfferPriceId: itemOfferPriceId,
11
          count: count,
12
        },
13
        success: function (data) {
14
          console.log(data);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
15
          inji.Server.request({
16
            url: 'ecommerce/cart/getCart',
17
            success: function (data) {
18
              $("#cart,.cartplace").html(data);
19
            }
20
          });
21
        }
22
      });
23
    }
24
    this.calcSum = function () {
25
      var form = $('.ecommerce .cart-order_page form');
26
      var formData = new FormData(form[0]);
27
      $('.ecommerce .cart-order_page').prepend($('<div style = "position:absolute;width:' + $('.ecommerce .cart-order_page').width() + 'px;height:' + $('.ecommerce .cart-order_page').height() + 'px;background-color: rgba(255, 255, 255, 0.4);z-index:1000000"></div>'));
28
      inji.Server.request({
29
        url: form.attr('action'),
30
        type: 'POST',
31
        data: formData,
32
        dataType: 'html',
33
        processData: false,
34
        success: function (data) {
35
          var html = $('<div>' + data.replace(/\n/g, " ") + '</div>');
36
          $('.ecommerce .cart-order_page').html(html.find('.ecommerce .cart-order_page'));
37
        }
38
      });
39
    }
40
    this.delItem = function (cart_item_id) {
41
      var form = $('.ecommerce .cart-order_page form');
42
      $('.cart_item_id' + cart_item_id).remove();
43
      var formData = new FormData(form[0]);
44
      $('.ecommerce .cart-order_page').prepend($('<div style = "position:absolute;width:' + $('.ecommerce .cart-order_page').width() + 'px;height:' + $('.ecommerce .cart-order_page').height() + 'px;background-color: rgba(255, 255, 255, 0.4);z-index:1000000"></div>'));
45
      inji.Server.request({
46
        url: form.attr('action'),
47
        type: 'POST',
48
        data: formData,
49
        dataType: 'html',
50
        processData: false,
51
        success: function (data) {
52
          var html = $('<div>' + data.replace(/\n/g, " ") + '</div>');
53
          $('.ecommerce .cart-order_page').html(html.find('.ecommerce .cart-order_page'));
54
        }
55
      });
56
    }
57
  },
58
  toggleFav: function (itemId, btn) {
59
    inji.Server.request({
60
      url: 'ecommerce/toggleFav/' + itemId,
61
    }, btn);
62
  }
63
}
64
inji.onLoad(function () {
65
66
  //plugin bootstrap minus and plus
67
  //http://jsfiddle.net/laelitenetwork/puJ6G/
68
  $('body').on('click', '.btn-number', function (e) {
69
    e.preventDefault();
70
71
    var fieldName = $(this).data('field');
72
    var type = $(this).data('type');
73
    var input = $("input[name='" + fieldName + "']");
74
    var currentVal = parseFloat(input.val());
75
    if (!isNaN(currentVal)) {
76
      if (type == 'minus') {
77
78
        if (currentVal > input.attr('min')) {
79
          input.val(currentVal - 1).change();
80
        }
81
        if (parseFloat(input.val()) == input.attr('min')) {
82
          $(this).attr('disabled', true);
83
        }
84
85
      } else if (type == 'plus') {
86
87
        if (currentVal < input.attr('max')) {
88
          input.val(currentVal + 1).change();
89
        }
90
        if (parseFloat(input.val()) == input.attr('max')) {
91
          $(this).attr('disabled', true);
92
        }
93
94
      }
95
    } else {
96
      input.val(0);
97
    }
98
  });
99
  $('body').on('focusin', '.input-number', function () {
100
    $(this).data('oldValue', $(this).val());
101
  });
102
  $('body').on('change', '.input-number', function () {
103
104
    var minValue = parseFloat($(this).attr('min'));
105
    var maxValue = parseFloat($(this).attr('max'));
106
    var valueCurrent = parseFloat($(this).val());
107
108
    var name = $(this).attr('name');
109
    if (valueCurrent >= minValue) {
110
      $(".btn-number[data-type='minus'][data-field='" + name + "']").removeAttr('disabled')
111
    } else {
112
      alert('Нельзя заказать меньше ' + minValue);
113
      $(this).val($(this).data('oldValue'));
114
    }
115
    if (valueCurrent <= maxValue) {
116
      $(".btn-number[data-type='plus'][data-field='" + name + "']").removeAttr('disabled')
117
    } else {
118
      alert('Извините, но больше нету');
119
      $(this).val($(this).data('oldValue'));
120
    }
121
122
123
  });
124
125
  $('body').on('keydown', ".input-number", function (e) {
126
    // Allow: backspace, delete, tab, escape, enter and .
127
    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190]) !== -1 ||
128
            // Allow: Ctrl+A
129
                    (e.keyCode == 65 && e.ctrlKey === true) ||
130
                    // Allow: home, end, left, right
131
                            (e.keyCode >= 35 && e.keyCode <= 39)) {
132
              // let it happen, don't do anything
133
              return;
134
            }
135
            // Ensure that it is a number and stop the keypress
136
            if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
137
              e.preventDefault();
138
            }
139
          });
140
141
})
142