ionrangeslider.js 80 KB

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