jquery.ui.slider.js 18 KB

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