-
Notifications
You must be signed in to change notification settings - Fork 0
/
develo-input-number.js
162 lines (126 loc) · 3.25 KB
/
develo-input-number.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
/**
* Simple jQuery plugin that adds buttons either side of the input to increase and decrease the quantity
*
* @author paul@develo
* @package develo-input-number
* @date 21/05/15
*/
+function( $ ) {
'use strict';
/**
* @param $el object containing "jQuerified" element
* @param options object
* @constructor
*/
var Input = function( $el, options ){
this.options = $.extend( {
className: 'develo-quantity-helper',
buttonClassName: 'button',
disabledClassName: 'disabled',
max: null,
min: null,
minusHtml: '-',
plusHtml: '+',
onDecreased: function( value ){},
onIncreased: function( value ){},
getInput: $el
}, options );
this.$input = $el;
this.render();
this.setupBindings();
};
/**
* Add the correct class and add the buttons to the element
*/
Input.prototype.render = function(){
this.$el = $( '<div/>' )
.addClass( this.options.className )
.insertBefore( this.$input );
this.$minus = $( '<a href="#"/>' )
.data( 'action', 'decrease' )
.html( this.options.minusHtml )
.addClass( this.options.buttonClassName )
.addClass( this.options.disabledClassName );
this.$plus = $( '<a href="#"/>' )
.data( 'action', 'increase' )
.html( this.options.plusHtml )
.addClass( this.options.buttonClassName );
this.$el.append( this.$minus, this.$input, this.$plus );
};
/**
* Setup our bindings
*/
Input.prototype.setupBindings = function(){
this.$minus.on( 'click', $.proxy( this.onButtonPress, this ) );
this.$plus.on( 'click', $.proxy( this.onButtonPress, this ) );
};
/**
* Called when a button has been pressed
*/
Input.prototype.onButtonPress = function( event ){
event.preventDefault();
var action = $( event.currentTarget ).data( 'action' );
action == 'increase' ?
this.increase():
this.decrease();
};
/**
* Increase the value by one
*/
Input.prototype.increase = function(){
this.set( this.get() +1 );
this.options.onIncreased( this.get() );
};
/**
* Decrease the value by one
*/
Input.prototype.decrease = function(){
this.set( this.get() -1 );
this.options.onDecreased( this.get() );
};
/**
* Get the input value
*
* @returns {number}
*/
Input.prototype.get = function(){
return parseInt( this.$input.val() );
};
/**
* Set the input value
* - Ensure the value is greater than the min
* - Ensure the new value is not larger max
*
* @param value
*/
Input.prototype.set = function( value ){
if( this.options.min != null && value <= this.options.min ) {
value = this.options.min;
this.$minus.addClass(this.options.disabledClassName);
} else {
this.$minus.removeClass(this.options.disabledClassName);
}
if( this.options.max && value > this.options.max ) {
value = this.options.max;
this.$plus.addClass(this.options.disabledClassName);
} else {
this.$plus.removeClass(this.options.disabledClassName);
}
this.$input.val( value );
this.$input.trigger( 'change' );
};
/**
* Create the jQuery plugin.
*
* @param options
*/
$.fn.develoQuantityHelper = function( options ){
this.each( function(){
var $el = $( this );
if( ! $el.data( 'develoInputNumber' ) ){
$el.data( 'develoInputNumber', new Input( $el, options ) );
}
});
return this;
};
}(jQuery);