PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/ui/jquery.ui.slider.js

http://github.com/jquery/jquery-ui
JavaScript | 680 lines | 555 code | 91 blank | 34 comment | 108 complexity | bd95357419adaaf034669fad3ca30c29 MD5 | raw file
  1. /*!
  2. * jQuery UI Slider @VERSION
  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: "@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. // 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, true );
  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. if ( key === "disabled" ) {
  369. this.element.toggleClass( "ui-state-disabled", !!value );
  370. }
  371. this._super( key, value );
  372. switch ( key ) {
  373. case "orientation":
  374. this._detectOrientation();
  375. this.element
  376. .removeClass( "ui-slider-horizontal ui-slider-vertical" )
  377. .addClass( "ui-slider-" + this.orientation );
  378. this._refreshValue();
  379. break;
  380. case "value":
  381. this._animateOff = true;
  382. this._refreshValue();
  383. this._change( null, 0 );
  384. this._animateOff = false;
  385. break;
  386. case "values":
  387. this._animateOff = true;
  388. this._refreshValue();
  389. for ( i = 0; i < valsLength; i += 1 ) {
  390. this._change( null, i );
  391. }
  392. this._animateOff = false;
  393. break;
  394. case "min":
  395. case "max":
  396. this._animateOff = true;
  397. this._refreshValue();
  398. this._animateOff = false;
  399. break;
  400. case "range":
  401. this._animateOff = true;
  402. this._refresh();
  403. this._animateOff = false;
  404. break;
  405. }
  406. },
  407. //internal value getter
  408. // _value() returns value trimmed by min and max, aligned by step
  409. _value: function() {
  410. var val = this.options.value;
  411. val = this._trimAlignValue( val );
  412. return val;
  413. },
  414. //internal values getter
  415. // _values() returns array of values trimmed by min and max, aligned by step
  416. // _values( index ) returns single value trimmed by min and max, aligned by step
  417. _values: function( index ) {
  418. var val,
  419. vals,
  420. i;
  421. if ( arguments.length ) {
  422. val = this.options.values[ index ];
  423. val = this._trimAlignValue( val );
  424. return val;
  425. } else if ( this.options.values && this.options.values.length ) {
  426. // .slice() creates a copy of the array
  427. // this copy gets trimmed by min and max and then returned
  428. vals = this.options.values.slice();
  429. for ( i = 0; i < vals.length; i+= 1) {
  430. vals[ i ] = this._trimAlignValue( vals[ i ] );
  431. }
  432. return vals;
  433. } else {
  434. return [];
  435. }
  436. },
  437. // returns the step-aligned value that val is closest to, between (inclusive) min and max
  438. _trimAlignValue: function( val ) {
  439. if ( val <= this._valueMin() ) {
  440. return this._valueMin();
  441. }
  442. if ( val >= this._valueMax() ) {
  443. return this._valueMax();
  444. }
  445. var step = ( this.options.step > 0 ) ? this.options.step : 1,
  446. valModStep = (val - this._valueMin()) % step,
  447. alignValue = val - valModStep;
  448. if ( Math.abs(valModStep) * 2 >= step ) {
  449. alignValue += ( valModStep > 0 ) ? step : ( -step );
  450. }
  451. // Since JavaScript has problems with large floats, round
  452. // the final value to 5 digits after the decimal point (see #4124)
  453. return parseFloat( alignValue.toFixed(5) );
  454. },
  455. _valueMin: function() {
  456. return this.options.min;
  457. },
  458. _valueMax: function() {
  459. return this.options.max;
  460. },
  461. _refreshValue: function() {
  462. var lastValPercent, valPercent, value, valueMin, valueMax,
  463. oRange = this.options.range,
  464. o = this.options,
  465. that = this,
  466. animate = ( !this._animateOff ) ? o.animate : false,
  467. _set = {};
  468. if ( this.options.values && this.options.values.length ) {
  469. this.handles.each(function( i ) {
  470. valPercent = ( that.values(i) - that._valueMin() ) / ( that._valueMax() - that._valueMin() ) * 100;
  471. _set[ that.orientation === "horizontal" ? "left" : "bottom" ] = valPercent + "%";
  472. $( this ).stop( 1, 1 )[ animate ? "animate" : "css" ]( _set, o.animate );
  473. if ( that.options.range === true ) {
  474. if ( that.orientation === "horizontal" ) {
  475. if ( i === 0 ) {
  476. that.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { left: valPercent + "%" }, o.animate );
  477. }
  478. if ( i === 1 ) {
  479. that.range[ animate ? "animate" : "css" ]( { width: ( valPercent - lastValPercent ) + "%" }, { queue: false, duration: o.animate } );
  480. }
  481. } else {
  482. if ( i === 0 ) {
  483. that.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { bottom: ( valPercent ) + "%" }, o.animate );
  484. }
  485. if ( i === 1 ) {
  486. that.range[ animate ? "animate" : "css" ]( { height: ( valPercent - lastValPercent ) + "%" }, { queue: false, duration: o.animate } );
  487. }
  488. }
  489. }
  490. lastValPercent = valPercent;
  491. });
  492. } else {
  493. value = this.value();
  494. valueMin = this._valueMin();
  495. valueMax = this._valueMax();
  496. valPercent = ( valueMax !== valueMin ) ?
  497. ( value - valueMin ) / ( valueMax - valueMin ) * 100 :
  498. 0;
  499. _set[ this.orientation === "horizontal" ? "left" : "bottom" ] = valPercent + "%";
  500. this.handle.stop( 1, 1 )[ animate ? "animate" : "css" ]( _set, o.animate );
  501. if ( oRange === "min" && this.orientation === "horizontal" ) {
  502. this.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { width: valPercent + "%" }, o.animate );
  503. }
  504. if ( oRange === "max" && this.orientation === "horizontal" ) {
  505. this.range[ animate ? "animate" : "css" ]( { width: ( 100 - valPercent ) + "%" }, { queue: false, duration: o.animate } );
  506. }
  507. if ( oRange === "min" && this.orientation === "vertical" ) {
  508. this.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { height: valPercent + "%" }, o.animate );
  509. }
  510. if ( oRange === "max" && this.orientation === "vertical" ) {
  511. this.range[ animate ? "animate" : "css" ]( { height: ( 100 - valPercent ) + "%" }, { queue: false, duration: o.animate } );
  512. }
  513. }
  514. },
  515. _handleEvents: {
  516. keydown: function( event ) {
  517. var allowed, curVal, newVal, step,
  518. index = $( event.target ).data( "ui-slider-handle-index" );
  519. switch ( event.keyCode ) {
  520. case $.ui.keyCode.HOME:
  521. case $.ui.keyCode.END:
  522. case $.ui.keyCode.PAGE_UP:
  523. case $.ui.keyCode.PAGE_DOWN:
  524. case $.ui.keyCode.UP:
  525. case $.ui.keyCode.RIGHT:
  526. case $.ui.keyCode.DOWN:
  527. case $.ui.keyCode.LEFT:
  528. event.preventDefault();
  529. if ( !this._keySliding ) {
  530. this._keySliding = true;
  531. $( event.target ).addClass( "ui-state-active" );
  532. allowed = this._start( event, index );
  533. if ( allowed === false ) {
  534. return;
  535. }
  536. }
  537. break;
  538. }
  539. step = this.options.step;
  540. if ( this.options.values && this.options.values.length ) {
  541. curVal = newVal = this.values( index );
  542. } else {
  543. curVal = newVal = this.value();
  544. }
  545. switch ( event.keyCode ) {
  546. case $.ui.keyCode.HOME:
  547. newVal = this._valueMin();
  548. break;
  549. case $.ui.keyCode.END:
  550. newVal = this._valueMax();
  551. break;
  552. case $.ui.keyCode.PAGE_UP:
  553. newVal = this._trimAlignValue( curVal + ( (this._valueMax() - this._valueMin()) / numPages ) );
  554. break;
  555. case $.ui.keyCode.PAGE_DOWN:
  556. newVal = this._trimAlignValue( curVal - ( (this._valueMax() - this._valueMin()) / numPages ) );
  557. break;
  558. case $.ui.keyCode.UP:
  559. case $.ui.keyCode.RIGHT:
  560. if ( curVal === this._valueMax() ) {
  561. return;
  562. }
  563. newVal = this._trimAlignValue( curVal + step );
  564. break;
  565. case $.ui.keyCode.DOWN:
  566. case $.ui.keyCode.LEFT:
  567. if ( curVal === this._valueMin() ) {
  568. return;
  569. }
  570. newVal = this._trimAlignValue( curVal - step );
  571. break;
  572. }
  573. this._slide( event, index, newVal );
  574. },
  575. click: function( event ) {
  576. event.preventDefault();
  577. },
  578. keyup: function( event ) {
  579. var index = $( event.target ).data( "ui-slider-handle-index" );
  580. if ( this._keySliding ) {
  581. this._keySliding = false;
  582. this._stop( event, index );
  583. this._change( event, index );
  584. $( event.target ).removeClass( "ui-state-active" );
  585. }
  586. }
  587. }
  588. });
  589. }(jQuery));