Debian Codegraph @

inst/www/shared/ionrangeslider/js/ion.rangeSlider.js

raw symbols SCIP
1 // Ion.RangeSlider
2 // version 2.3.1 Build: 382
3 // © Denis Ineshin, 2019
4 // https://github.com/IonDen
5 //
6 // Project page: http://ionden.com/a/plugins/ion.rangeSlider/en.html
7 // GitHub page: https://github.com/IonDen/ion.rangeSlider
8 //
9 // Released under MIT licence:
10 // http://ionden.com/a/plugins/licence-en.html
11 // =====================================================================================================================
12
13 ;(function(factory) {
14 if ((typeof jQuery === 'undefined' || !jQuery) && typeof define === "function" && define.amd) {
15 define(["jquery"], function (jQuery) {
16 return factory(jQuery, document, window, navigator);
17 });
18 } else if ((typeof jQuery === 'undefined' || !jQuery) && typeof exports === "object") {
19 factory(require("jquery"), document, window, navigator);
20 } else {
21 factory(jQuery, document, window, navigator);
22 }
23 } (function ($, document, window, navigator, undefined) {
24 "use strict";
25
26 // =================================================================================================================
27 // Service
28
29 var plugin_count = 0;
30
31 // IE8 fix
32 var is_old_ie = (function () {
33 var n = navigator.userAgent,
34 r = /msie\s\d+/i,
35 v;
36 if (n.search(r) > 0) {
37 v = r.exec(n).toString();
38 v = v.split(" ")[1];
39 if (v < 9) {
40 $("html").addClass("lt-ie9");
41 return true;
42 }
43 }
44 return false;
45 } ());
46 if (!Function.prototype.bind) {
47 Function.prototype.bind = function bind(that) {
48
49 var target = this;
50 var slice = [].slice;
51
52 if (typeof target != "function") {
53 throw new TypeError();
54 }
55
56 var args = slice.call(arguments, 1),
57 bound = function () {
58
59 if (this instanceof bound) {
60
61 var F = function(){};
62 F.prototype = target.prototype;
63 var self = new F();
64
65 var result = target.apply(
66 self,
67 args.concat(slice.call(arguments))
68 );
69 if (Object(result) === result) {
70 return result;
71 }
72 return self;
73
74 } else {
75
76 return target.apply(
77 that,
78 args.concat(slice.call(arguments))
79 );
80
81 }
82
83 };
84
85 return bound;
86 };
87 }
88 if (!Array.prototype.indexOf) {
89 Array.prototype.indexOf = function(searchElement, fromIndex) {
90 var k;
91 if (this == null) {
92 throw new TypeError('"this" is null or not defined');
93 }
94 var O = Object(this);
95 var len = O.length >>> 0;
96 if (len === 0) {
97 return -1;
98 }
99 var n = +fromIndex || 0;
100 if (Math.abs(n) === Infinity) {
101 n = 0;
102 }
103 if (n >= len) {
104 return -1;
105 }
106 k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
107 while (k < len) {
108 if (k in O && O[k] === searchElement) {
109 return k;
110 }
111 k++;
112 }
113 return -1;
114 };
115 }
116
117
118
119 // =================================================================================================================
120 // Template
121
122 var base_html =
123 '<span class="irs">' +
124 '<span class="irs-line" tabindex="0"></span>' +
125 '<span class="irs-min">0</span><span class="irs-max">1</span>' +
126 '<span class="irs-from">0</span><span class="irs-to">0</span><span class="irs-single">0</span>' +
127 '</span>' +
128 '<span class="irs-grid"></span>';
129
130 var single_html =
131 '<span class="irs-bar irs-bar--single"></span>' +
132 '<span class="irs-shadow shadow-single"></span>' +
133 '<span class="irs-handle single"><i></i><i></i><i></i></span>';
134
135 var double_html =
136 '<span class="irs-bar"></span>' +
137 '<span class="irs-shadow shadow-from"></span>' +
138 '<span class="irs-shadow shadow-to"></span>' +
139 '<span class="irs-handle from"><i></i><i></i><i></i></span>' +
140 '<span class="irs-handle to"><i></i><i></i><i></i></span>';
141
142 var disable_html =
143 '<span class="irs-disable-mask"></span>';
144
145
146
147 // =================================================================================================================
148 // Core
149
150 /**
151 * Main plugin constructor
152 *
153 * @param input {Object} link to base input element
154 * @param options {Object} slider config
155 * @param plugin_count {Number}
156 * @constructor
157 */
158 var IonRangeSlider = function (input, options, plugin_count) {
159 this.VERSION = "2.3.1";
160 this.input = input;
161 this.plugin_count = plugin_count;
162 this.current_plugin = 0;
163 this.calc_count = 0;
164 this.update_tm = 0;
165 this.old_from = 0;
166 this.old_to = 0;
167 this.old_min_interval = null;
168 this.raf_id = null;
169 this.dragging = false;
170 this.force_redraw = false;
171 this.no_diapason = false;
172 this.has_tab_index = true;
173 this.is_key = false;
174 this.is_update = false;
175 this.is_start = true;
176 this.is_finish = false;
177 this.is_active = false;
178 this.is_resize = false;
179 this.is_click = false;
180
181 options = options || {};
182
183 // cache for links to all DOM elements
184 this.$cache = {
185 win: $(window),
186 body: $(document.body),
187 input: $(input),
188 cont: null,
189 rs: null,
190 min: null,
191 max: null,
192 from: null,
193 to: null,
194 single: null,
195 bar: null,
196 line: null,
197 s_single: null,
198 s_from: null,
199 s_to: null,
200 shad_single: null,
201 shad_from: null,
202 shad_to: null,
203 edge: null,
204 grid: null,
205 grid_labels: []
206 };
207
208 // storage for measure variables
209 this.coords = {
210 // left
211 x_gap: 0,
212 x_pointer: 0,
213
214 // width
215 w_rs: 0,
216 w_rs_old: 0,
217 w_handle: 0,
218
219 // percents
220 p_gap: 0,
221 p_gap_left: 0,
222 p_gap_right: 0,
223 p_step: 0,
224 p_pointer: 0,
225 p_handle: 0,
226 p_single_fake: 0,
227 p_single_real: 0,
228 p_from_fake: 0,
229 p_from_real: 0,
230 p_to_fake: 0,
231 p_to_real: 0,
232 p_bar_x: 0,
233 p_bar_w: 0,
234
235 // grid
236 grid_gap: 0,
237 big_num: 0,
238 big: [],
239 big_w: [],
240 big_p: [],
241 big_x: []
242 };
243
244 // storage for labels measure variables
245 this.labels = {
246 // width
247 w_min: 0,
248 w_max: 0,
249 w_from: 0,
250 w_to: 0,
251 w_single: 0,
252
253 // percents
254 p_min: 0,
255 p_max: 0,
256 p_from_fake: 0,
257 p_from_left: 0,
258 p_to_fake: 0,
259 p_to_left: 0,
260 p_single_fake: 0,
261 p_single_left: 0
262 };
263
264
265
266 /**
267 * get and validate config
268 */
269 var $inp = this.$cache.input,
270 val = $inp.prop("value"),
271 config, config_from_data, prop;
272
273 // default config
274 config = {
275 skin: "flat",
276 type: "single",
277
278 min: 10,
279 max: 100,
280 from: null,
281 to: null,
282 step: 1,
283
284 min_interval: 0,
285 max_interval: 0,
286 drag_interval: false,
287
288 values: [],
289 p_values: [],
290
291 from_fixed: false,
292 from_min: null,
293 from_max: null,
294 from_shadow: false,
295
296 to_fixed: false,
297 to_min: null,
298 to_max: null,
299 to_shadow: false,
300
301 prettify_enabled: true,
302 prettify_separator: " ",
303 prettify: null,
304
305 force_edges: false,
306
307 keyboard: true,
308
309 grid: false,
310 grid_margin: true,
311 grid_num: 4,
312 grid_snap: false,
313
314 hide_min_max: false,
315 hide_from_to: false,
316
317 prefix: "",
318 postfix: "",
319 max_postfix: "",
320 decorate_both: true,
321 values_separator: " — ",
322
323 input_values_separator: ";",
324
325 disable: false,
326 block: false,
327
328 extra_classes: "",
329
330 scope: null,
331 onStart: null,
332 onChange: null,
333 onFinish: null,
334 onUpdate: null
335 };
336
337
338 // check if base element is input
339 if ($inp[0].nodeName !== "INPUT") {
340 console && console.warn && console.warn("Base element should be <input>!", $inp[0]);
341 }
342
343
344 // config from data-attributes extends js config
345 config_from_data = {
346 skin: $inp.data("skin"),
347 type: $inp.data("type"),
348
349 min: $inp.data("min"),
350 max: $inp.data("max"),
351 from: $inp.data("from"),
352 to: $inp.data("to"),
353 step: $inp.data("step"),
354
355 min_interval: $inp.data("minInterval"),
356 max_interval: $inp.data("maxInterval"),
357 drag_interval: $inp.data("dragInterval"),
358
359 values: $inp.data("values"),
360
361 from_fixed: $inp.data("fromFixed"),
362 from_min: $inp.data("fromMin"),
363 from_max: $inp.data("fromMax"),
364 from_shadow: $inp.data("fromShadow"),
365
366 to_fixed: $inp.data("toFixed"),
367 to_min: $inp.data("toMin"),
368 to_max: $inp.data("toMax"),
369 to_shadow: $inp.data("toShadow"),
370
371 prettify_enabled: $inp.data("prettifyEnabled"),
372 prettify_separator: $inp.data("prettifySeparator"),
373
374 force_edges: $inp.data("forceEdges"),
375
376 keyboard: $inp.data("keyboard"),
377
378 grid: $inp.data("grid"),
379 grid_margin: $inp.data("gridMargin"),
380 grid_num: $inp.data("gridNum"),
381 grid_snap: $inp.data("gridSnap"),
382
383 hide_min_max: $inp.data("hideMinMax"),
384 hide_from_to: $inp.data("hideFromTo"),
385
386 prefix: $inp.data("prefix"),
387 postfix: $inp.data("postfix"),
388 max_postfix: $inp.data("maxPostfix"),
389 decorate_both: $inp.data("decorateBoth"),
390 values_separator: $inp.data("valuesSeparator"),
391
392 input_values_separator: $inp.data("inputValuesSeparator"),
393
394 disable: $inp.data("disable"),
395 block: $inp.data("block"),
396
397 extra_classes: $inp.data("extraClasses"),
398 };
399 config_from_data.values = config_from_data.values && config_from_data.values.split(",");
400
401 for (prop in config_from_data) {
402 if (config_from_data.hasOwnProperty(prop)) {
403 if (config_from_data[prop] === undefined || config_from_data[prop] === "") {
404 delete config_from_data[prop];
405 }
406 }
407 }
408
409
410 // input value extends default config
411 if (val !== undefined && val !== "") {
412 val = val.split(config_from_data.input_values_separator || options.input_values_separator || ";");
413
414 if (val[0] && val[0] == +val[0]) {
415 val[0] = +val[0];
416 }
417 if (val[1] && val[1] == +val[1]) {
418 val[1] = +val[1];
419 }
420
421 if (options && options.values && options.values.length) {
422 config.from = val[0] && options.values.indexOf(val[0]);
423 config.to = val[1] && options.values.indexOf(val[1]);
424 } else {
425 config.from = val[0] && +val[0];
426 config.to = val[1] && +val[1];
427 }
428 }
429
430
431
432 // js config extends default config
433 $.extend(config, options);
434
435
436 // data config extends config
437 $.extend(config, config_from_data);
438 this.options = config;
439
440
441
442 // validate config, to be sure that all data types are correct
443 this.update_check = {};
444 this.validate();
445
446
447
448 // default result object, returned to callbacks
449 this.result = {
450 input: this.$cache.input,
451 slider: null,
452
453 min: this.options.min,
454 max: this.options.max,
455
456 from: this.options.from,
457 from_percent: 0,
458 from_value: null,
459
460 to: this.options.to,
461 to_percent: 0,
462 to_value: null
463 };
464
465
466
467 this.init();
468 };
469
470 IonRangeSlider.prototype = {
471
472 /**
473 * Starts or updates the plugin instance
474 *
475 * @param [is_update] {boolean}
476 */
477 init: function (is_update) {
478 this.no_diapason = false;
479 this.coords.p_step = this.convertToPercent(this.options.step, true);
480
481 this.target = "base";
482
483 this.toggleInput();
484 this.append();
485 this.setMinMax();
486
487 if (is_update) {
488 this.force_redraw = true;
489 this.calc(true);
490
491 // callbacks called
492 this.callOnUpdate();
493 } else {
494 this.force_redraw = true;
495 this.calc(true);
496
497 // callbacks called
498 this.callOnStart();
499 }
500
501 this.updateScene();
502 },
503
504 /**
505 * Appends slider template to a DOM
506 */
507 append: function () {
508 var container_html = '<span class="irs irs--' + this.options.skin + ' js-irs-' + this.plugin_count + ' ' + this.options.extra_classes + '"></span>';
509 this.$cache.input.before(container_html);
510 this.$cache.input.prop("readonly", true);
511 this.$cache.cont = this.$cache.input.prev();
512 this.result.slider = this.$cache.cont;
513
514 this.$cache.cont.html(base_html);
515 this.$cache.rs = this.$cache.cont.find(".irs");
516 this.$cache.min = this.$cache.cont.find(".irs-min");
517 this.$cache.max = this.$cache.cont.find(".irs-max");
518 this.$cache.from = this.$cache.cont.find(".irs-from");
519 this.$cache.to = this.$cache.cont.find(".irs-to");
520 this.$cache.single = this.$cache.cont.find(".irs-single");
521 this.$cache.line = this.$cache.cont.find(".irs-line");
522 this.$cache.grid = this.$cache.cont.find(".irs-grid");
523
524 if (this.options.type === "single") {
525 this.$cache.cont.append(single_html);
526 this.$cache.bar = this.$cache.cont.find(".irs-bar");
527 this.$cache.edge = this.$cache.cont.find(".irs-bar-edge");
528 this.$cache.s_single = this.$cache.cont.find(".single");
529 this.$cache.from[0].style.visibility = "hidden";
530 this.$cache.to[0].style.visibility = "hidden";
531 this.$cache.shad_single = this.$cache.cont.find(".shadow-single");
532 } else {
533 this.$cache.cont.append(double_html);
534 this.$cache.bar = this.$cache.cont.find(".irs-bar");
535 this.$cache.s_from = this.$cache.cont.find(".from");
536 this.$cache.s_to = this.$cache.cont.find(".to");
537 this.$cache.shad_from = this.$cache.cont.find(".shadow-from");
538 this.$cache.shad_to = this.$cache.cont.find(".shadow-to");
539
540 this.setTopHandler();
541 }
542
543 if (this.options.hide_from_to) {
544 this.$cache.from[0].style.display = "none";
545 this.$cache.to[0].style.display = "none";
546 this.$cache.single[0].style.display = "none";
547 }
548
549 this.appendGrid();
550
551 if (this.options.disable) {
552 this.appendDisableMask();
553 this.$cache.input[0].disabled = true;
554 } else {
555 this.$cache.input[0].disabled = false;
556 this.removeDisableMask();
557 this.bindEvents();
558 }
559
560 // block only if not disabled
561 if (!this.options.disable) {
562 if (this.options.block) {
563 this.appendDisableMask();
564 } else {
565 this.removeDisableMask();
566 }
567 }
568
569 if (this.options.drag_interval) {
570 this.$cache.bar[0].style.cursor = "ew-resize";
571 }
572 },
573
574 /**
575 * Determine which handler has a priority
576 * works only for double slider type
577 */
578 setTopHandler: function () {
579 var min = this.options.min,
580 max = this.options.max,
581 from = this.options.from,
582 to = this.options.to;
583
584 if (from > min && to === max) {
585 this.$cache.s_from.addClass("type_last");
586 } else if (to < max) {
587 this.$cache.s_to.addClass("type_last");
588 }
589 },
590
591 /**
592 * Determine which handles was clicked last
593 * and which handler should have hover effect
594 *
595 * @param target {String}
596 */
597 changeLevel: function (target) {
598 switch (target) {
599 case "single":
600 this.coords.p_gap = this.toFixed(this.coords.p_pointer - this.coords.p_single_fake);
601 this.$cache.s_single.addClass("state_hover");
602 break;
603 case "from":
604 this.coords.p_gap = this.toFixed(this.coords.p_pointer - this.coords.p_from_fake);
605 this.$cache.s_from.addClass("state_hover");
606 this.$cache.s_from.addClass("type_last");
607 this.$cache.s_to.removeClass("type_last");
608 break;
609 case "to":
610 this.coords.p_gap = this.toFixed(this.coords.p_pointer - this.coords.p_to_fake);
611 this.$cache.s_to.addClass("state_hover");
612 this.$cache.s_to.addClass("type_last");
613 this.$cache.s_from.removeClass("type_last");
614 break;
615 case "both":
616 this.coords.p_gap_left = this.toFixed(this.coords.p_pointer - this.coords.p_from_fake);
617 this.coords.p_gap_right = this.toFixed(this.coords.p_to_fake - this.coords.p_pointer);
618 this.$cache.s_to.removeClass("type_last");
619 this.$cache.s_from.removeClass("type_last");
620 break;
621 }
622 },
623
624 /**
625 * Then slider is disabled
626 * appends extra layer with opacity
627 */
628 appendDisableMask: function () {
629 this.$cache.cont.append(disable_html);
630 this.$cache.cont.addClass("irs-disabled");
631 },
632
633 /**
634 * Then slider is not disabled
635 * remove disable mask
636 */
637 removeDisableMask: function () {
638 this.$cache.cont.remove(".irs-disable-mask");
639 this.$cache.cont.removeClass("irs-disabled");
640 },
641
642 /**
643 * Remove slider instance
644 * and unbind all events
645 */
646 remove: function () {
647 this.$cache.cont.remove();
648 this.$cache.cont = null;
649
650 this.$cache.line.off("keydown.irs_" + this.plugin_count);
651
652 this.$cache.body.off("touchmove.irs_" + this.plugin_count);
653 this.$cache.body.off("mousemove.irs_" + this.plugin_count);
654
655 this.$cache.win.off("touchend.irs_" + this.plugin_count);
656 this.$cache.win.off("mouseup.irs_" + this.plugin_count);
657
658 if (is_old_ie) {
659 this.$cache.body.off("mouseup.irs_" + this.plugin_count);
660 this.$cache.body.off("mouseleave.irs_" + this.plugin_count);
661 }
662
663 this.$cache.grid_labels = [];
664 this.coords.big = [];
665 this.coords.big_w = [];
666 this.coords.big_p = [];
667 this.coords.big_x = [];
668
669 cancelAnimationFrame(this.raf_id);
670 },
671
672 /**
673 * bind all slider events
674 */
675 bindEvents: function () {
676 if (this.no_diapason) {
677 return;
678 }
679
680 this.$cache.body.on("touchmove.irs_" + this.plugin_count, this.pointerMove.bind(this));
681 this.$cache.body.on("mousemove.irs_" + this.plugin_count, this.pointerMove.bind(this));
682
683 this.$cache.win.on("touchend.irs_" + this.plugin_count, this.pointerUp.bind(this));
684 this.$cache.win.on("mouseup.irs_" + this.plugin_count, this.pointerUp.bind(this));
685
686 this.$cache.line.on("touchstart.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
687 this.$cache.line.on("mousedown.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
688
689 this.$cache.line.on("focus.irs_" + this.plugin_count, this.pointerFocus.bind(this));
690
691 if (this.options.drag_interval && this.options.type === "double") {
692 this.$cache.bar.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, "both"));
693 this.$cache.bar.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, "both"));
694 } else {
695 this.$cache.bar.on("touchstart.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
696 this.$cache.bar.on("mousedown.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
697 }
698
699 if (this.options.type === "single") {
700 this.$cache.single.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, "single"));
701 this.$cache.s_single.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, "single"));
702 this.$cache.shad_single.on("touchstart.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
703
704 this.$cache.single.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, "single"));
705 this.$cache.s_single.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, "single"));
706 this.$cache.edge.on("mousedown.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
707 this.$cache.shad_single.on("mousedown.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
708 } else {
709 this.$cache.single.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, null));
710 this.$cache.single.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, null));
711
712 this.$cache.from.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, "from"));
713 this.$cache.s_from.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, "from"));
714 this.$cache.to.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, "to"));
715 this.$cache.s_to.on("touchstart.irs_" + this.plugin_count, this.pointerDown.bind(this, "to"));
716 this.$cache.shad_from.on("touchstart.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
717 this.$cache.shad_to.on("touchstart.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
718
719 this.$cache.from.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, "from"));
720 this.$cache.s_from.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, "from"));
721 this.$cache.to.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, "to"));
722 this.$cache.s_to.on("mousedown.irs_" + this.plugin_count, this.pointerDown.bind(this, "to"));
723 this.$cache.shad_from.on("mousedown.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
724 this.$cache.shad_to.on("mousedown.irs_" + this.plugin_count, this.pointerClick.bind(this, "click"));
725 }
726
727 if (this.options.keyboard) {
728 this.$cache.line.on("keydown.irs_" + this.plugin_count, this.key.bind(this, "keyboard"));
729 }
730
731 if (is_old_ie) {
732 this.$cache.body.on("mouseup.irs_" + this.plugin_count, this.pointerUp.bind(this));
733 this.$cache.body.on("mouseleave.irs_" + this.plugin_count, this.pointerUp.bind(this));
734 }
735 },
736
737 /**
738 * Focus with tabIndex
739 *
740 * @param e {Object} event object
741 */
742 pointerFocus: function (e) {
743 if (!this.target) {
744 var x;
745 var $handle;
746
747 if (this.options.type === "single") {
748 $handle = this.$cache.single;
749 } else {
750 $handle = this.$cache.from;
751 }
752
753 x = $handle.offset().left;
754 x += ($handle.width() / 2) - 1;
755
756 this.pointerClick("single", {preventDefault: function () {}, stopPropagation: function () {}, pageX: x});
757 }
758 },
759
760 /**
761 * Mousemove or touchmove
762 * only for handlers
763 *
764 * @param e {Object} event object
765 */
766 pointerMove: function (e) {
767 if (!this.dragging) {
768 return;
769 }
770
771 var x = e.pageX || e.originalEvent.touches && e.originalEvent.touches[0].pageX;
772 this.coords.x_pointer = x - this.coords.x_gap;
773
774 this.calc();
775 },
776
777 /**
778 * Mouseup or touchend
779 * only for handlers
780 *
781 * @param e {Object} event object
782 */
783 pointerUp: function (e) {
784 if (this.current_plugin !== this.plugin_count) {
785 return;
786 }
787
788 if (this.is_active) {
789 this.is_active = false;
790 } else {
791 return;
792 }
793
794 this.$cache.cont.find(".state_hover").removeClass("state_hover");
795
796 this.force_redraw = true;
797
798 if (is_old_ie) {
799 $("*").prop("unselectable", false);
800 }
801
802 this.updateScene();
803 this.restoreOriginalMinInterval();
804
805 // callbacks call
806 if ($.contains(this.$cache.cont[0], e.target) || this.dragging) {
807 this.callOnFinish();
808 }
809
810 this.dragging = false;
811 },
812
813 /**
814 * Mousedown or touchstart
815 * only for handlers
816 *
817 * @param target {String|null}
818 * @param e {Object} event object
819 */
820 pointerDown: function (target, e) {
821 e.preventDefault();
822 e.stopPropagation();
823 var x = e.pageX || e.originalEvent.touches && e.originalEvent.touches[0].pageX;
824 if (e.button === 2) {
825 return;
826 }
827
828 if (target === "both") {
829 this.setTempMinInterval();
830 }
831
832 if (!target) {
833 target = this.target || "from";
834 }
835
836 this.current_plugin = this.plugin_count;
837 this.target = target;
838
839 this.is_active = true;
840 this.dragging = true;
841
842 this.coords.x_gap = this.$cache.rs.offset().left;
843 this.coords.x_pointer = x - this.coords.x_gap;
844
845 this.calcPointerPercent();
846 this.changeLevel(target);
847
848 if (is_old_ie) {
849 $("*").prop("unselectable", true);
850 }
851
852 this.$cache.line.trigger("focus");
853
854 this.updateScene();
855 },
856
857 /**
858 * Mousedown or touchstart
859 * for other slider elements, like diapason line
860 *
861 * @param target {String}
862 * @param e {Object} event object
863 */
864 pointerClick: function (target, e) {
865 e.preventDefault();
866 e.stopPropagation();
867 var x = e.pageX || e.originalEvent.touches && e.originalEvent.touches[0].pageX;
868 if (e.button === 2) {
869 return;
870 }
871
872 this.current_plugin = this.plugin_count;
873 this.target = target;
874
875 this.is_click = true;
876 this.coords.x_gap = this.$cache.rs.offset().left;
877 this.coords.x_pointer = +(x - this.coords.x_gap).toFixed();
878
879 this.force_redraw = true;
880 this.calc();
881
882 this.$cache.line.trigger("focus");
883 },
884
885 /**
886 * Keyborard controls for focused slider
887 *
888 * @param target {String}
889 * @param e {Object} event object
890 * @returns {boolean|undefined}
891 */
892 key: function (target, e) {
893 if (this.current_plugin !== this.plugin_count || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey) {
894 return;
895 }
896
897 switch (e.which) {
898 case 83: // W
899 case 65: // A
900 case 40: // DOWN
901 case 37: // LEFT
902 e.preventDefault();
903 this.moveByKey(false);
904 break;
905
906 case 87: // S
907 case 68: // D
908 case 38: // UP
909 case 39: // RIGHT
910 e.preventDefault();
911 this.moveByKey(true);
912 break;
913 }
914
915 return true;
916 },
917
918 /**
919 * Move by key
920 *
921 * @param right {boolean} direction to move
922 */
923 moveByKey: function (right) {
924 var p = this.coords.p_pointer;
925 var p_step = (this.options.max - this.options.min) / 100;
926 p_step = this.options.step / p_step;
927
928 if (right) {
929 p += p_step;
930 } else {
931 p -= p_step;
932 }
933
934 this.coords.x_pointer = this.toFixed(this.coords.w_rs / 100 * p);
935 this.is_key = true;
936 this.calc();
937 },
938
939 /**
940 * Set visibility and content
941 * of Min and Max labels
942 */
943 setMinMax: function () {
944 if (!this.options) {
945 return;
946 }
947
948 if (this.options.hide_min_max) {
949 this.$cache.min[0].style.display = "none";
950 this.$cache.max[0].style.display = "none";
951 return;
952 }
953
954 if (this.options.values.length) {
955 this.$cache.min.html(this.decorate(this.options.p_values[this.options.min]));
956 this.$cache.max.html(this.decorate(this.options.p_values[this.options.max]));
957 } else {
958 var min_pretty = this._prettify(this.options.min);
959 var max_pretty = this._prettify(this.options.max);
960
961 this.result.min_pretty = min_pretty;
962 this.result.max_pretty = max_pretty;
963
964 this.$cache.min.html(this.decorate(min_pretty, this.options.min));
965 this.$cache.max.html(this.decorate(max_pretty, this.options.max));
966 }
967
968 this.labels.w_min = this.$cache.min.outerWidth(false);
969 this.labels.w_max = this.$cache.max.outerWidth(false);
970 },
971
972 /**
973 * Then dragging interval, prevent interval collapsing
974 * using min_interval option
975 */
976 setTempMinInterval: function () {
977 var interval = this.result.to - this.result.from;
978
979 if (this.old_min_interval === null) {
980 this.old_min_interval = this.options.min_interval;
981 }
982
983 this.options.min_interval = interval;
984 },
985
986 /**
987 * Restore min_interval option to original
988 */
989 restoreOriginalMinInterval: function () {
990 if (this.old_min_interval !== null) {
991 this.options.min_interval = this.old_min_interval;
992 this.old_min_interval = null;
993 }
994 },
995
996
997
998 // =============================================================================================================
999 // Calculations
1000
1001 /**
1002 * All calculations and measures start here
1003 *
1004 * @param update {boolean=}
1005 */
1006 calc: function (update) {
1007 if (!this.options) {
1008 return;
1009 }
1010
1011 this.calc_count++;
1012
1013 if (this.calc_count === 10 || update) {
1014 this.calc_count = 0;
1015 this.coords.w_rs = this.$cache.rs.outerWidth(false);
1016
1017 this.calcHandlePercent();
1018 }
1019
1020 if (!this.coords.w_rs) {
1021 return;
1022 }
1023
1024 this.calcPointerPercent();
1025 var handle_x = this.getHandleX();
1026
1027
1028 if (this.target === "both") {
1029 this.coords.p_gap = 0;
1030 handle_x = this.getHandleX();
1031 }
1032
1033 if (this.target === "click") {
1034 this.coords.p_gap = this.coords.p_handle / 2;
1035 handle_x = this.getHandleX();
1036
1037 if (this.options.drag_interval) {
1038 this.target = "both_one";
1039 } else {
1040 this.target = this.chooseHandle(handle_x);
1041 }
1042 }
1043
1044 switch (this.target) {
1045 case "base":
1046 var w = (this.options.max - this.options.min) / 100,
1047 f = (this.result.from - this.options.min) / w,
1048 t = (this.result.to - this.options.min) / w;
1049
1050 this.coords.p_single_real = this.toFixed(f);
1051 this.coords.p_from_real = this.toFixed(f);
1052 this.coords.p_to_real = this.toFixed(t);
1053
1054 this.coords.p_single_real = this.checkDiapason(this.coords.p_single_real, this.options.from_min, this.options.from_max);
1055 this.coords.p_from_real = this.checkDiapason(this.coords.p_from_real, this.options.from_min, this.options.from_max);
1056 this.coords.p_to_real = this.checkDiapason(this.coords.p_to_real, this.options.to_min, this.options.to_max);
1057
1058 this.coords.p_single_fake = this.convertToFakePercent(this.coords.p_single_real);
1059 this.coords.p_from_fake = this.convertToFakePercent(this.coords.p_from_real);
1060 this.coords.p_to_fake = this.convertToFakePercent(this.coords.p_to_real);
1061
1062 this.target = null;
1063
1064 break;
1065
1066 case "single":
1067 if (this.options.from_fixed) {
1068 break;
1069 }
1070
1071 this.coords.p_single_real = this.convertToRealPercent(handle_x);
1072 this.coords.p_single_real = this.calcWithStep(this.coords.p_single_real);
1073 this.coords.p_single_real = this.checkDiapason(this.coords.p_single_real, this.options.from_min, this.options.from_max);
1074
1075 this.coords.p_single_fake = this.convertToFakePercent(this.coords.p_single_real);
1076
1077 break;
1078
1079 case "from":
1080 if (this.options.from_fixed) {
1081 break;
1082 }
1083
1084 this.coords.p_from_real = this.convertToRealPercent(handle_x);
1085 this.coords.p_from_real = this.calcWithStep(this.coords.p_from_real);
1086 if (this.coords.p_from_real > this.coords.p_to_real) {
1087 this.coords.p_from_real = this.coords.p_to_real;
1088 }
1089 this.coords.p_from_real = this.checkDiapason(this.coords.p_from_real, this.options.from_min, this.options.from_max);
1090 this.coords.p_from_real = this.checkMinInterval(this.coords.p_from_real, this.coords.p_to_real, "from");
1091 this.coords.p_from_real = this.checkMaxInterval(this.coords.p_from_real, this.coords.p_to_real, "from");
1092
1093 this.coords.p_from_fake = this.convertToFakePercent(this.coords.p_from_real);
1094
1095 break;
1096
1097 case "to":
1098 if (this.options.to_fixed) {
1099 break;
1100 }
1101
1102 this.coords.p_to_real = this.convertToRealPercent(handle_x);
1103 this.coords.p_to_real = this.calcWithStep(this.coords.p_to_real);
1104 if (this.coords.p_to_real < this.coords.p_from_real) {
1105 this.coords.p_to_real = this.coords.p_from_real;
1106 }
1107 this.coords.p_to_real = this.checkDiapason(this.coords.p_to_real, this.options.to_min, this.options.to_max);
1108 this.coords.p_to_real = this.checkMinInterval(this.coords.p_to_real, this.coords.p_from_real, "to");
1109 this.coords.p_to_real = this.checkMaxInterval(this.coords.p_to_real, this.coords.p_from_real, "to");
1110
1111 this.coords.p_to_fake = this.convertToFakePercent(this.coords.p_to_real);
1112
1113 break;
1114
1115 case "both":
1116 if (this.options.from_fixed || this.options.to_fixed) {
1117 break;
1118 }
1119
1120 handle_x = this.toFixed(handle_x + (this.coords.p_handle * 0.001));
1121
1122 this.coords.p_from_real = this.convertToRealPercent(handle_x) - this.coords.p_gap_left;
1123 this.coords.p_from_real = this.calcWithStep(this.coords.p_from_real);
1124 this.coords.p_from_real = this.checkDiapason(this.coords.p_from_real, this.options.from_min, this.options.from_max);
1125 this.coords.p_from_real = this.checkMinInterval(this.coords.p_from_real, this.coords.p_to_real, "from");
1126 this.coords.p_from_fake = this.convertToFakePercent(this.coords.p_from_real);
1127
1128 this.coords.p_to_real = this.convertToRealPercent(handle_x) + this.coords.p_gap_right;
1129 this.coords.p_to_real = this.calcWithStep(this.coords.p_to_real);
1130 this.coords.p_to_real = this.checkDiapason(this.coords.p_to_real, this.options.to_min, this.options.to_max);
1131 this.coords.p_to_real = this.checkMinInterval(this.coords.p_to_real, this.coords.p_from_real, "to");
1132 this.coords.p_to_fake = this.convertToFakePercent(this.coords.p_to_real);
1133
1134 break;
1135
1136 case "both_one":
1137 if (this.options.from_fixed || this.options.to_fixed) {
1138 break;
1139 }
1140
1141 var real_x = this.convertToRealPercent(handle_x),
1142 from = this.result.from_percent,
1143 to = this.result.to_percent,
1144 full = to - from,
1145 half = full / 2,
1146 new_from = real_x - half,
1147 new_to = real_x + half;
1148
1149 if (new_from < 0) {
1150 new_from = 0;
1151 new_to = new_from + full;
1152 }
1153
1154 if (new_to > 100) {
1155 new_to = 100;
1156 new_from = new_to - full;
1157 }
1158
1159 this.coords.p_from_real = this.calcWithStep(new_from);
1160 this.coords.p_from_real = this.checkDiapason(this.coords.p_from_real, this.options.from_min, this.options.from_max);
1161 this.coords.p_from_fake = this.convertToFakePercent(this.coords.p_from_real);
1162
1163 this.coords.p_to_real = this.calcWithStep(new_to);
1164 this.coords.p_to_real = this.checkDiapason(this.coords.p_to_real, this.options.to_min, this.options.to_max);
1165 this.coords.p_to_fake = this.convertToFakePercent(this.coords.p_to_real);
1166
1167 break;
1168 }
1169
1170 if (this.options.type === "single") {
1171 this.coords.p_bar_x = (this.coords.p_handle / 2);
1172 this.coords.p_bar_w = this.coords.p_single_fake;
1173
1174 this.result.from_percent = this.coords.p_single_real;
1175 this.result.from = this.convertToValue(this.coords.p_single_real);
1176 this.result.from_pretty = this._prettify(this.result.from);
1177
1178 if (this.options.values.length) {
1179 this.result.from_value = this.options.values[this.result.from];
1180 }
1181 } else {
1182 this.coords.p_bar_x = this.toFixed(this.coords.p_from_fake + (this.coords.p_handle / 2));
1183 this.coords.p_bar_w = this.toFixed(this.coords.p_to_fake - this.coords.p_from_fake);
1184
1185 this.result.from_percent = this.coords.p_from_real;
1186 this.result.from = this.convertToValue(this.coords.p_from_real);
1187 this.result.from_pretty = this._prettify(this.result.from);
1188 this.result.to_percent = this.coords.p_to_real;
1189 this.result.to = this.convertToValue(this.coords.p_to_real);
1190 this.result.to_pretty = this._prettify(this.result.to);
1191
1192 if (this.options.values.length) {
1193 this.result.from_value = this.options.values[this.result.from];
1194 this.result.to_value = this.options.values[this.result.to];
1195 }
1196 }
1197
1198 this.calcMinMax();
1199 this.calcLabels();
1200 },
1201
1202
1203 /**
1204 * calculates pointer X in percent
1205 */
1206 calcPointerPercent: function () {
1207 if (!this.coords.w_rs) {
1208 this.coords.p_pointer = 0;
1209 return;
1210 }
1211
1212 if (this.coords.x_pointer < 0 || isNaN(this.coords.x_pointer) ) {
1213 this.coords.x_pointer = 0;
1214 } else if (this.coords.x_pointer > this.coords.w_rs) {
1215 this.coords.x_pointer = this.coords.w_rs;
1216 }
1217
1218 this.coords.p_pointer = this.toFixed(this.coords.x_pointer / this.coords.w_rs * 100);
1219 },
1220
1221 convertToRealPercent: function (fake) {
1222 var full = 100 - this.coords.p_handle;
1223 return fake / full * 100;
1224 },
1225
1226 convertToFakePercent: function (real) {
1227 var full = 100 - this.coords.p_handle;
1228 return real / 100 * full;
1229 },
1230
1231 getHandleX: function () {
1232 var max = 100 - this.coords.p_handle,
1233 x = this.toFixed(this.coords.p_pointer - this.coords.p_gap);
1234
1235 if (x < 0) {
1236 x = 0;
1237 } else if (x > max) {
1238 x = max;
1239 }
1240
1241 return x;
1242 },
1243
1244 calcHandlePercent: function () {
1245 if (this.options.type === "single") {
1246 this.coords.w_handle = this.$cache.s_single.outerWidth(false);
1247 } else {
1248 this.coords.w_handle = this.$cache.s_from.outerWidth(false);
1249 }
1250
1251 this.coords.p_handle = this.toFixed(this.coords.w_handle / this.coords.w_rs * 100);
1252 },
1253
1254 /**
1255 * Find closest handle to pointer click
1256 *
1257 * @param real_x {Number}
1258 * @returns {String}
1259 */
1260 chooseHandle: function (real_x) {
1261 if (this.options.type === "single") {
1262 return "single";
1263 } else {
1264 var m_point = this.coords.p_from_real + ((this.coords.p_to_real - this.coords.p_from_real) / 2);
1265 if (real_x >= m_point) {
1266 return this.options.to_fixed ? "from" : "to";
1267 } else {
1268 return this.options.from_fixed ? "to" : "from";
1269 }
1270 }
1271 },
1272
1273 /**
1274 * Measure Min and Max labels width in percent
1275 */
1276 calcMinMax: function () {
1277 if (!this.coords.w_rs) {
1278 return;
1279 }
1280
1281 this.labels.p_min = this.labels.w_min / this.coords.w_rs * 100;
1282 this.labels.p_max = this.labels.w_max / this.coords.w_rs * 100;
1283 },
1284
1285 /**
1286 * Measure labels width and X in percent
1287 */
1288 calcLabels: function () {
1289 if (!this.coords.w_rs || this.options.hide_from_to) {
1290 return;
1291 }
1292
1293 if (this.options.type === "single") {
1294
1295 this.labels.w_single = this.$cache.single.outerWidth(false);
1296 this.labels.p_single_fake = this.labels.w_single / this.coords.w_rs * 100;
1297 this.labels.p_single_left = this.coords.p_single_fake + (this.coords.p_handle / 2) - (this.labels.p_single_fake / 2);
1298 this.labels.p_single_left = this.checkEdges(this.labels.p_single_left, this.labels.p_single_fake);
1299
1300 } else {
1301
1302 this.labels.w_from = this.$cache.from.outerWidth(false);
1303 this.labels.p_from_fake = this.labels.w_from / this.coords.w_rs * 100;
1304 this.labels.p_from_left = this.coords.p_from_fake + (this.coords.p_handle / 2) - (this.labels.p_from_fake / 2);
1305 this.labels.p_from_left = this.toFixed(this.labels.p_from_left);
1306 this.labels.p_from_left = this.checkEdges(this.labels.p_from_left, this.labels.p_from_fake);
1307
1308 this.labels.w_to = this.$cache.to.outerWidth(false);
1309 this.labels.p_to_fake = this.labels.w_to / this.coords.w_rs * 100;
1310 this.labels.p_to_left = this.coords.p_to_fake + (this.coords.p_handle / 2) - (this.labels.p_to_fake / 2);
1311 this.labels.p_to_left = this.toFixed(this.labels.p_to_left);
1312 this.labels.p_to_left = this.checkEdges(this.labels.p_to_left, this.labels.p_to_fake);
1313
1314 this.labels.w_single = this.$cache.single.outerWidth(false);
1315 this.labels.p_single_fake = this.labels.w_single / this.coords.w_rs * 100;
1316 this.labels.p_single_left = ((this.labels.p_from_left + this.labels.p_to_left + this.labels.p_to_fake) / 2) - (this.labels.p_single_fake / 2);
1317 this.labels.p_single_left = this.toFixed(this.labels.p_single_left);
1318 this.labels.p_single_left = this.checkEdges(this.labels.p_single_left, this.labels.p_single_fake);
1319
1320 }
1321 },
1322
1323
1324
1325 // =============================================================================================================
1326 // Drawings
1327
1328 /**
1329 * Main function called in request animation frame
1330 * to update everything
1331 */
1332 updateScene: function () {
1333 if (this.raf_id) {
1334 cancelAnimationFrame(this.raf_id);
1335 this.raf_id = null;
1336 }
1337
1338 clearTimeout(this.update_tm);
1339 this.update_tm = null;
1340
1341 if (!this.options) {
1342 return;
1343 }
1344
1345 this.drawHandles();
1346
1347 if (this.is_active) {
1348 this.raf_id = requestAnimationFrame(this.updateScene.bind(this));
1349 } else {
1350 this.update_tm = setTimeout(this.updateScene.bind(this), 300);
1351 }
1352 },
1353
1354 /**
1355 * Draw handles
1356 */
1357 drawHandles: function () {
1358 this.coords.w_rs = this.$cache.rs.outerWidth(false);
1359
1360 if (!this.coords.w_rs) {
1361 return;
1362 }
1363
1364 if (this.coords.w_rs !== this.coords.w_rs_old) {
1365 this.target = "base";
1366 this.is_resize = true;
1367 }
1368
1369 if (this.coords.w_rs !== this.coords.w_rs_old || this.force_redraw) {
1370 this.setMinMax();
1371 this.calc(true);
1372 this.drawLabels();
1373 if (this.options.grid) {
1374 this.calcGridMargin();
1375 this.calcGridLabels();
1376 }
1377 this.force_redraw = true;
1378 this.coords.w_rs_old = this.coords.w_rs;
1379 this.drawShadow();
1380 }
1381
1382 if (!this.coords.w_rs) {
1383 return;
1384 }
1385
1386 if (!this.dragging && !this.force_redraw && !this.is_key) {
1387 return;
1388 }
1389
1390 if (this.old_from !== this.result.from || this.old_to !== this.result.to || this.force_redraw || this.is_key) {
1391
1392 this.drawLabels();
1393
1394 this.$cache.bar[0].style.left = this.coords.p_bar_x + "%";
1395 this.$cache.bar[0].style.width = this.coords.p_bar_w + "%";
1396
1397 if (this.options.type === "single") {
1398 this.$cache.bar[0].style.left = 0;
1399 this.$cache.bar[0].style.width = this.coords.p_bar_w + this.coords.p_bar_x + "%";
1400
1401 this.$cache.s_single[0].style.left = this.coords.p_single_fake + "%";
1402
1403 this.$cache.single[0].style.left = this.labels.p_single_left + "%";
1404 } else {
1405 this.$cache.s_from[0].style.left = this.coords.p_from_fake + "%";
1406 this.$cache.s_to[0].style.left = this.coords.p_to_fake + "%";
1407
1408 if (this.old_from !== this.result.from || this.force_redraw) {
1409 this.$cache.from[0].style.left = this.labels.p_from_left + "%";
1410 }
1411 if (this.old_to !== this.result.to || this.force_redraw) {
1412 this.$cache.to[0].style.left = this.labels.p_to_left + "%";
1413 }
1414
1415 this.$cache.single[0].style.left = this.labels.p_single_left + "%";
1416 }
1417
1418 this.writeToInput();
1419
1420 if ((this.old_from !== this.result.from || this.old_to !== this.result.to) && !this.is_start) {
1421 this.$cache.input.trigger("change");
1422 this.$cache.input.trigger("input");
1423 }
1424
1425 this.old_from = this.result.from;
1426 this.old_to = this.result.to;
1427
1428 // callbacks call
1429 if (!this.is_resize && !this.is_update && !this.is_start && !this.is_finish) {
1430 this.callOnChange();
1431 }
1432 if (this.is_key || this.is_click) {
1433 this.is_key = false;
1434 this.is_click = false;
1435 this.callOnFinish();
1436 }
1437
1438 this.is_update = false;
1439 this.is_resize = false;
1440 this.is_finish = false;
1441 }
1442
1443 this.is_start = false;
1444 this.is_key = false;
1445 this.is_click = false;
1446 this.force_redraw = false;
1447 },
1448
1449 /**
1450 * Draw labels
1451 * measure labels collisions
1452 * collapse close labels
1453 */
1454 drawLabels: function () {
1455 if (!this.options) {
1456 return;
1457 }
1458
1459 var values_num = this.options.values.length;
1460 var p_values = this.options.p_values;
1461 var text_single;
1462 var text_from;
1463 var text_to;
1464 var from_pretty;
1465 var to_pretty;
1466
1467 if (this.options.hide_from_to) {
1468 return;
1469 }
1470
1471 if (this.options.type === "single") {
1472
1473 if (values_num) {
1474 text_single = this.decorate(p_values[this.result.from]);
1475 this.$cache.single.html(text_single);
1476 } else {
1477 from_pretty = this._prettify(this.result.from);
1478
1479 text_single = this.decorate(from_pretty, this.result.from);
1480 this.$cache.single.html(text_single);
1481 }
1482
1483 this.calcLabels();
1484
1485 if (this.labels.p_single_left < this.labels.p_min + 1) {
1486 this.$cache.min[0].style.visibility = "hidden";
1487 } else {
1488 this.$cache.min[0].style.visibility = "visible";
1489 }
1490
1491 if (this.labels.p_single_left + this.labels.p_single_fake > 100 - this.labels.p_max - 1) {
1492 this.$cache.max[0].style.visibility = "hidden";
1493 } else {
1494 this.$cache.max[0].style.visibility = "visible";
1495 }
1496
1497 } else {
1498
1499 if (values_num) {
1500
1501 if (this.options.decorate_both) {
1502 text_single = this.decorate(p_values[this.result.from]);
1503 text_single += this.options.values_separator;
1504 text_single += this.decorate(p_values[this.result.to]);
1505 } else {
1506 text_single = this.decorate(p_values[this.result.from] + this.options.values_separator + p_values[this.result.to]);
1507 }
1508 text_from = this.decorate(p_values[this.result.from]);
1509 text_to = this.decorate(p_values[this.result.to]);
1510
1511 this.$cache.single.html(text_single);
1512 this.$cache.from.html(text_from);
1513 this.$cache.to.html(text_to);
1514
1515 } else {
1516 from_pretty = this._prettify(this.result.from);
1517 to_pretty = this._prettify(this.result.to);
1518
1519 if (this.options.decorate_both) {
1520 text_single = this.decorate(from_pretty, this.result.from);
1521 text_single += this.options.values_separator;
1522 text_single += this.decorate(to_pretty, this.result.to);
1523 } else {
1524 text_single = this.decorate(from_pretty + this.options.values_separator + to_pretty, this.result.to);
1525 }
1526 text_from = this.decorate(from_pretty, this.result.from);
1527 text_to = this.decorate(to_pretty, this.result.to);
1528
1529 this.$cache.single.html(text_single);
1530 this.$cache.from.html(text_from);
1531 this.$cache.to.html(text_to);
1532
1533 }
1534
1535 this.calcLabels();
1536
1537 var min = Math.min(this.labels.p_single_left, this.labels.p_from_left),
1538 single_left = this.labels.p_single_left + this.labels.p_single_fake,
1539 to_left = this.labels.p_to_left + this.labels.p_to_fake,
1540 max = Math.max(single_left, to_left);
1541
1542 if (this.labels.p_from_left + this.labels.p_from_fake >= this.labels.p_to_left) {
1543 this.$cache.from[0].style.visibility = "hidden";
1544 this.$cache.to[0].style.visibility = "hidden";
1545 this.$cache.single[0].style.visibility = "visible";
1546
1547 if (this.result.from === this.result.to) {
1548 if (this.target === "from") {
1549 this.$cache.from[0].style.visibility = "visible";
1550 } else if (this.target === "to") {
1551 this.$cache.to[0].style.visibility = "visible";
1552 } else if (!this.target) {
1553 this.$cache.from[0].style.visibility = "visible";
1554 }
1555 this.$cache.single[0].style.visibility = "hidden";
1556 max = to_left;
1557 } else {
1558 this.$cache.from[0].style.visibility = "hidden";
1559 this.$cache.to[0].style.visibility = "hidden";
1560 this.$cache.single[0].style.visibility = "visible";
1561 max = Math.max(single_left, to_left);
1562 }
1563 } else {
1564 this.$cache.from[0].style.visibility = "visible";
1565 this.$cache.to[0].style.visibility = "visible";
1566 this.$cache.single[0].style.visibility = "hidden";
1567 }
1568
1569 if (min < this.labels.p_min + 1) {
1570 this.$cache.min[0].style.visibility = "hidden";
1571 } else {
1572 this.$cache.min[0].style.visibility = "visible";
1573 }
1574
1575 if (max > 100 - this.labels.p_max - 1) {
1576 this.$cache.max[0].style.visibility = "hidden";
1577 } else {
1578 this.$cache.max[0].style.visibility = "visible";
1579 }
1580
1581 }
1582 },
1583
1584 /**
1585 * Draw shadow intervals
1586 */
1587 drawShadow: function () {
1588 var o = this.options,
1589 c = this.$cache,
1590
1591 is_from_min = typeof o.from_min === "number" && !isNaN(o.from_min),
1592 is_from_max = typeof o.from_max === "number" && !isNaN(o.from_max),
1593 is_to_min = typeof o.to_min === "number" && !isNaN(o.to_min),
1594 is_to_max = typeof o.to_max === "number" && !isNaN(o.to_max),
1595
1596 from_min,
1597 from_max,
1598 to_min,
1599 to_max;
1600
1601 if (o.type === "single") {
1602 if (o.from_shadow && (is_from_min || is_from_max)) {
1603 from_min = this.convertToPercent(is_from_min ? o.from_min : o.min);
1604 from_max = this.convertToPercent(is_from_max ? o.from_max : o.max) - from_min;
1605 from_min = this.toFixed(from_min - (this.coords.p_handle / 100 * from_min));
1606 from_max = this.toFixed(from_max - (this.coords.p_handle / 100 * from_max));
1607 from_min = from_min + (this.coords.p_handle / 2);
1608
1609 c.shad_single[0].style.display = "block";
1610 c.shad_single[0].style.left = from_min + "%";
1611 c.shad_single[0].style.width = from_max + "%";
1612 } else {
1613 c.shad_single[0].style.display = "none";
1614 }
1615 } else {
1616 if (o.from_shadow && (is_from_min || is_from_max)) {
1617 from_min = this.convertToPercent(is_from_min ? o.from_min : o.min);
1618 from_max = this.convertToPercent(is_from_max ? o.from_max : o.max) - from_min;
1619 from_min = this.toFixed(from_min - (this.coords.p_handle / 100 * from_min));
1620 from_max = this.toFixed(from_max - (this.coords.p_handle / 100 * from_max));
1621 from_min = from_min + (this.coords.p_handle / 2);
1622
1623 c.shad_from[0].style.display = "block";
1624 c.shad_from[0].style.left = from_min + "%";
1625 c.shad_from[0].style.width = from_max + "%";
1626 } else {
1627 c.shad_from[0].style.display = "none";
1628 }
1629
1630 if (o.to_shadow && (is_to_min || is_to_max)) {
1631 to_min = this.convertToPercent(is_to_min ? o.to_min : o.min);
1632 to_max = this.convertToPercent(is_to_max ? o.to_max : o.max) - to_min;
1633 to_min = this.toFixed(to_min - (this.coords.p_handle / 100 * to_min));
1634 to_max = this.toFixed(to_max - (this.coords.p_handle / 100 * to_max));
1635 to_min = to_min + (this.coords.p_handle / 2);
1636
1637 c.shad_to[0].style.display = "block";
1638 c.shad_to[0].style.left = to_min + "%";
1639 c.shad_to[0].style.width = to_max + "%";
1640 } else {
1641 c.shad_to[0].style.display = "none";
1642 }
1643 }
1644 },
1645
1646
1647
1648 /**
1649 * Write values to input element
1650 */
1651 writeToInput: function () {
1652 if (this.options.type === "single") {
1653 if (this.options.values.length) {
1654 this.$cache.input.prop("value", this.result.from_value);
1655 } else {
1656 this.$cache.input.prop("value", this.result.from);
1657 }
1658 this.$cache.input.data("from", this.result.from);
1659 } else {
1660 if (this.options.values.length) {
1661 this.$cache.input.prop("value", this.result.from_value + this.options.input_values_separator + this.result.to_value);
1662 } else {
1663 this.$cache.input.prop("value", this.result.from + this.options.input_values_separator + this.result.to);
1664 }
1665 this.$cache.input.data("from", this.result.from);
1666 this.$cache.input.data("to", this.result.to);
1667 }
1668 },
1669
1670
1671
1672 // =============================================================================================================
1673 // Callbacks
1674
1675 callOnStart: function () {
1676 this.writeToInput();
1677
1678 if (this.options.onStart && typeof this.options.onStart === "function") {
1679 if (this.options.scope) {
1680 this.options.onStart.call(this.options.scope, this.result);
1681 } else {
1682 this.options.onStart(this.result);
1683 }
1684 }
1685 },
1686 callOnChange: function () {
1687 this.writeToInput();
1688
1689 if (this.options.onChange && typeof this.options.onChange === "function") {
1690 if (this.options.scope) {
1691 this.options.onChange.call(this.options.scope, this.result);
1692 } else {
1693 this.options.onChange(this.result);
1694 }
1695 }
1696 },
1697 callOnFinish: function () {
1698 this.writeToInput();
1699
1700 if (this.options.onFinish && typeof this.options.onFinish === "function") {
1701 if (this.options.scope) {
1702 this.options.onFinish.call(this.options.scope, this.result);
1703 } else {
1704 this.options.onFinish(this.result);
1705 }
1706 }
1707 },
1708 callOnUpdate: function () {
1709 this.writeToInput();
1710
1711 if (this.options.onUpdate && typeof this.options.onUpdate === "function") {
1712 if (this.options.scope) {
1713 this.options.onUpdate.call(this.options.scope, this.result);
1714 } else {
1715 this.options.onUpdate(this.result);
1716 }
1717 }
1718 },
1719
1720
1721
1722
1723 // =============================================================================================================
1724 // Service methods
1725
1726 toggleInput: function () {
1727 this.$cache.input.toggleClass("irs-hidden-input");
1728
1729 if (this.has_tab_index) {
1730 this.$cache.input.prop("tabindex", -1);
1731 } else {
1732 try {
1733 this.$cache.input.removeProp("tabindex");
1734 } catch(e) {
1735 // Do nothing (PhantomJS can throw an error with the
1736 // above, #2587)
1737 }
1738 }
1739
1740 this.has_tab_index = !this.has_tab_index;
1741 },
1742
1743 /**
1744 * Convert real value to percent
1745 *
1746 * @param value {Number} X in real
1747 * @param no_min {boolean=} don't use min value
1748 * @returns {Number} X in percent
1749 */
1750 convertToPercent: function (value, no_min) {
1751 var diapason = this.options.max - this.options.min,
1752 one_percent = diapason / 100,
1753 val, percent;
1754
1755 if (!diapason) {
1756 this.no_diapason = true;
1757 return 0;
1758 }
1759
1760 if (no_min) {
1761 val = value;
1762 } else {
1763 val = value - this.options.min;
1764 }
1765
1766 percent = val / one_percent;
1767
1768 return this.toFixed(percent);
1769 },
1770
1771 /**
1772 * Convert percent to real values
1773 *
1774 * @param percent {Number} X in percent
1775 * @returns {Number} X in real
1776 */
1777 convertToValue: function (percent) {
1778 var min = this.options.min,
1779 max = this.options.max,
1780 min_decimals = min.toString().split(".")[1],
1781 max_decimals = max.toString().split(".")[1],
1782 min_length, max_length,
1783 avg_decimals = 0,
1784 abs = 0;
1785
1786 if (percent === 0) {
1787 return this.options.min;
1788 }
1789 if (percent === 100) {
1790 return this.options.max;
1791 }
1792
1793
1794 if (min_decimals) {
1795 min_length = min_decimals.length;
1796 avg_decimals = min_length;
1797 }
1798 if (max_decimals) {
1799 max_length = max_decimals.length;
1800 avg_decimals = max_length;
1801 }
1802 if (min_length && max_length) {
1803 avg_decimals = (min_length >= max_length) ? min_length : max_length;
1804 }
1805
1806 if (min < 0) {
1807 abs = Math.abs(min);
1808 min = +(min + abs).toFixed(avg_decimals);
1809 max = +(max + abs).toFixed(avg_decimals);
1810 }
1811
1812 var number = ((max - min) / 100 * percent) + min,
1813 string = this.options.step.toString().split(".")[1],
1814 result;
1815
1816 if (string) {
1817 number = +number.toFixed(string.length);
1818 } else {
1819 number = number / this.options.step;
1820 number = number * this.options.step;
1821
1822 number = +number.toFixed(0);
1823 }
1824
1825 if (abs) {
1826 number -= abs;
1827 }
1828
1829 if (string) {
1830 result = +number.toFixed(string.length);
1831 } else {
1832 result = this.toFixed(number);
1833 }
1834
1835 if (result < this.options.min) {
1836 result = this.options.min;
1837 } else if (result > this.options.max) {
1838 result = this.options.max;
1839 }
1840
1841 return result;
1842 },
1843
1844 /**
1845 * Round percent value with step
1846 *
1847 * @param percent {Number}
1848 * @returns percent {Number} rounded
1849 */
1850 calcWithStep: function (percent) {
1851 var rounded = Math.round(percent / this.coords.p_step) * this.coords.p_step;
1852
1853 if (rounded > 100) {
1854 rounded = 100;
1855 }
1856 if (percent === 100) {
1857 rounded = 100;
1858 }
1859
1860 return this.toFixed(rounded);
1861 },
1862
1863 checkMinInterval: function (p_current, p_next, type) {
1864 var o = this.options,
1865 current,
1866 next;
1867
1868 if (!o.min_interval) {
1869 return p_current;
1870 }
1871
1872 current = this.convertToValue(p_current);
1873 next = this.convertToValue(p_next);
1874
1875 if (type === "from") {
1876
1877 if (next - current < o.min_interval) {
1878 current = next - o.min_interval;
1879 }
1880
1881 } else {
1882
1883 if (current - next < o.min_interval) {
1884 current = next + o.min_interval;
1885 }
1886
1887 }
1888
1889 return this.convertToPercent(current);
1890 },
1891
1892 checkMaxInterval: function (p_current, p_next, type) {
1893 var o = this.options,
1894 current,
1895 next;
1896
1897 if (!o.max_interval) {
1898 return p_current;
1899 }
1900
1901 current = this.convertToValue(p_current);
1902 next = this.convertToValue(p_next);
1903
1904 if (type === "from") {
1905
1906 if (next - current > o.max_interval) {
1907 current = next - o.max_interval;
1908 }
1909
1910 } else {
1911
1912 if (current - next > o.max_interval) {
1913 current = next + o.max_interval;
1914 }
1915
1916 }
1917
1918 return this.convertToPercent(current);
1919 },
1920
1921 checkDiapason: function (p_num, min, max) {
1922 var num = this.convertToValue(p_num),
1923 o = this.options;
1924
1925 if (typeof min !== "number") {
1926 min = o.min;
1927 }
1928
1929 if (typeof max !== "number") {
1930 max = o.max;
1931 }
1932
1933 if (num < min) {
1934 num = min;
1935 }
1936
1937 if (num > max) {
1938 num = max;
1939 }
1940
1941 return this.convertToPercent(num);
1942 },
1943
1944 toFixed: function (num) {
1945 num = num.toFixed(20);
1946 return +num;
1947 },
1948
1949 _prettify: function (num) {
1950 if (!this.options.prettify_enabled) {
1951 return num;
1952 }
1953
1954 if (this.options.prettify && typeof this.options.prettify === "function") {
1955 return this.options.prettify(num);
1956 } else {
1957 return this.prettify(num);
1958 }
1959 },
1960
1961 prettify: function (num) {
1962 var n = num.toString();
1963 return n.replace(/(\d{1,3}(?=(?:\d\d\d)+(?!\d)))/g, "$1" + this.options.prettify_separator);
1964 },
1965
1966 checkEdges: function (left, width) {
1967 if (!this.options.force_edges) {
1968 return this.toFixed(left);
1969 }
1970
1971 if (left < 0) {
1972 left = 0;
1973 } else if (left > 100 - width) {
1974 left = 100 - width;
1975 }
1976
1977 return this.toFixed(left);
1978 },
1979
1980 validate: function () {
1981 var o = this.options,
1982 r = this.result,
1983 v = o.values,
1984 vl = v.length,
1985 value,
1986 i;
1987
1988 if (typeof o.min === "string") o.min = +o.min;
1989 if (typeof o.max === "string") o.max = +o.max;
1990 if (typeof o.from === "string") o.from = +o.from;
1991 if (typeof o.to === "string") o.to = +o.to;
1992 if (typeof o.step === "string") o.step = +o.step;
1993
1994 if (typeof o.from_min === "string") o.from_min = +o.from_min;
1995 if (typeof o.from_max === "string") o.from_max = +o.from_max;
1996 if (typeof o.to_min === "string") o.to_min = +o.to_min;
1997 if (typeof o.to_max === "string") o.to_max = +o.to_max;
1998
1999 if (typeof o.grid_num === "string") o.grid_num = +o.grid_num;
2000
2001 if (o.max < o.min) {
2002 o.max = o.min;
2003 }
2004
2005 if (vl) {
2006 o.p_values = [];
2007 o.min = 0;
2008 o.max = vl - 1;
2009 o.step = 1;
2010 o.grid_num = o.max;
2011 o.grid_snap = true;
2012
2013 for (i = 0; i < vl; i++) {
2014 value = +v[i];
2015
2016 if (!isNaN(value)) {
2017 v[i] = value;
2018 value = this._prettify(value);
2019 } else {
2020 value = v[i];
2021 }
2022
2023 o.p_values.push(value);
2024 }
2025 }
2026
2027 if (typeof o.from !== "number" || isNaN(o.from)) {
2028 o.from = o.min;
2029 }
2030
2031 if (typeof o.to !== "number" || isNaN(o.to)) {
2032 o.to = o.max;
2033 }
2034
2035 if (o.type === "single") {
2036
2037 if (o.from < o.min) o.from = o.min;
2038 if (o.from > o.max) o.from = o.max;
2039
2040 } else {
2041
2042 if (o.from < o.min) o.from = o.min;
2043 if (o.from > o.max) o.from = o.max;
2044
2045 if (o.to < o.min) o.to = o.min;
2046 if (o.to > o.max) o.to = o.max;
2047
2048 if (this.update_check.from) {
2049
2050 if (this.update_check.from !== o.from) {
2051 if (o.from > o.to) o.from = o.to;
2052 }
2053 if (this.update_check.to !== o.to) {
2054 if (o.to < o.from) o.to = o.from;
2055 }
2056
2057 }
2058
2059 if (o.from > o.to) o.from = o.to;
2060 if (o.to < o.from) o.to = o.from;
2061
2062 }
2063
2064 if (typeof o.step !== "number" || isNaN(o.step) || !o.step || o.step < 0) {
2065 o.step = 1;
2066 }
2067
2068 if (typeof o.from_min === "number" && o.from < o.from_min) {
2069 o.from = o.from_min;
2070 }
2071
2072 if (typeof o.from_max === "number" && o.from > o.from_max) {
2073 o.from = o.from_max;
2074 }
2075
2076 if (typeof o.to_min === "number" && o.to < o.to_min) {
2077 o.to = o.to_min;
2078 }
2079
2080 if (typeof o.to_max === "number" && o.from > o.to_max) {
2081 o.to = o.to_max;
2082 }
2083
2084 if (r) {
2085 if (r.min !== o.min) {
2086 r.min = o.min;
2087 }
2088
2089 if (r.max !== o.max) {
2090 r.max = o.max;
2091 }
2092
2093 if (r.from < r.min || r.from > r.max) {
2094 r.from = o.from;
2095 }
2096
2097 if (r.to < r.min || r.to > r.max) {
2098 r.to = o.to;
2099 }
2100 }
2101
2102 if (typeof o.min_interval !== "number" || isNaN(o.min_interval) || !o.min_interval || o.min_interval < 0) {
2103 o.min_interval = 0;
2104 }
2105
2106 if (typeof o.max_interval !== "number" || isNaN(o.max_interval) || !o.max_interval || o.max_interval < 0) {
2107 o.max_interval = 0;
2108 }
2109
2110 if (o.min_interval && o.min_interval > o.max - o.min) {
2111 o.min_interval = o.max - o.min;
2112 }
2113
2114 if (o.max_interval && o.max_interval > o.max - o.min) {
2115 o.max_interval = o.max - o.min;
2116 }
2117 },
2118
2119 decorate: function (num, original) {
2120 var decorated = "",
2121 o = this.options;
2122
2123 if (o.prefix) {
2124 decorated += o.prefix;
2125 }
2126
2127 decorated += num;
2128
2129 if (o.max_postfix) {
2130 if (o.values.length && num === o.p_values[o.max]) {
2131 decorated += o.max_postfix;
2132 if (o.postfix) {
2133 decorated += " ";
2134 }
2135 } else if (original === o.max) {
2136 decorated += o.max_postfix;
2137 if (o.postfix) {
2138 decorated += " ";
2139 }
2140 }
2141 }
2142
2143 if (o.postfix) {
2144 decorated += o.postfix;
2145 }
2146
2147 return decorated;
2148 },
2149
2150 updateFrom: function () {
2151 this.result.from = this.options.from;
2152 this.result.from_percent = this.convertToPercent(this.result.from);
2153 this.result.from_pretty = this._prettify(this.result.from);
2154 if (this.options.values) {
2155 this.result.from_value = this.options.values[this.result.from];
2156 }
2157 },
2158
2159 updateTo: function () {
2160 this.result.to = this.options.to;
2161 this.result.to_percent = this.convertToPercent(this.result.to);
2162 this.result.to_pretty = this._prettify(this.result.to);
2163 if (this.options.values) {
2164 this.result.to_value = this.options.values[this.result.to];
2165 }
2166 },
2167
2168 updateResult: function () {
2169 this.result.min = this.options.min;
2170 this.result.max = this.options.max;
2171 this.updateFrom();
2172 this.updateTo();
2173 },
2174
2175
2176 // =============================================================================================================
2177 // Grid
2178
2179 appendGrid: function () {
2180 if (!this.options.grid) {
2181 return;
2182 }
2183
2184 var o = this.options,
2185 i, z,
2186
2187 total = o.max - o.min,
2188 big_num = o.grid_num,
2189 big_p = 0,
2190 big_w = 0,
2191
2192 small_max = 4,
2193 local_small_max,
2194 small_p,
2195 small_w = 0,
2196
2197 result,
2198 html = '';
2199
2200
2201
2202 this.calcGridMargin();
2203
2204 if (o.grid_snap) {
2205 big_num = total / o.step;
2206 }
2207
2208 if (big_num > 50) big_num = 50;
2209 big_p = this.toFixed(100 / big_num);
2210
2211 if (big_num > 4) {
2212 small_max = 3;
2213 }
2214 if (big_num > 7) {
2215 small_max = 2;
2216 }
2217 if (big_num > 14) {
2218 small_max = 1;
2219 }
2220 if (big_num > 28) {
2221 small_max = 0;
2222 }
2223
2224 for (i = 0; i < big_num + 1; i++) {
2225 local_small_max = small_max;
2226
2227 big_w = this.toFixed(big_p * i);
2228
2229 if (big_w > 100) {
2230 big_w = 100;
2231 }
2232 this.coords.big[i] = big_w;
2233
2234 small_p = (big_w - (big_p * (i - 1))) / (local_small_max + 1);
2235
2236 for (z = 1; z <= local_small_max; z++) {
2237 if (big_w === 0) {
2238 break;
2239 }
2240
2241 small_w = this.toFixed(big_w - (small_p * z));
2242
2243 html += '<span class="irs-grid-pol small" style="left: ' + small_w + '%"></span>';
2244 }
2245
2246 html += '<span class="irs-grid-pol" style="left: ' + big_w + '%"></span>';
2247
2248 result = this.convertToValue(big_w);
2249 if (o.values.length) {
2250 result = o.p_values[result];
2251 } else {
2252 result = this._prettify(result);
2253 }
2254
2255 html += '<span class="irs-grid-text js-grid-text-' + i + '" style="left: ' + big_w + '%">' + result + '</span>';
2256 }
2257 this.coords.big_num = Math.ceil(big_num + 1);
2258
2259
2260
2261 this.$cache.cont.addClass("irs-with-grid");
2262 this.$cache.grid.html(html);
2263 this.cacheGridLabels();
2264 },
2265
2266 cacheGridLabels: function () {
2267 var $label, i,
2268 num = this.coords.big_num;
2269
2270 for (i = 0; i < num; i++) {
2271 $label = this.$cache.grid.find(".js-grid-text-" + i);
2272 this.$cache.grid_labels.push($label);
2273 }
2274
2275 this.calcGridLabels();
2276 },
2277
2278 calcGridLabels: function () {
2279 var i, label, start = [], finish = [],
2280 num = this.coords.big_num;
2281
2282 for (i = 0; i < num; i++) {
2283 this.coords.big_w[i] = this.$cache.grid_labels[i].outerWidth(false);
2284 this.coords.big_p[i] = this.toFixed(this.coords.big_w[i] / this.coords.w_rs * 100);
2285 this.coords.big_x[i] = this.toFixed(this.coords.big_p[i] / 2);
2286
2287 start[i] = this.toFixed(this.coords.big[i] - this.coords.big_x[i]);
2288 finish[i] = this.toFixed(start[i] + this.coords.big_p[i]);
2289 }
2290
2291 if (this.options.force_edges) {
2292 if (start[0] < -this.coords.grid_gap) {
2293 start[0] = -this.coords.grid_gap;
2294 finish[0] = this.toFixed(start[0] + this.coords.big_p[0]);
2295
2296 this.coords.big_x[0] = this.coords.grid_gap;
2297 }
2298
2299 if (finish[num - 1] > 100 + this.coords.grid_gap) {
2300 finish[num - 1] = 100 + this.coords.grid_gap;
2301 start[num - 1] = this.toFixed(finish[num - 1] - this.coords.big_p[num - 1]);
2302
2303 this.coords.big_x[num - 1] = this.toFixed(this.coords.big_p[num - 1] - this.coords.grid_gap);
2304 }
2305 }
2306
2307 this.calcGridCollision(2, start, finish);
2308 this.calcGridCollision(4, start, finish);
2309
2310 for (i = 0; i < num; i++) {
2311 label = this.$cache.grid_labels[i][0];
2312
2313 if (this.coords.big_x[i] !== Number.POSITIVE_INFINITY) {
2314 label.style.marginLeft = -this.coords.big_x[i] + "%";
2315 }
2316 }
2317 },
2318
2319 // Collisions Calc Beta
2320 // TODO: Refactor then have plenty of time
2321 calcGridCollision: function (step, start, finish) {
2322 var i, next_i, label,
2323 num = this.coords.big_num;
2324
2325 for (i = 0; i < num; i += step) {
2326 next_i = i + (step / 2);
2327 if (next_i >= num) {
2328 break;
2329 }
2330
2331 label = this.$cache.grid_labels[next_i][0];
2332
2333 if (finish[i] <= start[next_i]) {
2334 label.style.visibility = "visible";
2335 } else {
2336 label.style.visibility = "hidden";
2337 }
2338 }
2339 },
2340
2341 calcGridMargin: function () {
2342 if (!this.options.grid_margin) {
2343 return;
2344 }
2345
2346 this.coords.w_rs = this.$cache.rs.outerWidth(false);
2347 if (!this.coords.w_rs) {
2348 return;
2349 }
2350
2351 if (this.options.type === "single") {
2352 this.coords.w_handle = this.$cache.s_single.outerWidth(false);
2353 } else {
2354 this.coords.w_handle = this.$cache.s_from.outerWidth(false);
2355 }
2356 this.coords.p_handle = this.toFixed(this.coords.w_handle / this.coords.w_rs * 100);
2357 this.coords.grid_gap = this.toFixed((this.coords.p_handle / 2) - 0.1);
2358
2359 this.$cache.grid[0].style.width = this.toFixed(100 - this.coords.p_handle) + "%";
2360 this.$cache.grid[0].style.left = this.coords.grid_gap + "%";
2361 },
2362
2363
2364
2365 // =============================================================================================================
2366 // Public methods
2367
2368 update: function (options) {
2369 if (!this.input) {
2370 return;
2371 }
2372
2373 this.is_update = true;
2374
2375 this.options.from = this.result.from;
2376 this.options.to = this.result.to;
2377 this.update_check.from = this.result.from;
2378 this.update_check.to = this.result.to;
2379
2380 this.options = $.extend(this.options, options);
2381 this.validate();
2382 this.updateResult(options);
2383
2384 this.toggleInput();
2385 this.remove();
2386 this.init(true);
2387 },
2388
2389 reset: function () {
2390 if (!this.input) {
2391 return;
2392 }
2393
2394 this.updateResult();
2395 this.update();
2396 },
2397
2398 destroy: function () {
2399 if (!this.input) {
2400 return;
2401 }
2402
2403 this.toggleInput();
2404 this.$cache.input.prop("readonly", false);
2405 $.data(this.input, "ionRangeSlider", null);
2406
2407 this.remove();
2408 this.input = null;
2409 this.options = null;
2410 }
2411 };
2412
2413 $.fn.ionRangeSlider = function (options) {
2414 return this.each(function() {
2415 if (!$.data(this, "ionRangeSlider")) {
2416 $.data(this, "ionRangeSlider", new IonRangeSlider(this, options, plugin_count++));
2417 }
2418 });
2419 };
2420
2421
2422
2423 // =================================================================================================================
2424 // http://paulirish.com/2011/requestanimationframe-for-smart-animating/
2425 // http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
2426
2427 // requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
2428
2429 // MIT license
2430
2431 (function() {
2432 var lastTime = 0;
2433 var vendors = ['ms', 'moz', 'webkit', 'o'];
2434 for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
2435 window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
2436 window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
2437 || window[vendors[x]+'CancelRequestAnimationFrame'];
2438 }
2439
2440 if (!window.requestAnimationFrame)
2441 window.requestAnimationFrame = function(callback, element) {
2442 var currTime = new Date().getTime();
2443 var timeToCall = Math.max(0, 16 - (currTime - lastTime));
2444 var id = window.setTimeout(function() { callback(currTime + timeToCall); },
2445 timeToCall);
2446 lastTime = currTime + timeToCall;
2447 return id;
2448 };
2449
2450 if (!window.cancelAnimationFrame)
2451 window.cancelAnimationFrame = function(id) {
2452 clearTimeout(id);
2453 };
2454 }());
2455
2456 }));