Skip to content

Commit 1d82b10

Browse files
committed
Make change event always fire as the user interacts with the knob
That is, when using: - touch - wheel - typing (by binding to the "input" event) Previously, typing did not update the knob until focus was lost (and no change event was fired), and using the keyboard arrows did not fire any change events. Also move event triggering to val() and change() so it works always in the same way and less duplication exists. Similar to aterrien#130 but fixes an issue with `diplayPrevious`
1 parent ec05292 commit 1d82b10

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060

6161
return false;
6262
}
63-
}
63+
},
64+
change: function (val) { console.log("Change: " + val) }
6465
});
6566

6667
// Example of infinite knob, iPod click wheel
@@ -309,4 +310,4 @@ <h1>jQuery Knob</h1>
309310
<p style="font-size:20px;">jQuery Knob is &copy; 2012 Anthony Terrien and dual licensed under the MIT or GPL licenses.</p>
310311
</div>
311312
</body>
312-
</html>
313+
</html>

js/jquery.knob.js

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
s.v[k] = $this.val();
142142

143143
$this.bind(
144-
'change'
144+
'change keyup'
145145
, function () {
146146
var val = {};
147147
val[k] = $this.val();
@@ -159,7 +159,7 @@
159159
(this.v == '') && (this.v = this.o.min);
160160

161161
this.$.bind(
162-
'change'
162+
'change keyup'
163163
, function () {
164164
s.val(s._validate(s.$.val()));
165165
}
@@ -302,15 +302,6 @@
302302
e.originalEvent.touches[s.t].pageX,
303303
e.originalEvent.touches[s.t].pageY
304304
);
305-
306-
if (v == s.cv) return;
307-
308-
if (
309-
s.cH
310-
&& (s.cH(v) === false)
311-
) return;
312-
313-
314305
s.change(s._validate(v));
315306
s._draw();
316307
};
@@ -345,13 +336,6 @@
345336

346337
var mouseMove = function (e) {
347338
var v = s.xy2val(e.pageX, e.pageY);
348-
if (v == s.cv) return;
349-
350-
if (
351-
s.cH
352-
&& (s.cH(v) === false)
353-
) return;
354-
355339
s.change(s._validate(v));
356340
s._draw();
357341
};
@@ -516,8 +500,13 @@
516500

517501
this.val = function (v) {
518502
if (null != v) {
519-
this.cv = this.o.stopper ? max(min(v, this.o.max), this.o.min) : v;
520-
this.v = this.cv;
503+
var newValue = this.o.stopper ? max(min(v, this.o.max), this.o.min) : v;
504+
if (
505+
newValue != this.cv // avoid double callback for same value
506+
&& this.cH
507+
&& (this.cH(this.cv) === false)
508+
) return;
509+
this.v = this.cv = newValue;
521510
this.$.val(this.v);
522511
this._draw();
523512
} else {
@@ -558,12 +547,6 @@
558547
,deltaX = ori.detail || ori.wheelDeltaX
559548
,deltaY = ori.detail || ori.wheelDeltaY
560549
,v = parseInt(s.$.val()) + (deltaX>0 || deltaY>0 ? s.o.step : deltaX<0 || deltaY<0 ? -s.o.step : 0);
561-
562-
if (
563-
s.cH
564-
&& (s.cH(v) === false)
565-
) return;
566-
567550
s.val(v);
568551
}
569552
, kval, to, m = 1, kv = {37:-s.o.step, 38:s.o.step, 39:s.o.step, 40:-s.o.step};
@@ -691,8 +674,12 @@
691674
};
692675

693676
this.change = function (v) {
677+
if (v == this.cv) return;
694678
this.cv = v;
695-
this.$.val(v);
679+
if (
680+
this.cH
681+
&& (this.cH(v) === false)
682+
) return;
696683
};
697684

698685
this.angle = function (v) {
@@ -757,4 +744,4 @@
757744
).parent();
758745
};
759746

760-
})(jQuery);
747+
})(jQuery);

0 commit comments

Comments
 (0)