ion.rangeSlider.js 78 KB

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