jquery.ui.slider.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*!
  2. * jQuery UI Slider 1.10.4
  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. * 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.4",
  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. if ( this.range ) {
  114. this.range.remove();
  115. }
  116. this.range = null;
  117. }
  118. },
  119. _setupEvents: function() {
  120. var elements = this.handles.add( this.range ).filter( "a" );
  121. this._off( elements );
  122. this._on( elements, this._handleEvents );
  123. this._hoverable( elements );
  124. this._focusable( elements );
  125. },
  126. _destroy: function() {
  127. this.handles.remove();
  128. if ( this.range ) {
  129. this.range.remove();
  130. }
  131. this.element
  132. .removeClass( "ui-slider" +
  133. " ui-slider-horizontal" +
  134. " ui-slider-vertical" +
  135. " ui-widget" +
  136. " ui-widget-content" +
  137. " ui-corner-all" );
  138. this._mouseDestroy();
  139. },
  140. _mouseCapture: function( event ) {
  141. var position, normValue, distance, closestHandle, index, allowed, offset, mouseOverHandle,
  142. that = this,
  143. o = this.options;
  144. if ( o.disabled ) {
  145. return false;
  146. }
  147. this.elementSize = {
  148. width: this.element.outerWidth(),
  149. height: this.element.outerHeight()
  150. };
  151. this.elementOffset = this.element.offset();
  152. position = { x: event.pageX, y: event.pageY };
  153. normValue = this._normValueFromMouse( position );
  154. distance = this._valueMax() - this._valueMin() + 1;
  155. this.handles.each(function( i ) {
  156. var thisDistance = Math.abs( normValue - that.values(i) );
  157. if (( distance > thisDistance ) ||
  158. ( distance === thisDistance &&
  159. (i === that._lastChangedValue || that.values(i) === o.min ))) {
  160. distance = thisDistance;
  161. closestHandle = $( this );
  162. index = i;
  163. }
  164. });
  165. allowed = this._start( event, index );
  166. if ( allowed === false ) {
  167. return false;
  168. }
  169. this._mouseSliding = true;
  170. this._handleIndex = index;
  171. closestHandle
  172. .addClass( "ui-state-active" )
  173. .focus();
  174. offset = closestHandle.offset();
  175. mouseOverHandle = !$( event.target ).parents().addBack().is( ".ui-slider-handle" );
  176. this._clickOffset = mouseOverHandle ? { left: 0, top: 0 } : {
  177. left: event.pageX - offset.left - ( closestHandle.width() / 2 ),
  178. top: event.pageY - offset.top -
  179. ( closestHandle.height() / 2 ) -
  180. ( parseInt( closestHandle.css("borderTopWidth"), 10 ) || 0 ) -
  181. ( parseInt( closestHandle.css("borderBottomWidth"), 10 ) || 0) +
  182. ( parseInt( closestHandle.css("marginTop"), 10 ) || 0)
  183. };
  184. if ( !this.handles.hasClass( "ui-state-hover" ) ) {
  185. this._slide( event, index, normValue );
  186. }
  187. this._animateOff = true;
  188. return true;
  189. },
  190. _mouseStart: function() {
  191. return true;
  192. },
  193. _mouseDrag: function( event ) {
  194. var position = { x: event.pageX, y: event.pageY },
  195. normValue = this._normValueFromMouse( position );
  196. this._slide( event, this._handleIndex, normValue );
  197. return false;
  198. },
  199. _mouseStop: function( event ) {
  200. this.handles.removeClass( "ui-state-active" );
  201. this._mouseSliding = false;
  202. this._stop( event, this._handleIndex );
  203. this._change( event, this._handleIndex );
  204. this._handleIndex = null;
  205. this._clickOffset = null;
  206. this._animateOff = false;
  207. return false;
  208. },
  209. _detectOrientation: function() {
  210. this.orientation = ( this.options.orientation === "vertical" ) ? "vertical" : "horizontal";
  211. },
  212. _normValueFromMouse: function( position ) {
  213. var pixelTotal,
  214. pixelMouse,
  215. percentMouse,
  216. valueTotal,
  217. valueMouse;
  218. if ( this.orientation === "horizontal" ) {
  219. pixelTotal = this.elementSize.width;
  220. pixelMouse = position.x - this.elementOffset.left - ( this._clickOffset ? this._clickOffset.left : 0 );
  221. } else {
  222. pixelTotal = this.elementSize.height;
  223. pixelMouse = position.y - this.elementOffset.top - ( this._clickOffset ? this._clickOffset.top : 0 );
  224. }
  225. percentMouse = ( pixelMouse / pixelTotal );
  226. if ( percentMouse > 1 ) {
  227. percentMouse = 1;
  228. }
  229. if ( percentMouse < 0 ) {
  230. percentMouse = 0;
  231. }
  232. if ( this.orientation === "vertical" ) {
  233. percentMouse = 1 - percentMouse;
  234. }
  235. valueTotal = this._valueMax() - this._valueMin();
  236. valueMouse = this._valueMin() + percentMouse * valueTotal;
  237. return this._trimAlignValue( valueMouse );
  238. },
  239. _start: function( event, index ) {
  240. var uiHash = {
  241. handle: this.handles[ index ],
  242. value: this.value()
  243. };
  244. if ( this.options.values && this.options.values.length ) {
  245. uiHash.value = this.values( index );
  246. uiHash.values = this.values();
  247. }
  248. return this._trigger( "start", event, uiHash );
  249. },
  250. _slide: function( event, index, newVal ) {
  251. var otherVal,
  252. newValues,
  253. allowed;
  254. if ( this.options.values && this.options.values.length ) {
  255. otherVal = this.values( index ? 0 : 1 );
  256. if ( ( this.options.values.length === 2 && this.options.range === true ) &&
  257. ( ( index === 0 && newVal > otherVal) || ( index === 1 && newVal < otherVal ) )
  258. ) {
  259. newVal = otherVal;
  260. }
  261. if ( newVal !== this.values( index ) ) {
  262. newValues = this.values();
  263. newValues[ index ] = newVal;
  264. // A slide can be canceled by returning false from the slide callback
  265. allowed = this._trigger( "slide", event, {
  266. handle: this.handles[ index ],
  267. value: newVal,
  268. values: newValues
  269. } );
  270. otherVal = this.values( index ? 0 : 1 );
  271. if ( allowed !== false ) {
  272. this.values( index, newVal );
  273. }
  274. }
  275. } else {
  276. if ( newVal !== this.value() ) {
  277. // A slide can be canceled by returning false from the slide callback
  278. allowed = this._trigger( "slide", event, {
  279. handle: this.handles[ index ],
  280. value: newVal
  281. } );
  282. if ( allowed !== false ) {
  283. this.value( newVal );
  284. }
  285. }
  286. }
  287. },
  288. _stop: function( event, index ) {
  289. var uiHash = {
  290. handle: this.handles[ index ],
  291. value: this.value()
  292. };
  293. if ( this.options.values && this.options.values.length ) {
  294. uiHash.value = this.values( index );
  295. uiHash.values = this.values();
  296. }
  297. this._trigger( "stop", event, uiHash );
  298. },
  299. _change: function( event, index ) {
  300. if ( !this._keySliding && !this._mouseSliding ) {
  301. var uiHash = {
  302. handle: this.handles[ index ],
  303. value: this.value()
  304. };
  305. if ( this.options.values && this.options.values.length ) {
  306. uiHash.value = this.values( index );
  307. uiHash.values = this.values();
  308. }
  309. //store the last changed value index for reference when handles overlap
  310. this._lastChangedValue = index;
  311. this._trigger( "change", event, uiHash );
  312. }
  313. },
  314. value: function( newValue ) {
  315. if ( arguments.length ) {
  316. this.options.value = this._trimAlignValue( newValue );
  317. this._refreshValue();
  318. this._change( null, 0 );
  319. return;
  320. }
  321. return this._value();
  322. },
  323. values: function( index, newValue ) {
  324. var vals,
  325. newValues,
  326. i;
  327. if ( arguments.length > 1 ) {
  328. this.options.values[ index ] = this._trimAlignValue( newValue );
  329. this._refreshValue();
  330. this._change( null, index );
  331. return;
  332. }
  333. if ( arguments.length ) {
  334. if ( $.isArray( arguments[ 0 ] ) ) {
  335. vals = this.options.values;
  336. newValues = arguments[ 0 ];
  337. for ( i = 0; i < vals.length; i += 1 ) {
  338. vals[ i ] = this._trimAlignValue( newValues[ i ] );
  339. this._change( null, i );
  340. }
  341. this._refreshValue();
  342. } else {
  343. if ( this.options.values && this.options.values.length ) {
  344. return this._values( index );
  345. } else {
  346. return this.value();
  347. }
  348. }
  349. } else {
  350. return this._values();
  351. }
  352. },
  353. _setOption: function( key, value ) {
  354. var i,
  355. valsLength = 0;
  356. if ( key === "range" && this.options.range === true ) {
  357. if ( value === "min" ) {
  358. this.options.value = this._values( 0 );
  359. this.options.values = null;
  360. } else if ( value === "max" ) {
  361. this.options.value = this._values( this.options.values.length-1 );
  362. this.options.values = null;
  363. }
  364. }
  365. if ( $.isArray( this.options.values ) ) {
  366. valsLength = this.options.values.length;
  367. }
  368. $.Widget.prototype._setOption.apply( this, arguments );
  369. switch ( key ) {
  370. case "orientation":
  371. this._detectOrientation();
  372. this.element
  373. .removeClass( "ui-slider-horizontal ui-slider-vertical" )
  374. .addClass( "ui-slider-" + this.orientation );
  375. this._refreshValue();
  376. break;
  377. case "value":
  378. this._animateOff = true;
  379. this._refreshValue();
  380. this._change( null, 0 );
  381. this._animateOff = false;
  382. break;
  383. case "values":
  384. this._animateOff = true;
  385. this._refreshValue();
  386. for ( i = 0; i < valsLength; i += 1 ) {
  387. this._change( null, i );
  388. }
  389. this._animateOff = false;
  390. break;
  391. case "min":
  392. case "max":
  393. this._animateOff = true;
  394. this._refreshValue();
  395. this._animateOff = false;
  396. break;
  397. case "range":
  398. this._animateOff = true;
  399. this._refresh();
  400. this._animateOff = false;
  401. break;
  402. }
  403. },
  404. //internal value getter
  405. // _value() returns value trimmed by min and max, aligned by step
  406. _value: function() {
  407. var val = this.options.value;
  408. val = this._trimAlignValue( val );
  409. return val;
  410. },
  411. //internal values getter
  412. // _values() returns array of values trimmed by min and max, aligned by step
  413. // _values( index ) returns single value trimmed by min and max, aligned by step
  414. _values: function( index ) {
  415. var val,
  416. vals,
  417. i;
  418. if ( arguments.length ) {
  419. val = this.options.values[ index ];
  420. val = this._trimAlignValue( val );
  421. return val;
  422. } else if ( this.options.values && this.options.values.length ) {
  423. // .slice() creates a copy of the array
  424. // this copy gets trimmed by min and max and then returned
  425. vals = this.options.values.slice();
  426. for ( i = 0; i < vals.length; i+= 1) {
  427. vals[ i ] = this._trimAlignValue( vals[ i ] );
  428. }
  429. return vals;
  430. } else {
  431. return [];
  432. }
  433. },
  434. // returns the step-aligned value that val is closest to, between (inclusive) min and max
  435. _trimAlignValue: function( val ) {
  436. if ( val <= this._valueMin() ) {
  437. return this._valueMin();
  438. }
  439. if ( val >= this._valueMax() ) {
  440. return this._valueMax();
  441. }
  442. var step = ( this.options.step > 0 ) ? this.options.step : 1,
  443. valModStep = (val - this._valueMin()) % step,
  444. alignValue = val - valModStep;
  445. if ( Math.abs(valModStep) * 2 >= step ) {
  446. alignValue += ( valModStep > 0 ) ? step : ( -step );
  447. }
  448. // Since JavaScript has problems with large floats, round
  449. // the final value to 5 digits after the decimal point (see #4124)
  450. return parseFloat( alignValue.toFixed(5) );
  451. },
  452. _valueMin: function() {
  453. return this.options.min;
  454. },
  455. _valueMax: function() {
  456. return this.options.max;
  457. },
  458. _refreshValue: function() {
  459. var lastValPercent, valPercent, value, valueMin, valueMax,
  460. oRange = this.options.range,
  461. o = this.options,
  462. that = this,
  463. animate = ( !this._animateOff ) ? o.animate : false,
  464. _set = {};
  465. if ( this.options.values && this.options.values.length ) {
  466. this.handles.each(function( i ) {
  467. valPercent = ( that.values(i) - that._valueMin() ) / ( that._valueMax() - that._valueMin() ) * 100;
  468. _set[ that.orientation === "horizontal" ? "left" : "bottom" ] = valPercent + "%";
  469. $( this ).stop( 1, 1 )[ animate ? "animate" : "css" ]( _set, o.animate );
  470. if ( that.options.range === true ) {
  471. if ( that.orientation === "horizontal" ) {
  472. if ( i === 0 ) {
  473. that.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { left: valPercent + "%" }, o.animate );
  474. }
  475. if ( i === 1 ) {
  476. that.range[ animate ? "animate" : "css" ]( { width: ( valPercent - lastValPercent ) + "%" }, { queue: false, duration: o.animate } );
  477. }
  478. } else {
  479. if ( i === 0 ) {
  480. that.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { bottom: ( valPercent ) + "%" }, o.animate );
  481. }
  482. if ( i === 1 ) {
  483. that.range[ animate ? "animate" : "css" ]( { height: ( valPercent - lastValPercent ) + "%" }, { queue: false, duration: o.animate } );
  484. }
  485. }
  486. }
  487. lastValPercent = valPercent;
  488. });
  489. } else {
  490. value = this.value();
  491. valueMin = this._valueMin();
  492. valueMax = this._valueMax();
  493. valPercent = ( valueMax !== valueMin ) ?
  494. ( value - valueMin ) / ( valueMax - valueMin ) * 100 :
  495. 0;
  496. _set[ this.orientation === "horizontal" ? "left" : "bottom" ] = valPercent + "%";
  497. this.handle.stop( 1, 1 )[ animate ? "animate" : "css" ]( _set, o.animate );
  498. if ( oRange === "min" && this.orientation === "horizontal" ) {
  499. this.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { width: valPercent + "%" }, o.animate );
  500. }
  501. if ( oRange === "max" && this.orientation === "horizontal" ) {
  502. this.range[ animate ? "animate" : "css" ]( { width: ( 100 - valPercent ) + "%" }, { queue: false, duration: o.animate } );
  503. }
  504. if ( oRange === "min" && this.orientation === "vertical" ) {
  505. this.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { height: valPercent + "%" }, o.animate );
  506. }
  507. if ( oRange === "max" && this.orientation === "vertical" ) {
  508. this.range[ animate ? "animate" : "css" ]( { height: ( 100 - valPercent ) + "%" }, { queue: false, duration: o.animate } );
  509. }
  510. }
  511. },
  512. _handleEvents: {
  513. keydown: function( event ) {
  514. var allowed, curVal, newVal, step,
  515. index = $( event.target ).data( "ui-slider-handle-index" );
  516. switch ( event.keyCode ) {
  517. case $.ui.keyCode.HOME:
  518. case $.ui.keyCode.END:
  519. case $.ui.keyCode.PAGE_UP:
  520. case $.ui.keyCode.PAGE_DOWN:
  521. case $.ui.keyCode.UP:
  522. case $.ui.keyCode.RIGHT:
  523. case $.ui.keyCode.DOWN:
  524. case $.ui.keyCode.LEFT:
  525. event.preventDefault();
  526. if ( !this._keySliding ) {
  527. this._keySliding = true;
  528. $( event.target ).addClass( "ui-state-active" );
  529. allowed = this._start( event, index );
  530. if ( allowed === false ) {
  531. return;
  532. }
  533. }
  534. break;
  535. }
  536. step = this.options.step;
  537. if ( this.options.values && this.options.values.length ) {
  538. curVal = newVal = this.values( index );
  539. } else {
  540. curVal = newVal = this.value();
  541. }
  542. switch ( event.keyCode ) {
  543. case $.ui.keyCode.HOME:
  544. newVal = this._valueMin();
  545. break;
  546. case $.ui.keyCode.END:
  547. newVal = this._valueMax();
  548. break;
  549. case $.ui.keyCode.PAGE_UP:
  550. newVal = this._trimAlignValue( curVal + ( (this._valueMax() - this._valueMin()) / numPages ) );
  551. break;
  552. case $.ui.keyCode.PAGE_DOWN:
  553. newVal = this._trimAlignValue( curVal - ( (this._valueMax() - this._valueMin()) / numPages ) );
  554. break;
  555. case $.ui.keyCode.UP:
  556. case $.ui.keyCode.RIGHT:
  557. if ( curVal === this._valueMax() ) {
  558. return;
  559. }
  560. newVal = this._trimAlignValue( curVal + step );
  561. break;
  562. case $.ui.keyCode.DOWN:
  563. case $.ui.keyCode.LEFT:
  564. if ( curVal === this._valueMin() ) {
  565. return;
  566. }
  567. newVal = this._trimAlignValue( curVal - step );
  568. break;
  569. }
  570. this._slide( event, index, newVal );
  571. },
  572. click: function( event ) {
  573. event.preventDefault();
  574. },
  575. keyup: function( event ) {
  576. var index = $( event.target ).data( "ui-slider-handle-index" );
  577. if ( this._keySliding ) {
  578. this._keySliding = false;
  579. this._stop( event, index );
  580. this._change( event, index );
  581. $( event.target ).removeClass( "ui-state-active" );
  582. }
  583. }
  584. }
  585. });
  586. }(jQuery));