jquery.ui.slider.js 17KB

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