_Spinner.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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._Spinner"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dijit.form._Spinner"] = true;
  8. dojo.provide("dijit.form._Spinner");
  9. dojo.require("dijit.form.ValidationTextBox");
  10. dojo.declare(
  11. "dijit.form._Spinner",
  12. dijit.form.RangeBoundTextBox,
  13. {
  14. // summary:
  15. // Mixin for validation widgets with a spinner.
  16. // description:
  17. // This class basically (conceptually) extends `dijit.form.ValidationTextBox`.
  18. // It modifies the template to have up/down arrows, and provides related handling code.
  19. // defaultTimeout: Number
  20. // Number of milliseconds before a held arrow key or up/down button becomes typematic
  21. defaultTimeout: 500,
  22. // minimumTimeout: Number
  23. // minimum number of milliseconds that typematic event fires when held key or button is held
  24. minimumTimeout: 10,
  25. // timeoutChangeRate: Number
  26. // Fraction of time used to change the typematic timer between events.
  27. // 1.0 means that each typematic event fires at defaultTimeout intervals.
  28. // < 1.0 means that each typematic event fires at an increasing faster rate.
  29. timeoutChangeRate: 0.90,
  30. // smallDelta: Number
  31. // Adjust the value by this much when spinning using the arrow keys/buttons
  32. smallDelta: 1,
  33. // largeDelta: Number
  34. // Adjust the value by this much when spinning using the PgUp/Dn keys
  35. largeDelta: 10,
  36. templateString: dojo.cache("dijit.form", "templates/Spinner.html", "<div class=\"dijit dijitReset dijitInlineTable dijitLeft\"\n\tid=\"widget_${id}\" role=\"presentation\"\n\t><div class=\"dijitReset dijitButtonNode dijitSpinnerButtonContainer\"\n\t\t><input class=\"dijitReset dijitInputField dijitSpinnerButtonInner\" type=\"text\" tabIndex=\"-1\" readonly=\"readonly\" role=\"presentation\"\n\t\t/><div class=\"dijitReset dijitLeft dijitButtonNode dijitArrowButton dijitUpArrowButton\"\n\t\t\tdojoAttachPoint=\"upArrowNode\"\n\t\t\t><div class=\"dijitArrowButtonInner\"\n\t\t\t\t><input class=\"dijitReset dijitInputField\" value=\"&#9650;\" type=\"text\" tabIndex=\"-1\" readonly=\"readonly\" role=\"presentation\"\n\t\t\t\t\t${_buttonInputDisabled}\n\t\t\t/></div\n\t\t></div\n\t\t><div class=\"dijitReset dijitLeft dijitButtonNode dijitArrowButton dijitDownArrowButton\"\n\t\t\tdojoAttachPoint=\"downArrowNode\"\n\t\t\t><div class=\"dijitArrowButtonInner\"\n\t\t\t\t><input class=\"dijitReset dijitInputField\" value=\"&#9660;\" type=\"text\" tabIndex=\"-1\" readonly=\"readonly\" role=\"presentation\"\n\t\t\t\t\t${_buttonInputDisabled}\n\t\t\t/></div\n\t\t></div\n\t></div\n\t><div class='dijitReset dijitValidationContainer'\n\t\t><input class=\"dijitReset dijitInputField dijitValidationIcon dijitValidationInner\" value=\"&#935;\" type=\"text\" tabIndex=\"-1\" readonly=\"readonly\" role=\"presentation\"\n\t/></div\n\t><div class=\"dijitReset dijitInputField dijitInputContainer\"\n\t\t><input class='dijitReset dijitInputInner' dojoAttachPoint=\"textbox,focusNode\" type=\"${type}\" dojoAttachEvent=\"onkeypress:_onKeyPress\"\n\t\t\trole=\"spinbutton\" autocomplete=\"off\" ${!nameAttrSetting}\n\t/></div\n></div>\n"),
  37. baseClass: "dijitTextBox dijitSpinner",
  38. // Set classes like dijitUpArrowButtonHover or dijitDownArrowButtonActive depending on
  39. // mouse action over specified node
  40. cssStateNodes: {
  41. "upArrowNode": "dijitUpArrowButton",
  42. "downArrowNode": "dijitDownArrowButton"
  43. },
  44. adjust: function(/*Object*/ val, /*Number*/ delta){
  45. // summary:
  46. // Overridable function used to adjust a primitive value(Number/Date/...) by the delta amount specified.
  47. // The val is adjusted in a way that makes sense to the object type.
  48. // tags:
  49. // protected extension
  50. return val;
  51. },
  52. _arrowPressed: function(/*Node*/ nodePressed, /*Number*/ direction, /*Number*/ increment){
  53. // summary:
  54. // Handler for arrow button or arrow key being pressed
  55. if(this.disabled || this.readOnly){ return; }
  56. this._setValueAttr(this.adjust(this.get('value'), direction*increment), false);
  57. dijit.selectInputText(this.textbox, this.textbox.value.length);
  58. },
  59. _arrowReleased: function(/*Node*/ node){
  60. // summary:
  61. // Handler for arrow button or arrow key being released
  62. this._wheelTimer = null;
  63. if(this.disabled || this.readOnly){ return; }
  64. },
  65. _typematicCallback: function(/*Number*/ count, /*DOMNode*/ node, /*Event*/ evt){
  66. var inc=this.smallDelta;
  67. if(node == this.textbox){
  68. var k=dojo.keys;
  69. var key = evt.charOrCode;
  70. inc = (key == k.PAGE_UP || key == k.PAGE_DOWN) ? this.largeDelta : this.smallDelta;
  71. node = (key == k.UP_ARROW || key == k.PAGE_UP) ? this.upArrowNode : this.downArrowNode;
  72. }
  73. if(count == -1){ this._arrowReleased(node); }
  74. else{ this._arrowPressed(node, (node == this.upArrowNode) ? 1 : -1, inc); }
  75. },
  76. _wheelTimer: null,
  77. _mouseWheeled: function(/*Event*/ evt){
  78. // summary:
  79. // Mouse wheel listener where supported
  80. dojo.stopEvent(evt);
  81. // FIXME: Safari bubbles
  82. // be nice to DOH and scroll as much as the event says to
  83. var scrollAmount = evt.detail ? (evt.detail * -1) : (evt.wheelDelta / 120);
  84. if(scrollAmount !== 0){
  85. var node = this[(scrollAmount > 0 ? "upArrowNode" : "downArrowNode" )];
  86. this._arrowPressed(node, scrollAmount, this.smallDelta);
  87. if(!this._wheelTimer){
  88. clearTimeout(this._wheelTimer);
  89. }
  90. this._wheelTimer = setTimeout(dojo.hitch(this,"_arrowReleased",node), 50);
  91. }
  92. },
  93. postCreate: function(){
  94. this.inherited(arguments);
  95. // extra listeners
  96. this.connect(this.domNode, !dojo.isMozilla ? "onmousewheel" : 'DOMMouseScroll', "_mouseWheeled");
  97. this._connects.push(dijit.typematic.addListener(this.upArrowNode, this.textbox, {charOrCode:dojo.keys.UP_ARROW,ctrlKey:false,altKey:false,shiftKey:false,metaKey:false}, this, "_typematicCallback", this.timeoutChangeRate, this.defaultTimeout, this.minimumTimeout));
  98. this._connects.push(dijit.typematic.addListener(this.downArrowNode, this.textbox, {charOrCode:dojo.keys.DOWN_ARROW,ctrlKey:false,altKey:false,shiftKey:false,metaKey:false}, this, "_typematicCallback", this.timeoutChangeRate, this.defaultTimeout, this.minimumTimeout));
  99. this._connects.push(dijit.typematic.addListener(this.upArrowNode, this.textbox, {charOrCode:dojo.keys.PAGE_UP,ctrlKey:false,altKey:false,shiftKey:false,metaKey:false}, this, "_typematicCallback", this.timeoutChangeRate, this.defaultTimeout, this.minimumTimeout));
  100. this._connects.push(dijit.typematic.addListener(this.downArrowNode, this.textbox, {charOrCode:dojo.keys.PAGE_DOWN,ctrlKey:false,altKey:false,shiftKey:false,metaKey:false}, this, "_typematicCallback", this.timeoutChangeRate, this.defaultTimeout, this.minimumTimeout));
  101. }
  102. });
  103. }