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