NumberSpinner.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dijit.form.NumberSpinner"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dijit.form.NumberSpinner"] = true;
  8. dojo.provide("dijit.form.NumberSpinner");
  9. dojo.require("dijit.form._Spinner");
  10. dojo.require("dijit.form.NumberTextBox");
  11. dojo.declare("dijit.form.NumberSpinner",
  12. [dijit.form._Spinner, dijit.form.NumberTextBoxMixin],
  13. {
  14. // summary:
  15. // Extends NumberTextBox to add up/down arrows and pageup/pagedown for incremental change to the value
  16. //
  17. // description:
  18. // A `dijit.form.NumberTextBox` extension to provide keyboard accessible value selection
  19. // as well as icons for spinning direction. When using the keyboard, the typematic rules
  20. // apply, meaning holding the key will gradually increase or decrease the value and
  21. // accelerate.
  22. //
  23. // example:
  24. // | new dijit.form.NumberSpinner({ constraints:{ max:300, min:100 }}, "someInput");
  25. adjust: function(/*Object*/ val, /*Number*/ delta){
  26. // summary:
  27. // Change Number val by the given amount
  28. // tags:
  29. // protected
  30. var tc = this.constraints,
  31. v = isNaN(val),
  32. gotMax = !isNaN(tc.max),
  33. gotMin = !isNaN(tc.min)
  34. ;
  35. if(v && delta != 0){ // blank or invalid value and they want to spin, so create defaults
  36. val = (delta > 0) ?
  37. gotMin ? tc.min : gotMax ? tc.max : 0 :
  38. gotMax ? this.constraints.max : gotMin ? tc.min : 0
  39. ;
  40. }
  41. var newval = val + delta;
  42. if(v || isNaN(newval)){ return val; }
  43. if(gotMax && (newval > tc.max)){
  44. newval = tc.max;
  45. }
  46. if(gotMin && (newval < tc.min)){
  47. newval = tc.min;
  48. }
  49. return newval;
  50. },
  51. _onKeyPress: function(e){
  52. if((e.charOrCode == dojo.keys.HOME || e.charOrCode == dojo.keys.END) && !(e.ctrlKey || e.altKey || e.metaKey)
  53. && typeof this.get('value') != 'undefined' /* gibberish, so HOME and END are default editing keys*/){
  54. var value = this.constraints[(e.charOrCode == dojo.keys.HOME ? "min" : "max")];
  55. if(typeof value == "number"){
  56. this._setValueAttr(value, false);
  57. }
  58. // eat home or end key whether we change the value or not
  59. dojo.stopEvent(e);
  60. }
  61. }
  62. });
  63. }