Completed
Branch master (ebb499)
by Alexey
04:15
created

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

Complexity

Total Complexity 32
Complexity/F 2.46

Size

Lines of Code 133
Function Count 13

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 49
dl 0
loc 133
rs 9.6
c 0
b 0
f 0
wmc 32
mnd 4
bc 27
fnc 13
bpm 2.0769
cpm 2.4615
noi 1

2 Functions

Rating   Name   Duplication   Size   Complexity  
B inji.onLoad 0 78 1
A cart.js ➔ ??? 0 53 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").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
}
59
inji.onLoad(function () {
60
61
  //plugin bootstrap minus and plus
62
  //http://jsfiddle.net/laelitenetwork/puJ6G/
63
  $('body').on('click', '.btn-number', function (e) {
64
    e.preventDefault();
65
66
    var fieldName = $(this).data('field');
67
    var type = $(this).data('type');
68
    var input = $("input[name='" + fieldName + "']");
69
    var currentVal = parseFloat(input.val());
70
    if (!isNaN(currentVal)) {
71
      if (type == 'minus') {
72
73
        if (currentVal > input.attr('min')) {
74
          input.val(currentVal - 1).change();
75
        }
76
        if (parseFloat(input.val()) == input.attr('min')) {
77
          $(this).attr('disabled', true);
78
        }
79
80
      } else if (type == 'plus') {
81
82
        if (currentVal < input.attr('max')) {
83
          input.val(currentVal + 1).change();
84
        }
85
        if (parseFloat(input.val()) == input.attr('max')) {
86
          $(this).attr('disabled', true);
87
        }
88
89
      }
90
    } else {
91
      input.val(0);
92
    }
93
  });
94
  $('body').on('focusin', '.input-number', function () {
95
    $(this).data('oldValue', $(this).val());
96
  });
97
  $('body').on('change', '.input-number', function () {
98
99
    var minValue = parseFloat($(this).attr('min'));
100
    var maxValue = parseFloat($(this).attr('max'));
101
    var valueCurrent = parseFloat($(this).val());
102
103
    var name = $(this).attr('name');
104
    if (valueCurrent >= minValue) {
105
      $(".btn-number[data-type='minus'][data-field='" + name + "']").removeAttr('disabled')
106
    } else {
107
      alert('Нельзя заказать меньше ' + minValue);
108
      $(this).val($(this).data('oldValue'));
109
    }
110
    if (valueCurrent <= maxValue) {
111
      $(".btn-number[data-type='plus'][data-field='" + name + "']").removeAttr('disabled')
112
    } else {
113
      alert('Извините, но больше нету');
114
      $(this).val($(this).data('oldValue'));
115
    }
116
117
118
  });
119
120
  $('body').on('keydown', ".input-number", function (e) {
121
    // Allow: backspace, delete, tab, escape, enter and .
122
    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190]) !== -1 ||
123
            // Allow: Ctrl+A
124
                    (e.keyCode == 65 && e.ctrlKey === true) ||
125
                    // Allow: home, end, left, right
126
                            (e.keyCode >= 35 && e.keyCode <= 39)) {
127
              // let it happen, don't do anything
128
              return;
129
            }
130
            // Ensure that it is a number and stop the keypress
131
            if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
132
              e.preventDefault();
133
            }
134
          });
135
136
})
137