您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

bootstrap-slider.js 70KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061
  1. /*! =======================================================
  2. VERSION 11.0.2
  3. ========================================================= */
  4. "use strict";
  5. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  6. /*! =========================================================
  7. * bootstrap-slider.js
  8. *
  9. * Maintainers:
  10. * Kyle Kemp
  11. * - Twitter: @seiyria
  12. * - Github: seiyria
  13. * Rohit Kalkur
  14. * - Twitter: @Rovolutionary
  15. * - Github: rovolution
  16. *
  17. * =========================================================
  18. *
  19. * bootstrap-slider is released under the MIT License
  20. * Copyright (c) 2019 Kyle Kemp, Rohit Kalkur, and contributors
  21. *
  22. * Permission is hereby granted, free of charge, to any person
  23. * obtaining a copy of this software and associated documentation
  24. * files (the "Software"), to deal in the Software without
  25. * restriction, including without limitation the rights to use,
  26. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  27. * copies of the Software, and to permit persons to whom the
  28. * Software is furnished to do so, subject to the following
  29. * conditions:
  30. *
  31. * The above copyright notice and this permission notice shall be
  32. * included in all copies or substantial portions of the Software.
  33. *
  34. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  35. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  36. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  37. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  38. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  39. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  40. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  41. * OTHER DEALINGS IN THE SOFTWARE.
  42. *
  43. * ========================================================= */
  44. /**
  45. * Bridget makes jQuery widgets
  46. * v1.0.1
  47. * MIT license
  48. */
  49. var windowIsDefined = (typeof window === "undefined" ? "undefined" : _typeof(window)) === "object";
  50. (function (factory) {
  51. if (typeof define === "function" && define.amd) {
  52. define(["jquery"], factory);
  53. } else if ((typeof module === "undefined" ? "undefined" : _typeof(module)) === "object" && module.exports) {
  54. var jQuery;
  55. try {
  56. jQuery = require("jquery");
  57. } catch (err) {
  58. jQuery = null;
  59. }
  60. module.exports = factory(jQuery);
  61. } else if (window) {
  62. window.Slider = factory(window.jQuery);
  63. }
  64. })(function ($) {
  65. // Constants
  66. var NAMESPACE_MAIN = 'slider';
  67. var NAMESPACE_ALTERNATE = 'bootstrapSlider';
  68. // Polyfill console methods
  69. if (windowIsDefined && !window.console) {
  70. window.console = {};
  71. }
  72. if (windowIsDefined && !window.console.log) {
  73. window.console.log = function () {};
  74. }
  75. if (windowIsDefined && !window.console.warn) {
  76. window.console.warn = function () {};
  77. }
  78. // Reference to Slider constructor
  79. var Slider;
  80. (function ($) {
  81. 'use strict';
  82. // -------------------------- utils -------------------------- //
  83. var slice = Array.prototype.slice;
  84. function noop() {}
  85. // -------------------------- definition -------------------------- //
  86. function defineBridget($) {
  87. // bail if no jQuery
  88. if (!$) {
  89. return;
  90. }
  91. // -------------------------- addOptionMethod -------------------------- //
  92. /**
  93. * adds option method -> $().plugin('option', {...})
  94. * @param {Function} PluginClass - constructor class
  95. */
  96. function addOptionMethod(PluginClass) {
  97. // don't overwrite original option method
  98. if (PluginClass.prototype.option) {
  99. return;
  100. }
  101. // option setter
  102. PluginClass.prototype.option = function (opts) {
  103. // bail out if not an object
  104. if (!$.isPlainObject(opts)) {
  105. return;
  106. }
  107. this.options = $.extend(true, this.options, opts);
  108. };
  109. }
  110. // -------------------------- plugin bridge -------------------------- //
  111. // helper function for logging errors
  112. // $.error breaks jQuery chaining
  113. var logError = typeof console === 'undefined' ? noop : function (message) {
  114. console.error(message);
  115. };
  116. /**
  117. * jQuery plugin bridge, access methods like $elem.plugin('method')
  118. * @param {String} namespace - plugin name
  119. * @param {Function} PluginClass - constructor class
  120. */
  121. function bridge(namespace, PluginClass) {
  122. // add to jQuery fn namespace
  123. $.fn[namespace] = function (options) {
  124. if (typeof options === 'string') {
  125. // call plugin method when first argument is a string
  126. // get arguments for method
  127. var args = slice.call(arguments, 1);
  128. for (var i = 0, len = this.length; i < len; i++) {
  129. var elem = this[i];
  130. var instance = $.data(elem, namespace);
  131. if (!instance) {
  132. logError("cannot call methods on " + namespace + " prior to initialization; " + "attempted to call '" + options + "'");
  133. continue;
  134. }
  135. if (!$.isFunction(instance[options]) || options.charAt(0) === '_') {
  136. logError("no such method '" + options + "' for " + namespace + " instance");
  137. continue;
  138. }
  139. // trigger method with arguments
  140. var returnValue = instance[options].apply(instance, args);
  141. // break look and return first value if provided
  142. if (returnValue !== undefined && returnValue !== instance) {
  143. return returnValue;
  144. }
  145. }
  146. // return this if no return value
  147. return this;
  148. } else {
  149. var objects = this.map(function () {
  150. var instance = $.data(this, namespace);
  151. if (instance) {
  152. // apply options & init
  153. instance.option(options);
  154. instance._init();
  155. } else {
  156. // initialize new instance
  157. instance = new PluginClass(this, options);
  158. $.data(this, namespace, instance);
  159. }
  160. return $(this);
  161. });
  162. if (objects.length === 1) {
  163. return objects[0];
  164. }
  165. return objects;
  166. }
  167. };
  168. }
  169. // -------------------------- bridget -------------------------- //
  170. /**
  171. * converts a Prototypical class into a proper jQuery plugin
  172. * the class must have a ._init method
  173. * @param {String} namespace - plugin name, used in $().pluginName
  174. * @param {Function} PluginClass - constructor class
  175. */
  176. $.bridget = function (namespace, PluginClass) {
  177. addOptionMethod(PluginClass);
  178. bridge(namespace, PluginClass);
  179. };
  180. return $.bridget;
  181. }
  182. // get jquery from browser global
  183. defineBridget($);
  184. })($);
  185. /*************************************************
  186. BOOTSTRAP-SLIDER SOURCE CODE
  187. **************************************************/
  188. (function ($) {
  189. var autoRegisterNamespace = void 0;
  190. var ErrorMsgs = {
  191. formatInvalidInputErrorMsg: function formatInvalidInputErrorMsg(input) {
  192. return "Invalid input value '" + input + "' passed in";
  193. },
  194. callingContextNotSliderInstance: "Calling context element does not have instance of Slider bound to it. Check your code to make sure the JQuery object returned from the call to the slider() initializer is calling the method"
  195. };
  196. var SliderScale = {
  197. linear: {
  198. getValue: function getValue(value, options) {
  199. if (value < options.min) {
  200. return options.min;
  201. } else if (value > options.max) {
  202. return options.max;
  203. } else {
  204. return value;
  205. }
  206. },
  207. toValue: function toValue(percentage) {
  208. var rawValue = percentage / 100 * (this.options.max - this.options.min);
  209. var shouldAdjustWithBase = true;
  210. if (this.options.ticks_positions.length > 0) {
  211. var minv,
  212. maxv,
  213. minp,
  214. maxp = 0;
  215. for (var i = 1; i < this.options.ticks_positions.length; i++) {
  216. if (percentage <= this.options.ticks_positions[i]) {
  217. minv = this.options.ticks[i - 1];
  218. minp = this.options.ticks_positions[i - 1];
  219. maxv = this.options.ticks[i];
  220. maxp = this.options.ticks_positions[i];
  221. break;
  222. }
  223. }
  224. var partialPercentage = (percentage - minp) / (maxp - minp);
  225. rawValue = minv + partialPercentage * (maxv - minv);
  226. shouldAdjustWithBase = false;
  227. }
  228. var adjustment = shouldAdjustWithBase ? this.options.min : 0;
  229. var value = adjustment + Math.round(rawValue / this.options.step) * this.options.step;
  230. return SliderScale.linear.getValue(value, this.options);
  231. },
  232. toPercentage: function toPercentage(value) {
  233. if (this.options.max === this.options.min) {
  234. return 0;
  235. }
  236. if (this.options.ticks_positions.length > 0) {
  237. var minv,
  238. maxv,
  239. minp,
  240. maxp = 0;
  241. for (var i = 0; i < this.options.ticks.length; i++) {
  242. if (value <= this.options.ticks[i]) {
  243. minv = i > 0 ? this.options.ticks[i - 1] : 0;
  244. minp = i > 0 ? this.options.ticks_positions[i - 1] : 0;
  245. maxv = this.options.ticks[i];
  246. maxp = this.options.ticks_positions[i];
  247. break;
  248. }
  249. }
  250. if (i > 0) {
  251. var partialPercentage = (value - minv) / (maxv - minv);
  252. return minp + partialPercentage * (maxp - minp);
  253. }
  254. }
  255. return 100 * (value - this.options.min) / (this.options.max - this.options.min);
  256. }
  257. },
  258. logarithmic: {
  259. /* Based on http://stackoverflow.com/questions/846221/logarithmic-slider */
  260. toValue: function toValue(percentage) {
  261. var offset = 1 - this.options.min;
  262. var min = Math.log(this.options.min + offset);
  263. var max = Math.log(this.options.max + offset);
  264. var value = Math.exp(min + (max - min) * percentage / 100) - offset;
  265. if (Math.round(value) === max) {
  266. return max;
  267. }
  268. value = this.options.min + Math.round((value - this.options.min) / this.options.step) * this.options.step;
  269. /* Rounding to the nearest step could exceed the min or
  270. * max, so clip to those values. */
  271. return SliderScale.linear.getValue(value, this.options);
  272. },
  273. toPercentage: function toPercentage(value) {
  274. if (this.options.max === this.options.min) {
  275. return 0;
  276. } else {
  277. var offset = 1 - this.options.min;
  278. var max = Math.log(this.options.max + offset);
  279. var min = Math.log(this.options.min + offset);
  280. var v = Math.log(value + offset);
  281. return 100 * (v - min) / (max - min);
  282. }
  283. }
  284. }
  285. };
  286. /*************************************************
  287. CONSTRUCTOR
  288. **************************************************/
  289. Slider = function Slider(element, options) {
  290. createNewSlider.call(this, element, options);
  291. return this;
  292. };
  293. function createNewSlider(element, options) {
  294. /*
  295. The internal state object is used to store data about the current 'state' of slider.
  296. This includes values such as the `value`, `enabled`, etc...
  297. */
  298. this._state = {
  299. value: null,
  300. enabled: null,
  301. offset: null,
  302. size: null,
  303. percentage: null,
  304. inDrag: false,
  305. over: false,
  306. tickIndex: null
  307. };
  308. // The objects used to store the reference to the tick methods if ticks_tooltip is on
  309. this.ticksCallbackMap = {};
  310. this.handleCallbackMap = {};
  311. if (typeof element === "string") {
  312. this.element = document.querySelector(element);
  313. } else if (element instanceof HTMLElement) {
  314. this.element = element;
  315. }
  316. /*************************************************
  317. Process Options
  318. **************************************************/
  319. options = options ? options : {};
  320. var optionTypes = Object.keys(this.defaultOptions);
  321. var isMinSet = options.hasOwnProperty('min');
  322. var isMaxSet = options.hasOwnProperty('max');
  323. for (var i = 0; i < optionTypes.length; i++) {
  324. var optName = optionTypes[i];
  325. // First check if an option was passed in via the constructor
  326. var val = options[optName];
  327. // If no data attrib, then check data atrributes
  328. val = typeof val !== 'undefined' ? val : getDataAttrib(this.element, optName);
  329. // Finally, if nothing was specified, use the defaults
  330. val = val !== null ? val : this.defaultOptions[optName];
  331. // Set all options on the instance of the Slider
  332. if (!this.options) {
  333. this.options = {};
  334. }
  335. this.options[optName] = val;
  336. }
  337. this.ticksAreValid = Array.isArray(this.options.ticks) && this.options.ticks.length > 0;
  338. // Lock to ticks only when ticks[] is defined and set
  339. if (!this.ticksAreValid) {
  340. this.options.lock_to_ticks = false;
  341. }
  342. // Check options.rtl
  343. if (this.options.rtl === 'auto') {
  344. var computedStyle = window.getComputedStyle(this.element);
  345. if (computedStyle != null) {
  346. this.options.rtl = computedStyle.direction === 'rtl';
  347. } else {
  348. // Fix for Firefox bug in versions less than 62:
  349. // https://bugzilla.mozilla.org/show_bug.cgi?id=548397
  350. // https://bugzilla.mozilla.org/show_bug.cgi?id=1467722
  351. this.options.rtl = this.element.style.direction === 'rtl';
  352. }
  353. }
  354. /*
  355. Validate `tooltip_position` against 'orientation`
  356. - if `tooltip_position` is incompatible with orientation, switch it to a default compatible with specified `orientation`
  357. -- default for "vertical" -> "right", "left" if rtl
  358. -- default for "horizontal" -> "top"
  359. */
  360. if (this.options.orientation === "vertical" && (this.options.tooltip_position === "top" || this.options.tooltip_position === "bottom")) {
  361. if (this.options.rtl) {
  362. this.options.tooltip_position = "left";
  363. } else {
  364. this.options.tooltip_position = "right";
  365. }
  366. } else if (this.options.orientation === "horizontal" && (this.options.tooltip_position === "left" || this.options.tooltip_position === "right")) {
  367. this.options.tooltip_position = "top";
  368. }
  369. function getDataAttrib(element, optName) {
  370. var dataName = "data-slider-" + optName.replace(/_/g, '-');
  371. var dataValString = element.getAttribute(dataName);
  372. try {
  373. return JSON.parse(dataValString);
  374. } catch (err) {
  375. return dataValString;
  376. }
  377. }
  378. /*************************************************
  379. Create Markup
  380. **************************************************/
  381. var origWidth = this.element.style.width;
  382. var updateSlider = false;
  383. var parent = this.element.parentNode;
  384. var sliderTrackSelection;
  385. var sliderTrackLow, sliderTrackHigh;
  386. var sliderMinHandle;
  387. var sliderMaxHandle;
  388. if (this.sliderElem) {
  389. updateSlider = true;
  390. } else {
  391. /* Create elements needed for slider */
  392. this.sliderElem = document.createElement("div");
  393. this.sliderElem.className = "slider";
  394. /* Create slider track elements */
  395. var sliderTrack = document.createElement("div");
  396. sliderTrack.className = "slider-track";
  397. sliderTrackLow = document.createElement("div");
  398. sliderTrackLow.className = "slider-track-low";
  399. sliderTrackSelection = document.createElement("div");
  400. sliderTrackSelection.className = "slider-selection";
  401. sliderTrackHigh = document.createElement("div");
  402. sliderTrackHigh.className = "slider-track-high";
  403. sliderMinHandle = document.createElement("div");
  404. sliderMinHandle.className = "slider-handle min-slider-handle";
  405. sliderMinHandle.setAttribute('role', 'slider');
  406. sliderMinHandle.setAttribute('aria-valuemin', this.options.min);
  407. sliderMinHandle.setAttribute('aria-valuemax', this.options.max);
  408. sliderMaxHandle = document.createElement("div");
  409. sliderMaxHandle.className = "slider-handle max-slider-handle";
  410. sliderMaxHandle.setAttribute('role', 'slider');
  411. sliderMaxHandle.setAttribute('aria-valuemin', this.options.min);
  412. sliderMaxHandle.setAttribute('aria-valuemax', this.options.max);
  413. sliderTrack.appendChild(sliderTrackLow);
  414. sliderTrack.appendChild(sliderTrackSelection);
  415. sliderTrack.appendChild(sliderTrackHigh);
  416. /* Create highlight range elements */
  417. this.rangeHighlightElements = [];
  418. var rangeHighlightsOpts = this.options.rangeHighlights;
  419. if (Array.isArray(rangeHighlightsOpts) && rangeHighlightsOpts.length > 0) {
  420. for (var j = 0; j < rangeHighlightsOpts.length; j++) {
  421. var rangeHighlightElement = document.createElement("div");
  422. var customClassString = rangeHighlightsOpts[j].class || "";
  423. rangeHighlightElement.className = "slider-rangeHighlight slider-selection " + customClassString;
  424. this.rangeHighlightElements.push(rangeHighlightElement);
  425. sliderTrack.appendChild(rangeHighlightElement);
  426. }
  427. }
  428. /* Add aria-labelledby to handle's */
  429. var isLabelledbyArray = Array.isArray(this.options.labelledby);
  430. if (isLabelledbyArray && this.options.labelledby[0]) {
  431. sliderMinHandle.setAttribute('aria-labelledby', this.options.labelledby[0]);
  432. }
  433. if (isLabelledbyArray && this.options.labelledby[1]) {
  434. sliderMaxHandle.setAttribute('aria-labelledby', this.options.labelledby[1]);
  435. }
  436. if (!isLabelledbyArray && this.options.labelledby) {
  437. sliderMinHandle.setAttribute('aria-labelledby', this.options.labelledby);
  438. sliderMaxHandle.setAttribute('aria-labelledby', this.options.labelledby);
  439. }
  440. /* Create ticks */
  441. this.ticks = [];
  442. if (Array.isArray(this.options.ticks) && this.options.ticks.length > 0) {
  443. this.ticksContainer = document.createElement('div');
  444. this.ticksContainer.className = 'slider-tick-container';
  445. for (i = 0; i < this.options.ticks.length; i++) {
  446. var tick = document.createElement('div');
  447. tick.className = 'slider-tick';
  448. if (this.options.ticks_tooltip) {
  449. var tickListenerReference = this._addTickListener();
  450. var enterCallback = tickListenerReference.addMouseEnter(this, tick, i);
  451. var leaveCallback = tickListenerReference.addMouseLeave(this, tick);
  452. this.ticksCallbackMap[i] = {
  453. mouseEnter: enterCallback,
  454. mouseLeave: leaveCallback
  455. };
  456. }
  457. this.ticks.push(tick);
  458. this.ticksContainer.appendChild(tick);
  459. }
  460. sliderTrackSelection.className += " tick-slider-selection";
  461. }
  462. this.tickLabels = [];
  463. if (Array.isArray(this.options.ticks_labels) && this.options.ticks_labels.length > 0) {
  464. this.tickLabelContainer = document.createElement('div');
  465. this.tickLabelContainer.className = 'slider-tick-label-container';
  466. for (i = 0; i < this.options.ticks_labels.length; i++) {
  467. var label = document.createElement('div');
  468. var noTickPositionsSpecified = this.options.ticks_positions.length === 0;
  469. var tickLabelsIndex = this.options.reversed && noTickPositionsSpecified ? this.options.ticks_labels.length - (i + 1) : i;
  470. label.className = 'slider-tick-label';
  471. label.innerHTML = this.options.ticks_labels[tickLabelsIndex];
  472. this.tickLabels.push(label);
  473. this.tickLabelContainer.appendChild(label);
  474. }
  475. }
  476. var createAndAppendTooltipSubElements = function createAndAppendTooltipSubElements(tooltipElem) {
  477. var arrow = document.createElement("div");
  478. arrow.className = "arrow";
  479. var inner = document.createElement("div");
  480. inner.className = "tooltip-inner";
  481. tooltipElem.appendChild(arrow);
  482. tooltipElem.appendChild(inner);
  483. };
  484. /* Create tooltip elements */
  485. var sliderTooltip = document.createElement("div");
  486. sliderTooltip.className = "tooltip tooltip-main";
  487. sliderTooltip.setAttribute('role', 'presentation');
  488. createAndAppendTooltipSubElements(sliderTooltip);
  489. var sliderTooltipMin = document.createElement("div");
  490. sliderTooltipMin.className = "tooltip tooltip-min";
  491. sliderTooltipMin.setAttribute('role', 'presentation');
  492. createAndAppendTooltipSubElements(sliderTooltipMin);
  493. var sliderTooltipMax = document.createElement("div");
  494. sliderTooltipMax.className = "tooltip tooltip-max";
  495. sliderTooltipMax.setAttribute('role', 'presentation');
  496. createAndAppendTooltipSubElements(sliderTooltipMax);
  497. /* Append components to sliderElem */
  498. this.sliderElem.appendChild(sliderTrack);
  499. this.sliderElem.appendChild(sliderTooltip);
  500. this.sliderElem.appendChild(sliderTooltipMin);
  501. this.sliderElem.appendChild(sliderTooltipMax);
  502. if (this.tickLabelContainer) {
  503. this.sliderElem.appendChild(this.tickLabelContainer);
  504. }
  505. if (this.ticksContainer) {
  506. this.sliderElem.appendChild(this.ticksContainer);
  507. }
  508. this.sliderElem.appendChild(sliderMinHandle);
  509. this.sliderElem.appendChild(sliderMaxHandle);
  510. /* Append slider element to parent container, right before the original <input> element */
  511. parent.insertBefore(this.sliderElem, this.element);
  512. /* Hide original <input> element */
  513. this.element.style.display = "none";
  514. }
  515. /* If JQuery exists, cache JQ references */
  516. if ($) {
  517. this.$element = $(this.element);
  518. this.$sliderElem = $(this.sliderElem);
  519. }
  520. /*************************************************
  521. Setup
  522. **************************************************/
  523. this.eventToCallbackMap = {};
  524. this.sliderElem.id = this.options.id;
  525. this.touchCapable = 'ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch;
  526. this.touchX = 0;
  527. this.touchY = 0;
  528. this.tooltip = this.sliderElem.querySelector('.tooltip-main');
  529. this.tooltipInner = this.tooltip.querySelector('.tooltip-inner');
  530. this.tooltip_min = this.sliderElem.querySelector('.tooltip-min');
  531. this.tooltipInner_min = this.tooltip_min.querySelector('.tooltip-inner');
  532. this.tooltip_max = this.sliderElem.querySelector('.tooltip-max');
  533. this.tooltipInner_max = this.tooltip_max.querySelector('.tooltip-inner');
  534. if (SliderScale[this.options.scale]) {
  535. this.options.scale = SliderScale[this.options.scale];
  536. }
  537. if (updateSlider === true) {
  538. // Reset classes
  539. this._removeClass(this.sliderElem, 'slider-horizontal');
  540. this._removeClass(this.sliderElem, 'slider-vertical');
  541. this._removeClass(this.sliderElem, 'slider-rtl');
  542. this._removeClass(this.tooltip, 'hide');
  543. this._removeClass(this.tooltip_min, 'hide');
  544. this._removeClass(this.tooltip_max, 'hide');
  545. // Undo existing inline styles for track
  546. ["left", "right", "top", "width", "height"].forEach(function (prop) {
  547. this._removeProperty(this.trackLow, prop);
  548. this._removeProperty(this.trackSelection, prop);
  549. this._removeProperty(this.trackHigh, prop);
  550. }, this);
  551. // Undo inline styles on handles
  552. [this.handle1, this.handle2].forEach(function (handle) {
  553. this._removeProperty(handle, 'left');
  554. this._removeProperty(handle, 'right');
  555. this._removeProperty(handle, 'top');
  556. }, this);
  557. // Undo inline styles and classes on tooltips
  558. [this.tooltip, this.tooltip_min, this.tooltip_max].forEach(function (tooltip) {
  559. this._removeProperty(tooltip, 'bs-tooltip-left');
  560. this._removeProperty(tooltip, 'bs-tooltip-right');
  561. this._removeProperty(tooltip, 'bs-tooltip-top');
  562. this._removeClass(tooltip, 'bs-tooltip-right');
  563. this._removeClass(tooltip, 'bs-tooltip-left');
  564. this._removeClass(tooltip, 'bs-tooltip-top');
  565. }, this);
  566. }
  567. if (this.options.orientation === 'vertical') {
  568. this._addClass(this.sliderElem, 'slider-vertical');
  569. this.stylePos = 'top';
  570. this.mousePos = 'pageY';
  571. this.sizePos = 'offsetHeight';
  572. } else {
  573. this._addClass(this.sliderElem, 'slider-horizontal');
  574. this.sliderElem.style.width = origWidth;
  575. this.options.orientation = 'horizontal';
  576. if (this.options.rtl) {
  577. this.stylePos = 'right';
  578. } else {
  579. this.stylePos = 'left';
  580. }
  581. this.mousePos = 'clientX';
  582. this.sizePos = 'offsetWidth';
  583. }
  584. // specific rtl class
  585. if (this.options.rtl) {
  586. this._addClass(this.sliderElem, 'slider-rtl');
  587. }
  588. this._setTooltipPosition();
  589. /* In case ticks are specified, overwrite the min and max bounds */
  590. if (Array.isArray(this.options.ticks) && this.options.ticks.length > 0) {
  591. if (!isMaxSet) {
  592. this.options.max = Math.max.apply(Math, this.options.ticks);
  593. }
  594. if (!isMinSet) {
  595. this.options.min = Math.min.apply(Math, this.options.ticks);
  596. }
  597. }
  598. if (Array.isArray(this.options.value)) {
  599. this.options.range = true;
  600. this._state.value = this.options.value;
  601. } else if (this.options.range) {
  602. // User wants a range, but value is not an array
  603. this._state.value = [this.options.value, this.options.max];
  604. } else {
  605. this._state.value = this.options.value;
  606. }
  607. this.trackLow = sliderTrackLow || this.trackLow;
  608. this.trackSelection = sliderTrackSelection || this.trackSelection;
  609. this.trackHigh = sliderTrackHigh || this.trackHigh;
  610. if (this.options.selection === 'none') {
  611. this._addClass(this.trackLow, 'hide');
  612. this._addClass(this.trackSelection, 'hide');
  613. this._addClass(this.trackHigh, 'hide');
  614. } else if (this.options.selection === 'after' || this.options.selection === 'before') {
  615. this._removeClass(this.trackLow, 'hide');
  616. this._removeClass(this.trackSelection, 'hide');
  617. this._removeClass(this.trackHigh, 'hide');
  618. }
  619. this.handle1 = sliderMinHandle || this.handle1;
  620. this.handle2 = sliderMaxHandle || this.handle2;
  621. if (updateSlider === true) {
  622. // Reset classes
  623. this._removeClass(this.handle1, 'round triangle');
  624. this._removeClass(this.handle2, 'round triangle hide');
  625. for (i = 0; i < this.ticks.length; i++) {
  626. this._removeClass(this.ticks[i], 'round triangle hide');
  627. }
  628. }
  629. var availableHandleModifiers = ['round', 'triangle', 'custom'];
  630. var isValidHandleType = availableHandleModifiers.indexOf(this.options.handle) !== -1;
  631. if (isValidHandleType) {
  632. this._addClass(this.handle1, this.options.handle);
  633. this._addClass(this.handle2, this.options.handle);
  634. for (i = 0; i < this.ticks.length; i++) {
  635. this._addClass(this.ticks[i], this.options.handle);
  636. }
  637. }
  638. this._state.offset = this._offset(this.sliderElem);
  639. this._state.size = this.sliderElem[this.sizePos];
  640. this.setValue(this._state.value);
  641. /******************************************
  642. Bind Event Listeners
  643. ******************************************/
  644. // Bind keyboard handlers
  645. this.handle1Keydown = this._keydown.bind(this, 0);
  646. this.handle1.addEventListener("keydown", this.handle1Keydown, false);
  647. this.handle2Keydown = this._keydown.bind(this, 1);
  648. this.handle2.addEventListener("keydown", this.handle2Keydown, false);
  649. this.mousedown = this._mousedown.bind(this);
  650. this.touchstart = this._touchstart.bind(this);
  651. this.touchmove = this._touchmove.bind(this);
  652. if (this.touchCapable) {
  653. this.sliderElem.addEventListener("touchstart", this.touchstart, false);
  654. this.sliderElem.addEventListener("touchmove", this.touchmove, false);
  655. }
  656. this.sliderElem.addEventListener("mousedown", this.mousedown, false);
  657. // Bind window handlers
  658. this.resize = this._resize.bind(this);
  659. window.addEventListener("resize", this.resize, false);
  660. // Bind tooltip-related handlers
  661. if (this.options.tooltip === 'hide') {
  662. this._addClass(this.tooltip, 'hide');
  663. this._addClass(this.tooltip_min, 'hide');
  664. this._addClass(this.tooltip_max, 'hide');
  665. } else if (this.options.tooltip === 'always') {
  666. this._showTooltip();
  667. this._alwaysShowTooltip = true;
  668. } else {
  669. this.showTooltip = this._showTooltip.bind(this);
  670. this.hideTooltip = this._hideTooltip.bind(this);
  671. if (this.options.ticks_tooltip) {
  672. var callbackHandle = this._addTickListener();
  673. //create handle1 listeners and store references in map
  674. var mouseEnter = callbackHandle.addMouseEnter(this, this.handle1);
  675. var mouseLeave = callbackHandle.addMouseLeave(this, this.handle1);
  676. this.handleCallbackMap.handle1 = {
  677. mouseEnter: mouseEnter,
  678. mouseLeave: mouseLeave
  679. };
  680. //create handle2 listeners and store references in map
  681. mouseEnter = callbackHandle.addMouseEnter(this, this.handle2);
  682. mouseLeave = callbackHandle.addMouseLeave(this, this.handle2);
  683. this.handleCallbackMap.handle2 = {
  684. mouseEnter: mouseEnter,
  685. mouseLeave: mouseLeave
  686. };
  687. } else {
  688. this.sliderElem.addEventListener("mouseenter", this.showTooltip, false);
  689. this.sliderElem.addEventListener("mouseleave", this.hideTooltip, false);
  690. if (this.touchCapable) {
  691. this.sliderElem.addEventListener("touchstart", this.showTooltip, false);
  692. this.sliderElem.addEventListener("touchmove", this.showTooltip, false);
  693. this.sliderElem.addEventListener("touchend", this.hideTooltip, false);
  694. }
  695. }
  696. this.handle1.addEventListener("focus", this.showTooltip, false);
  697. this.handle1.addEventListener("blur", this.hideTooltip, false);
  698. this.handle2.addEventListener("focus", this.showTooltip, false);
  699. this.handle2.addEventListener("blur", this.hideTooltip, false);
  700. if (this.touchCapable) {
  701. this.handle1.addEventListener("touchstart", this.showTooltip, false);
  702. this.handle1.addEventListener("touchmove", this.showTooltip, false);
  703. this.handle1.addEventListener("touchend", this.hideTooltip, false);
  704. this.handle2.addEventListener("touchstart", this.showTooltip, false);
  705. this.handle2.addEventListener("touchmove", this.showTooltip, false);
  706. this.handle2.addEventListener("touchend", this.hideTooltip, false);
  707. }
  708. }
  709. if (this.options.enabled) {
  710. this.enable();
  711. } else {
  712. this.disable();
  713. }
  714. }
  715. /*************************************************
  716. INSTANCE PROPERTIES/METHODS
  717. - Any methods bound to the prototype are considered
  718. part of the plugin's `public` interface
  719. **************************************************/
  720. Slider.prototype = {
  721. _init: function _init() {}, // NOTE: Must exist to support bridget
  722. constructor: Slider,
  723. defaultOptions: {
  724. id: "",
  725. min: 0,
  726. max: 10,
  727. step: 1,
  728. precision: 0,
  729. orientation: 'horizontal',
  730. value: 5,
  731. range: false,
  732. selection: 'before',
  733. tooltip: 'show',
  734. tooltip_split: false,
  735. lock_to_ticks: false,
  736. handle: 'round',
  737. reversed: false,
  738. rtl: 'auto',
  739. enabled: true,
  740. formatter: function formatter(val) {
  741. if (Array.isArray(val)) {
  742. return val[0] + " : " + val[1];
  743. } else {
  744. return val;
  745. }
  746. },
  747. natural_arrow_keys: false,
  748. ticks: [],
  749. ticks_positions: [],
  750. ticks_labels: [],
  751. ticks_snap_bounds: 0,
  752. ticks_tooltip: false,
  753. scale: 'linear',
  754. focus: false,
  755. tooltip_position: null,
  756. labelledby: null,
  757. rangeHighlights: []
  758. },
  759. getElement: function getElement() {
  760. return this.sliderElem;
  761. },
  762. getValue: function getValue() {
  763. if (this.options.range) {
  764. return this._state.value;
  765. } else {
  766. return this._state.value[0];
  767. }
  768. },
  769. setValue: function setValue(val, triggerSlideEvent, triggerChangeEvent) {
  770. if (!val) {
  771. val = 0;
  772. }
  773. var oldValue = this.getValue();
  774. this._state.value = this._validateInputValue(val);
  775. var applyPrecision = this._applyPrecision.bind(this);
  776. if (this.options.range) {
  777. this._state.value[0] = applyPrecision(this._state.value[0]);
  778. this._state.value[1] = applyPrecision(this._state.value[1]);
  779. if (this.ticksAreValid && this.options.lock_to_ticks) {
  780. this._state.value[0] = this.options.ticks[this._getClosestTickIndex(this._state.value[0])];
  781. this._state.value[1] = this.options.ticks[this._getClosestTickIndex(this._state.value[1])];
  782. }
  783. this._state.value[0] = Math.max(this.options.min, Math.min(this.options.max, this._state.value[0]));
  784. this._state.value[1] = Math.max(this.options.min, Math.min(this.options.max, this._state.value[1]));
  785. } else {
  786. this._state.value = applyPrecision(this._state.value);
  787. if (this.ticksAreValid && this.options.lock_to_ticks) {
  788. this._state.value = this.options.ticks[this._getClosestTickIndex(this._state.value)];
  789. }
  790. this._state.value = [Math.max(this.options.min, Math.min(this.options.max, this._state.value))];
  791. this._addClass(this.handle2, 'hide');
  792. if (this.options.selection === 'after') {
  793. this._state.value[1] = this.options.max;
  794. } else {
  795. this._state.value[1] = this.options.min;
  796. }
  797. }
  798. // Determine which ticks the handle(s) are set at (if applicable)
  799. this._setTickIndex();
  800. if (this.options.max > this.options.min) {
  801. this._state.percentage = [this._toPercentage(this._state.value[0]), this._toPercentage(this._state.value[1]), this.options.step * 100 / (this.options.max - this.options.min)];
  802. } else {
  803. this._state.percentage = [0, 0, 100];
  804. }
  805. this._layout();
  806. var newValue = this.options.range ? this._state.value : this._state.value[0];
  807. this._setDataVal(newValue);
  808. if (triggerSlideEvent === true) {
  809. this._trigger('slide', newValue);
  810. }
  811. var hasChanged = false;
  812. if (Array.isArray(newValue)) {
  813. hasChanged = oldValue[0] !== newValue[0] || oldValue[1] !== newValue[1];
  814. } else {
  815. hasChanged = oldValue !== newValue;
  816. }
  817. if (hasChanged && triggerChangeEvent === true) {
  818. this._trigger('change', {
  819. oldValue: oldValue,
  820. newValue: newValue
  821. });
  822. }
  823. return this;
  824. },
  825. destroy: function destroy() {
  826. // Remove event handlers on slider elements
  827. this._removeSliderEventHandlers();
  828. // Remove the slider from the DOM
  829. this.sliderElem.parentNode.removeChild(this.sliderElem);
  830. /* Show original <input> element */
  831. this.element.style.display = "";
  832. // Clear out custom event bindings
  833. this._cleanUpEventCallbacksMap();
  834. // Remove data values
  835. this.element.removeAttribute("data");
  836. // Remove JQuery handlers/data
  837. if ($) {
  838. this._unbindJQueryEventHandlers();
  839. if (autoRegisterNamespace === NAMESPACE_MAIN) {
  840. this.$element.removeData(autoRegisterNamespace);
  841. }
  842. this.$element.removeData(NAMESPACE_ALTERNATE);
  843. }
  844. },
  845. disable: function disable() {
  846. this._state.enabled = false;
  847. this.handle1.removeAttribute("tabindex");
  848. this.handle2.removeAttribute("tabindex");
  849. this._addClass(this.sliderElem, 'slider-disabled');
  850. this._trigger('slideDisabled');
  851. return this;
  852. },
  853. enable: function enable() {
  854. this._state.enabled = true;
  855. this.handle1.setAttribute("tabindex", 0);
  856. this.handle2.setAttribute("tabindex", 0);
  857. this._removeClass(this.sliderElem, 'slider-disabled');
  858. this._trigger('slideEnabled');
  859. return this;
  860. },
  861. toggle: function toggle() {
  862. if (this._state.enabled) {
  863. this.disable();
  864. } else {
  865. this.enable();
  866. }
  867. return this;
  868. },
  869. isEnabled: function isEnabled() {
  870. return this._state.enabled;
  871. },
  872. on: function on(evt, callback) {
  873. this._bindNonQueryEventHandler(evt, callback);
  874. return this;
  875. },
  876. off: function off(evt, callback) {
  877. if ($) {
  878. this.$element.off(evt, callback);
  879. this.$sliderElem.off(evt, callback);
  880. } else {
  881. this._unbindNonQueryEventHandler(evt, callback);
  882. }
  883. },
  884. getAttribute: function getAttribute(attribute) {
  885. if (attribute) {
  886. return this.options[attribute];
  887. } else {
  888. return this.options;
  889. }
  890. },
  891. setAttribute: function setAttribute(attribute, value) {
  892. this.options[attribute] = value;
  893. return this;
  894. },
  895. refresh: function refresh(options) {
  896. var currentValue = this.getValue();
  897. this._removeSliderEventHandlers();
  898. createNewSlider.call(this, this.element, this.options);
  899. // Don't reset slider's value on refresh if `useCurrentValue` is true
  900. if (options && options.useCurrentValue === true) {
  901. this.setValue(currentValue);
  902. }
  903. if ($) {
  904. // Bind new instance of slider to the element
  905. if (autoRegisterNamespace === NAMESPACE_MAIN) {
  906. $.data(this.element, NAMESPACE_MAIN, this);
  907. $.data(this.element, NAMESPACE_ALTERNATE, this);
  908. } else {
  909. $.data(this.element, NAMESPACE_ALTERNATE, this);
  910. }
  911. }
  912. return this;
  913. },
  914. relayout: function relayout() {
  915. this._resize();
  916. return this;
  917. },
  918. /******************************+
  919. HELPERS
  920. - Any method that is not part of the public interface.
  921. - Place it underneath this comment block and write its signature like so:
  922. _fnName : function() {...}
  923. ********************************/
  924. _removeTooltipListener: function _removeTooltipListener(event, handler) {
  925. this.handle1.removeEventListener(event, handler, false);
  926. this.handle2.removeEventListener(event, handler, false);
  927. },
  928. _removeSliderEventHandlers: function _removeSliderEventHandlers() {
  929. // Remove keydown event listeners
  930. this.handle1.removeEventListener("keydown", this.handle1Keydown, false);
  931. this.handle2.removeEventListener("keydown", this.handle2Keydown, false);
  932. //remove the listeners from the ticks and handles if they had their own listeners
  933. if (this.options.ticks_tooltip) {
  934. var ticks = this.ticksContainer.getElementsByClassName('slider-tick');
  935. for (var i = 0; i < ticks.length; i++) {
  936. ticks[i].removeEventListener('mouseenter', this.ticksCallbackMap[i].mouseEnter, false);
  937. ticks[i].removeEventListener('mouseleave', this.ticksCallbackMap[i].mouseLeave, false);
  938. }
  939. if (this.handleCallbackMap.handle1 && this.handleCallbackMap.handle2) {
  940. this.handle1.removeEventListener('mouseenter', this.handleCallbackMap.handle1.mouseEnter, false);
  941. this.handle2.removeEventListener('mouseenter', this.handleCallbackMap.handle2.mouseEnter, false);
  942. this.handle1.removeEventListener('mouseleave', this.handleCallbackMap.handle1.mouseLeave, false);
  943. this.handle2.removeEventListener('mouseleave', this.handleCallbackMap.handle2.mouseLeave, false);
  944. }
  945. }
  946. this.handleCallbackMap = null;
  947. this.ticksCallbackMap = null;
  948. if (this.showTooltip) {
  949. this._removeTooltipListener("focus", this.showTooltip);
  950. }
  951. if (this.hideTooltip) {
  952. this._removeTooltipListener("blur", this.hideTooltip);
  953. }
  954. // Remove event listeners from sliderElem
  955. if (this.showTooltip) {
  956. this.sliderElem.removeEventListener("mouseenter", this.showTooltip, false);
  957. }
  958. if (this.hideTooltip) {
  959. this.sliderElem.removeEventListener("mouseleave", this.hideTooltip, false);
  960. }
  961. this.sliderElem.removeEventListener("mousedown", this.mousedown, false);
  962. if (this.touchCapable) {
  963. // Remove touch event listeners from handles
  964. if (this.showTooltip) {
  965. this.handle1.removeEventListener("touchstart", this.showTooltip, false);
  966. this.handle1.removeEventListener("touchmove", this.showTooltip, false);
  967. this.handle2.removeEventListener("touchstart", this.showTooltip, false);
  968. this.handle2.removeEventListener("touchmove", this.showTooltip, false);
  969. }
  970. if (this.hideTooltip) {
  971. this.handle1.removeEventListener("touchend", this.hideTooltip, false);
  972. this.handle2.removeEventListener("touchend", this.hideTooltip, false);
  973. }
  974. // Remove event listeners from sliderElem
  975. if (this.showTooltip) {
  976. this.sliderElem.removeEventListener("touchstart", this.showTooltip, false);
  977. this.sliderElem.removeEventListener("touchmove", this.showTooltip, false);
  978. }
  979. if (this.hideTooltip) {
  980. this.sliderElem.removeEventListener("touchend", this.hideTooltip, false);
  981. }
  982. this.sliderElem.removeEventListener("touchstart", this.touchstart, false);
  983. this.sliderElem.removeEventListener("touchmove", this.touchmove, false);
  984. }
  985. // Remove window event listener
  986. window.removeEventListener("resize", this.resize, false);
  987. },
  988. _bindNonQueryEventHandler: function _bindNonQueryEventHandler(evt, callback) {
  989. if (this.eventToCallbackMap[evt] === undefined) {
  990. this.eventToCallbackMap[evt] = [];
  991. }
  992. this.eventToCallbackMap[evt].push(callback);
  993. },
  994. _unbindNonQueryEventHandler: function _unbindNonQueryEventHandler(evt, callback) {
  995. var callbacks = this.eventToCallbackMap[evt];
  996. if (callbacks !== undefined) {
  997. for (var i = 0; i < callbacks.length; i++) {
  998. if (callbacks[i] === callback) {
  999. callbacks.splice(i, 1);
  1000. break;
  1001. }
  1002. }
  1003. }
  1004. },
  1005. _cleanUpEventCallbacksMap: function _cleanUpEventCallbacksMap() {
  1006. var eventNames = Object.keys(this.eventToCallbackMap);
  1007. for (var i = 0; i < eventNames.length; i++) {
  1008. var eventName = eventNames[i];
  1009. delete this.eventToCallbackMap[eventName];
  1010. }
  1011. },
  1012. _showTooltip: function _showTooltip() {
  1013. if (this.options.tooltip_split === false) {
  1014. this._addClass(this.tooltip, 'show');
  1015. this.tooltip_min.style.display = 'none';
  1016. this.tooltip_max.style.display = 'none';
  1017. } else {
  1018. this._addClass(this.tooltip_min, 'show');
  1019. this._addClass(this.tooltip_max, 'show');
  1020. this.tooltip.style.display = 'none';
  1021. }
  1022. this._state.over = true;
  1023. },
  1024. _hideTooltip: function _hideTooltip() {
  1025. if (this._state.inDrag === false && this._alwaysShowTooltip !== true) {
  1026. this._removeClass(this.tooltip, 'show');
  1027. this._removeClass(this.tooltip_min, 'show');
  1028. this._removeClass(this.tooltip_max, 'show');
  1029. }
  1030. this._state.over = false;
  1031. },
  1032. _setToolTipOnMouseOver: function _setToolTipOnMouseOver(tempState) {
  1033. var self = this;
  1034. var formattedTooltipVal = this.options.formatter(!tempState ? this._state.value[0] : tempState.value[0]);
  1035. var positionPercentages = !tempState ? getPositionPercentages(this._state, this.options.reversed) : getPositionPercentages(tempState, this.options.reversed);
  1036. this._setText(this.tooltipInner, formattedTooltipVal);
  1037. this.tooltip.style[this.stylePos] = positionPercentages[0] + "%";
  1038. function getPositionPercentages(state, reversed) {
  1039. if (reversed) {
  1040. return [100 - state.percentage[0], self.options.range ? 100 - state.percentage[1] : state.percentage[1]];
  1041. }
  1042. return [state.percentage[0], state.percentage[1]];
  1043. }
  1044. },
  1045. _copyState: function _copyState() {
  1046. return {
  1047. value: [this._state.value[0], this._state.value[1]],
  1048. enabled: this._state.enabled,
  1049. offset: this._state.offset,
  1050. size: this._state.size,
  1051. percentage: [this._state.percentage[0], this._state.percentage[1], this._state.percentage[2]],
  1052. inDrag: this._state.inDrag,
  1053. over: this._state.over,
  1054. // deleted or null'd keys
  1055. dragged: this._state.dragged,
  1056. keyCtrl: this._state.keyCtrl
  1057. };
  1058. },
  1059. _addTickListener: function _addTickListener() {
  1060. return {
  1061. addMouseEnter: function addMouseEnter(reference, element, index) {
  1062. var enter = function enter() {
  1063. var tempState = reference._copyState();
  1064. // Which handle is being hovered over?
  1065. var val = element === reference.handle1 ? tempState.value[0] : tempState.value[1];
  1066. var per = void 0;
  1067. // Setup value and percentage for tick's 'mouseenter'
  1068. if (index !== undefined) {
  1069. val = reference.options.ticks[index];
  1070. per = reference.options.ticks_positions.length > 0 && reference.options.ticks_positions[index] || reference._toPercentage(reference.options.ticks[index]);
  1071. } else {
  1072. per = reference._toPercentage(val);
  1073. }
  1074. tempState.value[0] = val;
  1075. tempState.percentage[0] = per;
  1076. reference._setToolTipOnMouseOver(tempState);
  1077. reference._showTooltip();
  1078. };
  1079. element.addEventListener("mouseenter", enter, false);
  1080. return enter;
  1081. },
  1082. addMouseLeave: function addMouseLeave(reference, element) {
  1083. var leave = function leave() {
  1084. reference._hideTooltip();
  1085. };
  1086. element.addEventListener("mouseleave", leave, false);
  1087. return leave;
  1088. }
  1089. };
  1090. },
  1091. _layout: function _layout() {
  1092. var positionPercentages;
  1093. var formattedValue;
  1094. if (this.options.reversed) {
  1095. positionPercentages = [100 - this._state.percentage[0], this.options.range ? 100 - this._state.percentage[1] : this._state.percentage[1]];
  1096. } else {
  1097. positionPercentages = [this._state.percentage[0], this._state.percentage[1]];
  1098. }
  1099. this.handle1.style[this.stylePos] = positionPercentages[0] + "%";
  1100. this.handle1.setAttribute('aria-valuenow', this._state.value[0]);
  1101. formattedValue = this.options.formatter(this._state.value[0]);
  1102. if (isNaN(formattedValue)) {
  1103. this.handle1.setAttribute('aria-valuetext', formattedValue);
  1104. } else {
  1105. this.handle1.removeAttribute('aria-valuetext');
  1106. }
  1107. this.handle2.style[this.stylePos] = positionPercentages[1] + "%";
  1108. this.handle2.setAttribute('aria-valuenow', this._state.value[1]);
  1109. formattedValue = this.options.formatter(this._state.value[1]);
  1110. if (isNaN(formattedValue)) {
  1111. this.handle2.setAttribute('aria-valuetext', formattedValue);
  1112. } else {
  1113. this.handle2.removeAttribute('aria-valuetext');
  1114. }
  1115. /* Position highlight range elements */
  1116. if (this.rangeHighlightElements.length > 0 && Array.isArray(this.options.rangeHighlights) && this.options.rangeHighlights.length > 0) {
  1117. for (var _i = 0; _i < this.options.rangeHighlights.length; _i++) {
  1118. var startPercent = this._toPercentage(this.options.rangeHighlights[_i].start);
  1119. var endPercent = this._toPercentage(this.options.rangeHighlights[_i].end);
  1120. if (this.options.reversed) {
  1121. var sp = 100 - endPercent;
  1122. endPercent = 100 - startPercent;
  1123. startPercent = sp;
  1124. }
  1125. var currentRange = this._createHighlightRange(startPercent, endPercent);
  1126. if (currentRange) {
  1127. if (this.options.orientation === 'vertical') {
  1128. this.rangeHighlightElements[_i].style.top = currentRange.start + "%";
  1129. this.rangeHighlightElements[_i].style.height = currentRange.size + "%";
  1130. } else {
  1131. if (this.options.rtl) {
  1132. this.rangeHighlightElements[_i].style.right = currentRange.start + "%";
  1133. } else {
  1134. this.rangeHighlightElements[_i].style.left = currentRange.start + "%";
  1135. }
  1136. this.rangeHighlightElements[_i].style.width = currentRange.size + "%";
  1137. }
  1138. } else {
  1139. this.rangeHighlightElements[_i].style.display = "none";
  1140. }
  1141. }
  1142. }
  1143. /* Position ticks and labels */
  1144. if (Array.isArray(this.options.ticks) && this.options.ticks.length > 0) {
  1145. var styleSize = this.options.orientation === 'vertical' ? 'height' : 'width';
  1146. var styleMargin;
  1147. if (this.options.orientation === 'vertical') {
  1148. styleMargin = 'marginTop';
  1149. } else {
  1150. if (this.options.rtl) {
  1151. styleMargin = 'marginRight';
  1152. } else {
  1153. styleMargin = 'marginLeft';
  1154. }
  1155. }
  1156. var labelSize = this._state.size / (this.options.ticks.length - 1);
  1157. if (this.tickLabelContainer) {
  1158. var extraMargin = 0;
  1159. if (this.options.ticks_positions.length === 0) {
  1160. if (this.options.orientation !== 'vertical') {
  1161. this.tickLabelContainer.style[styleMargin] = -labelSize / 2 + "px";
  1162. }
  1163. extraMargin = this.tickLabelContainer.offsetHeight;
  1164. } else {
  1165. /* Chidren are position absolute, calculate height by finding the max offsetHeight of a child */
  1166. for (i = 0; i < this.tickLabelContainer.childNodes.length; i++) {
  1167. if (this.tickLabelContainer.childNodes[i].offsetHeight > extraMargin) {
  1168. extraMargin = this.tickLabelContainer.childNodes[i].offsetHeight;
  1169. }
  1170. }
  1171. }
  1172. if (this.options.orientation === 'horizontal') {
  1173. this.sliderElem.style.marginBottom = extraMargin + "px";
  1174. }
  1175. }
  1176. for (var i = 0; i < this.options.ticks.length; i++) {
  1177. var percentage = this.options.ticks_positions[i] || this._toPercentage(this.options.ticks[i]);
  1178. if (this.options.reversed) {
  1179. percentage = 100 - percentage;
  1180. }
  1181. this.ticks[i].style[this.stylePos] = percentage + "%";
  1182. /* Set class labels to denote whether ticks are in the selection */
  1183. this._removeClass(this.ticks[i], 'in-selection');
  1184. if (!this.options.range) {
  1185. if (this.options.selection === 'after' && percentage >= positionPercentages[0]) {
  1186. this._addClass(this.ticks[i], 'in-selection');
  1187. } else if (this.options.selection === 'before' && percentage <= positionPercentages[0]) {
  1188. this._addClass(this.ticks[i], 'in-selection');
  1189. }
  1190. } else if (percentage >= positionPercentages[0] && percentage <= positionPercentages[1]) {
  1191. this._addClass(this.ticks[i], 'in-selection');
  1192. }
  1193. if (this.tickLabels[i]) {
  1194. this.tickLabels[i].style[styleSize] = labelSize + "px";
  1195. if (this.options.orientation !== 'vertical' && this.options.ticks_positions[i] !== undefined) {
  1196. this.tickLabels[i].style.position = 'absolute';
  1197. this.tickLabels[i].style[this.stylePos] = percentage + "%";
  1198. this.tickLabels[i].style[styleMargin] = -labelSize / 2 + 'px';
  1199. } else if (this.options.orientation === 'vertical') {
  1200. if (this.options.rtl) {
  1201. this.tickLabels[i].style['marginRight'] = this.sliderElem.offsetWidth + "px";
  1202. } else {
  1203. this.tickLabels[i].style['marginLeft'] = this.sliderElem.offsetWidth + "px";
  1204. }
  1205. this.tickLabelContainer.style[styleMargin] = this.sliderElem.offsetWidth / 2 * -1 + 'px';
  1206. }
  1207. /* Set class labels to indicate tick labels are in the selection or selected */
  1208. this._removeClass(this.tickLabels[i], 'label-in-selection label-is-selection');
  1209. if (!this.options.range) {
  1210. if (this.options.selection === 'after' && percentage >= positionPercentages[0]) {
  1211. this._addClass(this.tickLabels[i], 'label-in-selection');
  1212. } else if (this.options.selection === 'before' && percentage <= positionPercentages[0]) {
  1213. this._addClass(this.tickLabels[i], 'label-in-selection');
  1214. }
  1215. if (percentage === positionPercentages[0]) {
  1216. this._addClass(this.tickLabels[i], 'label-is-selection');
  1217. }
  1218. } else if (percentage >= positionPercentages[0] && percentage <= positionPercentages[1]) {
  1219. this._addClass(this.tickLabels[i], 'label-in-selection');
  1220. if (percentage === positionPercentages[0] || positionPercentages[1]) {
  1221. this._addClass(this.tickLabels[i], 'label-is-selection');
  1222. }
  1223. }
  1224. }
  1225. }
  1226. }
  1227. var formattedTooltipVal;
  1228. if (this.options.range) {
  1229. formattedTooltipVal = this.options.formatter(this._state.value);
  1230. this._setText(this.tooltipInner, formattedTooltipVal);
  1231. this.tooltip.style[this.stylePos] = (positionPercentages[1] + positionPercentages[0]) / 2 + "%";
  1232. var innerTooltipMinText = this.options.formatter(this._state.value[0]);
  1233. this._setText(this.tooltipInner_min, innerTooltipMinText);
  1234. var innerTooltipMaxText = this.options.formatter(this._state.value[1]);
  1235. this._setText(this.tooltipInner_max, innerTooltipMaxText);
  1236. this.tooltip_min.style[this.stylePos] = positionPercentages[0] + "%";
  1237. this.tooltip_max.style[this.stylePos] = positionPercentages[1] + "%";
  1238. } else {
  1239. formattedTooltipVal = this.options.formatter(this._state.value[0]);
  1240. this._setText(this.tooltipInner, formattedTooltipVal);
  1241. this.tooltip.style[this.stylePos] = positionPercentages[0] + "%";
  1242. }
  1243. if (this.options.orientation === 'vertical') {
  1244. this.trackLow.style.top = '0';
  1245. this.trackLow.style.height = Math.min(positionPercentages[0], positionPercentages[1]) + '%';
  1246. this.trackSelection.style.top = Math.min(positionPercentages[0], positionPercentages[1]) + '%';
  1247. this.trackSelection.style.height = Math.abs(positionPercentages[0] - positionPercentages[1]) + '%';
  1248. this.trackHigh.style.bottom = '0';
  1249. this.trackHigh.style.height = 100 - Math.min(positionPercentages[0], positionPercentages[1]) - Math.abs(positionPercentages[0] - positionPercentages[1]) + '%';
  1250. } else {
  1251. if (this.stylePos === 'right') {
  1252. this.trackLow.style.right = '0';
  1253. } else {
  1254. this.trackLow.style.left = '0';
  1255. }
  1256. this.trackLow.style.width = Math.min(positionPercentages[0], positionPercentages[1]) + '%';
  1257. if (this.stylePos === 'right') {
  1258. this.trackSelection.style.right = Math.min(positionPercentages[0], positionPercentages[1]) + '%';
  1259. } else {
  1260. this.trackSelection.style.left = Math.min(positionPercentages[0], positionPercentages[1]) + '%';
  1261. }
  1262. this.trackSelection.style.width = Math.abs(positionPercentages[0] - positionPercentages[1]) + '%';
  1263. if (this.stylePos === 'right') {
  1264. this.trackHigh.style.left = '0';
  1265. } else {
  1266. this.trackHigh.style.right = '0';
  1267. }
  1268. this.trackHigh.style.width = 100 - Math.min(positionPercentages[0], positionPercentages[1]) - Math.abs(positionPercentages[0] - positionPercentages[1]) + '%';
  1269. var offset_min = this.tooltip_min.getBoundingClientRect();
  1270. var offset_max = this.tooltip_max.getBoundingClientRect();
  1271. if (this.options.tooltip_position === 'bottom') {
  1272. if (offset_min.right > offset_max.left) {
  1273. this._removeClass(this.tooltip_max, 'bs-tooltip-bottom');
  1274. this._addClass(this.tooltip_max, 'bs-tooltip-top');
  1275. this.tooltip_max.style.top = '';
  1276. this.tooltip_max.style.bottom = 22 + 'px';
  1277. } else {
  1278. this._removeClass(this.tooltip_max, 'bs-tooltip-top');
  1279. this._addClass(this.tooltip_max, 'bs-tooltip-bottom');
  1280. this.tooltip_max.style.top = this.tooltip_min.style.top;
  1281. this.tooltip_max.style.bottom = '';
  1282. }
  1283. } else {
  1284. if (offset_min.right > offset_max.left) {
  1285. this._removeClass(this.tooltip_max, 'bs-tooltip-top');
  1286. this._addClass(this.tooltip_max, 'bs-tooltip-bottom');
  1287. this.tooltip_max.style.top = 18 + 'px';
  1288. } else {
  1289. this._removeClass(this.tooltip_max, 'bs-tooltip-bottom');
  1290. this._addClass(this.tooltip_max, 'bs-tooltip-top');
  1291. this.tooltip_max.style.top = this.tooltip_min.style.top;
  1292. }
  1293. }
  1294. }
  1295. },
  1296. _createHighlightRange: function _createHighlightRange(start, end) {
  1297. if (this._isHighlightRange(start, end)) {
  1298. if (start > end) {
  1299. return { 'start': end, 'size': start - end };
  1300. }
  1301. return { 'start': start, 'size': end - start };
  1302. }
  1303. return null;
  1304. },
  1305. _isHighlightRange: function _isHighlightRange(start, end) {
  1306. if (0 <= start && start <= 100 && 0 <= end && end <= 100) {
  1307. return true;
  1308. } else {
  1309. return false;
  1310. }
  1311. },
  1312. _resize: function _resize(ev) {
  1313. /*jshint unused:false*/
  1314. this._state.offset = this._offset(this.sliderElem);
  1315. this._state.size = this.sliderElem[this.sizePos];
  1316. this._layout();
  1317. },
  1318. _removeProperty: function _removeProperty(element, prop) {
  1319. if (element.style.removeProperty) {
  1320. element.style.removeProperty(prop);
  1321. } else {
  1322. element.style.removeAttribute(prop);
  1323. }
  1324. },
  1325. _mousedown: function _mousedown(ev) {
  1326. if (!this._state.enabled) {
  1327. return false;
  1328. }
  1329. if (ev.preventDefault) {
  1330. ev.preventDefault();
  1331. }
  1332. this._state.offset = this._offset(this.sliderElem);
  1333. this._state.size = this.sliderElem[this.sizePos];
  1334. var percentage = this._getPercentage(ev);
  1335. if (this.options.range) {
  1336. var diff1 = Math.abs(this._state.percentage[0] - percentage);
  1337. var diff2 = Math.abs(this._state.percentage[1] - percentage);
  1338. this._state.dragged = diff1 < diff2 ? 0 : 1;
  1339. this._adjustPercentageForRangeSliders(percentage);
  1340. } else {
  1341. this._state.dragged = 0;
  1342. }
  1343. this._state.percentage[this._state.dragged] = percentage;
  1344. if (this.touchCapable) {
  1345. document.removeEventListener("touchmove", this.mousemove, false);
  1346. document.removeEventListener("touchend", this.mouseup, false);
  1347. }
  1348. if (this.mousemove) {
  1349. document.removeEventListener("mousemove", this.mousemove, false);
  1350. }
  1351. if (this.mouseup) {
  1352. document.removeEventListener("mouseup", this.mouseup, false);
  1353. }
  1354. this.mousemove = this._mousemove.bind(this);
  1355. this.mouseup = this._mouseup.bind(this);
  1356. if (this.touchCapable) {
  1357. // Touch: Bind touch events:
  1358. document.addEventListener("touchmove", this.mousemove, false);
  1359. document.addEventListener("touchend", this.mouseup, false);
  1360. }
  1361. // Bind mouse events:
  1362. document.addEventListener("mousemove", this.mousemove, false);
  1363. document.addEventListener("mouseup", this.mouseup, false);
  1364. this._state.inDrag = true;
  1365. var newValue = this._calculateValue();
  1366. this._trigger('slideStart', newValue);
  1367. this.setValue(newValue, false, true);
  1368. ev.returnValue = false;
  1369. if (this.options.focus) {
  1370. this._triggerFocusOnHandle(this._state.dragged);
  1371. }
  1372. return true;
  1373. },
  1374. _touchstart: function _touchstart(ev) {
  1375. this._mousedown(ev);
  1376. },
  1377. _triggerFocusOnHandle: function _triggerFocusOnHandle(handleIdx) {
  1378. if (handleIdx === 0) {
  1379. this.handle1.focus();
  1380. }
  1381. if (handleIdx === 1) {
  1382. this.handle2.focus();
  1383. }
  1384. },
  1385. _keydown: function _keydown(handleIdx, ev) {
  1386. if (!this._state.enabled) {
  1387. return false;
  1388. }
  1389. var dir;
  1390. switch (ev.keyCode) {
  1391. case 37: // left
  1392. case 40:
  1393. // down
  1394. dir = -1;
  1395. break;
  1396. case 39: // right
  1397. case 38:
  1398. // up
  1399. dir = 1;
  1400. break;
  1401. }
  1402. if (!dir) {
  1403. return;
  1404. }
  1405. // use natural arrow keys instead of from min to max
  1406. if (this.options.natural_arrow_keys) {
  1407. var isHorizontal = this.options.orientation === 'horizontal';
  1408. var isVertical = this.options.orientation === 'vertical';
  1409. var isRTL = this.options.rtl;
  1410. var isReversed = this.options.reversed;
  1411. if (isHorizontal) {
  1412. if (isRTL) {
  1413. if (!isReversed) {
  1414. dir = -dir;
  1415. }
  1416. } else {
  1417. if (isReversed) {
  1418. dir = -dir;
  1419. }
  1420. }
  1421. } else if (isVertical) {
  1422. if (!isReversed) {
  1423. dir = -dir;
  1424. }
  1425. }
  1426. }
  1427. var val;
  1428. if (this.ticksAreValid && this.options.lock_to_ticks) {
  1429. var index = void 0;
  1430. // Find tick index that handle 1/2 is currently on
  1431. index = this.options.ticks.indexOf(this._state.value[handleIdx]);
  1432. if (index === -1) {
  1433. // Set default to first tick
  1434. index = 0;
  1435. window.console.warn('(lock_to_ticks) _keydown: index should not be -1');
  1436. }
  1437. index += dir;
  1438. index = Math.max(0, Math.min(this.options.ticks.length - 1, index));
  1439. val = this.options.ticks[index];
  1440. } else {
  1441. val = this._state.value[handleIdx] + dir * this.options.step;
  1442. }
  1443. var percentage = this._toPercentage(val);
  1444. this._state.keyCtrl = handleIdx;
  1445. if (this.options.range) {
  1446. this._adjustPercentageForRangeSliders(percentage);
  1447. var val1 = !this._state.keyCtrl ? val : this._state.value[0];
  1448. var val2 = this._state.keyCtrl ? val : this._state.value[1];
  1449. // Restrict values within limits
  1450. val = [Math.max(this.options.min, Math.min(this.options.max, val1)), Math.max(this.options.min, Math.min(this.options.max, val2))];
  1451. } else {
  1452. val = Math.max(this.options.min, Math.min(this.options.max, val));
  1453. }
  1454. this._trigger('slideStart', val);
  1455. this.setValue(val, true, true);
  1456. this._trigger('slideStop', val);
  1457. this._pauseEvent(ev);
  1458. delete this._state.keyCtrl;
  1459. return false;
  1460. },
  1461. _pauseEvent: function _pauseEvent(ev) {
  1462. if (ev.stopPropagation) {
  1463. ev.stopPropagation();
  1464. }
  1465. if (ev.preventDefault) {
  1466. ev.preventDefault();
  1467. }
  1468. ev.cancelBubble = true;
  1469. ev.returnValue = false;
  1470. },
  1471. _mousemove: function _mousemove(ev) {
  1472. if (!this._state.enabled) {
  1473. return false;
  1474. }
  1475. var percentage = this._getPercentage(ev);
  1476. this._adjustPercentageForRangeSliders(percentage);
  1477. this._state.percentage[this._state.dragged] = percentage;
  1478. var val = this._calculateValue(true);
  1479. this.setValue(val, true, true);
  1480. return false;
  1481. },
  1482. _touchmove: function _touchmove(ev) {
  1483. if (ev.changedTouches === undefined) {
  1484. return;
  1485. }
  1486. // Prevent page from scrolling and only drag the slider
  1487. if (ev.preventDefault) {
  1488. ev.preventDefault();
  1489. }
  1490. },
  1491. _adjustPercentageForRangeSliders: function _adjustPercentageForRangeSliders(percentage) {
  1492. if (this.options.range) {
  1493. var precision = this._getNumDigitsAfterDecimalPlace(percentage);
  1494. precision = precision ? precision - 1 : 0;
  1495. var percentageWithAdjustedPrecision = this._applyToFixedAndParseFloat(percentage, precision);
  1496. if (this._state.dragged === 0 && this._applyToFixedAndParseFloat(this._state.percentage[1], precision) < percentageWithAdjustedPrecision) {
  1497. this._state.percentage[0] = this._state.percentage[1];
  1498. this._state.dragged = 1;
  1499. } else if (this._state.dragged === 1 && this._applyToFixedAndParseFloat(this._state.percentage[0], precision) > percentageWithAdjustedPrecision) {
  1500. this._state.percentage[1] = this._state.percentage[0];
  1501. this._state.dragged = 0;
  1502. } else if (this._state.keyCtrl === 0 && this._toPercentage(this._state.value[1]) < percentage) {
  1503. this._state.percentage[0] = this._state.percentage[1];
  1504. this._state.keyCtrl = 1;
  1505. this.handle2.focus();
  1506. } else if (this._state.keyCtrl === 1 && this._toPercentage(this._state.value[0]) > percentage) {
  1507. this._state.percentage[1] = this._state.percentage[0];
  1508. this._state.keyCtrl = 0;
  1509. this.handle1.focus();
  1510. }
  1511. }
  1512. },
  1513. _mouseup: function _mouseup(ev) {
  1514. if (!this._state.enabled) {
  1515. return false;
  1516. }
  1517. var percentage = this._getPercentage(ev);
  1518. this._adjustPercentageForRangeSliders(percentage);
  1519. this._state.percentage[this._state.dragged] = percentage;
  1520. if (this.touchCapable) {
  1521. // Touch: Unbind touch event handlers:
  1522. document.removeEventListener("touchmove", this.mousemove, false);
  1523. document.removeEventListener("touchend", this.mouseup, false);
  1524. }
  1525. // Unbind mouse event handlers:
  1526. document.removeEventListener("mousemove", this.mousemove, false);
  1527. document.removeEventListener("mouseup", this.mouseup, false);
  1528. this._state.inDrag = false;
  1529. if (this._state.over === false) {
  1530. this._hideTooltip();
  1531. }
  1532. var val = this._calculateValue(true);
  1533. this.setValue(val, false, true);
  1534. this._trigger('slideStop', val);
  1535. // No longer need 'dragged' after mouse up
  1536. this._state.dragged = null;
  1537. return false;
  1538. },
  1539. _setValues: function _setValues(index, val) {
  1540. var comp = 0 === index ? 0 : 100;
  1541. if (this._state.percentage[index] !== comp) {
  1542. val.data[index] = this._toValue(this._state.percentage[index]);
  1543. val.data[index] = this._applyPrecision(val.data[index]);
  1544. }
  1545. },
  1546. _calculateValue: function _calculateValue(snapToClosestTick) {
  1547. var val = {};
  1548. if (this.options.range) {
  1549. val.data = [this.options.min, this.options.max];
  1550. this._setValues(0, val);
  1551. this._setValues(1, val);
  1552. if (snapToClosestTick) {
  1553. val.data[0] = this._snapToClosestTick(val.data[0]);
  1554. val.data[1] = this._snapToClosestTick(val.data[1]);
  1555. }
  1556. } else {
  1557. val.data = this._toValue(this._state.percentage[0]);
  1558. val.data = parseFloat(val.data);
  1559. val.data = this._applyPrecision(val.data);
  1560. if (snapToClosestTick) {
  1561. val.data = this._snapToClosestTick(val.data);
  1562. }
  1563. }
  1564. return val.data;
  1565. },
  1566. _snapToClosestTick: function _snapToClosestTick(val) {
  1567. var min = [val, Infinity];
  1568. for (var i = 0; i < this.options.ticks.length; i++) {
  1569. var diff = Math.abs(this.options.ticks[i] - val);
  1570. if (diff <= min[1]) {
  1571. min = [this.options.ticks[i], diff];
  1572. }
  1573. }
  1574. if (min[1] <= this.options.ticks_snap_bounds) {
  1575. return min[0];
  1576. }
  1577. return val;
  1578. },
  1579. _applyPrecision: function _applyPrecision(val) {
  1580. var precision = this.options.precision || this._getNumDigitsAfterDecimalPlace(this.options.step);
  1581. return this._applyToFixedAndParseFloat(val, precision);
  1582. },
  1583. _getNumDigitsAfterDecimalPlace: function _getNumDigitsAfterDecimalPlace(num) {
  1584. var match = ('' + num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
  1585. if (!match) {
  1586. return 0;
  1587. }
  1588. return Math.max(0, (match[1] ? match[1].length : 0) - (match[2] ? +match[2] : 0));
  1589. },
  1590. _applyToFixedAndParseFloat: function _applyToFixedAndParseFloat(num, toFixedInput) {
  1591. var truncatedNum = num.toFixed(toFixedInput);
  1592. return parseFloat(truncatedNum);
  1593. },
  1594. /*
  1595. Credits to Mike Samuel for the following method!
  1596. Source: http://stackoverflow.com/questions/10454518/javascript-how-to-retrieve-the-number-of-decimals-of-a-string-number
  1597. */
  1598. _getPercentage: function _getPercentage(ev) {
  1599. if (this.touchCapable && (ev.type === 'touchstart' || ev.type === 'touchmove' || ev.type === 'touchend')) {
  1600. ev = ev.changedTouches[0];
  1601. }
  1602. var eventPosition = ev[this.mousePos];
  1603. var sliderOffset = this._state.offset[this.stylePos];
  1604. var distanceToSlide = eventPosition - sliderOffset;
  1605. if (this.stylePos === 'right') {
  1606. distanceToSlide = -distanceToSlide;
  1607. }
  1608. // Calculate what percent of the length the slider handle has slid
  1609. var percentage = distanceToSlide / this._state.size * 100;
  1610. percentage = Math.round(percentage / this._state.percentage[2]) * this._state.percentage[2];
  1611. if (this.options.reversed) {
  1612. percentage = 100 - percentage;
  1613. }
  1614. // Make sure the percent is within the bounds of the slider.
  1615. // 0% corresponds to the 'min' value of the slide
  1616. // 100% corresponds to the 'max' value of the slide
  1617. return Math.max(0, Math.min(100, percentage));
  1618. },
  1619. _validateInputValue: function _validateInputValue(val) {
  1620. if (!isNaN(+val)) {
  1621. return +val;
  1622. } else if (Array.isArray(val)) {
  1623. this._validateArray(val);
  1624. return val;
  1625. } else {
  1626. throw new Error(ErrorMsgs.formatInvalidInputErrorMsg(val));
  1627. }
  1628. },
  1629. _validateArray: function _validateArray(val) {
  1630. for (var i = 0; i < val.length; i++) {
  1631. var input = val[i];
  1632. if (typeof input !== 'number') {
  1633. throw new Error(ErrorMsgs.formatInvalidInputErrorMsg(input));
  1634. }
  1635. }
  1636. },
  1637. _setDataVal: function _setDataVal(val) {
  1638. this.element.setAttribute('data-value', val);
  1639. this.element.setAttribute('value', val);
  1640. this.element.value = val;
  1641. },
  1642. _trigger: function _trigger(evt, val) {
  1643. val = val || val === 0 ? val : undefined;
  1644. var callbackFnArray = this.eventToCallbackMap[evt];
  1645. if (callbackFnArray && callbackFnArray.length) {
  1646. for (var i = 0; i < callbackFnArray.length; i++) {
  1647. var callbackFn = callbackFnArray[i];
  1648. callbackFn(val);
  1649. }
  1650. }
  1651. /* If JQuery exists, trigger JQuery events */
  1652. if ($) {
  1653. this._triggerJQueryEvent(evt, val);
  1654. }
  1655. },
  1656. _triggerJQueryEvent: function _triggerJQueryEvent(evt, val) {
  1657. var eventData = {
  1658. type: evt,
  1659. value: val
  1660. };
  1661. this.$element.trigger(eventData);
  1662. this.$sliderElem.trigger(eventData);
  1663. },
  1664. _unbindJQueryEventHandlers: function _unbindJQueryEventHandlers() {
  1665. this.$element.off();
  1666. this.$sliderElem.off();
  1667. },
  1668. _setText: function _setText(element, text) {
  1669. if (typeof element.textContent !== "undefined") {
  1670. element.textContent = text;
  1671. } else if (typeof element.innerText !== "undefined") {
  1672. element.innerText = text;
  1673. }
  1674. },
  1675. _removeClass: function _removeClass(element, classString) {
  1676. var classes = classString.split(" ");
  1677. var newClasses = element.className;
  1678. for (var i = 0; i < classes.length; i++) {
  1679. var classTag = classes[i];
  1680. var regex = new RegExp("(?:\\s|^)" + classTag + "(?:\\s|$)");
  1681. newClasses = newClasses.replace(regex, " ");
  1682. }
  1683. element.className = newClasses.trim();
  1684. },
  1685. _addClass: function _addClass(element, classString) {
  1686. var classes = classString.split(" ");
  1687. var newClasses = element.className;
  1688. for (var i = 0; i < classes.length; i++) {
  1689. var classTag = classes[i];
  1690. var regex = new RegExp("(?:\\s|^)" + classTag + "(?:\\s|$)");
  1691. var ifClassExists = regex.test(newClasses);
  1692. if (!ifClassExists) {
  1693. newClasses += " " + classTag;
  1694. }
  1695. }
  1696. element.className = newClasses.trim();
  1697. },
  1698. _offsetLeft: function _offsetLeft(obj) {
  1699. return obj.getBoundingClientRect().left;
  1700. },
  1701. _offsetRight: function _offsetRight(obj) {
  1702. return obj.getBoundingClientRect().right;
  1703. },
  1704. _offsetTop: function _offsetTop(obj) {
  1705. var offsetTop = obj.offsetTop;
  1706. while ((obj = obj.offsetParent) && !isNaN(obj.offsetTop)) {
  1707. offsetTop += obj.offsetTop;
  1708. if (obj.tagName !== 'BODY') {
  1709. offsetTop -= obj.scrollTop;
  1710. }
  1711. }
  1712. return offsetTop;
  1713. },
  1714. _offset: function _offset(obj) {
  1715. return {
  1716. left: this._offsetLeft(obj),
  1717. right: this._offsetRight(obj),
  1718. top: this._offsetTop(obj)
  1719. };
  1720. },
  1721. _css: function _css(elementRef, styleName, value) {
  1722. if ($) {
  1723. $.style(elementRef, styleName, value);
  1724. } else {
  1725. var style = styleName.replace(/^-ms-/, "ms-").replace(/-([\da-z])/gi, function (all, letter) {
  1726. return letter.toUpperCase();
  1727. });
  1728. elementRef.style[style] = value;
  1729. }
  1730. },
  1731. _toValue: function _toValue(percentage) {
  1732. return this.options.scale.toValue.apply(this, [percentage]);
  1733. },
  1734. _toPercentage: function _toPercentage(value) {
  1735. return this.options.scale.toPercentage.apply(this, [value]);
  1736. },
  1737. _setTooltipPosition: function _setTooltipPosition() {
  1738. var tooltips = [this.tooltip, this.tooltip_min, this.tooltip_max];
  1739. if (this.options.orientation === 'vertical') {
  1740. var tooltipPos;
  1741. if (this.options.tooltip_position) {
  1742. tooltipPos = this.options.tooltip_position;
  1743. } else {
  1744. if (this.options.rtl) {
  1745. tooltipPos = 'left';
  1746. } else {
  1747. tooltipPos = 'right';
  1748. }
  1749. }
  1750. var oppositeSide = tooltipPos === 'left' ? 'right' : 'left';
  1751. tooltips.forEach(function (tooltip) {
  1752. this._addClass(tooltip, 'bs-tooltip-' + tooltipPos);
  1753. tooltip.style[oppositeSide] = '100%';
  1754. }.bind(this));
  1755. } else if (this.options.tooltip_position === 'bottom') {
  1756. tooltips.forEach(function (tooltip) {
  1757. this._addClass(tooltip, 'bs-tooltip-bottom');
  1758. tooltip.style.top = 22 + 'px';
  1759. }.bind(this));
  1760. } else {
  1761. tooltips.forEach(function (tooltip) {
  1762. this._addClass(tooltip, 'bs-tooltip-top');
  1763. tooltip.style.top = -this.tooltip.outerHeight - 14 + 'px';
  1764. }.bind(this));
  1765. }
  1766. },
  1767. _getClosestTickIndex: function _getClosestTickIndex(val) {
  1768. var difference = Math.abs(val - this.options.ticks[0]);
  1769. var index = 0;
  1770. for (var i = 0; i < this.options.ticks.length; ++i) {
  1771. var d = Math.abs(val - this.options.ticks[i]);
  1772. if (d < difference) {
  1773. difference = d;
  1774. index = i;
  1775. }
  1776. }
  1777. return index;
  1778. },
  1779. /**
  1780. * Attempts to find the index in `ticks[]` the slider values are set at.
  1781. * The indexes can be -1 to indicate the slider value is not set at a value in `ticks[]`.
  1782. */
  1783. _setTickIndex: function _setTickIndex() {
  1784. if (this.ticksAreValid) {
  1785. this._state.tickIndex = [this.options.ticks.indexOf(this._state.value[0]), this.options.ticks.indexOf(this._state.value[1])];
  1786. }
  1787. }
  1788. };
  1789. /*********************************
  1790. Attach to global namespace
  1791. *********************************/
  1792. if ($ && $.fn) {
  1793. if (!$.fn.slider) {
  1794. $.bridget(NAMESPACE_MAIN, Slider);
  1795. autoRegisterNamespace = NAMESPACE_MAIN;
  1796. } else {
  1797. if (windowIsDefined) {
  1798. window.console.warn("bootstrap-slider.js - WARNING: $.fn.slider namespace is already bound. Use the $.fn.bootstrapSlider namespace instead.");
  1799. }
  1800. autoRegisterNamespace = NAMESPACE_ALTERNATE;
  1801. }
  1802. $.bridget(NAMESPACE_ALTERNATE, Slider);
  1803. // Auto-Register data-provide="slider" Elements
  1804. $(function () {
  1805. $("input[data-provide=slider]")[autoRegisterNamespace]();
  1806. });
  1807. }
  1808. })($);
  1809. return Slider;
  1810. });