forked from llapgoch/jquery.stepper.widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstepper.widget.js
189 lines (155 loc) · 5.7 KB
/
stepper.widget.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
;(function($){
// Consts which can be ascertained by the update event.
// Show whether the value is different from the original
let CHANGE_TYPE_SAME = "same_value_entered";
let CHANGE_TYPE_DIFFERENT = "different_value_entered";
$.widget('llapgoch.stepper', {
options: {
// Used to store on the element's data attribute and event namespace
upSelector: '.js-qty-up',
downSelector: '.js-qty-down',
inputSelector: '.js-qty-input',
disabledClass:'disabled',
minDigit: 1,
maxQty: 999,
minQty: 0,
step: 1
},
originalValue: null,
value: 0,
_create: function(){
this._super();
this._addEvents();
let val = this._validateValue(this._getInput().val());
this.originalValue = val;
this.value = val;
this._setInputValue();
},
_destroy: function() {
this._removeEvents();
},
disable: function(){
this._super();
this._getInput().prop('disabled', 'disabled').addClass(this.options.disabledClass);
this._getDownElement().addClass(this.options.disabledClass);
this._getUpElement().addClass(this.options.disabledClass)
},
enable: function(){
this._super();
this._getInput().removeProp('disabled').addClass(this.options.disabledClass);
this._getDownElement().removeClass(this.options.disabledClass);
this._getUpElement().removeClass(this.options.disabledClass);
},
_validateValue: function(val){
val = parseFloat(val);
val = isNaN(val) ? 1 : val;
return Math.max(this.options.minQty, Math.min(val, this.options.maxQty));
},
_getEvents: function(){
let self = this;
let events = {};
events["click " + this.options.upSelector] = function(ev){
ev.preventDefault();
self.stepQuantity(self.options.step);
};
events["click " + this.options.downSelector] = function(ev){
ev.preventDefault();
self.stepQuantity(-self.options.step);
};
events["input " + this.options.inputSelector] = function(ev){
let val = self._getInput().val();
if(val !== "" && !isNaN(parseFloat(val))) {
self.updateQuantity(val);
}
};
events["blur " + this.options.inputSelector] = function(ev){
self.updateQuantity(self.value);
};
events["keydown " + this.options.inputSelector] = function(ev){
if(ev.keyCode === 38){
self.stepQuantity(self.options.step);
}
if(ev.keyCode === 40){
self.stepQuantity(-self.options.step);
}
};
events["wheel " + this.options.inputSelector] = function(ev){
let delta = Math.sign(ev.originalEvent.deltaY);
if(delta < 0){
self.stepQuantity(self.options.step);
} else {
self.stepQuantity(-self.options.step);
}
};
return events;
},
_addEvents: function(){
this._on(this.element, this._getEvents());
},
_removeEvents: function(){
let self = this;
let aEventName = Object.keys(this._getEvents());
$.each(aEventName, function(i, event){
self._off(self.element, event);
});
},
_fireEvent: function(eventName, event, data){
data = data || {};
this._trigger(eventName, event, $.extend({}, data, {
element: this.element
}));
},
_fireUpdate: function(ev){
let updateType = CHANGE_TYPE_SAME;
if(this.value !== this.originalValue){
updateType = CHANGE_TYPE_DIFFERENT;
}
this._fireEvent('update', ev, {
'value': this.value,
'updateType': updateType
});
},
_getInput: function(){
return $(this.options.inputSelector, this.element);
},
_getUpElement: function(){
return $(this.options.upSelector, this.element);
},
_getDownElement: function(){
return $(this.options.downSelector, this.element);
},
_setOption(key, value){
this._super(key, value);
if(key === 'minQty' || key === 'maxQty'){
this.value = this._validateValue(this.value);
this._setInputValue();
}
},
_setOptions: function( options ) {
let that = this;
$.each(options, function(key, value) {
that._setOption(key, value);
});
},
_setInputValue: function(){
let displayValue = this.value;
if(this.options.minDigit > 1){
displayValue = sprintf('%0'+this.options.minDigit+'d', displayValue);
}
this._getInput().val(displayValue);
},
updateQuantity: function(quantity, fireEvt = true) {
this.value = this._validateValue(quantity);
this._setInputValue();
if(fireEvt){
this._fireUpdate();
}
},
stepQuantity: function(value) {
this.updateQuantity(this.value + value);
},
getValue: function(){
return this.value;
}
});
}(jQuery));