jquery.knob.mod.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*!jQuery Knob*/
  2. /**
  3. * Downward compatible, touchable dial
  4. *
  5. * Version: 1.3.2
  6. * Requires: jQuery v1.7+
  7. *
  8. * Copyright (c) 2012 Anthony Terrien
  9. * Under MIT License (http://www.opensource.org/licenses/mit-license.php)
  10. *
  11. * Thanks to vor, eskimoblood, spiffistan, FabrizioC
  12. * Modded 2015-2016 by Mario Stephan for https://github.com/knowthelist/fhem-tablet-ui
  13. */
  14. (function (factory) {
  15. if (typeof exports === 'object') {
  16. // CommonJS
  17. module.exports = factory(require('jquery'));
  18. } else if (typeof define === 'function' && define.amd) {
  19. // AMD. Register as an anonymous module.
  20. define(['jquery'], factory);
  21. } else {
  22. // Browser globals
  23. factory(jQuery);
  24. }
  25. }(function ($) {
  26. /**
  27. * Kontrol library
  28. */
  29. "use strict";
  30. /**
  31. * Definition of globals and core
  32. */
  33. var k = {}, // kontrol
  34. max = Math.max,
  35. min = Math.min;
  36. k.c = {};
  37. k.c.d = $(document);
  38. k.c.t = function (e) {
  39. return e.originalEvent.touches.length - 1;
  40. };
  41. /**
  42. * Kontrol Object
  43. *
  44. * Definition of an abstract UI control
  45. *
  46. * Each concrete component must call this one.
  47. * <code>
  48. * k.o.call(this);
  49. * </code>
  50. */
  51. k.o = function () {
  52. var s = this;
  53. this.o = null; // array of options
  54. this.$ = null; // jQuery wrapped element
  55. this.i = null; // mixed HTMLInputElement or array of HTMLInputElement
  56. this.g = null; // deprecated 2D graphics context for 'pre-rendering'
  57. this.v = null; // value ; mixed array or integer
  58. this.cv = null; // change value ; not commited value
  59. this.x = 0; // canvas x position
  60. this.y = 0; // canvas y position
  61. this.w = 0; // canvas width
  62. this.tx = 0; // last touch x position
  63. this.ty = 0; // last touch y position
  64. this.h = 0; // canvas height
  65. this.$c = null; // jQuery canvas element
  66. this.c = null; // rendered canvas context
  67. this.t = 0; // touches index
  68. this.isInit = false;
  69. this.fgColor = null; // main color
  70. this.pColor = null; // previous color
  71. this.dH = null; // draw hook
  72. this.cH = null; // change hook
  73. this.eH = null; // cancel hook
  74. this.rH = null; // release hook
  75. this.scale = 1; // scale factor
  76. this.relative = false;
  77. this.relativeWidth = false;
  78. this.relativeHeight = false;
  79. this.$div = null; // component div
  80. this.run = function () {
  81. var cf = function (e, conf) {
  82. var k;
  83. for (k in conf) {
  84. s.o[k] = conf[k];
  85. }
  86. s._carve().init();
  87. s._configure()
  88. ._listen()
  89. ._draw();
  90. };
  91. if (this.$.data('kontroled')) return;
  92. this.$.data('kontroled', true);
  93. this.extend();
  94. this.o = $.extend({
  95. // Config
  96. min: this.$.data('min') !== undefined ? this.$.data('min') : 0,
  97. max: this.$.data('max') !== undefined ? this.$.data('max') : 100,
  98. stopper: true,
  99. readOnly: this.$.data('readonly') || (this.$.attr('readonly') === 'readonly'),
  100. // UI
  101. cursor: this.$.data('cursor') === true && 30 || this.$.data('cursor') || 0,
  102. thickness: this.$.data('thickness') &&
  103. Math.max(Math.min(this.$.data('thickness'), 1), 0.01) || 0.35,
  104. lineCap: this.$.data('linecap') || 'butt',
  105. width: this.$.data('width') || 200,
  106. height: this.$.data('height') || 200,
  107. displayInput: this.$.data('displayinput') == null || this.$.data('displayinput'),
  108. displayPrevious: this.$.data('displayprevious'),
  109. touchPosition: this.$.data('touchPosition') || '',
  110. fgColor: this.$.data('fgcolor') || '#87CEEB',
  111. inputColor: this.$.data('inputcolor'),
  112. font: this.$.data('font') || 'Arial',
  113. fontWeight: this.$.data('font-weight') || 'bold',
  114. inline: false,
  115. step: this.$.data('step') || 1,
  116. rotation: this.$.data('rotation'),
  117. // Hooks
  118. draw: null, // function () {}
  119. change: null, // function (value) {}
  120. cancel: null, // function () {}
  121. release: null, // function (value) {}
  122. // Output formatting, allows to add unit: %, ms ...
  123. format: function (v) {
  124. return v;
  125. },
  126. parse: function (v) {
  127. return parseFloat(v);
  128. }
  129. }, this.o);
  130. // finalize options
  131. this.o.flip = this.o.rotation === 'anticlockwise' || this.o.rotation === 'acw';
  132. if (!this.o.inputColor) {
  133. this.o.inputColor = this.o.fgColor;
  134. }
  135. // routing value
  136. if (this.$.is('fieldset')) {
  137. // fieldset = array of integer
  138. this.v = {};
  139. this.i = this.$.find('input');
  140. this.i.each(function (k) {
  141. var $this = $(this);
  142. s.i[k] = $this;
  143. s.v[k] = s.o.parse($this.val());
  144. $this.bind(
  145. 'change blur',
  146. function () {
  147. var val = {};
  148. val[k] = $this.val();
  149. s.val(s._validate(val));
  150. }
  151. );
  152. });
  153. this.$.find('legend').remove();
  154. } else {
  155. // input = integer
  156. this.i = this.$;
  157. this.v = this.o.parse(this.$.val());
  158. this.v === '' && (this.v = this.o.min);
  159. this.$.bind(
  160. 'change blur',
  161. function () {
  162. s.val(s._validate(s.o.parse(s.$.val())));
  163. }
  164. );
  165. }
  166. !this.o.displayInput && this.$.hide();
  167. // adds needed DOM elements (canvas, div)
  168. this.$c = $(document.createElement('canvas')).attr({
  169. width: this.o.width,
  170. height: this.o.height
  171. });
  172. // wraps all elements in a div
  173. // add to DOM before Canvas init is triggered
  174. this.$div = $('<div style="' + (this.o.inline ? 'display:inline;' : '') + 'width:' + this.o.width + 'px;height:' + this.o.height + 'px;' + '"></div>');
  175. this.$.wrap(this.$div).before(this.$c);
  176. this.$div = this.$.parent();
  177. if (typeof G_vmlCanvasManager !== 'undefined') {
  178. G_vmlCanvasManager.initElement(this.$c[0]);
  179. }
  180. this.c = this.$c[0].getContext ? this.$c[0].getContext('2d') : null;
  181. if (!this.c) {
  182. throw {
  183. name: "CanvasNotSupportedException",
  184. message: "Canvas not supported. Please use excanvas on IE8.0.",
  185. toString: function () {
  186. return this.name + ": " + this.message
  187. }
  188. }
  189. }
  190. // hdpi support
  191. this.scale = (window.devicePixelRatio || 1) / (
  192. this.c.webkitBackingStorePixelRatio ||
  193. this.c.mozBackingStorePixelRatio ||
  194. this.c.msBackingStorePixelRatio ||
  195. this.c.oBackingStorePixelRatio ||
  196. this.c.backingStorePixelRatio || 1
  197. );
  198. // detects relative width / height
  199. this.relativeWidth = this.o.width % 1 !== 0 && this.o.width.indexOf('%');
  200. this.relativeHeight = this.o.height % 1 !== 0 && this.o.height.indexOf('%');
  201. this.relative = this.relativeWidth || this.relativeHeight;
  202. // computes size and carves the component
  203. this._carve();
  204. // prepares props for transaction
  205. if (this.v instanceof Object) {
  206. this.cv = {};
  207. this.copy(this.v, this.cv);
  208. } else {
  209. this.cv = this.v;
  210. }
  211. // binds configure event
  212. this.$
  213. .bind("configure", cf)
  214. .parent()
  215. .bind("configure", cf);
  216. // finalize init
  217. this._listen()
  218. ._configure()
  219. ._xy()
  220. .init();
  221. this.isInit = true;
  222. this.$.val(this.o.format(this.v));
  223. this._draw();
  224. this.$.data('knob', this);
  225. return this;
  226. };
  227. this._carve = function () {
  228. if (this.relative) {
  229. var w = this.relativeWidth ?
  230. this.$div.parent().width() *
  231. parseInt(this.o.width) / 100 : this.$div.parent().width(),
  232. h = this.relativeHeight ?
  233. this.$div.parent().height() *
  234. parseInt(this.o.height) / 100 : this.$div.parent().height();
  235. // apply relative
  236. this.w = this.h = Math.min(w, h);
  237. } else {
  238. this.w = this.o.width;
  239. this.h = this.o.height;
  240. }
  241. // finalize div
  242. this.$div.css({
  243. 'width': this.w + 'px',
  244. 'height': this.h + 'px'
  245. });
  246. // finalize canvas with computed width
  247. this.$c.attr({
  248. width: this.w,
  249. height: this.h
  250. });
  251. // scaling
  252. if (this.scale !== 1) {
  253. this.$c[0].width = this.$c[0].width * this.scale;
  254. this.$c[0].height = this.$c[0].height * this.scale;
  255. this.$c.width(this.w);
  256. this.$c.height(this.h);
  257. }
  258. return this;
  259. }
  260. this._draw = function () {
  261. // canvas pre-rendering
  262. var d = true;
  263. s.g = s.c;
  264. s.clear();
  265. s.dH && (d = s.dH());
  266. d !== false && s.draw();
  267. };
  268. this._touch = function (e) {
  269. var touchMove = function (e) {
  270. var v = s.xy2val(
  271. e.originalEvent.touches[s.t].pageX,
  272. e.originalEvent.touches[s.t].pageY
  273. );
  274. if (v == s.cv) return;
  275. if (s.cH && s.cH(v) === false) return;
  276. s.change(s._validate(v));
  277. s._draw();
  278. };
  279. // get touches index
  280. this.t = k.c.t(e);
  281. // First touch
  282. touchMove(e);
  283. // Touch events listeners
  284. k.c.d
  285. .bind("touchmove.k", touchMove)
  286. .bind(
  287. "touchend.k",
  288. function () {
  289. k.c.d.unbind('touchmove.k touchend.k');
  290. s.val(s.cv);
  291. s._moveInput(false);
  292. }
  293. );
  294. return this;
  295. };
  296. this._mouse = function (e) {
  297. var mouseMove = function (e) {
  298. var v = s.xy2val(e.pageX, e.pageY);
  299. if (v == s.cv) return;
  300. if (s.cH && (s.cH(v) === false)) return;
  301. s.change(s._validate(v));
  302. s._draw();
  303. };
  304. // First click
  305. mouseMove(e);
  306. // Mouse events listeners
  307. k.c.d
  308. .bind("mousemove.k", mouseMove)
  309. .bind(
  310. // Escape key cancel current change
  311. "keyup.k",
  312. function (e) {
  313. if (e.keyCode === 27) {
  314. k.c.d.unbind("mouseup.k mousemove.k keyup.k");
  315. if (s.eH && s.eH() === false)
  316. return;
  317. s.cancel();
  318. }
  319. }
  320. )
  321. .bind(
  322. "mouseup.k",
  323. function (e) {
  324. k.c.d.unbind('mousemove.k mouseup.k keyup.k');
  325. s.val(s.cv);
  326. }
  327. );
  328. return this;
  329. };
  330. this._xy = function () {
  331. var o = this.$c.offset();
  332. this.x = o.left;
  333. this.y = o.top;
  334. return this;
  335. };
  336. this._moveInput = function (isTouched) {
  337. if (this.o.touchPosition) {
  338. if (this.o.touchPosition == 'left' && isTouched) {
  339. var s = (this.i.val().length == 4) ? 0.1 : 0.0;
  340. this.i.animate({
  341. 'margin-top': '-' + ((this.w / 6) >> 0) + 'px',
  342. 'margin-left': '-' + ((this.w * (1.2 - s)) >> 0) + 'px',
  343. });
  344. } else if (this.o.touchPosition == 'right' && isTouched) {
  345. var s = (this.i.val().length == 4) ? 0.0 : 0.1;
  346. this.i.animate({
  347. 'margin-top': '-' + ((this.w / 6) >> 0) + 'px',
  348. 'margin-left': '-' + ((this.w * (3 / 6 - s)) >> 0) + 'px',
  349. });
  350. } else {
  351. this.i.animate({
  352. 'margin-top': ((this.w / 3) >> 0) + 'px',
  353. 'margin-left': '-' + ((this.w * 3 / 4 + 2) >> 0) + 'px',
  354. });
  355. }
  356. }
  357. }
  358. this._listen = function () {
  359. function getAndroidVersion(ua) {
  360. ua = (ua || navigator.userAgent).toLowerCase();
  361. var match = ua.match(/android\s([0-9\.]*)/);
  362. return match ? match[1] : false;
  363. }
  364. var android = getAndroidVersion();
  365. var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
  366. var onlyTouch = ((android && parseFloat(android) < 5) || iOS);
  367. if (!this.o.readOnly) {
  368. if (!onlyTouch) {
  369. this.$c.bind(
  370. "mousedown",
  371. function (e) {
  372. e.preventDefault();
  373. s._xy()._mouse(e);
  374. }
  375. );
  376. }
  377. this.$c.bind(
  378. "touchstart",
  379. function (e) {
  380. e.preventDefault();
  381. s._xy()._touch(e);
  382. s._moveInput(true);
  383. }
  384. );
  385. this.listen();
  386. } else {
  387. this.$c.unbind("mousedown");
  388. this.$c.unbind("touchstart");
  389. this.$.attr('readonly', 'readonly');
  390. }
  391. if (this.relative) {
  392. $(window).resize(function () {
  393. s._carve().init();
  394. s._draw();
  395. });
  396. }
  397. return this;
  398. };
  399. this._configure = function () {
  400. // Hooks
  401. if (this.o.draw) this.dH = this.o.draw;
  402. if (this.o.change) this.cH = this.o.change;
  403. if (this.o.cancel) this.eH = this.o.cancel;
  404. if (this.o.release) this.rH = this.o.release;
  405. if (this.o.displayPrevious) {
  406. this.pColor = this.h2rgba(this.o.fgColor, "0.4");
  407. this.fgColor = this.h2rgba(this.o.fgColor, "0.6");
  408. } else {
  409. this.fgColor = this.o.fgColor;
  410. }
  411. return this;
  412. };
  413. this._clear = function () {
  414. this.$c[0].width = this.$c[0].width;
  415. };
  416. this._validate = function (v) {
  417. var val = (~~(((v < 0) ? -0.5 : 0.5) + (v / this.o.step))) * this.o.step;
  418. return Math.round(val * 100) / 100;
  419. };
  420. // Abstract methods
  421. this.listen = function () {}; // on start, one time
  422. this.extend = function () {}; // each time configure triggered
  423. this.init = function () {}; // each time configure triggered
  424. this.change = function (v) {}; // on change
  425. this.val = function (v) {}; // on release
  426. this.xy2val = function (x, y) {}; //
  427. this.draw = function () {}; // on change / on release
  428. this.clear = function () {
  429. this._clear();
  430. };
  431. // Utils
  432. this.h2rgba = function (h, a) {
  433. var rgb;
  434. h = h.substring(1, 7)
  435. rgb = [
  436. parseInt(h.substring(0, 2), 16),
  437. parseInt(h.substring(2, 4), 16),
  438. parseInt(h.substring(4, 6), 16)
  439. ];
  440. return "rgba(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + "," + a + ")";
  441. };
  442. this.copy = function (f, t) {
  443. for (var i in f) {
  444. t[i] = f[i];
  445. }
  446. };
  447. };
  448. /**
  449. * k.Dial
  450. */
  451. k.Dial = function () {
  452. k.o.call(this);
  453. this.startAngle = null;
  454. this.xy = null;
  455. this.radius = null;
  456. this.lineWidth = null;
  457. this.cursorExt = null;
  458. this.w2 = null;
  459. this.PI2 = 2 * Math.PI;
  460. this.extend = function () {
  461. this.o = $.extend({
  462. bgColor: this.$.data('bgcolor') || '#EEEEEE',
  463. angleOffset: this.$.data('angleoffset') || 0,
  464. angleArc: this.$.data('anglearc') || 360,
  465. inline: true
  466. }, this.o);
  467. };
  468. this.val = function (v, triggerRelease) {
  469. if (null != v) {
  470. // reverse format
  471. v = this.o.parse(v);
  472. if (triggerRelease !== false && v != this.v && this.rH && this.rH(v) === false) {
  473. return;
  474. }
  475. this.cv = this.o.stopper ? max(min(v, this.o.max), this.o.min) : v;
  476. this.v = this.cv;
  477. this.$.val(this.o.format(this.v));
  478. this._draw();
  479. } else {
  480. return this.v;
  481. }
  482. };
  483. this.xy2val = function (x, y) {
  484. var a, ret;
  485. this.tx = x;
  486. this.ty = y;
  487. a = Math.atan2(
  488. x - (this.x + this.w2), -(y - this.y - this.w2)
  489. ) - this.angleOffset;
  490. if (this.o.flip) {
  491. a = this.angleArc - a - this.PI2;
  492. }
  493. if (this.angleArc != this.PI2 && (a < 0) && (a > -0.5)) {
  494. // if isset angleArc option, set to min if .5 under min
  495. a = 0;
  496. } else if (a < 0) {
  497. a += this.PI2;
  498. }
  499. ret = (a * (this.o.max - this.o.min) / this.angleArc) + this.o.min;
  500. this.o.stopper && (ret = max(min(ret, this.o.max), this.o.min));
  501. return ret;
  502. };
  503. this.listen = function () {
  504. // bind MouseWheel
  505. var s = this,
  506. mwTimerStop,
  507. mwTimerRelease,
  508. mw = function (e) {
  509. e.preventDefault();
  510. var ori = e.originalEvent,
  511. deltaX = ori.detail || ori.wheelDeltaX,
  512. deltaY = ori.detail || ori.wheelDeltaY,
  513. v = s._validate(s.o.parse(s.$.val())) + (
  514. deltaX > 0 || deltaY > 0 ? s.o.step : deltaX < 0 || deltaY < 0 ? -s.o.step : 0
  515. );
  516. v = max(min(v, s.o.max), s.o.min);
  517. s.val(v, false);
  518. if (s.rH) {
  519. // Handle mousewheel stop
  520. clearTimeout(mwTimerStop);
  521. mwTimerStop = setTimeout(function () {
  522. s.rH(v);
  523. mwTimerStop = null;
  524. }, 100);
  525. // Handle mousewheel releases
  526. if (!mwTimerRelease) {
  527. mwTimerRelease = setTimeout(function () {
  528. if (mwTimerStop)
  529. s.rH(v);
  530. mwTimerRelease = null;
  531. }, 200);
  532. }
  533. }
  534. },
  535. kval,
  536. to,
  537. m = 1,
  538. kv = {
  539. 37: -s.o.step,
  540. 38: s.o.step,
  541. 39: s.o.step,
  542. 40: -s.o.step
  543. };
  544. this.$
  545. .bind(
  546. "keydown",
  547. function (e) {
  548. var kc = e.keyCode;
  549. // numpad support
  550. if (kc >= 96 && kc <= 105) {
  551. kc = e.keyCode = kc - 48;
  552. }
  553. kval = parseInt(String.fromCharCode(kc));
  554. if (isNaN(kval)) {
  555. (kc !== 13) // enter
  556. && kc !== 8 // bs
  557. && kc !== 9 // tab
  558. && kc !== 189 // -
  559. && (kc !== 190 || s.$.val().match(/\./)) // . allowed once
  560. && e.preventDefault();
  561. // arrows
  562. if ($.inArray(kc, [37, 38, 39, 40]) > -1) {
  563. e.preventDefault();
  564. var v = s.o.parse(s.$.val()) + kv[kc] * m;
  565. s.o.stopper && (v = max(min(v, s.o.max), s.o.min));
  566. s.change(s._validate(v));
  567. s._draw();
  568. // long time keydown speed-up
  569. to = window.setTimeout(function () {
  570. m *= 2;
  571. }, 30);
  572. }
  573. }
  574. }
  575. )
  576. .bind(
  577. "keyup",
  578. function (e) {
  579. if (isNaN(kval)) {
  580. if (to) {
  581. window.clearTimeout(to);
  582. to = null;
  583. m = 1;
  584. s.val(s.$.val());
  585. }
  586. } else {
  587. // kval postcond
  588. (s.$.val() > s.o.max && s.$.val(s.o.max)) || (s.$.val() < s.o.min && s.$.val(s.o.min));
  589. }
  590. }
  591. );
  592. //this.$c.bind("mousewheel DOMMouseScroll", mw);
  593. //this.$.bind("mousewheel DOMMouseScroll", mw)
  594. };
  595. this.init = function () {
  596. if (this.v < this.o.min || this.v > this.o.max) {
  597. this.v = this.o.min;
  598. }
  599. this.$.val(this.v);
  600. this.w2 = this.w / 2;
  601. this.cursorExt = this.o.cursor / 100;
  602. this.xy = this.w2 * this.scale;
  603. this.lineWidth = this.xy * this.o.thickness;
  604. this.lineCap = this.o.lineCap;
  605. this.radius = this.xy - this.lineWidth / 2;
  606. this.o.angleOffset && (this.o.angleOffset = isNaN(this.o.angleOffset) ? 0 : this.o.angleOffset);
  607. this.o.angleArc && (this.o.angleArc = isNaN(this.o.angleArc) ? this.PI2 : this.o.angleArc);
  608. // deg to rad
  609. this.angleOffset = this.o.angleOffset * Math.PI / 180;
  610. this.angleArc = this.o.angleArc * Math.PI / 180;
  611. // compute start and end angles
  612. this.startAngle = 1.5 * Math.PI + this.angleOffset;
  613. this.endAngle = 1.5 * Math.PI + this.angleOffset + this.angleArc;
  614. var s = max(
  615. String(Math.abs(this.o.max)).length,
  616. String(Math.abs(this.o.min)).length,
  617. 2
  618. ) + 2;
  619. this.o.displayInput && this.i.css({
  620. 'width': ((this.w / 2 + 4) >> 0) + 'px',
  621. 'height': ((this.w / 3) >> 0) + 'px',
  622. 'position': 'absolute',
  623. 'vertical-align': 'middle',
  624. 'margin-top': ((this.w / 3) >> 0) + 'px',
  625. 'margin-left': '-' + ((this.w * 3 / 4 + 2) >> 0) + 'px',
  626. 'border': 0,
  627. 'background': 'none',
  628. 'font': this.o.fontWeight + ' ' + ((this.w / s) >> 0) + 'px ' + this.o.font,
  629. 'text-align': 'center',
  630. 'color': this.o.inputColor || this.o.fgColor,
  631. 'padding': '0px',
  632. '-webkit-appearance': 'none'
  633. }) || this.i.css({
  634. 'width': '0px',
  635. 'visibility': 'hidden'
  636. });
  637. };
  638. this.change = function (v) {
  639. this.cv = v;
  640. this.$.val(this.o.format(v));
  641. };
  642. this.angle = function (v) {
  643. return (v - this.o.min) * this.angleArc / (this.o.max - this.o.min);
  644. };
  645. this.arc = function (v) {
  646. var sa, ea;
  647. v = this.angle(v);
  648. if (this.o.flip) {
  649. sa = this.endAngle + 0.00001;
  650. ea = sa - v - 0.00001;
  651. } else {
  652. sa = this.startAngle - 0.00001;
  653. ea = sa + v + 0.00001;
  654. }
  655. this.o.cursor && (sa = ea - this.cursorExt) && (ea = ea + this.cursorExt);
  656. return {
  657. s: sa,
  658. e: ea,
  659. d: this.o.flip && !this.o.cursor
  660. };
  661. };
  662. this.draw = function () {
  663. var c = this.g, // context
  664. a = this.arc(this.cv), // Arc
  665. pa, // Previous arc
  666. r = 1;
  667. c.lineWidth = this.lineWidth;
  668. c.lineCap = this.lineCap;
  669. if (this.o.bgColor !== "none") {
  670. c.beginPath();
  671. c.strokeStyle = this.o.bgColor;
  672. c.arc(this.xy, this.xy, this.radius, this.endAngle - 0.00001, this.startAngle + 0.00001, true);
  673. c.stroke();
  674. }
  675. if (this.o.displayPrevious) {
  676. pa = this.arc(this.v);
  677. c.beginPath();
  678. c.strokeStyle = this.pColor;
  679. c.arc(this.xy, this.xy, this.radius, pa.s, pa.e, pa.d);
  680. c.stroke();
  681. r = this.cv == this.v;
  682. }
  683. c.beginPath();
  684. c.strokeStyle = r ? this.o.fgColor : this.fgColor;
  685. c.arc(this.xy, this.xy, this.radius, a.s, a.e, a.d);
  686. c.stroke();
  687. };
  688. this.cancel = function () {
  689. this.val(this.v);
  690. };
  691. };
  692. $.fn.dial = $.fn.knob = function (o) {
  693. return this.each(
  694. function () {
  695. var d = new k.Dial();
  696. d.o = o;
  697. d.$ = $(this);
  698. d.run();
  699. }
  700. ).parent();
  701. };
  702. }));