|
5 | 5 | //
|
6 | 6 | // the semi-colon before function invocation is a safety net against concatenated
|
7 | 7 | // scripts and/or other plugins which may not be closed properly.
|
8 |
| -; |
9 | 8 | (function($, window, document, undefined) {
|
10 |
| - |
11 | 9 | "use strict";
|
12 |
| - |
13 | 10 | var pluginName = "stepper",
|
14 | 11 | 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: "", |
18 | 16 | decimals: 0,
|
19 |
| - unit: '%', |
20 |
| - initialValue: '', |
21 | 17 | min: 0,
|
22 | 18 | max: 100,
|
23 |
| - stepSize: 1 |
| 19 | + step: 1, |
| 20 | + unit: "%" |
24 | 21 | };
|
25 | 22 |
|
26 | 23 | // The actual plugin constructor
|
|
37 | 34 |
|
38 | 35 | // Avoid Plugin.prototype conflicts
|
39 | 36 | $.extend(Plugin.prototype, {
|
40 |
| - |
41 | 37 | init: function() {
|
42 |
| - |
43 | 38 | // 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; |
47 | 42 |
|
48 | 43 | // Cache elements
|
49 | 44 | this.$el = $(this.element);
|
50 | 45 | this.$input = this.$el.find(this.settings.selectorInputNumber);
|
51 | 46 | this.$progress = this.$el.find(this.settings.selectorProgressBar);
|
52 | 47 |
|
53 | 48 | // 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; |
56 | 56 |
|
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); |
58 | 60 |
|
59 |
| - this.setValue(this.initialValue); |
| 61 | + this.setValue(this.value); |
60 | 62 |
|
61 | 63 | // 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)); |
68 | 71 | },
|
69 | 72 |
|
70 | 73 | 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(); |
73 | 76 |
|
74 | 77 | this._changeStart();
|
75 | 78 | },
|
|
79 | 82 | },
|
80 | 83 |
|
81 | 84 | 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); |
85 | 99 | }
|
86 | 100 | },
|
87 | 101 |
|
|
95 | 109 | },
|
96 | 110 |
|
97 | 111 | onKeyPress: function(e) {
|
98 |
| - // key press == 'Enter' we exit the input field |
| 112 | + // exit the input field if key pressed is 'Enter' |
99 | 113 | if (e.keyCode === 13) {
|
100 | 114 | this.$input.blur();
|
101 | 115 | }
|
|
112 | 126 | value = this._roundValue(value);
|
113 | 127 |
|
114 | 128 | var n = value;
|
115 |
| - n = n.toFixed(this.settings.decimals); |
| 129 | + n = n.toFixed(this.decimals); |
116 | 130 |
|
117 |
| - n += this.settings.unit; |
| 131 | + n += this.unit; |
118 | 132 | this.$input.val(n);
|
119 | 133 |
|
120 | 134 | this._updateProgress(value);
|
|
137 | 151 | },
|
138 | 152 |
|
139 | 153 | _roundValue: function(v) {
|
140 |
| - var nbrDecimals = 2; |
| 154 | + var maxDecimals = 2; |
141 | 155 |
|
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; |
144 | 158 | },
|
145 | 159 |
|
146 | 160 | _changeStart: function() {
|
147 |
| - this.curDown = true; |
| 161 | + this._curDown = true; |
148 | 162 | this.$el.addClass(this.settings.classNameChanging);
|
149 | 163 | },
|
150 | 164 |
|
151 | 165 | _changeEnd: function() {
|
152 |
| - this.curDown = false; |
| 166 | + this._curDown = false; |
153 | 167 | this.$el.removeClass(this.settings.classNameChanging);
|
154 |
| - }, |
155 |
| - |
| 168 | + } |
156 | 169 | });
|
157 | 170 |
|
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 |
160 | 172 | $.fn[pluginName] = function(options) {
|
161 | 173 | return this.each(function() {
|
162 | 174 | if (!$.data(this, "plugin-" + pluginName)) {
|
163 |
| - $.data(this, "plugin-" + pluginName, |
164 |
| - new Plugin(this, options)); |
| 175 | + $.data(this, "plugin-" + pluginName, new Plugin(this, options)); |
165 | 176 | }
|
166 | 177 | });
|
167 | 178 | };
|
168 |
| - |
169 | 179 | })(jQuery, window, document);
|
0 commit comments