bootstrap-slider.js 63 KB

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