-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.stepper.js
179 lines (146 loc) · 5.03 KB
/
jquery.stepper.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
// jquery.stepper.js
// https://github.com/ncou/jquery.stepper.basic
// ------------------------------------------------------
// Author: NCOU
//
// the semi-colon before function invocation is a safety net against concatenated
// scripts and/or other plugins which may not be closed properly.
(function($, window, document, undefined) {
"use strict";
var pluginName = "stepper",
defaults = {
selectorProgressBar: ".stepper-progress",
selectorInputNumber: ".stepper-number",
classNameChanging: "is-changing",
value: "",
decimals: 0,
min: 0,
max: 100,
step: 1,
unit: "%"
};
// The actual plugin constructor
function Plugin(element, options) {
this.element = element;
this.settings = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
// Avoid Plugin.prototype conflicts
$.extend(Plugin.prototype, {
init: function() {
// local variable
this._curDown = false;
this._mouseDownX = 0;
this._mouseDownValue = 0;
// Cache elements
this.$el = $(this.element);
this.$input = this.$el.find(this.settings.selectorInputNumber);
this.$progress = this.$el.find(this.settings.selectorProgressBar);
// init values
this.decimals = this.$input.attr("decimals") || this.settings.decimals;
this.min = this.$input.attr("min") || this.settings.min;
this.max = this.$input.attr("max") || this.settings.max;
this.step = this.$input.attr("step") || this.settings.step;
this.unit = this.$input[0].hasAttribute("unit")
? this.$input.attr("unit")
: this.settings.unit;
this.value =
this.$input.val() ||
(this.settings.value !== "" ? this.settings.value : this.max);
this.setValue(this.value);
// Bind events
this.$input.on("keydown", this.onKeyPress.bind(this));
this.$input.on("blur", this.onBlur.bind(this));
this.$input.on("paste input", this.onChange.bind(this));
this.$el.on("mousedown touchstart", this.onMouseDown.bind(this));
this.$el.on("wheel", this.onMouseWheel.bind(this));
$(document).on("mouseup touchend", this.onMouseUp.bind(this));
$(document).on("mousemove touchmove", this.onMouseMove.bind(this));
},
onMouseDown: function(e) {
this._mouseDownX = e.clientX || e.originalEvent.touches[0].clientX;
this._mouseDownValue = this.getValue();
this._changeStart();
},
onMouseUp: function(e) {
this._changeEnd();
},
onMouseMove: function(e) {
if (this._curDown === true) {
var t =
(e.clientX || e.originalEvent.touches[0].clientX) - this._mouseDownX;
this.setValue(this._mouseDownValue + t * this.step);
}
},
onMouseWheel: function(e) {
// prevent [wheel increase and mousemove increase] and [wheel increase] if the input field is not focused
if (this._curDown === false && this.$input.is(":focus")) {
e.preventDefault();
// delta is 1 if scroll up, or -1 if scroll down
var d = e.originalEvent.deltaY < 0 ? 1 : -1;
this.setValue(this.getValue() + d * this.step);
}
},
onChange: function(e) {
this._updateProgress(this.getValue());
},
onBlur: function(e) {
this._changeEnd();
this.setValue(this.getValue());
},
onKeyPress: function(e) {
// exit the input field if key pressed is 'Enter'
if (e.keyCode === 13) {
this.$input.blur();
}
},
getValue: function() {
return parseFloat(this.$input.val()) || 0;
},
setValue: function(amount) {
var value;
value = Math.max(Math.min(amount, this.max), this.min);
value = this._roundValue(value);
var n = value;
n = n.toFixed(this.decimals);
n += this.unit;
this.$input.val(n);
this._updateProgress(value);
},
_updateProgress: function(v) {
var r = this._valueToPercent(v) / 100;
this.$progress.css("transform", "scaleX(" + r + ")");
this.$input.trigger("change");
},
_percentToValue: function(v) {
return this.min + v / 100 * (this.max - this.min);
},
_valueToPercent: function(v) {
var t = (v - this.min) / (this.max - this.min) * 100;
return Math.max(Math.min(t, 100), 0);
},
_roundValue: function(v) {
var maxDecimals = 2;
var t = Math.pow(10, maxDecimals);
return Math.round(v * t) / t;
},
_changeStart: function() {
this._curDown = true;
this.$el.addClass(this.settings.classNameChanging);
},
_changeEnd: function() {
this._curDown = false;
this.$el.removeClass(this.settings.classNameChanging);
}
});
// A lightweight plugin wrapper around the constructor, preventing against multiple instantiations
$.fn[pluginName] = function(options) {
return this.each(function() {
if (!$.data(this, "plugin-" + pluginName)) {
$.data(this, "plugin-" + pluginName, new Plugin(this, options));
}
});
};
})(jQuery, window, document);