Test Failed
Push — master ( 5d1429...25dee5 )
by Alexey
04:54
created

inji.Ecommerce.toggleFav   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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