You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ion.rangeSlider.js 83KB

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