slider.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*!
  2. * jQuery UI Slider @VERSION
  3. * http://jqueryui.com
  4. *
  5. * Copyright 2014 jQuery Foundation and other contributors
  6. * Released under the MIT license.
  7. * http://jquery.org/license
  8. *
  9. * http://api.jqueryui.com/slider/
  10. */
  11. (function( factory ) {
  12. if ( typeof define === "function" && define.amd ) {
  13. // AMD. Register as an anonymous module.
  14. define([
  15. "jquery",
  16. "./core",
  17. "./mouse",
  18. "./widget"
  19. ], factory );
  20. } else {
  21. // Browser globals
  22. factory( jQuery );
  23. }
  24. }(function( $ ) {
  25. return $.widget( "ui.slider", $.ui.mouse, {
  26. version: "@VERSION",
  27. widgetEventPrefix: "slide",
  28. options: {
  29. animate: false,
  30. distance: 0,
  31. max: 100,
  32. min: 0,
  33. orientation: "horizontal",
  34. range: false,
  35. step: 1,
  36. value: 0,
  37. values: null,
  38. // callbacks
  39. change: null,
  40. slide: null,
  41. start: null,
  42. stop: null
  43. },
  44. // number of pages in a slider
  45. // (how many times can you page up/down to go through the whole range)
  46. numPages: 5,
  47. _create: function() {
  48. this._keySliding = false;
  49. this._mouseSliding = false;
  50. this._animateOff = true;
  51. this._handleIndex = null;
  52. this._detectOrientation();
  53. this._mouseInit();
  54. this.element
  55. .addClass( "ui-slider" +
  56. " ui-slider-" + this.orientation +
  57. " ui-widget" +
  58. " ui-widget-content" +
  59. " ui-corner-all");
  60. this._refresh();
  61. this._setOption( "disabled", this.options.disabled );
  62. this._animateOff = false;
  63. },
  64. _refresh: function() {
  65. this._createRange();
  66. this._createHandles();
  67. this._setupEvents();
  68. this._refreshValue();
  69. },
  70. _createHandles: function() {
  71. var i, handleCount,
  72. options = this.options,
  73. existingHandles = this.element.find( ".ui-slider-handle" ).addClass( "ui-state-default ui-corner-all" ),
  74. handle = "<span class='ui-slider-handle ui-state-default ui-corner-all' tabindex='0'></span>",
  75. handles = [];
  76. handleCount = ( options.values && options.values.length ) || 1;
  77. if ( existingHandles.length > handleCount ) {
  78. existingHandles.slice( handleCount ).remove();
  79. existingHandles = existingHandles.slice( 0, handleCount );
  80. }
  81. for ( i = existingHandles.length; i < handleCount; i++ ) {
  82. handles.push( handle );
  83. }
  84. this.handles = existingHandles.add( $( handles.join( "" ) ).appendTo( this.element ) );
  85. this.handle = this.handles.eq( 0 );
  86. this.handles.each(function( i ) {
  87. $( this ).data( "ui-slider-handle-index", i );
  88. });
  89. },
  90. _createRange: function() {
  91. var options = this.options,
  92. classes = "";
  93. if ( options.range ) {
  94. if ( options.range === true ) {
  95. if ( !options.values ) {
  96. options.values = [ this._valueMin(), this._valueMin() ];
  97. } else if ( options.values.length && options.values.length !== 2 ) {
  98. options.values = [ options.values[0], options.values[0] ];
  99. } else if ( $.isArray( options.values ) ) {
  100. options.values = options.values.slice(0);
  101. }
  102. }
  103. if ( !this.range || !this.range.length ) {
  104. this.range = $( "<div></div>" )
  105. .appendTo( this.element );
  106. classes = "ui-slider-range" +
  107. // note: this isn't the most fittingly semantic framework class for this element,
  108. // but worked best visually with a variety of themes
  109. " ui-widget-header ui-corner-all";
  110. } else {
  111. this.range.removeClass( "ui-slider-range-min ui-slider-range-max" )
  112. // Handle range switching from true to min/max
  113. .css({
  114. "left": "",
  115. "bottom": ""
  116. });
  117. }
  118. this.range.addClass( classes +
  119. ( ( options.range === "min" || options.range === "max" ) ? " ui-slider-range-" + options.range : "" ) );
  120. } else {
  121. if ( this.range ) {
  122. this.range.remove();
  123. }
  124. this.range = null;
  125. }
  126. },
  127. _setupEvents: function() {
  128. this._off( this.handles );
  129. this._on( this.handles, this._handleEvents );
  130. this._hoverable( this.handles );
  131. this._focusable( this.handles );
  132. },
  133. _destroy: function() {
  134. this.handles.remove();
  135. if ( this.range ) {
  136. this.range.remove();
  137. }
  138. this.element
  139. .removeClass( "ui-slider" +
  140. " ui-slider-horizontal" +
  141. " ui-slider-vertical" +
  142. " ui-widget" +
  143. " ui-widget-content" +
  144. " ui-corner-all" );
  145. this._mouseDestroy();
  146. },
  147. _mouseCapture: function( event ) {
  148. var position, normValue, distance, closestHandle, index, allowed, offset, mouseOverHandle,
  149. that = this,
  150. o = this.options;
  151. if ( o.disabled ) {
  152. return false;
  153. }
  154. this.elementSize = {
  155. width: this.element.outerWidth(),
  156. height: this.element.outerHeight()
  157. };
  158. this.elementOffset = this.element.offset();
  159. position = { x: event.pageX, y: event.pageY };
  160. normValue = this._normValueFromMouse( position );
  161. distance = this._valueMax() - this._valueMin() + 1;
  162. this.handles.each(function( i ) {
  163. var thisDistance = Math.abs( normValue - that.values(i) );
  164. if (( distance > thisDistance ) ||
  165. ( distance === thisDistance &&
  166. (i === that._lastChangedValue || that.values(i) === o.min ))) {
  167. distance = thisDistance;
  168. closestHandle = $( this );
  169. index = i;
  170. }
  171. });
  172. allowed = this._start( event, index );
  173. if ( allowed === false ) {
  174. return false;
  175. }
  176. this._mouseSliding = true;
  177. this._handleIndex = index;
  178. closestHandle
  179. .addClass( "ui-state-active" )
  180. .focus();
  181. offset = closestHandle.offset();
  182. mouseOverHandle = !$( event.target ).parents().addBack().is( ".ui-slider-handle" );
  183. this._clickOffset = mouseOverHandle ? { left: 0, top: 0 } : {
  184. left: event.pageX - offset.left - ( closestHandle.width() / 2 ),
  185. top: event.pageY - offset.top -
  186. ( closestHandle.height() / 2 ) -
  187. ( parseInt( closestHandle.css("borderTopWidth"), 10 ) || 0 ) -
  188. ( parseInt( closestHandle.css("borderBottomWidth"), 10 ) || 0) +
  189. ( parseInt( closestHandle.css("marginTop"), 10 ) || 0)
  190. };
  191. if ( !this.handles.hasClass( "ui-state-hover" ) ) {
  192. this._slide( event, index, normValue );
  193. }
  194. this._animateOff = true;
  195. return true;
  196. },
  197. _mouseStart: function() {
  198. return true;
  199. },
  200. _mouseDrag: function( event ) {
  201. var position = { x: event.pageX, y: event.pageY },
  202. normValue = this._normValueFromMouse( position );
  203. this._slide( event, this._handleIndex, normValue );
  204. return false;
  205. },
  206. _mouseStop: function( event ) {
  207. this.handles.removeClass( "ui-state-active" );
  208. this._mouseSliding = false;
  209. this._stop( event, this._handleIndex );
  210. this._change( event, this._handleIndex );
  211. this._handleIndex = null;
  212. this._clickOffset = null;
  213. this._animateOff = false;
  214. return false;
  215. },
  216. _detectOrientation: function() {
  217. this.orientation = ( this.options.orientation === "vertical" ) ? "vertical" : "horizontal";
  218. },
  219. _normValueFromMouse: function( position ) {
  220. var pixelTotal,
  221. pixelMouse,
  222. percentMouse,
  223. valueTotal,
  224. valueMouse;
  225. if ( this.orientation === "horizontal" ) {
  226. pixelTotal = this.elementSize.width;
  227. pixelMouse = position.x - this.elementOffset.left - ( this._clickOffset ? this._clickOffset.left : 0 );
  228. } else {
  229. pixelTotal = this.elementSize.height;
  230. pixelMouse = position.y - this.elementOffset.top - ( this._clickOffset ? this._clickOffset.top : 0 );
  231. }
  232. percentMouse = ( pixelMouse / pixelTotal );
  233. if ( percentMouse > 1 ) {
  234. percentMouse = 1;
  235. }
  236. if ( percentMouse < 0 ) {
  237. percentMouse = 0;
  238. }
  239. if ( this.orientation === "vertical" ) {
  240. percentMouse = 1 - percentMouse;
  241. }
  242. valueTotal = this._valueMax() - this._valueMin();
  243. valueMouse = this._valueMin() + percentMouse * valueTotal;
  244. return this._trimAlignValue( valueMouse );
  245. },
  246. _start: function( event, index ) {
  247. var uiHash = {
  248. handle: this.handles[ index ],
  249. value: this.value()
  250. };
  251. if ( this.options.values && this.options.values.length ) {
  252. uiHash.value = this.values( index );
  253. uiHash.values = this.values();
  254. }
  255. return this._trigger( "start", event, uiHash );
  256. },
  257. _slide: function( event, index, newVal ) {
  258. var otherVal,
  259. newValues,
  260. allowed;
  261. if ( this.options.values && this.options.values.length ) {
  262. otherVal = this.values( index ? 0 : 1 );
  263. if ( ( this.options.values.length === 2 && this.options.range === true ) &&
  264. ( ( index === 0 && newVal > otherVal) || ( index === 1 && newVal < otherVal ) )
  265. ) {
  266. newVal = otherVal;
  267. }
  268. if ( newVal !== this.values( index ) ) {
  269. newValues = this.values();
  270. newValues[ index ] = newVal;
  271. // A slide can be canceled by returning false from the slide callback
  272. allowed = this._trigger( "slide", event, {
  273. handle: this.handles[ index ],
  274. value: newVal,
  275. values: newValues
  276. } );
  277. otherVal = this.values( index ? 0 : 1 );
  278. if ( allowed !== false ) {
  279. this.values( index, newVal );
  280. }
  281. }
  282. } else {
  283. if ( newVal !== this.value() ) {
  284. // A slide can be canceled by returning false from the slide callback
  285. allowed = this._trigger( "slide", event, {
  286. handle: this.handles[ index ],
  287. value: newVal
  288. } );
  289. if ( allowed !== false ) {
  290. this.value( newVal );
  291. }
  292. }
  293. }
  294. },
  295. _stop: function( event, index ) {
  296. var uiHash = {
  297. handle: this.handles[ index ],
  298. value: this.value()
  299. };
  300. if ( this.options.values && this.options.values.length ) {
  301. uiHash.value = this.values( index );
  302. uiHash.values = this.values();
  303. }
  304. this._trigger( "stop", event, uiHash );
  305. },
  306. _change: function( event, index ) {
  307. if ( !this._keySliding && !this._mouseSliding ) {
  308. var uiHash = {
  309. handle: this.handles[ index ],
  310. value: this.value()
  311. };
  312. if ( this.options.values && this.options.values.length ) {
  313. uiHash.value = this.values( index );
  314. uiHash.values = this.values();
  315. }
  316. //store the last changed value index for reference when handles overlap
  317. this._lastChangedValue = index;
  318. this._trigger( "change", event, uiHash );
  319. }
  320. },
  321. value: function( newValue ) {
  322. if ( arguments.length ) {
  323. this.options.value = this._trimAlignValue( newValue );
  324. this._refreshValue();
  325. this._change( null, 0 );
  326. return;
  327. }
  328. return this._value();
  329. },
  330. values: function( index, newValue ) {
  331. var vals,
  332. newValues,
  333. i;
  334. if ( arguments.length > 1 ) {
  335. this.options.values[ index ] = this._trimAlignValue( newValue );
  336. this._refreshValue();
  337. this._change( null, index );
  338. return;
  339. }
  340. if ( arguments.length ) {
  341. if ( $.isArray( arguments[ 0 ] ) ) {
  342. vals = this.options.values;
  343. newValues = arguments[ 0 ];
  344. for ( i = 0; i < vals.length; i += 1 ) {
  345. vals[ i ] = this._trimAlignValue( newValues[ i ] );
  346. this._change( null, i );
  347. }
  348. this._refreshValue();
  349. } else {
  350. if ( this.options.values && this.options.values.length ) {
  351. return this._values( index );
  352. } else {
  353. return this.value();
  354. }
  355. }
  356. } else {
  357. return this._values();
  358. }
  359. },
  360. _setOption: function( key, value ) {
  361. var i,
  362. valsLength = 0;
  363. if ( key === "range" && this.options.range === true ) {
  364. if ( value === "min" ) {
  365. this.options.value = this._values( 0 );
  366. this.options.values = null;
  367. } else if ( value === "max" ) {
  368. this.options.value = this._values( this.options.values.length - 1 );
  369. this.options.values = null;
  370. }
  371. }
  372. if ( $.isArray( this.options.values ) ) {
  373. valsLength = this.options.values.length;
  374. }
  375. if ( key === "disabled" ) {
  376. this.element.toggleClass( "ui-state-disabled", !!value );
  377. }
  378. this._super( key, value );
  379. switch ( key ) {
  380. case "orientation":
  381. this._detectOrientation();
  382. this.element
  383. .removeClass( "ui-slider-horizontal ui-slider-vertical" )
  384. .addClass( "ui-slider-" + this.orientation );
  385. this._refreshValue();
  386. // Reset positioning from previous orientation
  387. this.handles.css( value === "horizontal" ? "bottom" : "left", "" );
  388. break;
  389. case "value":
  390. this._animateOff = true;
  391. this._refreshValue();
  392. this._change( null, 0 );
  393. this._animateOff = false;
  394. break;
  395. case "values":
  396. this._animateOff = true;
  397. this._refreshValue();
  398. for ( i = 0; i < valsLength; i += 1 ) {
  399. this._change( null, i );
  400. }
  401. this._animateOff = false;
  402. break;
  403. case "min":
  404. case "max":
  405. this._animateOff = true;
  406. this._refreshValue();
  407. this._animateOff = false;
  408. break;
  409. case "range":
  410. this._animateOff = true;
  411. this._refresh();
  412. this._animateOff = false;
  413. break;
  414. }
  415. },
  416. //internal value getter
  417. // _value() returns value trimmed by min and max, aligned by step
  418. _value: function() {
  419. var val = this.options.value;
  420. val = this._trimAlignValue( val );
  421. return val;
  422. },
  423. //internal values getter
  424. // _values() returns array of values trimmed by min and max, aligned by step
  425. // _values( index ) returns single value trimmed by min and max, aligned by step
  426. _values: function( index ) {
  427. var val,
  428. vals,
  429. i;
  430. if ( arguments.length ) {
  431. val = this.options.values[ index ];
  432. val = this._trimAlignValue( val );
  433. return val;
  434. } else if ( this.options.values && this.options.values.length ) {
  435. // .slice() creates a copy of the array
  436. // this copy gets trimmed by min and max and then returned
  437. vals = this.options.values.slice();
  438. for ( i = 0; i < vals.length; i+= 1) {
  439. vals[ i ] = this._trimAlignValue( vals[ i ] );
  440. }
  441. return vals;
  442. } else {
  443. return [];
  444. }
  445. },
  446. // returns the step-aligned value that val is closest to, between (inclusive) min and max
  447. _trimAlignValue: function( val ) {
  448. if ( val <= this._valueMin() ) {
  449. return this._valueMin();
  450. }
  451. if ( val >= this._valueMax() ) {
  452. return this._valueMax();
  453. }
  454. var step = ( this.options.step > 0 ) ? this.options.step : 1,
  455. valModStep = (val - this._valueMin()) % step,
  456. alignValue = val - valModStep;
  457. if ( Math.abs(valModStep) * 2 >= step ) {
  458. alignValue += ( valModStep > 0 ) ? step : ( -step );
  459. }
  460. // Since JavaScript has problems with large floats, round
  461. // the final value to 5 digits after the decimal point (see #4124)
  462. return parseFloat( alignValue.toFixed(5) );
  463. },
  464. _valueMin: function() {
  465. return this.options.min;
  466. },
  467. _valueMax: function() {
  468. return this.options.max;
  469. },
  470. _refreshValue: function() {
  471. var lastValPercent, valPercent, value, valueMin, valueMax,
  472. oRange = this.options.range,
  473. o = this.options,
  474. that = this,
  475. animate = ( !this._animateOff ) ? o.animate : false,
  476. _set = {};
  477. if ( this.options.values && this.options.values.length ) {
  478. this.handles.each(function( i ) {
  479. valPercent = ( that.values(i) - that._valueMin() ) / ( that._valueMax() - that._valueMin() ) * 100;
  480. _set[ that.orientation === "horizontal" ? "left" : "bottom" ] = valPercent + "%";
  481. $( this ).stop( 1, 1 )[ animate ? "animate" : "css" ]( _set, o.animate );
  482. if ( that.options.range === true ) {
  483. if ( that.orientation === "horizontal" ) {
  484. if ( i === 0 ) {
  485. that.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { left: valPercent + "%" }, o.animate );
  486. }
  487. if ( i === 1 ) {
  488. that.range[ animate ? "animate" : "css" ]( { width: ( valPercent - lastValPercent ) + "%" }, { queue: false, duration: o.animate } );
  489. }
  490. } else {
  491. if ( i === 0 ) {
  492. that.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { bottom: ( valPercent ) + "%" }, o.animate );
  493. }
  494. if ( i === 1 ) {
  495. that.range[ animate ? "animate" : "css" ]( { height: ( valPercent - lastValPercent ) + "%" }, { queue: false, duration: o.animate } );
  496. }
  497. }
  498. }
  499. lastValPercent = valPercent;
  500. });
  501. } else {
  502. value = this.value();
  503. valueMin = this._valueMin();
  504. valueMax = this._valueMax();
  505. valPercent = ( valueMax !== valueMin ) ?
  506. ( value - valueMin ) / ( valueMax - valueMin ) * 100 :
  507. 0;
  508. _set[ this.orientation === "horizontal" ? "left" : "bottom" ] = valPercent + "%";
  509. this.handle.stop( 1, 1 )[ animate ? "animate" : "css" ]( _set, o.animate );
  510. if ( oRange === "min" && this.orientation === "horizontal" ) {
  511. this.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { width: valPercent + "%" }, o.animate );
  512. }
  513. if ( oRange === "max" && this.orientation === "horizontal" ) {
  514. this.range[ animate ? "animate" : "css" ]( { width: ( 100 - valPercent ) + "%" }, { queue: false, duration: o.animate } );
  515. }
  516. if ( oRange === "min" && this.orientation === "vertical" ) {
  517. this.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { height: valPercent + "%" }, o.animate );
  518. }
  519. if ( oRange === "max" && this.orientation === "vertical" ) {
  520. this.range[ animate ? "animate" : "css" ]( { height: ( 100 - valPercent ) + "%" }, { queue: false, duration: o.animate } );
  521. }
  522. }
  523. },
  524. _handleEvents: {
  525. keydown: function( event ) {
  526. var allowed, curVal, newVal, step,
  527. index = $( event.target ).data( "ui-slider-handle-index" );
  528. switch ( event.keyCode ) {
  529. case $.ui.keyCode.HOME:
  530. case $.ui.keyCode.END:
  531. case $.ui.keyCode.PAGE_UP:
  532. case $.ui.keyCode.PAGE_DOWN:
  533. case $.ui.keyCode.UP:
  534. case $.ui.keyCode.RIGHT:
  535. case $.ui.keyCode.DOWN:
  536. case $.ui.keyCode.LEFT:
  537. event.preventDefault();
  538. if ( !this._keySliding ) {
  539. this._keySliding = true;
  540. $( event.target ).addClass( "ui-state-active" );
  541. allowed = this._start( event, index );
  542. if ( allowed === false ) {
  543. return;
  544. }
  545. }
  546. break;
  547. }
  548. step = this.options.step;
  549. if ( this.options.values && this.options.values.length ) {
  550. curVal = newVal = this.values( index );
  551. } else {
  552. curVal = newVal = this.value();
  553. }
  554. switch ( event.keyCode ) {
  555. case $.ui.keyCode.HOME:
  556. newVal = this._valueMin();
  557. break;
  558. case $.ui.keyCode.END:
  559. newVal = this._valueMax();
  560. break;
  561. case $.ui.keyCode.PAGE_UP:
  562. newVal = this._trimAlignValue(
  563. curVal + ( ( this._valueMax() - this._valueMin() ) / this.numPages )
  564. );
  565. break;
  566. case $.ui.keyCode.PAGE_DOWN:
  567. newVal = this._trimAlignValue(
  568. curVal - ( (this._valueMax() - this._valueMin()) / this.numPages ) );
  569. break;
  570. case $.ui.keyCode.UP:
  571. case $.ui.keyCode.RIGHT:
  572. if ( curVal === this._valueMax() ) {
  573. return;
  574. }
  575. newVal = this._trimAlignValue( curVal + step );
  576. break;
  577. case $.ui.keyCode.DOWN:
  578. case $.ui.keyCode.LEFT:
  579. if ( curVal === this._valueMin() ) {
  580. return;
  581. }
  582. newVal = this._trimAlignValue( curVal - step );
  583. break;
  584. }
  585. this._slide( event, index, newVal );
  586. },
  587. keyup: function( event ) {
  588. var index = $( event.target ).data( "ui-slider-handle-index" );
  589. if ( this._keySliding ) {
  590. this._keySliding = false;
  591. this._stop( event, index );
  592. this._change( event, index );
  593. $( event.target ).removeClass( "ui-state-active" );
  594. }
  595. }
  596. }
  597. });
  598. }));