fuelux.spinner.js
10.8 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
* Fuel UX Spinbox
* https://github.com/ExactTarget/fuelux
*
* Copyright (c) 2014 ExactTarget
* Licensed under the BSD New license.
*/
// -- BEGIN UMD WRAPPER PREFACE --
// For more information on UMD visit:
// https://github.com/umdjs/umd/blob/master/jqueryPlugin.js
(function (factory) {
if (typeof define === 'function' && define.amd) {
// if AMD loader is available, register as an anonymous module.
define(['jquery'], factory);
} else {
// OR use browser globals if AMD is not present
factory(jQuery);
}
}(function ($, undefined) {
// -- END UMD WRAPPER PREFACE --
// -- BEGIN MODULE CODE HERE --
var old = $.fn.spinbox;
// SPINBOX CONSTRUCTOR AND PROTOTYPE
var Spinbox = function (element, options) {
this.$element = $(element);
this.options = $.extend({}, $.fn.spinbox.defaults, options);
this.$input = this.$element.find('.spinbox-input');
this.$element.on('focusin.fu.spinbox', this.$input, $.proxy(this.changeFlag, this));
this.$element.on('focusout.fu.spinbox', this.$input, $.proxy(this.change, this));
this.$element.on('keydown.fu.spinbox', this.$input, $.proxy(this.keydown, this));
this.$element.on('keyup.fu.spinbox', this.$input, $.proxy(this.keyup, this));
this.bindMousewheelListeners();
this.mousewheelTimeout = {};
if (this.options.hold) {
this.$element.on('mousedown.fu.spinbox', '.spinbox-up', $.proxy(function() { this.startSpin(true); } , this));
this.$element.on('mouseup.fu.spinbox', '.spinbox-up, .spinbox-down', $.proxy(this.stopSpin, this));
this.$element.on('mouseout.fu.spinbox', '.spinbox-up, .spinbox-down', $.proxy(this.stopSpin, this));
this.$element.on('mousedown.fu.spinbox', '.spinbox-down', $.proxy(function() {this.startSpin(false);} , this));
} else {
this.$element.on('click.fu.spinbox', '.spinbox-up', $.proxy(function() { this.step(true); } , this));
this.$element.on('click.fu.spinbox', '.spinbox-down', $.proxy(function() { this.step(false); }, this));
}
this.switches = {
count: 1,
enabled: true
};
if (this.options.speed === 'medium') {
this.switches.speed = 300;
} else if (this.options.speed === 'fast') {
this.switches.speed = 100;
} else {
this.switches.speed = 500;
}
this.lastValue = this.options.value;
this.render();
if (this.options.disabled) {
this.disable();
}
};
Spinbox.prototype = {
constructor: Spinbox,
destroy: function() {
this.$element.remove();
// any external bindings
// [none]
// set input value attrbute
this.$element.find('input').each(function() {
$(this).attr('value', $(this).val());
});
// empty elements to return to original markup
// [none]
// returns string of markup
return this.$element[0].outerHTML;
},
render: function () {
var inputValue = this.parseInput( this.$input.val() );
var maxUnitLength = '';
// if input is empty and option value is default, 0
if (inputValue !== '' && this.options.value === 0) {
this.value(inputValue);
} else {
this.output ( this.options.value );
}
if ( this.options.units.length ) {
$.each(this.options.units, function(index, value){
if( value.length > maxUnitLength.length) {
maxUnitLength = value;
}
});
}
},
output: function(value, updateField) {
value = (value + '').split('.').join(this.options.decimalMark);
updateField = ( updateField || true );
if ( updateField ) { this.$input.val(value); }
return value;
},
parseInput: function(value) {
value = (value + '').split(this.options.decimalMark).join('.');
return value;
},
change: function () {
var newVal = this.parseInput( this.$input.val() ) || '';
if(this.options.units.length || this.options.decimalMark !== '.'){
newVal = this.parseValueWithUnit(newVal);
} else if (newVal/1){
newVal = this.options.value = this.checkMaxMin(newVal/1);
} else {
newVal = this.checkMaxMin(newVal.replace(/[^0-9.-]/g,'') || '');
this.options.value = newVal/1;
}
this.output ( newVal );
this.changeFlag = false;
this.triggerChangedEvent();
},
changeFlag: function(){
this.changeFlag = true;
},
stopSpin: function () {
if(this.switches.timeout!==undefined){
clearTimeout(this.switches.timeout);
this.switches.count = 1;
this.triggerChangedEvent();
}
},
triggerChangedEvent: function () {
var currentValue = this.value();
if (currentValue === this.lastValue) return;
this.lastValue = currentValue;
// Primary changed event
this.$element.trigger('changed.fu.spinbox', this.output(currentValue, false)); // no DOM update
},
startSpin: function (type) {
if (!this.options.disabled) {
var divisor = this.switches.count;
if (divisor === 1) {
this.step(type);
divisor = 1;
} else if (divisor < 3){
divisor = 1.5;
} else if (divisor < 8){
divisor = 2.5;
} else {
divisor = 4;
}
this.switches.timeout = setTimeout($.proxy(function() {this.iterate(type);} ,this),this.switches.speed/divisor);
this.switches.count++;
}
},
iterate: function (type) {
this.step(type);
this.startSpin(type);
},
step: function (isIncrease) {
// isIncrease: true is up, false is down
var digits, multiple, currentValue, limitValue;
// trigger change event
if( this.changeFlag ) {
this.change();
}
// get current value and min/max options
currentValue = this.options.value;
limitValue = isIncrease ? this.options.max : this.options.min;
if ((isIncrease ? currentValue < limitValue : currentValue > limitValue)) {
var newVal = currentValue + (isIncrease ? 1 : -1) * this.options.step;
// raise to power of 10 x number of decimal places, then round
if(this.options.step % 1 !== 0){
digits = (this.options.step + '').split('.')[1].length;
multiple = Math.pow(10, digits);
newVal = Math.round(newVal * multiple) / multiple;
}
// if outside limits, set to limit value
if (isIncrease ? newVal > limitValue : newVal < limitValue) {
this.value(limitValue);
}
else {
this.value(newVal);
}
}
else if (this.options.cycle) {
var cycleVal = isIncrease ? this.options.min : this.options.max;
this.value(cycleVal);
}
},
value: function (value) {
if ( value || value === 0 ) {
if( this.options.units.length || this.options.decimalMark !== '.' ) {
this.output( this.parseValueWithUnit( value + (this.unit || '') ) );
return this;
} else if ( !isNaN(parseFloat(value)) && isFinite(value) ) {
this.options.value = value/1;
this.output ( value + (this.unit ? this.unit : '') ) ;
return this;
}
} else {
if( this.changeFlag ) {
this.change();
}
if( this.unit ){
return this.options.value + this.unit;
} else {
return this.output(this.options.value, false); // no DOM update
}
}
},
isUnitLegal: function (unit) {
var legalUnit;
$.each(this.options.units, function(index, value){
if( value.toLowerCase() === unit.toLowerCase()){
legalUnit = unit.toLowerCase();
return false;
}
});
return legalUnit;
},
// strips units and add them back
parseValueWithUnit: function( value ){
var unit = value.replace(/[^a-zA-Z]/g,'');
var number = value.replace(/[^0-9.-]/g,'');
if(unit){
unit = this.isUnitLegal(unit);
}
this.options.value = this.checkMaxMin(number/1);
this.unit = unit || undefined;
return this.options.value + (unit || '');
},
checkMaxMin: function(value){
// if unreadable
if ( isNaN( parseFloat(value) ) ) {
return value;
}
// if not within range return the limit
if ( !( value <= this.options.max && value >= this.options.min ) ){
value = value >= this.options.max ? this.options.max : this.options.min;
}
return value;
},
disable: function () {
this.options.disabled = true;
this.$element.addClass('disabled');
this.$input.attr('disabled','');
this.$element.find('button').addClass('disabled');
},
enable: function () {
this.options.disabled = false;
this.$element.removeClass('disabled');
this.$input.removeAttr("disabled");
this.$element.find('button').removeClass('disabled');
},
keydown: function(event){
var keyCode = event.keyCode;
if(keyCode===38){
this.step(true);
}else if(keyCode===40){
this.step(false);
}
},
keyup: function(event){
var keyCode = event.keyCode;
if(keyCode===38 || keyCode===40){
this.triggerChangedEvent();
}
},
bindMousewheelListeners: function(){
var inputEl = this.$input.get(0);
if(inputEl.addEventListener){
//IE 9, Chrome, Safari, Opera
inputEl.addEventListener('mousewheel', $.proxy(this.mousewheelHandler, this), false);
// Firefox
inputEl.addEventListener('DOMMouseScroll', $.proxy(this.mousewheelHandler, this), false);
}else{
// IE <9
inputEl.attachEvent('onmousewheel', $.proxy(this.mousewheelHandler, this));
}
},
mousewheelHandler: function(event){
var e = window.event || event; // old IE support
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
var self = this;
clearTimeout(this.mousewheelTimeout);
this.mousewheelTimeout = setTimeout(function(){
self.triggerChangedEvent();
}, 300);
if(delta>0){//ACE
this.step(true);
}else{
this.step(false);
}
if(e.preventDefault){
e.preventDefault();
}else{
e.returnValue = false;
}
return false;
}
};
// SPINBOX PLUGIN DEFINITION
$.fn.spinbox = function (option) {
var args = Array.prototype.slice.call( arguments, 1 );
var methodReturn;
var $set = this.each(function () {
var $this = $( this );
var data = $this.data('fu.spinbox');
var options = typeof option === 'object' && option;
if( !data ) {
$this.data('fu.spinbox', (data = new Spinbox( this, options ) ) );
}
if( typeof option === 'string' ) {
methodReturn = data[ option ].apply( data, args );
}
});
return ( methodReturn === undefined ) ? $set : methodReturn;
};
// value needs to be 0 for this.render();
$.fn.spinbox.defaults = {
value: 0,
min: 0,
max: 999,
step: 1,
hold: true,
speed: 'medium',
disabled: false,
cycle: false,
units: [],
decimalMark: '.'
};
$.fn.spinbox.Constructor = Spinbox;
$.fn.spinbox.noConflict = function () {
$.fn.spinbox = old;
return this;
};
// DATA-API
$(document).on('mousedown.fu.spinbox.data-api', '[data-initialize=spinbox]', function (e) {
var $control = $(e.target).closest('.spinbox');
if ( !$control.data('fu.spinbox') ) {
$control.spinbox($control.data());
}
});
// Must be domReady for AMD compatibility
$(function () {
$('[data-initialize=spinbox]').each(function () {
var $this = $(this);
if (!$this.data('fu.spinbox')) {
$this.spinbox($this.data());
}
});
});
// -- BEGIN UMD WRAPPER AFTERWORD --
}));
// -- END UMD WRAPPER AFTERWORD --