NumberSpinner.js.uncompressed.js 2.0 KB

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