PageSourceSearch

https://iranartstars.com/wp-content/plugins/reviewer/public/assets/js/jquery.knob.js?ver=3.14.2

js iranartstars.com collected 2026-10-03 18:28:03 UTC 24,920 bytes, 766 lines download raw bytes

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