_FormValueMixin.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. define("dijit/form/_FormValueMixin", [
  2. "dojo/_base/declare", // declare
  3. "dojo/dom-attr", // domAttr.set
  4. "dojo/keys", // keys.ESCAPE
  5. "dojo/_base/sniff", // has("ie"), has("quirks")
  6. "./_FormWidgetMixin"
  7. ], function(declare, domAttr, keys, has, _FormWidgetMixin){
  8. /*=====
  9. var _FormWidgetMixin = dijit.form._FormWidgetMixin;
  10. =====*/
  11. // module:
  12. // dijit/form/_FormValueMixin
  13. // summary:
  14. // Mixin for widgets corresponding to native HTML elements such as <input> or <select> that have user changeable values.
  15. return declare("dijit.form._FormValueMixin", _FormWidgetMixin, {
  16. // summary:
  17. // Mixin for widgets corresponding to native HTML elements such as <input> or <select> that have user changeable values.
  18. // description:
  19. // Each _FormValueMixin represents a single input value, and has a (possibly hidden) <input> element,
  20. // to which it serializes it's input value, so that form submission (either normal submission or via FormBind?)
  21. // works as expected.
  22. // readOnly: Boolean
  23. // Should this widget respond to user input?
  24. // In markup, this is specified as "readOnly".
  25. // Similar to disabled except readOnly form values are submitted.
  26. readOnly: false,
  27. _setReadOnlyAttr: function(/*Boolean*/ value){
  28. domAttr.set(this.focusNode, 'readOnly', value);
  29. this._set("readOnly", value);
  30. },
  31. postCreate: function(){
  32. this.inherited(arguments);
  33. if(has("ie")){ // IE won't stop the event with keypress
  34. this.connect(this.focusNode || this.domNode, "onkeydown", this._onKeyDown);
  35. }
  36. // Update our reset value if it hasn't yet been set (because this.set()
  37. // is only called when there *is* a value)
  38. if(this._resetValue === undefined){
  39. this._lastValueReported = this._resetValue = this.value;
  40. }
  41. },
  42. _setValueAttr: function(/*anything*/ newValue, /*Boolean?*/ priorityChange){
  43. // summary:
  44. // Hook so set('value', value) works.
  45. // description:
  46. // Sets the value of the widget.
  47. // If the value has changed, then fire onChange event, unless priorityChange
  48. // is specified as null (or false?)
  49. this._handleOnChange(newValue, priorityChange);
  50. },
  51. _handleOnChange: function(/*anything*/ newValue, /*Boolean?*/ priorityChange){
  52. // summary:
  53. // Called when the value of the widget has changed. Saves the new value in this.value,
  54. // and calls onChange() if appropriate. See _FormWidget._handleOnChange() for details.
  55. this._set("value", newValue);
  56. this.inherited(arguments);
  57. },
  58. undo: function(){
  59. // summary:
  60. // Restore the value to the last value passed to onChange
  61. this._setValueAttr(this._lastValueReported, false);
  62. },
  63. reset: function(){
  64. // summary:
  65. // Reset the widget's value to what it was at initialization time
  66. this._hasBeenBlurred = false;
  67. this._setValueAttr(this._resetValue, true);
  68. },
  69. _onKeyDown: function(e){
  70. if(e.keyCode == keys.ESCAPE && !(e.ctrlKey || e.altKey || e.metaKey)){
  71. var te;
  72. if(has("ie") < 9 || (has("ie") && has("quirks"))){
  73. e.preventDefault(); // default behavior needs to be stopped here since keypress is too late
  74. te = document.createEventObject();
  75. te.keyCode = keys.ESCAPE;
  76. te.shiftKey = e.shiftKey;
  77. e.srcElement.fireEvent('onkeypress', te);
  78. }
  79. }
  80. }
  81. });
  82. });