-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.spinput.js
76 lines (61 loc) · 1.73 KB
/
jquery.spinput.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
// jQuery.spinput
// http://github.com/proc
(function() {
$.fn.spinput = function(options) {
var Spinput = function(element, options) {
this.settings = {
step: 1,
max: null,
min: null
};
$.extend(this.settings, options);
this.element = $(element);
this.initialize();
};
Spinput.prototype.initialize = function() {
var that = this;
this.element.on('keydown', function(event) {
switch( event.which ) {
case 38:
that.increment();
break;
case 40:
that.decrement();
break;
};
});
};
Spinput.prototype.clean = function(val) {
var cleaned_val = parseInt(this.element.val());
return (isNaN(cleaned_val) ? 0 : cleaned_val);
};
Spinput.prototype.update = function(optype) {
this.element.val(this.counter);
this.element.trigger('spinput-update', {
optype: optype
});
};
Spinput.prototype.increment = function() {
if(this.counter === this.settings.max) {
return;
}
this.counter = this.clean(this.element.val()) + this.settings.step;
if(this.settings.max && (this.counter >= this.settings.max)) {
this.counter = this.settings.max;
}
this.update('increment');
};
Spinput.prototype.decrement = function() {
if(this.counter === this.settings.min) {
return;
}
this.counter = this.clean(this.element.val()) - this.settings.step;
if(this.settings.min && (this.counter <= this.settings.min)) {
this.counter = this.settings.min;
}
this.update('decrement');
};
var spinput = new Spinput(this, options);
return this;
}
})();