jquery.ui.spinner.js 12 KB

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