_Spinner.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. if (!dojo._hasResource["dijit.form._Spinner"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dijit.form._Spinner"] = true;
  6. dojo.provide("dijit.form._Spinner");
  7. dojo.require("dijit.form.ValidationTextBox");
  8. dojo.declare("dijit.form._Spinner", dijit.form.RangeBoundTextBox, {
  9. // summary: Mixin for validation widgets with a spinner
  10. // description: This class basically (conceptually) extends
  11. // dijit.form.ValidationTextBox.
  12. // It modifies the template to have up/down arrows, and provides related
  13. // handling code.
  14. // defaultTimeout: Number
  15. // number of milliseconds before a held key or button becomes typematic
  16. defaultTimeout : 500,
  17. // timeoutChangeRate: Number
  18. // fraction of time used to change the typematic timer between events
  19. // 1.0 means that each typematic event fires at defaultTimeout intervals
  20. // < 1.0 means that each typematic event fires at an increasing faster
  21. // rate
  22. timeoutChangeRate : 0.90,
  23. // smallDelta: Number
  24. // adjust the value by this much when spinning using the arrow
  25. // keys/buttons
  26. smallDelta : 1,
  27. // largeDelta: Number
  28. // adjust the value by this much when spinning using the PgUp/Dn keys
  29. largeDelta : 10,
  30. templateString : "<table class=\"dijit dijitReset dijitInlineTable dijitLeft\" cellspacing=\"0\" cellpadding=\"0\"\n\tid=\"widget_${id}\" name=\"${name}\"\n\tdojoAttachEvent=\"onmouseenter:_onMouse,onmouseleave:_onMouse,onkeypress:_onKeyPress\"\n\twaiRole=\"presentation\"\n\t><tr class=\"dijitReset\"\n\t\t><td rowspan=\"2\" class=\"dijitReset dijitStretch dijitInputField\" width=\"100%\"\n\t\t\t><input dojoAttachPoint=\"textbox,focusNode\" type=\"${type}\" dojoAttachEvent=\"onfocus,onkeyup\"\n\t\t\t\twaiRole=\"spinbutton\" autocomplete=\"off\" name=\"${name}\"\n\t\t></td\n\t\t><td rowspan=\"2\" class=\"dijitReset dijitValidationIconField\" width=\"0%\" \n\t\t\t><div dojoAttachPoint='iconNode' class='dijitValidationIcon'></div\n\t\t></td\n\t\t><td class=\"dijitReset dijitRight dijitButtonNode dijitUpArrowButton\" width=\"0%\"\n\t\t\t\tdojoAttachPoint=\"upArrowNode\"\n\t\t\t\tdojoAttachEvent=\"onmousedown:_handleUpArrowEvent,onmouseup:_handleUpArrowEvent,onmouseover:_handleUpArrowEvent,onmouseout:_handleUpArrowEvent\"\n\t\t\t\tstateModifier=\"UpArrow\"\n\t\t\t><div class=\"dijitA11yUpArrow\">&#9650;</div\n\t\t></td\n\t></tr\n\t><tr class=\"dijitReset\"\n\t\t><td class=\"dijitReset dijitRight dijitButtonNode dijitDownArrowButton\" width=\"0%\"\n\t\t\t\tdojoAttachPoint=\"downArrowNode\"\n\t\t\t\tdojoAttachEvent=\"onmousedown:_handleDownArrowEvent,onmouseup:_handleDownArrowEvent,onmouseover:_handleDownArrowEvent,onmouseout:_handleDownArrowEvent\"\n\t\t\t\tstateModifier=\"DownArrow\"\n\t\t\t><div class=\"dijitA11yDownArrow\">&#9660;</div\n\t\t></td\n\t></tr\n></table>\n\n",
  31. baseClass : "dijitSpinner",
  32. adjust : function(/* Object */val, /* Number */delta) {
  33. // summary: user replaceable function used to adjust a primitive
  34. // value(Number/Date/...) by the delta amount specified
  35. // the val is adjusted in a way that makes sense to the object type
  36. return val;
  37. },
  38. _handleUpArrowEvent : function(/* Event */e) {
  39. this._onMouse(e, this.upArrowNode);
  40. },
  41. _handleDownArrowEvent : function(/* Event */e) {
  42. this._onMouse(e, this.downArrowNode);
  43. },
  44. _arrowPressed : function(/* Node */nodePressed, /* Number */direction) {
  45. if (this.disabled) {
  46. return;
  47. }
  48. dojo.addClass(nodePressed, "dijitSpinnerButtonActive");
  49. this.setValue(this.adjust(this.getValue(), direction
  50. * this.smallDelta), false);
  51. },
  52. _arrowReleased : function(/* Node */node) {
  53. if (this.disabled) {
  54. return;
  55. }
  56. this._wheelTimer = null;
  57. dijit.focus(this.textbox);
  58. dojo.removeClass(node, "dijitSpinnerButtonActive");
  59. },
  60. _typematicCallback : function(/* Number */count, /* DOMNode */node, /* Event */
  61. evt) {
  62. if (node == this.textbox) {
  63. node = (evt.keyCode == dojo.keys.UP_ARROW)
  64. ? this.upArrowNode
  65. : this.downArrowNode;
  66. }
  67. if (count == -1) {
  68. this._arrowReleased(node);
  69. } else {
  70. this._arrowPressed(node, (node == this.upArrowNode) ? 1 : -1);
  71. }
  72. },
  73. _wheelTimer : null,
  74. _mouseWheeled : function(/* Event */evt) {
  75. dojo.stopEvent(evt);
  76. var scrollAmount = 0;
  77. if (typeof evt.wheelDelta == 'number') { // IE
  78. scrollAmount = evt.wheelDelta;
  79. } else if (typeof evt.detail == 'number') { // Mozilla+Firefox
  80. scrollAmount = -evt.detail;
  81. }
  82. if (scrollAmount > 0) {
  83. var node = this.upArrowNode;
  84. var dir = +1;
  85. } else if (scrollAmount < 0) {
  86. var node = this.downArrowNode;
  87. var dir = -1;
  88. } else {
  89. return;
  90. }
  91. this._arrowPressed(node, dir);
  92. if (this._wheelTimer != null) {
  93. clearTimeout(this._wheelTimer);
  94. }
  95. var _this = this;
  96. this._wheelTimer = setTimeout(function() {
  97. _this._arrowReleased(node);
  98. }, 50);
  99. },
  100. postCreate : function() {
  101. this.inherited('postCreate', arguments);
  102. // extra listeners
  103. this.connect(this.textbox, dojo.isIE
  104. ? "onmousewheel"
  105. : 'DOMMouseScroll', "_mouseWheeled");
  106. dijit.typematic.addListener(this.upArrowNode, this.textbox, {
  107. keyCode : dojo.keys.UP_ARROW,
  108. ctrlKey : false,
  109. altKey : false,
  110. shiftKey : false
  111. }, this, "_typematicCallback", this.timeoutChangeRate,
  112. this.defaultTimeout);
  113. dijit.typematic.addListener(this.downArrowNode, this.textbox, {
  114. keyCode : dojo.keys.DOWN_ARROW,
  115. ctrlKey : false,
  116. altKey : false,
  117. shiftKey : false
  118. }, this, "_typematicCallback", this.timeoutChangeRate,
  119. this.defaultTimeout);
  120. }
  121. });
  122. }