0c60ee702704b3f1ceae5d2716ab125ca9e38703.svn-base 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. Ext.form.TextField = Ext.extend(Ext.form.Field, {
  7. grow : false,
  8. growMin : 30,
  9. growMax : 800,
  10. vtype : null,
  11. maskRe : null,
  12. disableKeyFilter : false,
  13. allowBlank : true,
  14. minLength : 0,
  15. maxLength : Number.MAX_VALUE,
  16. minLengthText : "The minimum length for this field is {0}",
  17. maxLengthText : "The maximum length for this field is {0}",
  18. selectOnFocus : false,
  19. blankText : "This field is required",
  20. validator : null,
  21. regex : null,
  22. regexText : "",
  23. emptyText : null,
  24. emptyClass : "x-form-empty-field",
  25. initComponent : function() {
  26. Ext.form.TextField.superclass.initComponent.call(this);
  27. this.addEvents("autosize")
  28. },
  29. initEvents : function() {
  30. Ext.form.TextField.superclass.initEvents.call(this);
  31. if (this.validationEvent == "keyup") {
  32. this.validationTask = new Ext.util.DelayedTask(this.validate, this);
  33. this.el.on("keyup", this.filterValidation, this)
  34. } else {
  35. if (this.validationEvent !== false) {
  36. this.el.on(this.validationEvent, this.validate, this, {
  37. buffer : this.validationDelay
  38. })
  39. }
  40. }
  41. if (this.selectOnFocus || this.emptyText) {
  42. this.on("focus", this.preFocus, this);
  43. if (this.emptyText) {
  44. this.on("blur", this.postBlur, this);
  45. this.applyEmptyText()
  46. }
  47. }
  48. if (this.maskRe
  49. || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Ext.form.VTypes[this.vtype
  50. + "Mask"]))) {
  51. this.el.on("keypress", this.filterKeys, this)
  52. }
  53. if (this.grow) {
  54. this.el.on("keyup", this.onKeyUp, this, {
  55. buffer : 50
  56. });
  57. this.el.on("click", this.autoSize, this)
  58. }
  59. },
  60. processValue : function(A) {
  61. if (this.stripCharsRe) {
  62. var B = A.replace(this.stripCharsRe, "");
  63. if (B !== A) {
  64. this.setRawValue(B);
  65. return B
  66. }
  67. }
  68. return A
  69. },
  70. filterValidation : function(A) {
  71. if (!A.isNavKeyPress()) {
  72. this.validationTask.delay(this.validationDelay)
  73. }
  74. },
  75. onKeyUp : function(A) {
  76. if (!A.isNavKeyPress()) {
  77. this.autoSize()
  78. }
  79. },
  80. reset : function() {
  81. Ext.form.TextField.superclass.reset.call(this);
  82. this.applyEmptyText()
  83. },
  84. applyEmptyText : function() {
  85. if (this.rendered && this.emptyText && this.getRawValue().length < 1) {
  86. this.setRawValue(this.emptyText);
  87. this.el.addClass(this.emptyClass)
  88. }
  89. },
  90. preFocus : function() {
  91. if (this.emptyText) {
  92. if (this.el.dom.value == this.emptyText) {
  93. this.setRawValue("")
  94. }
  95. this.el.removeClass(this.emptyClass)
  96. }
  97. if (this.selectOnFocus) {
  98. this.el.dom.select()
  99. }
  100. },
  101. postBlur : function() {
  102. this.applyEmptyText()
  103. },
  104. filterKeys : function(B) {
  105. var A = B.getKey();
  106. if (!Ext.isIE
  107. && (B.isNavKeyPress() || A == B.BACKSPACE || (A == B.DELETE && B.button == -1))) {
  108. return
  109. }
  110. var D = B.getCharCode(), C = String.fromCharCode(D);
  111. if (Ext.isIE && (B.isSpecialKey() || !C)) {
  112. return
  113. }
  114. if (!this.maskRe.test(C)) {
  115. B.stopEvent()
  116. }
  117. },
  118. setValue : function(A) {
  119. if (this.emptyText && this.el && A !== undefined && A !== null
  120. && A !== "") {
  121. this.el.removeClass(this.emptyClass)
  122. }
  123. Ext.form.TextField.superclass.setValue.apply(this, arguments);
  124. this.applyEmptyText();
  125. this.autoSize()
  126. },
  127. validateValue : function(A) {
  128. if (A.length < 1 || A === this.emptyText) {
  129. if (this.allowBlank) {
  130. this.clearInvalid();
  131. return true
  132. } else {
  133. this.markInvalid(this.blankText);
  134. return false
  135. }
  136. }
  137. if (A.length < this.minLength) {
  138. this.markInvalid(String.format(this.minLengthText, this.minLength));
  139. return false
  140. }
  141. if (A.length > this.maxLength) {
  142. this.markInvalid(String.format(this.maxLengthText, this.maxLength));
  143. return false
  144. }
  145. if (this.vtype) {
  146. var C = Ext.form.VTypes;
  147. if (!C[this.vtype](A, this)) {
  148. this.markInvalid(this.vtypeText || C[this.vtype + "Text"]);
  149. return false
  150. }
  151. }
  152. if (typeof this.validator == "function") {
  153. var B = this.validator(A);
  154. if (B !== true) {
  155. this.markInvalid(B);
  156. return false
  157. }
  158. }
  159. if (this.regex && !this.regex.test(A)) {
  160. this.markInvalid(this.regexText);
  161. return false
  162. }
  163. return true
  164. },
  165. selectText : function(E, A) {
  166. var C = this.getRawValue();
  167. if (C.length > 0) {
  168. E = E === undefined ? 0 : E;
  169. A = A === undefined ? C.length : A;
  170. var D = this.el.dom;
  171. if (D.setSelectionRange) {
  172. D.setSelectionRange(E, A)
  173. } else {
  174. if (D.createTextRange) {
  175. var B = D.createTextRange();
  176. B.moveStart("character", E);
  177. B.moveEnd("character", C.length - A);
  178. B.select()
  179. }
  180. }
  181. }
  182. },
  183. autoSize : function() {
  184. if (!this.grow || !this.rendered) {
  185. return
  186. }
  187. if (!this.metrics) {
  188. this.metrics = Ext.util.TextMetrics.createInstance(this.el)
  189. }
  190. var C = this.el;
  191. var B = C.dom.value;
  192. var D = document.createElement("div");
  193. D.appendChild(document.createTextNode(B));
  194. B = D.innerHTML;
  195. D = null;
  196. B += "&#160;";
  197. var A = Math.min(this.growMax, Math.max(this.metrics.getWidth(B) + 10,
  198. this.growMin));
  199. this.el.setWidth(A);
  200. this.fireEvent("autosize", this, A)
  201. }
  202. });
  203. Ext.reg("textfield", Ext.form.TextField);