bootstrap-slider.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /* =========================================================
  2. * bootstrap-slider.js v3.0.0
  3. * =========================================================
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. * ========================================================= */
  17. (function( $ ) {
  18. var ErrorMsgs = {
  19. formatInvalidInputErrorMsg : function(input) {
  20. return "Invalid input value '" + input + "' passed in";
  21. },
  22. 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"
  23. };
  24. var Slider = function(element, options) {
  25. var el = this.element = $(element).hide();
  26. var origWidth = $(element)[0].style.width;
  27. var updateSlider = false;
  28. var parent = this.element.parent();
  29. if (parent.hasClass('slider') === true) {
  30. updateSlider = true;
  31. this.picker = parent;
  32. } else {
  33. this.picker = $('<div class="slider">'+
  34. '<div class="slider-track">'+
  35. '<div class="slider-selection"></div>'+
  36. '<div class="slider-handle"></div>'+
  37. '<div class="slider-handle"></div>'+
  38. '</div>'+
  39. '<div id="tooltip" class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'+
  40. '<div id="tooltip_min" class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'+
  41. '<div id="tooltip_max" class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'+
  42. '</div>')
  43. .insertBefore(this.element)
  44. .append(this.element);
  45. }
  46. this.id = this.element.data('slider-id')||options.id;
  47. if (this.id) {
  48. this.picker[0].id = this.id;
  49. }
  50. if (('ontouchstart' in window) || window.DocumentTouch && document instanceof window.DocumentTouch) {
  51. this.touchCapable = true;
  52. }
  53. var tooltip = this.element.data('slider-tooltip')||options.tooltip;
  54. this.tooltip = this.picker.find('#tooltip');
  55. this.tooltipInner = this.tooltip.find('div.tooltip-inner');
  56. this.tooltip_min = this.picker.find('#tooltip_min');
  57. this.tooltipInner_min = this.tooltip_min.find('div.tooltip-inner');
  58. this.tooltip_max = this.picker.find('#tooltip_max');
  59. this.tooltipInner_max= this.tooltip_max.find('div.tooltip-inner');
  60. if (updateSlider === true) {
  61. // Reset classes
  62. this.picker.removeClass('slider-horizontal');
  63. this.picker.removeClass('slider-vertical');
  64. this.tooltip.removeClass('hide');
  65. this.tooltip_min.removeClass('hide');
  66. this.tooltip_max.removeClass('hide');
  67. }
  68. this.orientation = this.element.data('slider-orientation')||options.orientation;
  69. switch(this.orientation) {
  70. case 'vertical':
  71. this.picker.addClass('slider-vertical');
  72. this.stylePos = 'top';
  73. this.mousePos = 'pageY';
  74. this.sizePos = 'offsetHeight';
  75. this.tooltip.addClass('right')[0].style.left = '100%';
  76. this.tooltip_min.addClass('right')[0].style.left = '100%';
  77. this.tooltip_max.addClass('right')[0].style.left = '100%';
  78. break;
  79. default:
  80. this.picker
  81. .addClass('slider-horizontal')
  82. .css('width', origWidth);
  83. this.orientation = 'horizontal';
  84. this.stylePos = 'left';
  85. this.mousePos = 'pageX';
  86. this.sizePos = 'offsetWidth';
  87. this.tooltip.addClass('top')[0].style.top = -this.tooltip.outerHeight() - 14 + 'px';
  88. this.tooltip_min.addClass('top')[0].style.top = -this.tooltip_min.outerHeight() - 14 + 'px';
  89. this.tooltip_max.addClass('top')[0].style.top = -this.tooltip_max.outerHeight() - 14 + 'px';
  90. break;
  91. }
  92. var self = this;
  93. $.each(['min',
  94. 'max',
  95. 'step',
  96. 'precision',
  97. 'value',
  98. 'reversed',
  99. 'handle'
  100. ], function(i, attr) {
  101. if (typeof el.data('slider-' + attr) !== 'undefined') {
  102. self[attr] = el.data('slider-' + attr);
  103. } else if (typeof options[attr] !== 'undefined') {
  104. self[attr] = options[attr];
  105. } else if (typeof el.prop(attr) !== 'undefined') {
  106. self[attr] = el.prop(attr);
  107. } else {
  108. self[attr] = 0; // to prevent empty string issues in calculations in IE
  109. }
  110. });
  111. if (this.value instanceof Array) {
  112. if (updateSlider && !this.range) {
  113. this.value = this.value[0];
  114. } else {
  115. this.range = true;
  116. }
  117. } else if (this.range) {
  118. // User wants a range, but value is not an array
  119. this.value = [this.value, this.max];
  120. }
  121. this.selection = this.element.data('slider-selection')||options.selection;
  122. this.selectionEl = this.picker.find('.slider-selection');
  123. if (this.selection === 'none') {
  124. this.selectionEl.addClass('hide');
  125. }
  126. this.selectionElStyle = this.selectionEl[0].style;
  127. this.handle1 = this.picker.find('.slider-handle:first');
  128. this.handle1Stype = this.handle1[0].style;
  129. this.handle2 = this.picker.find('.slider-handle:last');
  130. this.handle2Stype = this.handle2[0].style;
  131. if (updateSlider === true) {
  132. // Reset classes
  133. this.handle1.removeClass('round triangle');
  134. this.handle2.removeClass('round triangle hide');
  135. }
  136. switch(this.handle) {
  137. case 'round':
  138. this.handle1.addClass('round');
  139. this.handle2.addClass('round');
  140. break;
  141. case 'triangle':
  142. this.handle1.addClass('triangle');
  143. this.handle2.addClass('triangle');
  144. break;
  145. }
  146. this.offset = this.picker.offset();
  147. this.size = this.picker[0][this.sizePos];
  148. this.formater = options.formater;
  149. this.tooltip_separator = options.tooltip_separator;
  150. this.tooltip_split = options.tooltip_split;
  151. this.setValue(this.value);
  152. this.handle1.on({
  153. keydown: $.proxy(this.keydown, this, 0)
  154. });
  155. this.handle2.on({
  156. keydown: $.proxy(this.keydown, this, 1)
  157. });
  158. if (this.touchCapable) {
  159. // Touch: Bind touch events:
  160. this.picker.on({
  161. touchstart: $.proxy(this.mousedown, this)
  162. });
  163. }
  164. // Bind mouse events:
  165. this.picker.on({
  166. mousedown: $.proxy(this.mousedown, this)
  167. });
  168. if(tooltip === 'hide') {
  169. this.tooltip.addClass('hide');
  170. this.tooltip_min.addClass('hide');
  171. this.tooltip_max.addClass('hide');
  172. } else if(tooltip === 'always') {
  173. this.showTooltip();
  174. this.alwaysShowTooltip = true;
  175. } else {
  176. this.picker.on({
  177. mouseenter: $.proxy(this.showTooltip, this),
  178. mouseleave: $.proxy(this.hideTooltip, this)
  179. });
  180. this.handle1.on({
  181. focus: $.proxy(this.showTooltip, this),
  182. blur: $.proxy(this.hideTooltip, this)
  183. });
  184. this.handle2.on({
  185. focus: $.proxy(this.showTooltip, this),
  186. blur: $.proxy(this.hideTooltip, this)
  187. });
  188. }
  189. this.enabled = options.enabled &&
  190. (this.element.data('slider-enabled') === undefined || this.element.data('slider-enabled') === true);
  191. if(this.enabled) {
  192. this.enable();
  193. } else {
  194. this.disable();
  195. }
  196. };
  197. Slider.prototype = {
  198. constructor: Slider,
  199. over: false,
  200. inDrag: false,
  201. showTooltip: function(){
  202. if (this.tooltip_split === false ){
  203. this.tooltip.addClass('in');
  204. } else {
  205. this.tooltip_min.addClass('in');
  206. this.tooltip_max.addClass('in');
  207. }
  208. this.over = true;
  209. },
  210. hideTooltip: function(){
  211. if (this.inDrag === false && this.alwaysShowTooltip !== true) {
  212. this.tooltip.removeClass('in');
  213. this.tooltip_min.removeClass('in');
  214. this.tooltip_max.removeClass('in');
  215. }
  216. this.over = false;
  217. },
  218. layout: function(){
  219. var positionPercentages;
  220. if(this.reversed) {
  221. positionPercentages = [ 100 - this.percentage[0], this.percentage[1] ];
  222. } else {
  223. positionPercentages = [ this.percentage[0], this.percentage[1] ];
  224. }
  225. this.handle1Stype[this.stylePos] = positionPercentages[0]+'%';
  226. this.handle2Stype[this.stylePos] = positionPercentages[1]+'%';
  227. if (this.orientation === 'vertical') {
  228. this.selectionElStyle.top = Math.min(positionPercentages[0], positionPercentages[1]) +'%';
  229. this.selectionElStyle.height = Math.abs(positionPercentages[0] - positionPercentages[1]) +'%';
  230. } else {
  231. this.selectionElStyle.left = Math.min(positionPercentages[0], positionPercentages[1]) +'%';
  232. this.selectionElStyle.width = Math.abs(positionPercentages[0] - positionPercentages[1]) +'%';
  233. var offset_min = this.tooltip_min[0].getBoundingClientRect();
  234. var offset_max = this.tooltip_max[0].getBoundingClientRect();
  235. if (offset_min.right > offset_max.left) {
  236. this.tooltip_max.removeClass('top');
  237. this.tooltip_max.addClass('bottom')[0].style.top = 18 + 'px';
  238. } else {
  239. this.tooltip_max.removeClass('bottom');
  240. this.tooltip_max.addClass('top')[0].style.top = -30 + 'px';
  241. }
  242. }
  243. if (this.range) {
  244. this.tooltipInner.text(
  245. this.formater(this.value[0]) + this.tooltip_separator + this.formater(this.value[1])
  246. );
  247. this.tooltip[0].style[this.stylePos] = this.size * (positionPercentages[0] + (positionPercentages[1] - positionPercentages[0])/2)/100 - (this.orientation === 'vertical' ? this.tooltip.outerHeight()/2 : this.tooltip.outerWidth()/2) +'px';
  248. this.tooltipInner_min.text(
  249. this.formater(this.value[0])
  250. );
  251. this.tooltipInner_max.text(
  252. this.formater(this.value[1])
  253. );
  254. this.tooltip_min[0].style[this.stylePos] = this.size * ( (positionPercentages[0])/100) - (this.orientation === 'vertical' ? this.tooltip_min.outerHeight()/2 : this.tooltip_min.outerWidth()/2) +'px';
  255. this.tooltip_max[0].style[this.stylePos] = this.size * ( (positionPercentages[1])/100) - (this.orientation === 'vertical' ? this.tooltip_max.outerHeight()/2 : this.tooltip_max.outerWidth()/2) +'px';
  256. } else {
  257. this.tooltipInner.text(
  258. this.formater(this.value[0])
  259. );
  260. this.tooltip[0].style[this.stylePos] = this.size * positionPercentages[0]/100 - (this.orientation === 'vertical' ? this.tooltip.outerHeight()/2 : this.tooltip.outerWidth()/2) +'px';
  261. }
  262. },
  263. mousedown: function(ev) {
  264. if(!this.isEnabled()) {
  265. return false;
  266. }
  267. // Touch: Get the original event:
  268. if (this.touchCapable && ev.type === 'touchstart') {
  269. ev = ev.originalEvent;
  270. }
  271. this.triggerFocusOnHandle();
  272. this.offset = this.picker.offset();
  273. this.size = this.picker[0][this.sizePos];
  274. var percentage = this.getPercentage(ev);
  275. if (this.range) {
  276. var diff1 = Math.abs(this.percentage[0] - percentage);
  277. var diff2 = Math.abs(this.percentage[1] - percentage);
  278. this.dragged = (diff1 < diff2) ? 0 : 1;
  279. } else {
  280. this.dragged = 0;
  281. }
  282. this.percentage[this.dragged] = this.reversed ? 100 - percentage : percentage;
  283. this.layout();
  284. if (this.touchCapable) {
  285. // Touch: Bind touch events:
  286. $(document).on({
  287. touchmove: $.proxy(this.mousemove, this),
  288. touchend: $.proxy(this.mouseup, this)
  289. });
  290. }
  291. // Bind mouse events:
  292. $(document).on({
  293. mousemove: $.proxy(this.mousemove, this),
  294. mouseup: $.proxy(this.mouseup, this)
  295. });
  296. this.inDrag = true;
  297. var val = this.calculateValue();
  298. this.element.trigger({
  299. type: 'slideStart',
  300. value: val
  301. })
  302. .data('value', val)
  303. .prop('value', val);
  304. this.setValue(val);
  305. return true;
  306. },
  307. triggerFocusOnHandle: function(handleIdx) {
  308. if(handleIdx === 0) {
  309. this.handle1.focus();
  310. }
  311. if(handleIdx === 1) {
  312. this.handle2.focus();
  313. }
  314. },
  315. keydown: function(handleIdx, ev) {
  316. if(!this.isEnabled()) {
  317. return false;
  318. }
  319. var dir;
  320. switch (ev.which) {
  321. case 37: // left
  322. case 40: // down
  323. dir = -1;
  324. break;
  325. case 39: // right
  326. case 38: // up
  327. dir = 1;
  328. break;
  329. }
  330. if (!dir) {
  331. return;
  332. }
  333. var oneStepValuePercentageChange = dir * this.percentage[2];
  334. var percentage = this.percentage[handleIdx] + oneStepValuePercentageChange;
  335. if (percentage > 100) {
  336. percentage = 100;
  337. } else if (percentage < 0) {
  338. percentage = 0;
  339. }
  340. this.dragged = handleIdx;
  341. this.adjustPercentageForRangeSliders(percentage);
  342. this.percentage[this.dragged] = percentage;
  343. this.layout();
  344. var val = this.calculateValue();
  345. this.element.trigger({
  346. type: 'slideStart',
  347. value: val
  348. })
  349. .data('value', val)
  350. .prop('value', val);
  351. this.slide(val);
  352. this.element
  353. .trigger({
  354. type: 'slideStop',
  355. value: val
  356. })
  357. .data('value', val)
  358. .prop('value', val);
  359. return false;
  360. },
  361. mousemove: function(ev) {
  362. if(!this.isEnabled()) {
  363. return false;
  364. }
  365. // Touch: Get the original event:
  366. if (this.touchCapable && ev.type === 'touchmove') {
  367. ev = ev.originalEvent;
  368. }
  369. var percentage = this.getPercentage(ev);
  370. this.adjustPercentageForRangeSliders(percentage);
  371. this.percentage[this.dragged] = this.reversed ? 100 - percentage : percentage;
  372. this.layout();
  373. var val = this.calculateValue();
  374. this.slide(val);
  375. return false;
  376. },
  377. slide: function(val) {
  378. this.setValue(val);
  379. var slideEventValue = this.range ? this.value : this.value[0];
  380. this.element
  381. .trigger({
  382. 'type': 'slide',
  383. 'value': slideEventValue
  384. })
  385. .data('value', this.value)
  386. .prop('value', this.value);
  387. },
  388. adjustPercentageForRangeSliders: function(percentage) {
  389. if (this.range) {
  390. if (this.dragged === 0 && this.percentage[1] < percentage) {
  391. this.percentage[0] = this.percentage[1];
  392. this.dragged = 1;
  393. } else if (this.dragged === 1 && this.percentage[0] > percentage) {
  394. this.percentage[1] = this.percentage[0];
  395. this.dragged = 0;
  396. }
  397. }
  398. },
  399. mouseup: function() {
  400. if(!this.isEnabled()) {
  401. return false;
  402. }
  403. if (this.touchCapable) {
  404. // Touch: Unbind touch event handlers:
  405. $(document).off({
  406. touchmove: this.mousemove,
  407. touchend: this.mouseup
  408. });
  409. }
  410. // Unbind mouse event handlers:
  411. $(document).off({
  412. mousemove: this.mousemove,
  413. mouseup: this.mouseup
  414. });
  415. this.inDrag = false;
  416. if (this.over === false) {
  417. this.hideTooltip();
  418. }
  419. var val = this.calculateValue();
  420. this.layout();
  421. this.element
  422. .data('value', val)
  423. .prop('value', val)
  424. .trigger({
  425. type: 'slideStop',
  426. value: val
  427. });
  428. return false;
  429. },
  430. calculateValue: function() {
  431. var val;
  432. if (this.range) {
  433. val = [this.min,this.max];
  434. if (this.percentage[0] !== 0){
  435. val[0] = (Math.max(this.min, this.min + Math.round((this.diff * this.percentage[0]/100)/this.step)*this.step));
  436. val[0] = this.applyPrecision(val[0]);
  437. }
  438. if (this.percentage[1] !== 100){
  439. val[1] = (Math.min(this.max, this.min + Math.round((this.diff * this.percentage[1]/100)/this.step)*this.step));
  440. val[1] = this.applyPrecision(val[1]);
  441. }
  442. this.value = val;
  443. } else {
  444. val = (this.min + Math.round((this.diff * this.percentage[0]/100)/this.step)*this.step);
  445. if (val < this.min) {
  446. val = this.min;
  447. }
  448. else if (val > this.max) {
  449. val = this.max;
  450. }
  451. val = parseFloat(val);
  452. val = this.applyPrecision(val);
  453. this.value = [val, this.value[1]];
  454. }
  455. return val;
  456. },
  457. applyPrecision: function(val) {
  458. var precision = this.precision || this.getNumDigitsAfterDecimalPlace(this.step);
  459. return this.applyToFixedAndParseFloat(val, precision);
  460. },
  461. /*
  462. Credits to Mike Samuel for the following method!
  463. Source: http://stackoverflow.com/questions/10454518/javascript-how-to-retrieve-the-number-of-decimals-of-a-string-number
  464. */
  465. getNumDigitsAfterDecimalPlace: function(num) {
  466. var match = (''+num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
  467. if (!match) { return 0; }
  468. return Math.max(0, (match[1] ? match[1].length : 0) - (match[2] ? +match[2] : 0));
  469. },
  470. applyToFixedAndParseFloat: function(num, toFixedInput) {
  471. var truncatedNum = num.toFixed(toFixedInput);
  472. return parseFloat(truncatedNum);
  473. },
  474. getPercentage: function(ev) {
  475. if (this.touchCapable && (ev.type === 'touchstart' || ev.type === 'touchmove')) {
  476. ev = ev.touches[0];
  477. }
  478. var percentage = (ev[this.mousePos] - this.offset[this.stylePos])*100/this.size;
  479. percentage = Math.round(percentage/this.percentage[2])*this.percentage[2];
  480. return Math.max(0, Math.min(100, percentage));
  481. },
  482. getValue: function() {
  483. if (this.range) {
  484. return this.value;
  485. }
  486. return this.value[0];
  487. },
  488. setValue: function(val) {
  489. if (!val) {
  490. val = 0;
  491. }
  492. this.value = this.validateInputValue(val);
  493. if (this.range) {
  494. this.value[0] = this.applyPrecision(this.value[0]);
  495. this.value[1] = this.applyPrecision(this.value[1]);
  496. this.value[0] = Math.max(this.min, Math.min(this.max, this.value[0]));
  497. this.value[1] = Math.max(this.min, Math.min(this.max, this.value[1]));
  498. } else {
  499. this.value = this.applyPrecision(this.value);
  500. this.value = [ Math.max(this.min, Math.min(this.max, this.value))];
  501. this.handle2.addClass('hide');
  502. if (this.selection === 'after') {
  503. this.value[1] = this.max;
  504. } else {
  505. this.value[1] = this.min;
  506. }
  507. }
  508. this.diff = this.max - this.min;
  509. if (this.diff > 0) {
  510. this.percentage = [
  511. (this.value[0] - this.min) * 100 / this.diff,
  512. (this.value[1] - this.min) * 100 / this.diff,
  513. this.step * 100 / this.diff
  514. ];
  515. } else {
  516. this.percentage = [0, 0, 100];
  517. }
  518. this.layout();
  519. },
  520. validateInputValue : function(val) {
  521. if(typeof val === 'number') {
  522. return val;
  523. } else if(val instanceof Array) {
  524. $.each(val, function(i, input) { if (typeof input !== 'number') { throw new Error( ErrorMsgs.formatInvalidInputErrorMsg(input) ); }});
  525. return val;
  526. } else {
  527. throw new Error( ErrorMsgs.formatInvalidInputErrorMsg(val) );
  528. }
  529. },
  530. destroy: function(){
  531. this.handle1.off();
  532. this.handle2.off();
  533. this.element.off().show().insertBefore(this.picker);
  534. this.picker.off().remove();
  535. $(this.element).removeData('slider');
  536. },
  537. disable: function() {
  538. this.enabled = false;
  539. this.handle1.removeAttr("tabindex");
  540. this.handle2.removeAttr("tabindex");
  541. this.picker.addClass('slider-disabled');
  542. this.element.trigger('slideDisabled');
  543. },
  544. enable: function() {
  545. this.enabled = true;
  546. this.handle1.attr("tabindex", 0);
  547. this.handle2.attr("tabindex", 0);
  548. this.picker.removeClass('slider-disabled');
  549. this.element.trigger('slideEnabled');
  550. },
  551. toggle: function() {
  552. if(this.enabled) {
  553. this.disable();
  554. } else {
  555. this.enable();
  556. }
  557. },
  558. isEnabled: function() {
  559. return this.enabled;
  560. },
  561. setAttribute: function(attribute, value) {
  562. this[attribute] = value;
  563. },
  564. getAttribute: function(attribute) {
  565. return this[attribute];
  566. }
  567. };
  568. var publicMethods = {
  569. getValue : Slider.prototype.getValue,
  570. setValue : Slider.prototype.setValue,
  571. setAttribute : Slider.prototype.setAttribute,
  572. getAttribute : Slider.prototype.getAttribute,
  573. destroy : Slider.prototype.destroy,
  574. disable : Slider.prototype.disable,
  575. enable : Slider.prototype.enable,
  576. toggle : Slider.prototype.toggle,
  577. isEnabled: Slider.prototype.isEnabled
  578. };
  579. $.fn.slider = function (option) {
  580. if (typeof option === 'string' && option !== 'refresh') {
  581. var args = Array.prototype.slice.call(arguments, 1);
  582. return invokePublicMethod.call(this, option, args);
  583. } else {
  584. return createNewSliderInstance.call(this, option);
  585. }
  586. };
  587. function invokePublicMethod(methodName, args) {
  588. if(publicMethods[methodName]) {
  589. var sliderObject = retrieveSliderObjectFromElement(this);
  590. var result = publicMethods[methodName].apply(sliderObject, args);
  591. if (typeof result === "undefined") {
  592. return $(this);
  593. } else {
  594. return result;
  595. }
  596. } else {
  597. throw new Error("method '" + methodName + "()' does not exist for slider.");
  598. }
  599. }
  600. function retrieveSliderObjectFromElement(element) {
  601. var sliderObject = $(element).data('slider');
  602. if(sliderObject && sliderObject instanceof Slider) {
  603. return sliderObject;
  604. } else {
  605. throw new Error(ErrorMsgs.callingContextNotSliderInstance);
  606. }
  607. }
  608. function createNewSliderInstance(opts) {
  609. var $this = $(this);
  610. $this.each(function() {
  611. var $this = $(this),
  612. slider = $this.data('slider'),
  613. options = typeof opts === 'object' && opts;
  614. // If slider already exists, use its attributes
  615. // as options so slider refreshes properly
  616. if (slider && !options) {
  617. options = {};
  618. $.each($.fn.slider.defaults, function(key) {
  619. options[key] = slider[key];
  620. });
  621. }
  622. $this.data('slider', (new Slider(this, $.extend({}, $.fn.slider.defaults, options))));
  623. });
  624. return $this;
  625. }
  626. $.fn.slider.defaults = {
  627. min: 0,
  628. max: 10,
  629. step: 1,
  630. precision: 0,
  631. orientation: 'horizontal',
  632. value: 5,
  633. range: false,
  634. selection: 'before',
  635. tooltip: 'show',
  636. tooltip_separator: ':',
  637. tooltip_split: false,
  638. handle: 'round',
  639. reversed : false,
  640. enabled: true,
  641. formater: function(value) {
  642. return value;
  643. }
  644. };
  645. $.fn.slider.Constructor = Slider;
  646. })( window.jQuery );
  647. /* vim: set noexpandtab tabstop=4 shiftwidth=4 autoindent: */