Skip to content

Commit f785736

Browse files
committed
format() function (allows to add % or other unit) #170, #172, #134, #124, #65, #18
1 parent 030fe57 commit f785736

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Hooks
8484

8585
* 'cancel' : triggered on [esc] keydown
8686

87-
* 'error' : called if the browser doesn't support canvases and the plugin didn't initialize as a result
87+
* 'format' : allows to format output (add unit %, ms ...)
8888

8989
The scope (this) of each hook function is the current Knob instance (refer to the demo code).
9090

bower.json

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aterrien/jQuery-Knob",
3-
"version": "1.2.5",
3+
"version": "1.2.6",
44
"main": "js/jquery.knob.js",
55
"description": "Nice, downward compatible, touchable, jQuery dial.",
66
"license": "MIT",

excanvas.js

100644100755
File mode changed.

js/jquery.knob.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Downward compatible, touchable dial
44
*
5-
* Version: 1.2.5 (23/01/2014)
5+
* Version: 1.2.6 (23/02/2014)
66
* Requires: jQuery v1.7+
77
*
88
* Copyright (c) 2012 Anthony Terrien
@@ -115,7 +115,15 @@
115115
draw : null, // function () {}
116116
change : null, // function (value) {}
117117
cancel : null, // function () {}
118-
release : null // function (value) {}
118+
release : null, // function (value) {}
119+
120+
// Output formatting, allows to add unit: %, ms ...
121+
format: function(v) {
122+
return v;
123+
},
124+
parse: function (v) {
125+
return parseFloat(v);
126+
}
119127
}, this.o
120128
);
121129

@@ -156,7 +164,7 @@
156164
this.$.bind(
157165
'change blur'
158166
, function () {
159-
s.val(s._validate(s.$.val()));
167+
s.val(s._validate(s.o.parse(s.$.val())));
160168
}
161169
);
162170

@@ -236,7 +244,7 @@
236244

237245
this.isInit = true;
238246

239-
// the most important !
247+
this.$.val(this.o.format(this.v));
240248
this._draw();
241249

242250
return this;
@@ -504,14 +512,17 @@
504512
this.val = function (v, triggerRelease) {
505513
if (null != v) {
506514

515+
// reverse format
516+
v = this.o.parse(v);
517+
507518
if (
508519
triggerRelease !== false && (v != this.v) && this.rH &&
509520
(this.rH(v) === false)
510521
) return;
511522

512523
this.cv = this.o.stopper ? max(min(v, this.o.max), this.o.min) : v;
513524
this.v = this.cv;
514-
this.$.val(this.v);
525+
this.$.val(this.o.format(this.v));
515526
this._draw();
516527
} else {
517528
return this.v;
@@ -550,7 +561,7 @@
550561
var ori = e.originalEvent
551562
,deltaX = ori.detail || ori.wheelDeltaX
552563
,deltaY = ori.detail || ori.wheelDeltaY
553-
,v = s._validate(s.$.val())
564+
,v = s._validate(s.o.parse(s.$.val()))
554565
+ (deltaX>0 || deltaY>0 ? s.o.step : deltaX<0 || deltaY<0 ? -s.o.step : 0);
555566

556567
v = max(min(v, s.o.max), s.o.min);
@@ -602,7 +613,7 @@
602613
if ($.inArray(kc,[37,38,39,40]) > -1) {
603614
e.preventDefault();
604615

605-
var v = parseFloat(s.$.val()) + kv[kc] * m;
616+
var v = s.o.parse(s.$.val()) + kv[kc] * m;
606617
s.o.stopper && (v = max(min(v, s.o.max), s.o.min));
607618

608619
s.change(v);
@@ -698,7 +709,7 @@
698709

699710
this.change = function (v) {
700711
this.cv = v;
701-
this.$.val(v);
712+
this.$.val(this.o.format(v));
702713
};
703714

704715
this.angle = function (v) {

knob.jquery.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"ui",
1010
"input"
1111
],
12-
"version": "1.2.5",
12+
"version": "1.2.6",
1313
"author": {
1414
"name": "Anthony Terrien",
1515
"url": "https://github.com/aterrien"

0 commit comments

Comments
 (0)