TextBox.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. if (!dojo._hasResource["dijit.form.TextBox"]) { // _hasResource checks added by
  2. // build. Do not use
  3. // _hasResource directly in your
  4. // code.
  5. dojo._hasResource["dijit.form.TextBox"] = true;
  6. dojo.provide("dijit.form.TextBox");
  7. dojo.require("dijit.form._FormWidget");
  8. dojo.declare("dijit.form.TextBox", dijit.form._FormWidget, {
  9. // summary:
  10. // A generic textbox field.
  11. // Serves as a base class to derive more specialized functionality in
  12. // subclasses.
  13. // trim: Boolean
  14. // Removes leading and trailing whitespace if true. Default is false.
  15. trim : false,
  16. // uppercase: Boolean
  17. // Converts all characters to uppercase if true. Default is false.
  18. uppercase : false,
  19. // lowercase: Boolean
  20. // Converts all characters to lowercase if true. Default is false.
  21. lowercase : false,
  22. // propercase: Boolean
  23. // Converts the first character of each word to uppercase if true.
  24. propercase : false,
  25. // maxLength: String
  26. // HTML INPUT tag maxLength declaration.
  27. maxLength : "",
  28. templateString : "<input class=\"dojoTextBox\" dojoAttachPoint='textbox,focusNode' name=\"${name}\"\n\tdojoAttachEvent='onmouseenter:_onMouse,onmouseleave:_onMouse,onfocus:_onMouse,onblur:_onMouse,onkeyup,onkeypress:_onKeyPress'\n\tautocomplete=\"off\" type=\"${type}\"\n\t/>\n",
  29. baseClass : "dijitTextBox",
  30. attributeMap : dojo.mixin(dojo
  31. .clone(dijit.form._FormWidget.prototype.attributeMap),
  32. {
  33. maxLength : "focusNode"
  34. }),
  35. getDisplayedValue : function() {
  36. return this.filter(this.textbox.value);
  37. },
  38. getValue : function() {
  39. return this.parse(this.getDisplayedValue(), this.constraints);
  40. },
  41. setValue : function(value, /* Boolean, optional */priorityChange, /*
  42. * String,
  43. * optional
  44. */
  45. formattedValue) {
  46. var filteredValue = this.filter(value);
  47. if ((typeof filteredValue == typeof value)
  48. && (formattedValue == null || formattedValue == undefined)) {
  49. formattedValue = this.format(filteredValue, this.constraints);
  50. }
  51. if (formattedValue != null && formattedValue != undefined) {
  52. this.textbox.value = formattedValue;
  53. }
  54. dijit.form.TextBox.superclass.setValue.call(this, filteredValue,
  55. priorityChange);
  56. },
  57. setDisplayedValue : function(/* String */value) {
  58. this.textbox.value = value;
  59. this.setValue(this.getValue(), true);
  60. },
  61. forWaiValuenow : function() {
  62. return this.getDisplayedValue();
  63. },
  64. format : function(/* String */value, /* Object */constraints) {
  65. // summary: Replacable function to convert a value to a properly
  66. // formatted string
  67. return ((value == null || value == undefined)
  68. ? ""
  69. : (value.toString ? value.toString() : value));
  70. },
  71. parse : function(/* String */value, /* Object */constraints) {
  72. // summary: Replacable function to convert a formatted string to a
  73. // value
  74. return value;
  75. },
  76. postCreate : function() {
  77. // setting the value here is needed since value="" in the template
  78. // causes "undefined"
  79. // and setting in the DOM (instead of the JS object) helps with form
  80. // reset actions
  81. this.textbox.setAttribute("value", this.getDisplayedValue());
  82. this.inherited('postCreate', arguments);
  83. if (this.srcNodeRef) {
  84. dojo.style(this.textbox, "cssText", this.style);
  85. this.textbox.className += " " + this["class"];
  86. }
  87. this._layoutHack();
  88. },
  89. _layoutHack : function() {
  90. // summary: work around table sizing bugs on FF2 by forcing redraw
  91. if (dojo.isFF == 2 && this.domNode.tagName == "TABLE") {
  92. var node = this.domNode;
  93. var old = node.style.opacity;
  94. node.style.opacity = "0.999";
  95. setTimeout(function() {
  96. node.style.opacity = old;
  97. }, 0);
  98. }
  99. },
  100. filter : function(val) {
  101. // summary: Apply various filters to textbox value
  102. if (val == undefined || val == null) {
  103. return "";
  104. } else if (typeof val != "string") {
  105. return val;
  106. }
  107. if (this.trim) {
  108. val = dojo.trim(val);
  109. }
  110. if (this.uppercase) {
  111. val = val.toUpperCase();
  112. }
  113. if (this.lowercase) {
  114. val = val.toLowerCase();
  115. }
  116. if (this.propercase) {
  117. val = val.replace(/[^\s]+/g, function(word) {
  118. return word.substring(0, 1).toUpperCase()
  119. + word.substring(1);
  120. });
  121. }
  122. return val;
  123. },
  124. // event handlers, you can over-ride these in your own subclasses
  125. _onBlur : function() {
  126. this.setValue(this.getValue(), (this.isValid
  127. ? this.isValid()
  128. : true));
  129. },
  130. onkeyup : function() {
  131. // TODO: it would be nice to massage the value (ie: automatic
  132. // uppercase, etc) as the user types
  133. // but this messes up the cursor position if you are typing into the
  134. // middle of a word, and
  135. // also trimming doesn't work correctly (it prevents spaces between
  136. // words too!)
  137. // this.setValue(this.getValue());
  138. }
  139. });
  140. }