NumberSpinner.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. define("dijit/form/NumberSpinner", [
  2. "dojo/_base/declare", // declare
  3. "dojo/_base/event", // event.stop
  4. "dojo/keys", // keys.END keys.HOME
  5. "./_Spinner",
  6. "./NumberTextBox"
  7. ], function(declare, event, keys, _Spinner, NumberTextBox){
  8. /*=====
  9. var _Spinner = dijit.form._Spinner;
  10. var NumberTextBox = dijit.form.NumberTextBox;
  11. =====*/
  12. // module:
  13. // dijit/form/NumberSpinner
  14. // summary:
  15. // Extends NumberTextBox to add up/down arrows and pageup/pagedown for incremental change to the value
  16. return declare("dijit.form.NumberSpinner", [_Spinner, NumberTextBox.Mixin], {
  17. // summary:
  18. // Extends NumberTextBox to add up/down arrows and pageup/pagedown for incremental change to the value
  19. //
  20. // description:
  21. // A `dijit.form.NumberTextBox` extension to provide keyboard accessible value selection
  22. // as well as icons for spinning direction. When using the keyboard, the typematic rules
  23. // apply, meaning holding the key will gradually increase or decrease the value and
  24. // accelerate.
  25. //
  26. // example:
  27. // | new dijit.form.NumberSpinner({ constraints:{ max:300, min:100 }}, "someInput");
  28. adjust: function(/*Object*/ val, /*Number*/ delta){
  29. // summary:
  30. // Change Number val by the given amount
  31. // tags:
  32. // protected
  33. var tc = this.constraints,
  34. v = isNaN(val),
  35. gotMax = !isNaN(tc.max),
  36. gotMin = !isNaN(tc.min)
  37. ;
  38. if(v && delta != 0){ // blank or invalid value and they want to spin, so create defaults
  39. val = (delta > 0) ?
  40. gotMin ? tc.min : gotMax ? tc.max : 0 :
  41. gotMax ? this.constraints.max : gotMin ? tc.min : 0
  42. ;
  43. }
  44. var newval = val + delta;
  45. if(v || isNaN(newval)){ return val; }
  46. if(gotMax && (newval > tc.max)){
  47. newval = tc.max;
  48. }
  49. if(gotMin && (newval < tc.min)){
  50. newval = tc.min;
  51. }
  52. return newval;
  53. },
  54. _onKeyPress: function(e){
  55. if((e.charOrCode == keys.HOME || e.charOrCode == keys.END) && !(e.ctrlKey || e.altKey || e.metaKey)
  56. && typeof this.get('value') != 'undefined' /* gibberish, so HOME and END are default editing keys*/){
  57. var value = this.constraints[(e.charOrCode == keys.HOME ? "min" : "max")];
  58. if(typeof value == "number"){
  59. this._setValueAttr(value, false);
  60. }
  61. // eat home or end key whether we change the value or not
  62. event.stop(e);
  63. }
  64. }
  65. });
  66. });