Skip to content
Snippets Groups Projects
ion.rangeSlider.js 82.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • hwp's avatar
    hwp committed
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    // Ion.RangeSlider
    // version 2.3.1 Build: 382
    // © Denis Ineshin, 2019
    // https://github.com/IonDen
    //
    // Project page:    http://ionden.com/a/plugins/ion.rangeSlider/en.html
    // GitHub page:     https://github.com/IonDen/ion.rangeSlider
    //
    // Released under MIT licence:
    // http://ionden.com/a/plugins/licence-en.html
    // =====================================================================================================================
    
    ;(function(factory) {
        if ((typeof jQuery === 'undefined' || !jQuery) && typeof define === "function" && define.amd) {
            define(["jquery"], function (jQuery) {
                return factory(jQuery, document, window, navigator);
            });
        } else if ((typeof jQuery === 'undefined' || !jQuery) && typeof exports === "object") {
            factory(require("jquery"), document, window, navigator);
        } else {
            factory(jQuery, document, window, navigator);
        }
    } (function ($, document, window, navigator, undefined) {
        "use strict";
    
        // =================================================================================================================
        // Service
    
        var plugin_count = 0;
    
        // IE8 fix
        var is_old_ie = (function () {
            var n = navigator.userAgent,
                r = /msie\s\d+/i,
                v;
            if (n.search(r) > 0) {
                v = r.exec(n).toString();
                v = v.split(" ")[1];
                if (v < 9) {
                    $("html").addClass("lt-ie9");
                    return true;
                }
            }
            return false;
        } ());
        if (!Function.prototype.bind) {
            Function.prototype.bind = function bind(that) {
    
                var target = this;
                var slice = [].slice;
    
                if (typeof target != "function") {
                    throw new TypeError();
                }
    
                var args = slice.call(arguments, 1),
                    bound = function () {
    
                        if (this instanceof bound) {
    
                            var F = function(){};
                            F.prototype = target.prototype;
                            var self = new F();
    
                            var result = target.apply(
                                self,
                                args.concat(slice.call(arguments))
                            );
                            if (Object(result) === result) {
                                return result;
                            }
                            return self;
    
                        } else {
    
                            return target.apply(
                                that,
                                args.concat(slice.call(arguments))
                            );
    
                        }
    
                    };
    
                return bound;
            };
        }
        if (!Array.prototype.indexOf) {
            Array.prototype.indexOf = function(searchElement, fromIndex) {
                var k;
                if (this == null) {
                    throw new TypeError('"this" is null or not defined');
                }
                var O = Object(this);
                var len = O.length >>> 0;
                if (len === 0) {
                    return -1;
                }
                var n = +fromIndex || 0;
                if (Math.abs(n) === Infinity) {
                    n = 0;
                }
                if (n >= len) {
                    return -1;
                }
                k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
                while (k < len) {
                    if (k in O && O[k] === searchElement) {
                        return k;
                    }
                    k++;
                }
                return -1;
            };
        }
    
    
    
        // =================================================================================================================
        // Template
    
        var base_html =
            '<span class="irs">' +
            '<span class="irs-line" tabindex="0"></span>' +
            '<span class="irs-min">0</span><span class="irs-max">1</span>' +
            '<span class="irs-from">0</span><span class="irs-to">0</span><span class="irs-single">0</span>' +
            '</span>' +
            '<span class="irs-grid"></span>';
    
        var single_html =
            '<span class="irs-bar irs-bar--single"></span>' +
            '<span class="irs-shadow shadow-single"></span>' +
            '<span class="irs-handle single"><i></i><i></i><i></i></span>';
    
        var double_html =
            '<span class="irs-bar"></span>' +
            '<span class="irs-shadow shadow-from"></span>' +
            '<span class="irs-shadow shadow-to"></span>' +
            '<span class="irs-handle from"><i></i><i></i><i></i></span>' +
            '<span class="irs-handle to"><i></i><i></i><i></i></span>';
    
        var disable_html =
            '<span class="irs-disable-mask"></span>';
    
    
    
        // =================================================================================================================
        // Core
    
        /**
         * Main plugin constructor
         *
         * @param input {Object} link to base input element
         * @param options {Object} slider config
         * @param plugin_count {Number}
         * @constructor
         */
        var IonRangeSlider = function (input, options, plugin_count) {
            this.VERSION = "2.3.1";
            this.input = input;
            this.plugin_count = plugin_count;
            this.current_plugin = 0;
            this.calc_count = 0;
            this.update_tm = 0;
            this.old_from = 0;
            this.old_to = 0;
            this.old_min_interval = null;
            this.raf_id = null;
            this.dragging = false;
            this.force_redraw = false;
            this.no_diapason = false;
            this.has_tab_index = true;
            this.is_key = false;
            this.is_update = false;
            this.is_start = true;
            this.is_finish = false;
            this.is_active = false;
            this.is_resize = false;
            this.is_click = false;
    
            options = options || {};
    
            // cache for links to all DOM elements
            this.$cache = {
                win: $(window),
                body: $(document.body),
                input: $(input),
                cont: null,
                rs: null,
                min: null,
                max: null,
                from: null,
                to: null,
                single: null,
                bar: null,
                line: null,
                s_single: null,
                s_from: null,
                s_to: null,
                shad_single: null,
                shad_from: null,
                shad_to: null,
                edge: null,
                grid: null,
                grid_labels: []
            };
    
            // storage for measure variables
            this.coords = {
                // left
                x_gap: 0,
                x_pointer: 0,
    
                // width
                w_rs: 0,
                w_rs_old: 0,
                w_handle: 0,
    
                // percents
                p_gap: 0,
                p_gap_left: 0,
                p_gap_right: 0,
                p_step: 0,
                p_pointer: 0,
                p_handle: 0,
                p_single_fake: 0,
                p_single_real: 0,
                p_from_fake: 0,
                p_from_real: 0,
                p_to_fake: 0,
                p_to_real: 0,
                p_bar_x: 0,
                p_bar_w: 0,
    
                // grid
                grid_gap: 0,
                big_num: 0,
                big: [],
                big_w: [],
                big_p: [],
                big_x: []
            };
    
            // storage for labels measure variables
            this.labels = {
                // width
                w_min: 0,
                w_max: 0,
                w_from: 0,
                w_to: 0,
                w_single: 0,
    
                // percents
                p_min: 0,
                p_max: 0,
                p_from_fake: 0,
                p_from_left: 0,
                p_to_fake: 0,
                p_to_left: 0,
                p_single_fake: 0,
                p_single_left: 0
            };
    
    
    
            /**
             * get and validate config
             */
            var $inp = this.$cache.input,
                val = $inp.prop("value"),
                config, config_from_data, prop;
    
            // default config
            config = {
                skin: "flat",
                type: "single",
    
                min: 10,
                max: 100,
                from: null,
                to: null,
                step: 1,
    
                min_interval: 0,
                max_interval: 0,
                drag_interval: false,
    
                values: [],
                p_values: [],
    
                from_fixed: false,
                from_min: null,
                from_max: null,
                from_shadow: false,
    
                to_fixed: false,
                to_min: null,
                to_max: null,
                to_shadow: false,
    
                prettify_enabled: true,
                prettify_separator: " ",
                prettify: null,
    
                force_edges: false,
    
                keyboard: true,
    
                grid: false,
                grid_margin: true,
                grid_num: 4,
                grid_snap: false,
    
                hide_min_max: false,
                hide_from_to: false,
    
                prefix: "",
                postfix: "",
                max_postfix: "",
                decorate_both: true,
                values_separator: "",
    
                input_values_separator: ";",
    
                disable: false,
                block: false,
    
                extra_classes: "",
    
                scope: null,
                onStart: null,
                onChange: null,
                onFinish: null,
                onUpdate: null
            };
    
    
            // check if base element is input
            if ($inp[0].nodeName !== "INPUT") {
                console && console.warn && console.warn("Base element should be <input>!", $inp[0]);
            }
    
    
            // config from data-attributes extends js config
            config_from_data = {
                skin: $inp.data("skin"),
                type: $inp.data("type"),
    
                min: $inp.data("min"),
                max: $inp.data("max"),
                from: $inp.data("from"),
                to: $inp.data("to"),
                step: $inp.data("step"),
    
                min_interval: $inp.data("minInterval"),
                max_interval: $inp.data("maxInterval"),
                drag_interval: $inp.data("dragInterval"),
    
                values: $inp.data("values"),
    
                from_fixed: $inp.data("fromFixed"),
                from_min: $inp.data("fromMin"),
                from_max: $inp.data("fromMax"),
                from_shadow: $inp.data("fromShadow"),
    
                to_fixed: $inp.data("toFixed"),
                to_min: $inp.data("toMin"),
                to_max: $inp.data("toMax"),
                to_shadow: $inp.data("toShadow"),
    
                prettify_enabled: $inp.data("prettifyEnabled"),
                prettify_separator: $inp.data("prettifySeparator"),
    
                force_edges: $inp.data("forceEdges"),
    
                keyboard: $inp.data("keyboard"),
    
                grid: $inp.data("grid"),
                grid_margin: $inp.data("gridMargin"),
                grid_num: $inp.data("gridNum"),
                grid_snap: $inp.data("gridSnap"),
    
                hide_min_max: $inp.data("hideMinMax"),
                hide_from_to: $inp.data("hideFromTo"),
    
                prefix: $inp.data("prefix"),
                postfix: $inp.data("postfix"),
                max_postfix: $inp.data("maxPostfix"),
                decorate_both: $inp.data("decorateBoth"),
                values_separator: $inp.data("valuesSeparator"),
    
                input_values_separator: $inp.data("inputValuesSeparator"),
    
                disable: $inp.data("disable"),
                block: $inp.data("block"),
    
                extra_classes: $inp.data("extraClasses"),
            };
            config_from_data.values = config_from_data.values && config_from_data.values.split(",");
    
            for (prop in config_from_data) {
                if (config_from_data.hasOwnProperty(prop)) {
                    if (config_from_data[prop] === undefined || config_from_data[prop] === "") {
                        delete config_from_data[prop];
                    }
                }
            }
    
    
            // input value extends default config
            if (val !== undefined && val !== "") {
                val = val.split(config_from_data.input_values_separator || options.input_values_separator || ";");
    
                if (val[0] && val[0] == +val[0]) {
                    val[0] = +val[0];
                }
                if (val[1] && val[1] == +val[1]) {
                    val[1] = +val[1];
                }
    
                if (options && options.values && options.values.length) {
                    config.from = val[0] && options.values.indexOf(val[0]);
                    config.to = val[1] && options.values.indexOf(val[1]);
                } else {
                    config.from = val[0] && +val[0];
                    config.to = val[1] && +val[1];
                }
            }
    
    
    
            // js config extends default config
            $.extend(config, options);
    
    
            // data config extends config
            $.extend(config, config_from_data);
            this.options = config;
    
    
    
            // validate config, to be sure that all data types are correct
            this.update_check = {};
            this.validate();
    
    
    
            // default result object, returned to callbacks
            this.result = {
                input: this.$cache.input,
                slider: null,
    
                min: this.options.min,
                max: this.options.max,
    
                from: this.options.from,
                from_percent: 0,
                from_value: null,
    
                to: this.options.to,
                to_percent: 0,
                to_value: null
            };
    
    
    
            this.init();
        };
    
        IonRangeSlider.prototype = {
    
            /**
             * Starts or updates the plugin instance
             *
             * @param [is_update] {boolean}
             */
            init: function (is_update) {
                this.no_diapason = false;
                this.coords.p_step = this.convertToPercent(this.options.step, true);
    
                this.target = "base";
    
                this.toggleInput();
                this.append();
                this.setMinMax();
    
                if (is_update) {
                    this.force_redraw = true;
                    this.calc(true);
    
                    // callbacks called
                    this.callOnUpdate();
                } else {
                    this.force_redraw = true;
                    this.calc(true);
    
                    // callbacks called
                    this.callOnStart();
                }
    
                this.updateScene();
            },
    
            /**
             * Appends slider template to a DOM
             */
            append: function () {
                var container_html = '<span class="irs irs--' + this.options.skin + ' js-irs-' + this.plugin_count + ' ' + this.options.extra_classes + '"></span>';
                this.$cache.input.before(container_html);
                this.$cache.input.prop("readonly", true);
                this.$cache.cont = this.$cache.input.prev();
                this.result.slider = this.$cache.cont;
    
                this.$cache.cont.html(base_html);
                this.$cache.rs = this.$cache.cont.find(".irs");
                this.$cache.min = this.$cache.cont.find(".irs-min");
                this.$cache.max = this.$cache.cont.find(".irs-max");
                this.$cache.from = this.$cache.cont.find(".irs-from");
                this.$cache.to = this.$cache.cont.find(".irs-to");
                this.$cache.single = this.$cache.cont.find(".irs-single");
                this.$cache.line = this.$cache.cont.find(".irs-line");
                this.$cache.grid = this.$cache.cont.find(".irs-grid");
    
                if (this.options.type === "single") {
                    this.$cache.cont.append(single_html);
                    this.$cache.bar = this.$cache.cont.find(".irs-bar");
                    this.$cache.edge = this.$cache.cont.find(".irs-bar-edge");
                    this.$cache.s_single = this.$cache.cont.find(".single");
                    this.$cache.from[0].style.visibility = "hidden";
                    this.$cache.to[0].style.visibility = "hidden";
                    this.$cache.shad_single = this.$cache.cont.find(".shadow-single");
                } else {
                    this.$cache.cont.append(double_html);
                    this.$cache.bar = this.$cache.cont.find(".irs-bar");
                    this.$cache.s_from = this.$cache.cont.find(".from");
                    this.$cache.s_to = this.$cache.cont.find(".to");
                    this.$cache.shad_from = this.$cache.cont.find(".shadow-from");
                    this.$cache.shad_to = this.$cache.cont.find(".shadow-to");
    
                    this.setTopHandler();
                }
    
                if (this.options.hide_from_to) {
                    this.$cache.from[0].style.display = "none";
                    this.$cache.to[0].style.display = "none";
                    this.$cache.single[0].style.display = "none";
                }
    
                this.appendGrid();
    
                if (this.options.disable) {
                    this.appendDisableMask();
                    this.$cache.input[0].disabled = true;
                } else {
                    this.$cache.input[0].disabled = false;
                    this.removeDisableMask();
                    this.bindEvents();
                }
    
                // block only if not disabled
                if (!this.options.disable) {
                    if (this.options.block) {
                        this.appendDisableMask();
                    } else {
                        this.removeDisableMask();
                    }
                }
    
                if (this.options.drag_interval) {
                    this.$cache.bar[0].style.cursor = "ew-resize";
                }
            },
    
            /**
             * Determine which handler has a priority
             * works only for double slider type
             */
            setTopHandler: function () {
                var min = this.options.min,
                    max = this.options.max,
                    from = this.options.from,
                    to = this.options.to;
    
                if (from > min && to === max) {
                    this.$cache.s_from.addClass("type_last");
                } else if (to < max) {
                    this.$cache.s_to.addClass("type_last");
                }
            },
    
            /**
             * Determine which handles was clicked last
             * and which handler should have hover effect
             *
             * @param target {String}
             */
            changeLevel: function (target) {
                switch (target) {
                    case "single":
                        this.coords.p_gap = this.toFixed(this.coords.p_pointer - this.coords.p_single_fake);
                        this.$cache.s_single.addClass("state_hover");
                        break;
                    case "from":
                        this.coords.p_gap = this.toFixed(this.coords.p_pointer - this.coords.p_from_fake);
                        this.$cache.s_from.addClass("state_hover");
                        this.$cache.s_from.addClass("type_last");
                        this.$cache.s_to.removeClass("type_last");
                        break;
                    case "to":
                        this.coords.p_gap = this.toFixed(this.coords.p_pointer - this.coords.p_to_fake);
                        this.$cache.s_to.addClass("state_hover");
                        this.$cache.s_to.addClass("type_last");
                        this.$cache.s_from.removeClass("type_last");
                        break;
                    case "both":
                        this.coords.p_gap_left = this.toFixed(this.coords.p_pointer - this.coords.p_from_fake);
                        this.coords.p_gap_right = this.toFixed(this.coords.p_to_fake - this.coords.p_pointer);
                        this.$cache.s_to.removeClass("type_last");
                        this.$cache.s_from.removeClass("type_last");
                        break;
                }
            },
    
            /**
             * Then slider is disabled
             * appends extra layer with opacity
             */
            appendDisableMask: function () {
                this.$cache.cont.append(disable_html);
                this.$cache.cont.addClass("irs-disabled");
            },
    
            /**
             * Then slider is not disabled
             * remove disable mask
             */
            removeDisableMask: function () {
                this.$cache.cont.remove(".irs-disable-mask");
                this.$cache.cont.removeClass("irs-disabled");
            },
    
            /**
             * Remove slider instance
             * and unbind all events
             */
            remove: function () {
                this.$cache.cont.remove();
                this.$cache.cont = null;
    
                this.$cache.line.off("keydown.irs_" + this.plugin_count);
    
                this.$cache.body.off("touchmove.irs_" + this.plugin_count);
                this.$cache.body.off("mousemove.irs_" + this.plugin_count);
    
                this.$cache.win.off("touchend.irs_" + this.plugin_count);
                this.$cache.win.off("mouseup.irs_" + this.plugin_count);
    
                if (is_old_ie) {
                    this.$cache.body.off("mouseup.irs_" + this.plugin_count);
                    this.$cache.body.off("mouseleave.irs_" + this.plugin_count);
                }
    
                this.$cache.grid_labels = [];
                this.coords.big = [];
                this.coords.big_w = [];
                this.coords.big_p = [];
                this.coords.big_x = [];
    
                cancelAnimationFrame(this.raf_id);
            },
    
            /**
             * bind all slider events
             */
            bindEvents: function () {
                if (this.no_diapason) {
                    return;
                }
    
                this.$cache.body.on("touchmove.irs_" + this.plugin_count, this.pointerMove.bind(this));
                this.$cache.body.on("mousemove.irs_" + this.plugin_count, this.pointerMove.bind(this));
    
                this.$cache.win.on("touchend.irs_" + this.plugin_count, this.pointerUp.bind(this));
                this.$cache.win.on("mouseup.irs_" + this.plugin_count, this.pointerUp.bind(this));
    
                this.$cache.line.on("touchstart.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
                this.$cache.line.on("mousedown.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
    
                this.$cache.line.on("focus.irs_" + this.plugin_count, this.pointerFocus.bind(this));
    
                if (this.options.drag_interval && this.options.type === "double") {
                    this.$cache.bar.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, "both"));
                    this.$cache.bar.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, "both"));
                } else {
                    this.$cache.bar.on("touchstart.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
                    this.$cache.bar.on("mousedown.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
                }
    
                if (this.options.type === "single") {
                    this.$cache.single.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, "single"));
                    this.$cache.s_single.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, "single"));
                    this.$cache.shad_single.on("touchstart.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
    
                    this.$cache.single.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, "single"));
                    this.$cache.s_single.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, "single"));
                    this.$cache.edge.on("mousedown.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
                    this.$cache.shad_single.on("mousedown.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
                } else {
                    this.$cache.single.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, null));
                    this.$cache.single.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, null));
    
                    this.$cache.from.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, "from"));
                    this.$cache.s_from.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, "from"));
                    this.$cache.to.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, "to"));
                    this.$cache.s_to.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, "to"));
                    this.$cache.shad_from.on("touchstart.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
                    this.$cache.shad_to.on("touchstart.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
    
                    this.$cache.from.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, "from"));
                    this.$cache.s_from.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, "from"));
                    this.$cache.to.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, "to"));
                    this.$cache.s_to.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, "to"));
                    this.$cache.shad_from.on("mousedown.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
                    this.$cache.shad_to.on("mousedown.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
                }
    
                if (this.options.keyboard) {
                    this.$cache.line.on("keydown.irs_" + this.plugin_count, this.key.bind(this, "keyboard"));
                }
    
                if (is_old_ie) {
                    this.$cache.body.on("mouseup.irs_" + this.plugin_count, this.pointerUp.bind(this));
                    this.$cache.body.on("mouseleave.irs_" + this.plugin_count, this.pointerUp.bind(this));
                }
            },
    
            /**
             * Focus with tabIndex
             *
             * @param e {Object} event object
             */
            pointerFocus: function (e) {
                if (!this.target) {
                    var x;
                    var $handle;
    
                    if (this.options.type === "single") {
                        $handle = this.$cache.single;
                    } else {
                        $handle = this.$cache.from;
                    }
    
                    x = $handle.offset().left;
                    x += ($handle.width() / 2) - 1;
    
                    this.pointerClick("single", {preventDefault: function () {}, pageX: x});
                }
            },
    
            /**
             * Mousemove or touchmove
             * only for handlers
             *
             * @param e {Object} event object
             */
            pointerMove: function (e) {
                if (!this.dragging) {
                    return;
                }
    
                var x = e.pageX || e.originalEvent.touches && e.originalEvent.touches[0].pageX;
                this.coords.x_pointer = x - this.coords.x_gap;
    
                this.calc();
            },
    
            /**
             * Mouseup or touchend
             * only for handlers
             *
             * @param e {Object} event object
             */
            pointerUp: function (e) {
                if (this.current_plugin !== this.plugin_count) {
                    return;
                }
    
                if (this.is_active) {
                    this.is_active = false;
                } else {
                    return;
                }
    
                this.$cache.cont.find(".state_hover").removeClass("state_hover");
    
                this.force_redraw = true;
    
                if (is_old_ie) {
                    $("*").prop("unselectable", false);
                }
    
                this.updateScene();
                this.restoreOriginalMinInterval();
    
                // callbacks call
                if ($.contains(this.$cache.cont[0], e.target) || this.dragging) {
                    this.callOnFinish();
                }
    
                this.dragging = false;
            },
    
            /**
             * Mousedown or touchstart
             * only for handlers
             *
             * @param target {String|null}
             * @param e {Object} event object
             */
            pointerDown: function (target, e) {
                e.preventDefault();
                var x = e.pageX || e.originalEvent.touches && e.originalEvent.touches[0].pageX;
                if (e.button === 2) {
                    return;
                }
    
                if (target === "both") {
                    this.setTempMinInterval();
                }
    
                if (!target) {
                    target = this.target || "from";
                }
    
                this.current_plugin = this.plugin_count;
                this.target = target;
    
                this.is_active = true;
                this.dragging = true;
    
                this.coords.x_gap = this.$cache.rs.offset().left;
                this.coords.x_pointer = x - this.coords.x_gap;
    
                this.calcPointerPercent();
                this.changeLevel(target);
    
                if (is_old_ie) {
                    $("*").prop("unselectable", true);
                }
    
                this.$cache.line.trigger("focus");
    
                this.updateScene();
            },
    
            /**
             * Mousedown or touchstart
             * for other slider elements, like diapason line
             *
             * @param target {String}
             * @param e {Object} event object
             */
            pointerClick: function (target, e) {
                e.preventDefault();
                var x = e.pageX || e.originalEvent.touches && e.originalEvent.touches[0].pageX;
                if (e.button === 2) {
                    return;
                }
    
                this.current_plugin = this.plugin_count;
                this.target = target;
    
                this.is_click = true;
                this.coords.x_gap = this.$cache.rs.offset().left;
                this.coords.x_pointer = +(x - this.coords.x_gap).toFixed();
    
                this.force_redraw = true;
                this.calc();
    
                this.$cache.line.trigger("focus");
            },
    
            /**
             * Keyborard controls for focused slider
             *
             * @param target {String}
             * @param e {Object} event object
             * @returns {boolean|undefined}
             */
            key: function (target, e) {
                if (this.current_plugin !== this.plugin_count || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey) {
                    return;
                }
    
                switch (e.which) {
                    case 83: // W
                    case 65: // A
                    case 40: // DOWN
                    case 37: // LEFT
                        e.preventDefault();
                        this.moveByKey(false);
                        break;
    
                    case 87: // S
                    case 68: // D
                    case 38: // UP
                    case 39: // RIGHT
                        e.preventDefault();
                        this.moveByKey(true);
                        break;
                }
    
                return true;
            },
    
            /**
             * Move by key
             *
             * @param right {boolean} direction to move
             */
            moveByKey: function (right) {
                var p = this.coords.p_pointer;
                var p_step = (this.options.max - this.options.min) / 100;
                p_step = this.options.step / p_step;
    
                if (right) {
                    p += p_step;
                } else {
                    p -= p_step;
                }
    
                this.coords.x_pointer = this.toFixed(this.coords.w_rs / 100 * p);
                this.is_key = true;
                this.calc();
            },
    
            /**
             * Set visibility and content
             * of Min and Max labels
             */
            setMinMax: function () {
                if (!this.options) {
                    return;
                }
    
                if (this.options.hide_min_max) {
                    this.$cache.min[0].style.display = "none";
                    this.$cache.max[0].style.display = "none";
                    return;
                }
    
                if (this.options.values.length) {
                    this.$cache.min.html(this.decorate(this.options.p_values[this.options.min]));
                    this.$cache.max.html(this.decorate(this.options.p_values[this.options.max]));
                } else {
                    var min_pretty = this._prettify(this.options.min);
                    var max_pretty = this._prettify(this.options.max);
    
                    this.result.min_pretty = min_pretty;
                    this.result.max_pretty = max_pretty;
    
                    this.$cache.min.html(this.decorate(min_pretty, this.options.min));
                    this.$cache.max.html(this.decorate(max_pretty, this.options.max));
                }
    
                this.labels.w_min = this.$cache.min.outerWidth(false);
                this.labels.w_max = this.$cache.max.outerWidth(false);
            },
    
            /**
             * Then dragging interval, prevent interval collapsing
             * using min_interval option
             */
            setTempMinInterval: function () {
                var interval = this.result.to - this.result.from;
    
                if (this.old_min_interval === null) {
                    this.old_min_interval = this.options.min_interval;
                }
    
                this.options.min_interval = interval;
            },
    
            /**
             * Restore min_interval option to original
             */
            restoreOriginalMinInterval: function () {
                if (this.old_min_interval !== null) {
                    this.options.min_interval = this.old_min_interval;
                    this.old_min_interval = null;
                }
            },
    
    
    
            // =============================================================================================================
            // Calculations
    
            /**
             * All calculations and measures start here