bootstrap-slider.js 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  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. });