/javascripts/lib/docs/source/TextField.html

https://bitbucket.org/ksokmesa/sina-asian · HTML · 557 lines · 523 code · 34 blank · 0 comment · 0 complexity · 7b881602a356c51a60a68d85f024ecca MD5 · raw file

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>The source code</title>
  5. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  6. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  7. </head>
  8. <body onload="prettyPrint();">
  9. <pre class="prettyprint lang-js">/*!
  10. * Ext JS Library 3.2.1
  11. * Copyright(c) 2006-2010 Ext JS, Inc.
  12. * licensing@extjs.com
  13. * http://www.extjs.com/license
  14. */
  15. <div id="cls-Ext.form.TextField"></div>/**
  16. * @class Ext.form.TextField
  17. * @extends Ext.form.Field
  18. * <p>Basic text field. Can be used as a direct replacement for traditional text inputs,
  19. * or as the base class for more sophisticated input controls (like {@link Ext.form.TextArea}
  20. * and {@link Ext.form.ComboBox}).</p>
  21. * <p><b><u>Validation</u></b></p>
  22. * <p>The validation procedure is described in the documentation for {@link #validateValue}.</p>
  23. * <p><b><u>Alter Validation Behavior</u></b></p>
  24. * <p>Validation behavior for each field can be configured:</p>
  25. * <div class="mdetail-params"><ul>
  26. * <li><code>{@link Ext.form.TextField#invalidText invalidText}</code> : the default validation message to
  27. * show if any validation step above does not provide a message when invalid</li>
  28. * <li><code>{@link Ext.form.TextField#maskRe maskRe}</code> : filter out keystrokes before any validation occurs</li>
  29. * <li><code>{@link Ext.form.TextField#stripCharsRe stripCharsRe}</code> : filter characters after being typed in,
  30. * but before being validated</li>
  31. * <li><code>{@link Ext.form.Field#invalidClass invalidClass}</code> : alternate style when invalid</li>
  32. * <li><code>{@link Ext.form.Field#validateOnBlur validateOnBlur}</code>,
  33. * <code>{@link Ext.form.Field#validationDelay validationDelay}</code>, and
  34. * <code>{@link Ext.form.Field#validationEvent validationEvent}</code> : modify how/when validation is triggered</li>
  35. * </ul></div>
  36. *
  37. * @constructor Creates a new TextField
  38. * @param {Object} config Configuration options
  39. *
  40. * @xtype textfield
  41. */
  42. Ext.form.TextField = Ext.extend(Ext.form.Field, {
  43. <div id="cfg-Ext.form.TextField-vtypeText"></div>/**
  44. * @cfg {String} vtypeText A custom error message to display in place of the default message provided
  45. * for the <b><code>{@link #vtype}</code></b> currently set for this field (defaults to <tt>''</tt>). <b>Note</b>:
  46. * only applies if <b><code>{@link #vtype}</code></b> is set, else ignored.
  47. */
  48. <div id="cfg-Ext.form.TextField-stripCharsRe"></div>/**
  49. * @cfg {RegExp} stripCharsRe A JavaScript RegExp object used to strip unwanted content from the value
  50. * before validation (defaults to <tt>null</tt>).
  51. */
  52. <div id="cfg-Ext.form.TextField-grow"></div>/**
  53. * @cfg {Boolean} grow <tt>true</tt> if this field should automatically grow and shrink to its content
  54. * (defaults to <tt>false</tt>)
  55. */
  56. grow : false,
  57. <div id="cfg-Ext.form.TextField-growMin"></div>/**
  58. * @cfg {Number} growMin The minimum width to allow when <code><b>{@link #grow}</b> = true</code> (defaults
  59. * to <tt>30</tt>)
  60. */
  61. growMin : 30,
  62. <div id="cfg-Ext.form.TextField-growMax"></div>/**
  63. * @cfg {Number} growMax The maximum width to allow when <code><b>{@link #grow}</b> = true</code> (defaults
  64. * to <tt>800</tt>)
  65. */
  66. growMax : 800,
  67. <div id="cfg-Ext.form.TextField-vtype"></div>/**
  68. * @cfg {String} vtype A validation type name as defined in {@link Ext.form.VTypes} (defaults to <tt>null</tt>)
  69. */
  70. vtype : null,
  71. <div id="cfg-Ext.form.TextField-maskRe"></div>/**
  72. * @cfg {RegExp} maskRe An input mask regular expression that will be used to filter keystrokes that do
  73. * not match (defaults to <tt>null</tt>)
  74. */
  75. maskRe : null,
  76. <div id="cfg-Ext.form.TextField-disableKeyFilter"></div>/**
  77. * @cfg {Boolean} disableKeyFilter Specify <tt>true</tt> to disable input keystroke filtering (defaults
  78. * to <tt>false</tt>)
  79. */
  80. disableKeyFilter : false,
  81. <div id="cfg-Ext.form.TextField-allowBlank"></div>/**
  82. * @cfg {Boolean} allowBlank Specify <tt>false</tt> to validate that the value's length is > 0 (defaults to
  83. * <tt>true</tt>)
  84. */
  85. allowBlank : true,
  86. <div id="cfg-Ext.form.TextField-minLength"></div>/**
  87. * @cfg {Number} minLength Minimum input field length required (defaults to <tt>0</tt>)
  88. */
  89. minLength : 0,
  90. <div id="cfg-Ext.form.TextField-maxLength"></div>/**
  91. * @cfg {Number} maxLength Maximum input field length allowed by validation (defaults to Number.MAX_VALUE).
  92. * This behavior is intended to provide instant feedback to the user by improving usability to allow pasting
  93. * and editing or overtyping and back tracking. To restrict the maximum number of characters that can be
  94. * entered into the field use <tt><b>{@link Ext.form.Field#autoCreate autoCreate}</b></tt> to add
  95. * any attributes you want to a field, for example:<pre><code>
  96. var myField = new Ext.form.NumberField({
  97. id: 'mobile',
  98. anchor:'90%',
  99. fieldLabel: 'Mobile',
  100. maxLength: 16, // for validation
  101. autoCreate: {tag: 'input', type: 'text', size: '20', autocomplete: 'off', maxlength: '10'}
  102. });
  103. </code></pre>
  104. */
  105. maxLength : Number.MAX_VALUE,
  106. <div id="cfg-Ext.form.TextField-minLengthText"></div>/**
  107. * @cfg {String} minLengthText Error text to display if the <b><tt>{@link #minLength minimum length}</tt></b>
  108. * validation fails (defaults to <tt>'The minimum length for this field is {minLength}'</tt>)
  109. */
  110. minLengthText : 'The minimum length for this field is {0}',
  111. <div id="cfg-Ext.form.TextField-maxLengthText"></div>/**
  112. * @cfg {String} maxLengthText Error text to display if the <b><tt>{@link #maxLength maximum length}</tt></b>
  113. * validation fails (defaults to <tt>'The maximum length for this field is {maxLength}'</tt>)
  114. */
  115. maxLengthText : 'The maximum length for this field is {0}',
  116. <div id="cfg-Ext.form.TextField-selectOnFocus"></div>/**
  117. * @cfg {Boolean} selectOnFocus <tt>true</tt> to automatically select any existing field text when the field
  118. * receives input focus (defaults to <tt>false</tt>)
  119. */
  120. selectOnFocus : false,
  121. <div id="cfg-Ext.form.TextField-blankText"></div>/**
  122. * @cfg {String} blankText The error text to display if the <b><tt>{@link #allowBlank}</tt></b> validation
  123. * fails (defaults to <tt>'This field is required'</tt>)
  124. */
  125. blankText : 'This field is required',
  126. <div id="cfg-Ext.form.TextField-validator"></div>/**
  127. * @cfg {Function} validator
  128. * <p>A custom validation function to be called during field validation ({@link #validateValue})
  129. * (defaults to <tt>null</tt>). If specified, this function will be called first, allowing the
  130. * developer to override the default validation process.</p>
  131. * <br><p>This function will be passed the following Parameters:</p>
  132. * <div class="mdetail-params"><ul>
  133. * <li><code>value</code>: <i>Mixed</i>
  134. * <div class="sub-desc">The current field value</div></li>
  135. * </ul></div>
  136. * <br><p>This function is to Return:</p>
  137. * <div class="mdetail-params"><ul>
  138. * <li><code>true</code>: <i>Boolean</i>
  139. * <div class="sub-desc"><code>true</code> if the value is valid</div></li>
  140. * <li><code>msg</code>: <i>String</i>
  141. * <div class="sub-desc">An error message if the value is invalid</div></li>
  142. * </ul></div>
  143. */
  144. validator : null,
  145. <div id="cfg-Ext.form.TextField-regex"></div>/**
  146. * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation
  147. * (defaults to <tt>null</tt>). If the test fails, the field will be marked invalid using
  148. * <b><tt>{@link #regexText}</tt></b>.
  149. */
  150. regex : null,
  151. <div id="cfg-Ext.form.TextField-regexText"></div>/**
  152. * @cfg {String} regexText The error text to display if <b><tt>{@link #regex}</tt></b> is used and the
  153. * test fails during validation (defaults to <tt>''</tt>)
  154. */
  155. regexText : '',
  156. <div id="cfg-Ext.form.TextField-emptyText"></div>/**
  157. * @cfg {String} emptyText The default text to place into an empty field (defaults to <tt>null</tt>).
  158. * <b>Note</b>: that this value will be submitted to the server if this field is enabled and configured
  159. * with a {@link #name}.
  160. */
  161. emptyText : null,
  162. <div id="cfg-Ext.form.TextField-emptyClass"></div>/**
  163. * @cfg {String} emptyClass The CSS class to apply to an empty field to style the <b><tt>{@link #emptyText}</tt></b>
  164. * (defaults to <tt>'x-form-empty-field'</tt>). This class is automatically added and removed as needed
  165. * depending on the current field value.
  166. */
  167. emptyClass : 'x-form-empty-field',
  168. <div id="cfg-Ext.form.TextField-enableKeyEvents"></div>/**
  169. * @cfg {Boolean} enableKeyEvents <tt>true</tt> to enable the proxying of key events for the HTML input
  170. * field (defaults to <tt>false</tt>)
  171. */
  172. initComponent : function(){
  173. Ext.form.TextField.superclass.initComponent.call(this);
  174. this.addEvents(
  175. <div id="event-Ext.form.TextField-autosize"></div>/**
  176. * @event autosize
  177. * Fires when the <tt><b>{@link #autoSize}</b></tt> function is triggered. The field may or
  178. * may not have actually changed size according to the default logic, but this event provides
  179. * a hook for the developer to apply additional logic at runtime to resize the field if needed.
  180. * @param {Ext.form.Field} this This text field
  181. * @param {Number} width The new field width
  182. */
  183. 'autosize',
  184. <div id="event-Ext.form.TextField-keydown"></div>/**
  185. * @event keydown
  186. * Keydown input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
  187. * is set to true.
  188. * @param {Ext.form.TextField} this This text field
  189. * @param {Ext.EventObject} e
  190. */
  191. 'keydown',
  192. <div id="event-Ext.form.TextField-keyup"></div>/**
  193. * @event keyup
  194. * Keyup input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
  195. * is set to true.
  196. * @param {Ext.form.TextField} this This text field
  197. * @param {Ext.EventObject} e
  198. */
  199. 'keyup',
  200. <div id="event-Ext.form.TextField-keypress"></div>/**
  201. * @event keypress
  202. * Keypress input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
  203. * is set to true.
  204. * @param {Ext.form.TextField} this This text field
  205. * @param {Ext.EventObject} e
  206. */
  207. 'keypress'
  208. );
  209. },
  210. // private
  211. initEvents : function(){
  212. Ext.form.TextField.superclass.initEvents.call(this);
  213. if(this.validationEvent == 'keyup'){
  214. this.validationTask = new Ext.util.DelayedTask(this.validate, this);
  215. this.mon(this.el, 'keyup', this.filterValidation, this);
  216. }
  217. else if(this.validationEvent !== false && this.validationEvent != 'blur'){
  218. this.mon(this.el, this.validationEvent, this.validate, this, {buffer: this.validationDelay});
  219. }
  220. if(this.selectOnFocus || this.emptyText){
  221. this.mon(this.el, 'mousedown', this.onMouseDown, this);
  222. if(this.emptyText){
  223. this.applyEmptyText();
  224. }
  225. }
  226. if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Ext.form.VTypes[this.vtype+'Mask']))){
  227. this.mon(this.el, 'keypress', this.filterKeys, this);
  228. }
  229. if(this.grow){
  230. this.mon(this.el, 'keyup', this.onKeyUpBuffered, this, {buffer: 50});
  231. this.mon(this.el, 'click', this.autoSize, this);
  232. }
  233. if(this.enableKeyEvents){
  234. this.mon(this.el, {
  235. scope: this,
  236. keyup: this.onKeyUp,
  237. keydown: this.onKeyDown,
  238. keypress: this.onKeyPress
  239. });
  240. }
  241. },
  242. onMouseDown: function(e){
  243. if(!this.hasFocus){
  244. this.mon(this.el, 'mouseup', Ext.emptyFn, this, { single: true, preventDefault: true });
  245. }
  246. },
  247. processValue : function(value){
  248. if(this.stripCharsRe){
  249. var newValue = value.replace(this.stripCharsRe, '');
  250. if(newValue !== value){
  251. this.setRawValue(newValue);
  252. return newValue;
  253. }
  254. }
  255. return value;
  256. },
  257. filterValidation : function(e){
  258. if(!e.isNavKeyPress()){
  259. this.validationTask.delay(this.validationDelay);
  260. }
  261. },
  262. //private
  263. onDisable: function(){
  264. Ext.form.TextField.superclass.onDisable.call(this);
  265. if(Ext.isIE){
  266. this.el.dom.unselectable = 'on';
  267. }
  268. },
  269. //private
  270. onEnable: function(){
  271. Ext.form.TextField.superclass.onEnable.call(this);
  272. if(Ext.isIE){
  273. this.el.dom.unselectable = '';
  274. }
  275. },
  276. // private
  277. onKeyUpBuffered : function(e){
  278. if(this.doAutoSize(e)){
  279. this.autoSize();
  280. }
  281. },
  282. // private
  283. doAutoSize : function(e){
  284. return !e.isNavKeyPress();
  285. },
  286. // private
  287. onKeyUp : function(e){
  288. this.fireEvent('keyup', this, e);
  289. },
  290. // private
  291. onKeyDown : function(e){
  292. this.fireEvent('keydown', this, e);
  293. },
  294. // private
  295. onKeyPress : function(e){
  296. this.fireEvent('keypress', this, e);
  297. },
  298. <div id="method-Ext.form.TextField-reset"></div>/**
  299. * Resets the current field value to the originally-loaded value and clears any validation messages.
  300. * Also adds <tt><b>{@link #emptyText}</b></tt> and <tt><b>{@link #emptyClass}</b></tt> if the
  301. * original value was blank.
  302. */
  303. reset : function(){
  304. Ext.form.TextField.superclass.reset.call(this);
  305. this.applyEmptyText();
  306. },
  307. applyEmptyText : function(){
  308. if(this.rendered && this.emptyText && this.getRawValue().length < 1 && !this.hasFocus){
  309. this.setRawValue(this.emptyText);
  310. this.el.addClass(this.emptyClass);
  311. }
  312. },
  313. // private
  314. preFocus : function(){
  315. var el = this.el;
  316. if(this.emptyText){
  317. if(el.dom.value == this.emptyText){
  318. this.setRawValue('');
  319. }
  320. el.removeClass(this.emptyClass);
  321. }
  322. if(this.selectOnFocus){
  323. el.dom.select();
  324. }
  325. },
  326. // private
  327. postBlur : function(){
  328. this.applyEmptyText();
  329. },
  330. // private
  331. filterKeys : function(e){
  332. if(e.ctrlKey){
  333. return;
  334. }
  335. var k = e.getKey();
  336. if(Ext.isGecko && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
  337. return;
  338. }
  339. var cc = String.fromCharCode(e.getCharCode());
  340. if(!Ext.isGecko && e.isSpecialKey() && !cc){
  341. return;
  342. }
  343. if(!this.maskRe.test(cc)){
  344. e.stopEvent();
  345. }
  346. },
  347. setValue : function(v){
  348. if(this.emptyText && this.el && !Ext.isEmpty(v)){
  349. this.el.removeClass(this.emptyClass);
  350. }
  351. Ext.form.TextField.superclass.setValue.apply(this, arguments);
  352. this.applyEmptyText();
  353. this.autoSize();
  354. return this;
  355. },
  356. <div id="method-Ext.form.TextField-getErrors"></div>/**
  357. * <p>Validates a value according to the field's validation rules and returns an array of errors
  358. * for any failing validations. Validation rules are processed in the following order:</p>
  359. * <div class="mdetail-params"><ul>
  360. *
  361. * <li><b>1. Field specific validator</b>
  362. * <div class="sub-desc">
  363. * <p>A validator offers a way to customize and reuse a validation specification.
  364. * If a field is configured with a <code>{@link #validator}</code>
  365. * function, it will be passed the current field value. The <code>{@link #validator}</code>
  366. * function is expected to return either:
  367. * <div class="mdetail-params"><ul>
  368. * <li>Boolean <tt>true</tt> if the value is valid (validation continues).</li>
  369. * <li>a String to represent the invalid message if invalid (validation halts).</li>
  370. * </ul></div>
  371. * </div></li>
  372. *
  373. * <li><b>2. Basic Validation</b>
  374. * <div class="sub-desc">
  375. * <p>If the <code>{@link #validator}</code> has not halted validation,
  376. * basic validation proceeds as follows:</p>
  377. *
  378. * <div class="mdetail-params"><ul>
  379. *
  380. * <li><code>{@link #allowBlank}</code> : (Invalid message =
  381. * <code>{@link #emptyText}</code>)<div class="sub-desc">
  382. * Depending on the configuration of <code>{@link #allowBlank}</code>, a
  383. * blank field will cause validation to halt at this step and return
  384. * Boolean true or false accordingly.
  385. * </div></li>
  386. *
  387. * <li><code>{@link #minLength}</code> : (Invalid message =
  388. * <code>{@link #minLengthText}</code>)<div class="sub-desc">
  389. * If the passed value does not satisfy the <code>{@link #minLength}</code>
  390. * specified, validation halts.
  391. * </div></li>
  392. *
  393. * <li><code>{@link #maxLength}</code> : (Invalid message =
  394. * <code>{@link #maxLengthText}</code>)<div class="sub-desc">
  395. * If the passed value does not satisfy the <code>{@link #maxLength}</code>
  396. * specified, validation halts.
  397. * </div></li>
  398. *
  399. * </ul></div>
  400. * </div></li>
  401. *
  402. * <li><b>3. Preconfigured Validation Types (VTypes)</b>
  403. * <div class="sub-desc">
  404. * <p>If none of the prior validation steps halts validation, a field
  405. * configured with a <code>{@link #vtype}</code> will utilize the
  406. * corresponding {@link Ext.form.VTypes VTypes} validation function.
  407. * If invalid, either the field's <code>{@link #vtypeText}</code> or
  408. * the VTypes vtype Text property will be used for the invalid message.
  409. * Keystrokes on the field will be filtered according to the VTypes
  410. * vtype Mask property.</p>
  411. * </div></li>
  412. *
  413. * <li><b>4. Field specific regex test</b>
  414. * <div class="sub-desc">
  415. * <p>If none of the prior validation steps halts validation, a field's
  416. * configured <code>{@link #regex}</code> test will be processed.
  417. * The invalid message for this test is configured with
  418. * <code>{@link #regexText}</code>.</p>
  419. * </div></li>
  420. *
  421. * @param {Mixed} value The value to validate. The processed raw value will be used if nothing is passed
  422. * @return {Array} Array of any validation errors
  423. */
  424. getErrors: function(value) {
  425. var errors = Ext.form.TextField.superclass.getErrors.apply(this, arguments);
  426. value = value || this.processValue(this.getRawValue());
  427. if (Ext.isFunction(this.validator)) {
  428. var msg = this.validator(value);
  429. if (msg !== true) {
  430. errors.push(msg);
  431. }
  432. }
  433. if (value.length < 1 || value === this.emptyText) {
  434. if (this.allowBlank) {
  435. //if value is blank and allowBlank is true, there cannot be any additional errors
  436. return errors;
  437. } else {
  438. errors.push(this.blankText);
  439. }
  440. }
  441. if (!this.allowBlank && (value.length < 1 || value === this.emptyText)) { // if it's blank
  442. errors.push(this.blankText);
  443. }
  444. if (value.length < this.minLength) {
  445. errors.push(String.format(this.minLengthText, this.minLength));
  446. }
  447. if (value.length > this.maxLength) {
  448. errors.push(String.format(this.maxLengthText, this.maxLength));
  449. }
  450. if (this.vtype) {
  451. var vt = Ext.form.VTypes;
  452. if(!vt[this.vtype](value, this)){
  453. errors.push(this.vtypeText || vt[this.vtype +'Text']);
  454. }
  455. }
  456. if (this.regex && !this.regex.test(value)) {
  457. errors.push(this.regexText);
  458. }
  459. return errors;
  460. },
  461. <div id="method-Ext.form.TextField-selectText"></div>/**
  462. * Selects text in this field
  463. * @param {Number} start (optional) The index where the selection should start (defaults to 0)
  464. * @param {Number} end (optional) The index where the selection should end (defaults to the text length)
  465. */
  466. selectText : function(start, end){
  467. var v = this.getRawValue();
  468. var doFocus = false;
  469. if(v.length > 0){
  470. start = start === undefined ? 0 : start;
  471. end = end === undefined ? v.length : end;
  472. var d = this.el.dom;
  473. if(d.setSelectionRange){
  474. d.setSelectionRange(start, end);
  475. }else if(d.createTextRange){
  476. var range = d.createTextRange();
  477. range.moveStart('character', start);
  478. range.moveEnd('character', end-v.length);
  479. range.select();
  480. }
  481. doFocus = Ext.isGecko || Ext.isOpera;
  482. }else{
  483. doFocus = true;
  484. }
  485. if(doFocus){
  486. this.focus();
  487. }
  488. },
  489. <div id="method-Ext.form.TextField-autoSize"></div>/**
  490. * Automatically grows the field to accomodate the width of the text up to the maximum field width allowed.
  491. * This only takes effect if <tt><b>{@link #grow}</b> = true</tt>, and fires the {@link #autosize} event.
  492. */
  493. autoSize : function(){
  494. if(!this.grow || !this.rendered){
  495. return;
  496. }
  497. if(!this.metrics){
  498. this.metrics = Ext.util.TextMetrics.createInstance(this.el);
  499. }
  500. var el = this.el;
  501. var v = el.dom.value;
  502. var d = document.createElement('div');
  503. d.appendChild(document.createTextNode(v));
  504. v = d.innerHTML;
  505. Ext.removeNode(d);
  506. d = null;
  507. v += '&#160;';
  508. var w = Math.min(this.growMax, Math.max(this.metrics.getWidth(v) + /* add extra padding */ 10, this.growMin));
  509. this.el.setWidth(w);
  510. this.fireEvent('autosize', this, w);
  511. },
  512. onDestroy: function(){
  513. if(this.validationTask){
  514. this.validationTask.cancel();
  515. this.validationTask = null;
  516. }
  517. Ext.form.TextField.superclass.onDestroy.call(this);
  518. }
  519. });
  520. Ext.reg('textfield', Ext.form.TextField);
  521. </pre>
  522. </body>
  523. </html>