-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.nice-number.js
184 lines (167 loc) · 4.52 KB
/
jquery.nice-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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
(function ($) {
$.fn.niceNumber = function (options) {
var defaults = {
autoSize: true,
autoSizeBuffer: 1,
buttonDecrement: '-',
buttonIncrement: '+',
buttonPosition: 'around',
/**
callbackFunction
@param {$input} currentInput - the input running the callback
@param {number} amount - the amount after increase/decrease
@param {object} settings - the passed niceNumber settings
**/
onDecrement: false,
onIncrement: false,
};
var settings = $.extend(defaults, options);
return this.each(function () {
var currentInput = this,
$currentInput = $(currentInput),
maxValue = $currentInput.attr('max'),
minValue = $currentInput.attr('min'),
attrMax = null,
attrMin = null;
// Skip already initialized input
if ($currentInput.attr('data-nice-number-initialized')) return;
// Handle max and min values
if(currentInput.value >100){
currentInput.value=100;
}
else if(currentInput.value <1){
currentInput.value = 1;
}
if (
maxValue !== 101 &&
maxValue !== false
) {
attrMax = parseFloat(maxValue);
}
if (
minValue !== 0 &&
minValue !== false
) {
attrMin = parseFloat(minValue);
}
// Fix issue with initial value being < min
if (attrMin && !currentInput.value) {
$currentInput.val(attrMin);
}
// Generate container
var $inputContainer = $('<div/>', {
class: 'nice-number',
}).insertAfter(currentInput);
// Generate interval (object so it is passed by reference)
var interval = {};
// Generate buttons
var $minusButton = $('<button/>')
.attr('type', 'button')
.html(settings.buttonDecrement)
.on('mousedown mouseup mouseleave', function (event) {
changeInterval(event.type, interval, function () {
var currentValue = parseFloat($currentInput.val() || 0);
if (
attrMin == null ||
attrMin < currentValue
) {
var newValue = currentValue - 1;
$currentInput.val(newValue);
if (settings.onDecrement) {
settings.onDecrement(
$currentInput,
newValue,
settings
);
}
}
});
// Trigger the input event here to avoid event spam
if (event.type == 'mouseup' || event.type == 'mouseleave') {
$currentInput.trigger('input');
}
});
var $plusButton = $('<button/>')
.attr('type', 'button')
.html(settings.buttonIncrement)
.on('mousedown mouseup mouseleave', function (event) {
changeInterval(event.type, interval, function () {
var currentValue = parseFloat($currentInput.val() || 0);
if (
attrMax == null ||
attrMax > currentValue
) {
var newValue = currentValue + 1;
$currentInput.val(newValue);
if (settings.onIncrement) {
settings.onIncrement(
$currentInput,
newValue,
settings
);
}
}
});
// Trigger the input event here to avoid event spam
if (event.type == 'mouseup' || event.type == 'mouseleave') {
$currentInput.trigger('input');
}
});
// Remember that we have initialized this input
$currentInput.attr('data-nice-number-initialized', true);
// Append elements
switch (settings.buttonPosition) {
case 'left':
$minusButton.appendTo($inputContainer);
$plusButton.appendTo($inputContainer);
$currentInput.appendTo($inputContainer);
break;
case 'right':
$currentInput.appendTo($inputContainer);
$minusButton.appendTo($inputContainer);
$plusButton.appendTo($inputContainer);
break;
case 'around':
default:
$minusButton.appendTo($inputContainer);
$currentInput.appendTo($inputContainer);
$plusButton.appendTo($inputContainer);
break;
}
// Nicely size input
if (settings.autoSize) {
$currentInput.width(
$currentInput.val().length + settings.autoSizeBuffer + 'ch'
);
$currentInput.on('keyup input', function () {
$currentInput.animate(
{
width:
$currentInput.val().length +
settings.autoSizeBuffer +
'ch'
},
200
);
});
}
});
};
function changeInterval(eventType, interval, callback) {
if (eventType == 'mousedown') {
interval.timeout = setTimeout(function () {
interval.actualInterval = setInterval(function () {
callback();
}, 100);
}, 200);
callback();
} else {
if (interval.timeout) {
clearTimeout(interval.timeout);
}
if (interval.actualInterval) {
clearInterval(interval.actualInterval);
}
}
}
})(jQuery);