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