/*!jquery knob*/ /**������ȫ��õ�bootstrapģ�壺http://www.bootstrapmb.com * downward compatible, touchable dial * * version: 1.2.11 * requires: jquery v1.7+ * * copyright (c) 2012 anthony terrien * under mit license (http://www.opensource.org/licenses/mit-license.php) * * thanks to vor, eskimoblood, spiffistan, fabrizioc */ (function (factory) { if (typeof exports === 'object') { // commonjs module.exports = factory(require('jquery')); } else if (typeof define === 'function' && define.amd) { // amd. register as an anonymous module. define(['jquery'], factory); } else { // browser globals factory(jquery); } }(function ($) { /** * kontrol library */ "use strict"; /** * definition of globals and core */ var k = {}, // kontrol max = math.max, min = math.min; k.c = {}; k.c.d = $(document); k.c.t = function (e) { return e.originalevent.touches.length - 1; }; /** * kontrol object * * definition of an abstract ui control * * each concrete component must call this one. * * k.o.call(this); * */ k.o = function () { var s = this; this.o = null; // array of options this.$ = null; // jquery wrapped element this.i = null; // mixed htmlinputelement or array of htmlinputelement this.g = null; // deprecated 2d graphics context for 'pre-rendering' this.v = null; // value ; mixed array or integer this.cv = null; // change value ; not commited value this.x = 0; // canvas x position this.y = 0; // canvas y position this.w = 0; // canvas width this.h = 0; // canvas height this.$c = null; // jquery canvas element this.c = null; // rendered canvas context this.t = 0; // touches index this.isinit = false; this.fgcolor = null; // main color this.pcolor = null; // previous color this.dh = null; // draw hook this.ch = null; // change hook this.eh = null; // cancel hook this.rh = null; // release hook this.scale = 1; // scale factor this.relative = false; this.relativewidth = false; this.relativeheight = false; this.$div = null; // component div this.run = function () { var cf = function (e, conf) { var k; for (k in conf) { s.o[k] = conf[k]; } s._carve().init(); s._configure() ._draw(); }; if (this.$.data('kontroled')) return; this.$.data('kontroled', true); this.extend(); this.o = $.extend({ // config min: this.$.data('min') !== undefined ? this.$.data('min') : 0, max: this.$.data('max') !== undefined ? this.$.data('max') : 100, stopper: true, readonly: this.$.data('readonly') || (this.$.attr('readonly') === 'readonly'), // ui cursor: this.$.data('cursor') === true && 30 || this.$.data('cursor') || 0, thickness: this.$.data('thickness') && math.max(math.min(this.$.data('thickness'), 1), 0.01) || 0.35, linecap: this.$.data('linecap') || 'butt', width: this.$.data('width') || 200, height: this.$.data('height') || 200, displayinput: this.$.data('displayinput') == null || this.$.data('displayinput'), displayprevious: this.$.data('displayprevious'), fgcolor: this.$.data('fgcolor') || '#87ceeb', inputcolor: this.$.data('inputcolor'), font: this.$.data('font') || 'arial', fontweight: this.$.data('font-weight') || 'bold', inline: false, step: this.$.data('step') || 1, rotation: this.$.data('rotation'), // hooks draw: null, // function () {} change: null, // function (value) {} cancel: null, // function () {} release: null, // function (value) {} // output formatting, allows to add unit: %, ms ... format: function(v) { return v; }, parse: function (v) { return parsefloat(v); } }, this.o ); // finalize options this.o.flip = this.o.rotation === 'anticlockwise' || this.o.rotation === 'acw'; if (!this.o.inputcolor) { this.o.inputcolor = this.o.fgcolor; } // routing value if (this.$.is('fieldset')) { // fieldset = array of integer this.v = {}; this.i = this.$.find('input'); this.i.each(function(k) { var $this = $(this); s.i[k] = $this; s.v[k] = s.o.parse($this.val()); $this.bind( 'change blur', function () { var val = {}; val[k] = $this.val(); s.val(s._validate(val)); } ); }); this.$.find('legend').remove(); } else { // input = integer this.i = this.$; this.v = this.o.parse(this.$.val()); this.v === '' && (this.v = this.o.min); this.$.bind( 'change blur', function () { s.val(s._validate(s.o.parse(s.$.val()))); } ); } !this.o.displayinput && this.$.hide(); // adds needed dom elements (canvas, div) this.$c = $(document.createelement('canvas')).attr({ width: this.o.width, height: this.o.height }); // wraps all elements in a div // add to dom before canvas init is triggered this.$div = $('
'); this.$.wrap(this.$div).before(this.$c); this.$div = this.$.parent(); if (typeof g_vmlcanvasmanager !== 'undefined') { g_vmlcanvasmanager.initelement(this.$c[0]); } this.c = this.$c[0].getcontext ? this.$c[0].getcontext('2d') : null; if (!this.c) { throw { name: "canvasnotsupportedexception", message: "canvas not supported. please use excanvas on ie8.0.", tostring: function(){return this.name + ": " + this.message} } } // hdpi support this.scale = (window.devicepixelratio || 1) / ( this.c.webkitbackingstorepixelratio || this.c.mozbackingstorepixelratio || this.c.msbackingstorepixelratio || this.c.obackingstorepixelratio || this.c.backingstorepixelratio || 1 ); // detects relative width / height this.relativewidth = this.o.width % 1 !== 0 && this.o.width.indexof('%'); this.relativeheight = this.o.height % 1 !== 0 && this.o.height.indexof('%'); this.relative = this.relativewidth || this.relativeheight; // computes size and carves the component this._carve(); // prepares props for transaction if (this.v instanceof object) { this.cv = {}; this.copy(this.v, this.cv); } else { this.cv = this.v; } // binds configure event this.$ .bind("configure", cf) .parent() .bind("configure", cf); // finalize init this._listen() ._configure() ._xy() .init(); this.isinit = true; this.$.val(this.o.format(this.v)); this._draw(); return this; }; this._carve = function() { if (this.relative) { var w = this.relativewidth ? this.$div.parent().width() * parseint(this.o.width) / 100 : this.$div.parent().width(), h = this.relativeheight ? this.$div.parent().height() * parseint(this.o.height) / 100 : this.$div.parent().height(); // apply relative this.w = this.h = math.min(w, h); } else { this.w = this.o.width; this.h = this.o.height; } // finalize div this.$div.css({ 'width': this.w + 'px', 'height': this.h + 'px' }); // finalize canvas with computed width this.$c.attr({ width: this.w, height: this.h }); // scaling if (this.scale !== 1) { this.$c[0].width = this.$c[0].width * this.scale; this.$c[0].height = this.$c[0].height * this.scale; this.$c.width(this.w); this.$c.height(this.h); } return this; } this._draw = function () { // canvas pre-rendering var d = true; s.g = s.c; s.clear(); s.dh && (d = s.dh()); d !== false && s.draw(); }; this._touch = function (e) { var touchmove = function (e) { var v = s.xy2val( e.originalevent.touches[s.t].pagex, e.originalevent.touches[s.t].pagey ); if (v == s.cv) return; if (s.ch && s.ch(v) === false) return; s.change(s._validate(v)); s._draw(); }; // get touches index this.t = k.c.t(e); // first touch touchmove(e); // touch events listeners k.c.d .bind("touchmove.k", touchmove) .bind( "touchend.k", function () { k.c.d.unbind('touchmove.k touchend.k'); s.val(s.cv); } ); return this; }; this._mouse = function (e) { var mousemove = function (e) { var v = s.xy2val(e.pagex, e.pagey); if (v == s.cv) return; if (s.ch && (s.ch(v) === false)) return; s.change(s._validate(v)); s._draw(); }; // first click mousemove(e); // mouse events listeners k.c.d .bind("mousemove.k", mousemove) .bind( // escape key cancel current change "keyup.k", function (e) { if (e.keycode === 27) { k.c.d.unbind("mouseup.k mousemove.k keyup.k"); if (s.eh && s.eh() === false) return; s.cancel(); } } ) .bind( "mouseup.k", function (e) { k.c.d.unbind('mousemove.k mouseup.k keyup.k'); s.val(s.cv); } ); return this; }; this._xy = function () { var o = this.$c.offset(); this.x = o.left; this.y = o.top; return this; }; this._listen = function () { if (!this.o.readonly) { this.$c .bind( "mousedown", function (e) { e.preventdefault(); s._xy()._mouse(e); } ) .bind( "touchstart", function (e) { e.preventdefault(); s._xy()._touch(e); } ); this.listen(); } else { this.$.attr('readonly', 'readonly'); } if (this.relative) { $(window).resize(function() { s._carve().init(); s._draw(); }); } return this; }; this._configure = function () { // hooks if (this.o.draw) this.dh = this.o.draw; if (this.o.change) this.ch = this.o.change; if (this.o.cancel) this.eh = this.o.cancel; if (this.o.release) this.rh = this.o.release; if (this.o.displayprevious) { this.pcolor = this.h2rgba(this.o.fgcolor, "0.4"); this.fgcolor = this.h2rgba(this.o.fgcolor, "0.6"); } else { this.fgcolor = this.o.fgcolor; } return this; }; this._clear = function () { this.$c[0].width = this.$c[0].width; }; this._validate = function (v) { var val = (~~ (((v < 0) ? -0.5 : 0.5) + (v/this.o.step))) * this.o.step; return math.round(val * 100) / 100; }; // abstract methods this.listen = function () {}; // on start, one time this.extend = function () {}; // each time configure triggered this.init = function () {}; // each time configure triggered this.change = function (v) {}; // on change this.val = function (v) {}; // on release this.xy2val = function (x, y) {}; // this.draw = function () {}; // on change / on release this.clear = function () { this._clear(); }; // utils this.h2rgba = function (h, a) { var rgb; h = h.substring(1,7) rgb = [ parseint(h.substring(0,2), 16), parseint(h.substring(2,4), 16), parseint(h.substring(4,6), 16) ]; return "rgba(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + "," + a + ")"; }; this.copy = function (f, t) { for (var i in f) { t[i] = f[i]; } }; }; /** * k.dial */ k.dial = function () { k.o.call(this); this.startangle = null; this.xy = null; this.radius = null; this.linewidth = null; this.cursorext = null; this.w2 = null; this.pi2 = 2*math.pi; this.extend = function () { this.o = $.extend({ bgcolor: this.$.data('bgcolor') || '#eeeeee', angleoffset: this.$.data('angleoffset') || 0, anglearc: this.$.data('anglearc') || 360, inline: true }, this.o); }; this.val = function (v, triggerrelease) { if (null != v) { // reverse format v = this.o.parse(v); if (triggerrelease !== false && v != this.v && this.rh && this.rh(v) === false) { return; } this.cv = this.o.stopper ? max(min(v, this.o.max), this.o.min) : v; this.v = this.cv; this.$.val(this.o.format(this.v)); this._draw(); } else { return this.v; } }; this.xy2val = function (x, y) { var a, ret; a = math.atan2( x - (this.x + this.w2), - (y - this.y - this.w2) ) - this.angleoffset; if (this.o.flip) { a = this.anglearc - a - this.pi2; } if (this.anglearc != this.pi2 && (a < 0) && (a > -0.5)) { // if isset anglearc option, set to min if .5 under min a = 0; } else if (a < 0) { a += this.pi2; } ret = (a * (this.o.max - this.o.min) / this.anglearc) + this.o.min; this.o.stopper && (ret = max(min(ret, this.o.max), this.o.min)); return ret; }; this.listen = function () { // bind mousewheel var s = this, mwtimerstop, mwtimerrelease, mw = function (e) { e.preventdefault(); var ori = e.originalevent, deltax = ori.detail || ori.wheeldeltax, deltay = ori.detail || ori.wheeldeltay, v = s._validate(s.o.parse(s.$.val())) + ( deltax > 0 || deltay > 0 ? s.o.step : deltax < 0 || deltay < 0 ? -s.o.step : 0 ); v = max(min(v, s.o.max), s.o.min); s.val(v, false); if (s.rh) { // handle mousewheel stop cleartimeout(mwtimerstop); mwtimerstop = settimeout(function () { s.rh(v); mwtimerstop = null; }, 100); // handle mousewheel releases if (!mwtimerrelease) { mwtimerrelease = settimeout(function () { if (mwtimerstop) s.rh(v); mwtimerrelease = null; }, 200); } } }, kval, to, m = 1, kv = { 37: -s.o.step, 38: s.o.step, 39: s.o.step, 40: -s.o.step }; this.$ .bind( "keydown", function (e) { var kc = e.keycode; // numpad support if (kc >= 96 && kc <= 105) { kc = e.keycode = kc - 48; } kval = parseint(string.fromcharcode(kc)); if (isnan(kval)) { (kc !== 13) // enter && kc !== 8 // bs && kc !== 9 // tab && kc !== 189 // - && (kc !== 190 || s.$.val().match(/\./)) // . allowed once && e.preventdefault(); // arrows if ($.inarray(kc,[37,38,39,40]) > -1) { e.preventdefault(); var v = s.o.parse(s.$.val()) + kv[kc] * m; s.o.stopper && (v = max(min(v, s.o.max), s.o.min)); s.change(s._validate(v)); s._draw(); // long time keydown speed-up to = window.settimeout(function () { m *= 2; }, 30); } } } ) .bind( "keyup", function (e) { if (isnan(kval)) { if (to) { window.cleartimeout(to); to = null; m = 1; s.val(s.$.val()); } } else { // kval postcond (s.$.val() > s.o.max && s.$.val(s.o.max)) || (s.$.val() < s.o.min && s.$.val(s.o.min)); } } ); this.$c.bind("mousewheel dommousescroll", mw); this.$.bind("mousewheel dommousescroll", mw) }; this.init = function () { if (this.v < this.o.min || this.v > this.o.max) { this.v = this.o.min; } this.$.val(this.v); this.w2 = this.w / 2; this.cursorext = this.o.cursor / 100; this.xy = this.w2 * this.scale; this.linewidth = this.xy * this.o.thickness; this.linecap = this.o.linecap; this.radius = this.xy - this.linewidth / 2; this.o.angleoffset && (this.o.angleoffset = isnan(this.o.angleoffset) ? 0 : this.o.angleoffset); this.o.anglearc && (this.o.anglearc = isnan(this.o.anglearc) ? this.pi2 : this.o.anglearc); // deg to rad this.angleoffset = this.o.angleoffset * math.pi / 180; this.anglearc = this.o.anglearc * math.pi / 180; // compute start and end angles this.startangle = 1.5 * math.pi + this.angleoffset; this.endangle = 1.5 * math.pi + this.angleoffset + this.anglearc; var s = max( string(math.abs(this.o.max)).length, string(math.abs(this.o.min)).length, 2 ) + 2; this.o.displayinput && this.i.css({ 'width' : ((this.w / 2 + 4) >> 0) + 'px', 'height' : ((this.w / 3) >> 0) + 'px', 'position' : 'absolute', 'vertical-align' : 'middle', 'margin-top' : ((this.w / 3) >> 0) + 'px', 'margin-left' : '-' + ((this.w * 3 / 4 + 2) >> 0) + 'px', 'border' : 0, 'background' : 'none', 'font' : this.o.fontweight + ' ' + ((this.w / s) >> 0) + 'px ' + this.o.font, 'text-align' : 'center', 'color' : this.o.inputcolor || this.o.fgcolor, 'padding' : '0px', '-webkit-appearance': 'none' }) || this.i.css({ 'width': '0px', 'visibility': 'hidden' }); }; this.change = function (v) { this.cv = v; this.$.val(this.o.format(v)); }; this.angle = function (v) { return (v - this.o.min) * this.anglearc / (this.o.max - this.o.min); }; this.arc = function (v) { var sa, ea; v = this.angle(v); if (this.o.flip) { sa = this.endangle + 0.00001; ea = sa - v - 0.00001; } else { sa = this.startangle - 0.00001; ea = sa + v + 0.00001; } this.o.cursor && (sa = ea - this.cursorext) && (ea = ea + this.cursorext); return { s: sa, e: ea, d: this.o.flip && !this.o.cursor }; }; this.draw = function () { var c = this.g, // context a = this.arc(this.cv), // arc pa, // previous arc r = 1; c.linewidth = this.linewidth; c.linecap = this.linecap; if (this.o.bgcolor !== "none") { c.beginpath(); c.strokestyle = this.o.bgcolor; c.arc(this.xy, this.xy, this.radius, this.endangle - 0.00001, this.startangle + 0.00001, true); c.stroke(); } if (this.o.displayprevious) { pa = this.arc(this.v); c.beginpath(); c.strokestyle = this.pcolor; c.arc(this.xy, this.xy, this.radius, pa.s, pa.e, pa.d); c.stroke(); r = this.cv == this.v; } c.beginpath(); c.strokestyle = r ? this.o.fgcolor : this.fgcolor ; c.arc(this.xy, this.xy, this.radius, a.s, a.e, a.d); c.stroke(); }; this.cancel = function () { this.val(this.v); }; }; $.fn.dial = $.fn.knob = function (o) { return this.each( function () { var d = new k.dial(); d.o = o; d.$ = $(this); d.run(); } ).parent(); }; }));