Skip to content

Commit 1e82317

Browse files
authored
add wheel event + text input parameters
1 parent f0c4ac5 commit 1e82317

File tree

1 file changed

+54
-44
lines changed

1 file changed

+54
-44
lines changed

jquery.stepper.js

+54-44
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,19 @@
55
//
66
// the semi-colon before function invocation is a safety net against concatenated
77
// scripts and/or other plugins which may not be closed properly.
8-
;
98
(function($, window, document, undefined) {
10-
119
"use strict";
12-
1310
var pluginName = "stepper",
1411
defaults = {
15-
selectorProgressBar: '.stepper-progress',
16-
selectorInputNumber: '.stepper-number',
17-
classNameChanging: 'is-changing',
12+
selectorProgressBar: ".stepper-progress",
13+
selectorInputNumber: ".stepper-number",
14+
classNameChanging: "is-changing",
15+
value: "",
1816
decimals: 0,
19-
unit: '%',
20-
initialValue: '',
2117
min: 0,
2218
max: 100,
23-
stepSize: 1
19+
step: 1,
20+
unit: "%"
2421
};
2522

2623
// The actual plugin constructor
@@ -37,39 +34,45 @@
3734

3835
// Avoid Plugin.prototype conflicts
3936
$.extend(Plugin.prototype, {
40-
4137
init: function() {
42-
4338
// local variable
44-
this.curDown = false;
45-
this.mouseDownX = 0;
46-
this.mouseDownValue = 0;
39+
this._curDown = false;
40+
this._mouseDownX = 0;
41+
this._mouseDownValue = 0;
4742

4843
// Cache elements
4944
this.$el = $(this.element);
5045
this.$input = this.$el.find(this.settings.selectorInputNumber);
5146
this.$progress = this.$el.find(this.settings.selectorProgressBar);
5247

5348
// init values
54-
this.min = this.$input.attr('min') || this.settings.min;
55-
this.max = this.$input.attr('max') || this.settings.max;
49+
this.decimals = this.$input.attr("decimals") || this.settings.decimals;
50+
this.min = this.$input.attr("min") || this.settings.min;
51+
this.max = this.$input.attr("max") || this.settings.max;
52+
this.step = this.$input.attr("step") || this.settings.step;
53+
this.unit = this.$input[0].hasAttribute("unit")
54+
? this.$input.attr("unit")
55+
: this.settings.unit;
5656

57-
this.initialValue = this.$input.val() || (this.settings.initialValue !== "" ? this.settings.initialValue : this.max);
57+
this.value =
58+
this.$input.val() ||
59+
(this.settings.value !== "" ? this.settings.value : this.max);
5860

59-
this.setValue(this.initialValue);
61+
this.setValue(this.value);
6062

6163
// Bind events
62-
this.$input.on('keydown', this.onKeyPress.bind(this));
63-
this.$input.on('blur', this.onBlur.bind(this));
64-
this.$input.on('paste input', this.onChange.bind(this));
65-
this.$el.on('mousedown touchstart', this.onMouseDown.bind(this));
66-
$(document).on('mouseup touchend', this.onMouseUp.bind(this));
67-
$(document).on('mousemove touchmove', this.onMouseMove.bind(this));
64+
this.$input.on("keydown", this.onKeyPress.bind(this));
65+
this.$input.on("blur", this.onBlur.bind(this));
66+
this.$input.on("paste input", this.onChange.bind(this));
67+
this.$el.on("mousedown touchstart", this.onMouseDown.bind(this));
68+
this.$el.on("wheel", this.onMouseWheel.bind(this));
69+
$(document).on("mouseup touchend", this.onMouseUp.bind(this));
70+
$(document).on("mousemove touchmove", this.onMouseMove.bind(this));
6871
},
6972

7073
onMouseDown: function(e) {
71-
this.mouseDownX = e.clientX || e.originalEvent.touches[0].clientX;
72-
this.mouseDownValue = this.getValue();
74+
this._mouseDownX = e.clientX || e.originalEvent.touches[0].clientX;
75+
this._mouseDownValue = this.getValue();
7376

7477
this._changeStart();
7578
},
@@ -79,9 +82,20 @@
7982
},
8083

8184
onMouseMove: function(e) {
82-
if (this.curDown === true) {
83-
var t = (e.clientX || e.originalEvent.touches[0].clientX) - this.mouseDownX;
84-
this.setValue(this.mouseDownValue + t * this.settings.stepSize);
85+
if (this._curDown === true) {
86+
var t =
87+
(e.clientX || e.originalEvent.touches[0].clientX) - this._mouseDownX;
88+
this.setValue(this._mouseDownValue + t * this.step);
89+
}
90+
},
91+
92+
onMouseWheel: function(e) {
93+
// prevent [wheel increase and mousemove increase] and [wheel increase] if the input field is not focused
94+
if (this._curDown === false && this.$input.is(":focus")) {
95+
e.preventDefault();
96+
// delta is 1 if scroll up, or -1 if scroll down
97+
var d = e.originalEvent.deltaY < 0 ? 1 : -1;
98+
this.setValue(this.getValue() + d * this.step);
8599
}
86100
},
87101

@@ -95,7 +109,7 @@
95109
},
96110

97111
onKeyPress: function(e) {
98-
// key press == 'Enter' we exit the input field
112+
// exit the input field if key pressed is 'Enter'
99113
if (e.keyCode === 13) {
100114
this.$input.blur();
101115
}
@@ -112,9 +126,9 @@
112126
value = this._roundValue(value);
113127

114128
var n = value;
115-
n = n.toFixed(this.settings.decimals);
129+
n = n.toFixed(this.decimals);
116130

117-
n += this.settings.unit;
131+
n += this.unit;
118132
this.$input.val(n);
119133

120134
this._updateProgress(value);
@@ -137,33 +151,29 @@
137151
},
138152

139153
_roundValue: function(v) {
140-
var nbrDecimals = 2;
154+
var maxDecimals = 2;
141155

142-
var t = Math.pow(10, nbrDecimals);
143-
return Math.round(v * t) / t
156+
var t = Math.pow(10, maxDecimals);
157+
return Math.round(v * t) / t;
144158
},
145159

146160
_changeStart: function() {
147-
this.curDown = true;
161+
this._curDown = true;
148162
this.$el.addClass(this.settings.classNameChanging);
149163
},
150164

151165
_changeEnd: function() {
152-
this.curDown = false;
166+
this._curDown = false;
153167
this.$el.removeClass(this.settings.classNameChanging);
154-
},
155-
168+
}
156169
});
157170

158-
// A really lightweight plugin wrapper around the constructor,
159-
// preventing against multiple instantiations
171+
// A lightweight plugin wrapper around the constructor, preventing against multiple instantiations
160172
$.fn[pluginName] = function(options) {
161173
return this.each(function() {
162174
if (!$.data(this, "plugin-" + pluginName)) {
163-
$.data(this, "plugin-" + pluginName,
164-
new Plugin(this, options));
175+
$.data(this, "plugin-" + pluginName, new Plugin(this, options));
165176
}
166177
});
167178
};
168-
169179
})(jQuery, window, document);

0 commit comments

Comments
 (0)