jquery.ui.spinner.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*!
  2. * jQuery UI Spinner 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/spinner/
  10. *
  11. * Depends:
  12. * jquery.ui.core.js
  13. * jquery.ui.widget.js
  14. * jquery.ui.button.js
  15. */
  16. (function( $ ) {
  17. function modifier( fn ) {
  18. return function() {
  19. var previous = this.element.val();
  20. fn.apply( this, arguments );
  21. this._refresh();
  22. if ( previous !== this.element.val() ) {
  23. this._trigger( "change" );
  24. }
  25. };
  26. }
  27. $.widget( "ui.spinner", {
  28. version: "@VERSION",
  29. defaultElement: "<input>",
  30. widgetEventPrefix: "spin",
  31. options: {
  32. culture: null,
  33. icons: {
  34. down: "ui-icon-triangle-1-s",
  35. up: "ui-icon-triangle-1-n"
  36. },
  37. incremental: true,
  38. max: null,
  39. min: null,
  40. numberFormat: null,
  41. page: 10,
  42. step: 1,
  43. change: null,
  44. spin: null,
  45. start: null,
  46. stop: null
  47. },
  48. _create: function() {
  49. // handle string values that need to be parsed
  50. this._setOption( "max", this.options.max );
  51. this._setOption( "min", this.options.min );
  52. this._setOption( "step", this.options.step );
  53. // format the value, but don't constrain
  54. this._value( this.element.val(), true );
  55. this._draw();
  56. this._on( this._events );
  57. this._refresh();
  58. // turning off autocomplete prevents the browser from remembering the
  59. // value when navigating through history, so we re-enable autocomplete
  60. // if the page is unloaded before the widget is destroyed. #7790
  61. this._on( this.window, {
  62. beforeunload: function() {
  63. this.element.removeAttr( "autocomplete" );
  64. }
  65. });
  66. },
  67. _getCreateOptions: function() {
  68. var options = {},
  69. element = this.element;
  70. $.each( [ "min", "max", "step" ], function( i, option ) {
  71. var value = element.attr( option );
  72. if ( value !== undefined && value.length ) {
  73. options[ option ] = value;
  74. }
  75. });
  76. return options;
  77. },
  78. _events: {
  79. keydown: function( event ) {
  80. if ( this._start( event ) && this._keydown( event ) ) {
  81. event.preventDefault();
  82. }
  83. },
  84. keyup: "_stop",
  85. focus: function() {
  86. this.previous = this.element.val();
  87. },
  88. blur: function( event ) {
  89. if ( this.cancelBlur ) {
  90. delete this.cancelBlur;
  91. return;
  92. }
  93. this._refresh();
  94. if ( this.previous !== this.element.val() ) {
  95. this._trigger( "change", event );
  96. }
  97. },
  98. mousewheel: function( event, delta ) {
  99. if ( !delta ) {
  100. return;
  101. }
  102. if ( !this.spinning && !this._start( event ) ) {
  103. return false;
  104. }
  105. this._spin( (delta > 0 ? 1 : -1) * this.options.step, event );
  106. clearTimeout( this.mousewheelTimer );
  107. this.mousewheelTimer = this._delay(function() {
  108. if ( this.spinning ) {
  109. this._stop( event );
  110. }
  111. }, 100 );
  112. event.preventDefault();
  113. },
  114. "mousedown .ui-spinner-button": function( event ) {
  115. var previous;
  116. // We never want the buttons to have focus; whenever the user is
  117. // interacting with the spinner, the focus should be on the input.
  118. // If the input is focused then this.previous is properly set from
  119. // when the input first received focus. If the input is not focused
  120. // then we need to set this.previous based on the value before spinning.
  121. previous = this.element[0] === this.document[0].activeElement ?
  122. this.previous : this.element.val();
  123. function checkFocus() {
  124. var isActive = this.element[0] === this.document[0].activeElement;
  125. if ( !isActive ) {
  126. this.element.focus();
  127. this.previous = previous;
  128. // support: IE
  129. // IE sets focus asynchronously, so we need to check if focus
  130. // moved off of the input because the user clicked on the button.
  131. this._delay(function() {
  132. this.previous = previous;
  133. });
  134. }
  135. }
  136. // ensure focus is on (or stays on) the text field
  137. event.preventDefault();
  138. checkFocus.call( this );
  139. // support: IE
  140. // IE doesn't prevent moving focus even with event.preventDefault()
  141. // so we set a flag to know when we should ignore the blur event
  142. // and check (again) if focus moved off of the input.
  143. this.cancelBlur = true;
  144. this._delay(function() {
  145. delete this.cancelBlur;
  146. checkFocus.call( this );
  147. });
  148. if ( this._start( event ) === false ) {
  149. return;
  150. }
  151. this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
  152. },
  153. "mouseup .ui-spinner-button": "_stop",
  154. "mouseenter .ui-spinner-button": function( event ) {
  155. // button will add ui-state-active if mouse was down while mouseleave and kept down
  156. if ( !$( event.currentTarget ).hasClass( "ui-state-active" ) ) {
  157. return;
  158. }
  159. if ( this._start( event ) === false ) {
  160. return false;
  161. }
  162. this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
  163. },
  164. // TODO: do we really want to consider this a stop?
  165. // shouldn't we just stop the repeater and wait until mouseup before
  166. // we trigger the stop event?
  167. "mouseleave .ui-spinner-button": "_stop"
  168. },
  169. _draw: function() {
  170. var uiSpinner = this.uiSpinner = this.element
  171. .addClass( "ui-spinner-input" )
  172. .attr( "autocomplete", "off" )
  173. .wrap( this._uiSpinnerHtml() )
  174. .parent()
  175. // add buttons
  176. .append( this._buttonHtml() );
  177. this.element.attr( "role", "spinbutton" );
  178. // button bindings
  179. this.buttons = uiSpinner.find( ".ui-spinner-button" )
  180. .attr( "tabIndex", -1 )
  181. .button()
  182. .removeClass( "ui-corner-all" );
  183. // IE 6 doesn't understand height: 50% for the buttons
  184. // unless the wrapper has an explicit height
  185. if ( this.buttons.height() > Math.ceil( uiSpinner.height() * 0.5 ) &&
  186. uiSpinner.height() > 0 ) {
  187. uiSpinner.height( uiSpinner.height() );
  188. }
  189. // disable spinner if element was already disabled
  190. if ( this.options.disabled ) {
  191. this.disable();
  192. }
  193. },
  194. _keydown: function( event ) {
  195. var options = this.options,
  196. keyCode = $.ui.keyCode;
  197. switch ( event.keyCode ) {
  198. case keyCode.UP:
  199. this._repeat( null, 1, event );
  200. return true;
  201. case keyCode.DOWN:
  202. this._repeat( null, -1, event );
  203. return true;
  204. case keyCode.PAGE_UP:
  205. this._repeat( null, options.page, event );
  206. return true;
  207. case keyCode.PAGE_DOWN:
  208. this._repeat( null, -options.page, event );
  209. return true;
  210. }
  211. return false;
  212. },
  213. _uiSpinnerHtml: function() {
  214. return "<span class='ui-spinner ui-widget ui-widget-content ui-corner-all'></span>";
  215. },
  216. _buttonHtml: function() {
  217. return "" +
  218. "<a class='ui-spinner-button ui-spinner-up ui-corner-tr'>" +
  219. "<span class='ui-icon " + this.options.icons.up + "'>&#9650;</span>" +
  220. "</a>" +
  221. "<a class='ui-spinner-button ui-spinner-down ui-corner-br'>" +
  222. "<span class='ui-icon " + this.options.icons.down + "'>&#9660;</span>" +
  223. "</a>";
  224. },
  225. _start: function( event ) {
  226. if ( !this.spinning && this._trigger( "start", event ) === false ) {
  227. return false;
  228. }
  229. if ( !this.counter ) {
  230. this.counter = 1;
  231. }
  232. this.spinning = true;
  233. return true;
  234. },
  235. _repeat: function( i, steps, event ) {
  236. i = i || 500;
  237. clearTimeout( this.timer );
  238. this.timer = this._delay(function() {
  239. this._repeat( 40, steps, event );
  240. }, i );
  241. this._spin( steps * this.options.step, event );
  242. },
  243. _spin: function( step, event ) {
  244. var value = this.value() || 0;
  245. if ( !this.counter ) {
  246. this.counter = 1;
  247. }
  248. value = this._adjustValue( value + step * this._increment( this.counter ) );
  249. if ( !this.spinning || this._trigger( "spin", event, { value: value } ) !== false) {
  250. this._value( value );
  251. this.counter++;
  252. }
  253. },
  254. _increment: function( i ) {
  255. var incremental = this.options.incremental;
  256. if ( incremental ) {
  257. return $.isFunction( incremental ) ?
  258. incremental( i ) :
  259. Math.floor( i*i*i/50000 - i*i/500 + 17*i/200 + 1 );
  260. }
  261. return 1;
  262. },
  263. _precision: function() {
  264. var precision = this._precisionOf( this.options.step );
  265. if ( this.options.min !== null ) {
  266. precision = Math.max( precision, this._precisionOf( this.options.min ) );
  267. }
  268. return precision;
  269. },
  270. _precisionOf: function( num ) {
  271. var str = num.toString(),
  272. decimal = str.indexOf( "." );
  273. return decimal === -1 ? 0 : str.length - decimal - 1;
  274. },
  275. _adjustValue: function( value ) {
  276. var base, aboveMin,
  277. options = this.options;
  278. // make sure we're at a valid step
  279. // - find out where we are relative to the base (min or 0)
  280. base = options.min !== null ? options.min : 0;
  281. aboveMin = value - base;
  282. // - round to the nearest step
  283. aboveMin = Math.round(aboveMin / options.step) * options.step;
  284. // - rounding is based on 0, so adjust back to our base
  285. value = base + aboveMin;
  286. // fix precision from bad JS floating point math
  287. value = parseFloat( value.toFixed( this._precision() ) );
  288. // clamp the value
  289. if ( options.max !== null && value > options.max) {
  290. return options.max;
  291. }
  292. if ( options.min !== null && value < options.min ) {
  293. return options.min;
  294. }
  295. return value;
  296. },
  297. _stop: function( event ) {
  298. if ( !this.spinning ) {
  299. return;
  300. }
  301. clearTimeout( this.timer );
  302. clearTimeout( this.mousewheelTimer );
  303. this.counter = 0;
  304. this.spinning = false;
  305. this._trigger( "stop", event );
  306. },
  307. _setOption: function( key, value ) {
  308. if ( key === "culture" || key === "numberFormat" ) {
  309. var prevValue = this._parse( this.element.val() );
  310. this.options[ key ] = value;
  311. this.element.val( this._format( prevValue ) );
  312. return;
  313. }
  314. if ( key === "max" || key === "min" || key === "step" ) {
  315. if ( typeof value === "string" ) {
  316. value = this._parse( value );
  317. }
  318. }
  319. this._super( key, value );
  320. if ( key === "disabled" ) {
  321. if ( value ) {
  322. this.element.prop( "disabled", true );
  323. this.buttons.button( "disable" );
  324. } else {
  325. this.element.prop( "disabled", false );
  326. this.buttons.button( "enable" );
  327. }
  328. }
  329. },
  330. _setOptions: modifier(function( options ) {
  331. this._super( options );
  332. this._value( this.element.val() );
  333. }),
  334. _parse: function( val ) {
  335. if ( typeof val === "string" && val !== "" ) {
  336. val = window.Globalize && this.options.numberFormat ?
  337. Globalize.parseFloat( val, 10, this.options.culture ) : +val;
  338. }
  339. return val === "" || isNaN( val ) ? null : val;
  340. },
  341. _format: function( value ) {
  342. if ( value === "" ) {
  343. return "";
  344. }
  345. return window.Globalize && this.options.numberFormat ?
  346. Globalize.format( value, this.options.numberFormat, this.options.culture ) :
  347. value;
  348. },
  349. _refresh: function() {
  350. this.element.attr({
  351. "aria-valuemin": this.options.min,
  352. "aria-valuemax": this.options.max,
  353. // TODO: what should we do with values that can't be parsed?
  354. "aria-valuenow": this._parse( this.element.val() )
  355. });
  356. },
  357. // update the value without triggering change
  358. _value: function( value, allowAny ) {
  359. var parsed;
  360. if ( value !== "" ) {
  361. parsed = this._parse( value );
  362. if ( parsed !== null ) {
  363. if ( !allowAny ) {
  364. parsed = this._adjustValue( parsed );
  365. }
  366. value = this._format( parsed );
  367. }
  368. }
  369. this.element.val( value );
  370. this._refresh();
  371. },
  372. _destroy: function() {
  373. this.element
  374. .removeClass( "ui-spinner-input" )
  375. .prop( "disabled", false )
  376. .removeAttr( "autocomplete" )
  377. .removeAttr( "role" )
  378. .removeAttr( "aria-valuemin" )
  379. .removeAttr( "aria-valuemax" )
  380. .removeAttr( "aria-valuenow" );
  381. this.uiSpinner.replaceWith( this.element );
  382. },
  383. stepUp: modifier(function( steps ) {
  384. this._stepUp( steps );
  385. }),
  386. _stepUp: function( steps ) {
  387. this._spin( (steps || 1) * this.options.step );
  388. },
  389. stepDown: modifier(function( steps ) {
  390. this._stepDown( steps );
  391. }),
  392. _stepDown: function( steps ) {
  393. this._spin( (steps || 1) * -this.options.step );
  394. },
  395. pageUp: modifier(function( pages ) {
  396. this._stepUp( (pages || 1) * this.options.page );
  397. }),
  398. pageDown: modifier(function( pages ) {
  399. this._stepDown( (pages || 1) * this.options.page );
  400. }),
  401. value: function( newVal ) {
  402. if ( !arguments.length ) {
  403. return this._parse( this.element.val() );
  404. }
  405. modifier( this._value ).call( this, newVal );
  406. },
  407. widget: function() {
  408. return this.uiSpinner;
  409. }
  410. });
  411. }( jQuery ) );