ion.rangeSlider.js 83 KB

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