選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

bootstrap-slider.js 93KB

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