jquery.ui.slider.js 17 KB

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