source: trunk/web/addons/job_monarch/lib/extjs/source/widgets/form/TextField.js @ 619

Last change on this file since 619 was 619, checked in by ramonb, 15 years ago

lib/:

  • added new AJAX dependancies: ExtJS, pChart, Lightbox2
File size: 14.5 KB
Line 
1/*
2 * Ext JS Library 2.2.1
3 * Copyright(c) 2006-2009, Ext JS, LLC.
4 * licensing@extjs.com
5 *
6 * http://extjs.com/license
7 */
8
9/**
10 * @class Ext.form.TextField
11 * @extends Ext.form.Field
12 * Basic text field.  Can be used as a direct replacement for traditional text inputs, or as the base
13 * class for more sophisticated input controls (like {@link Ext.form.TextArea} and {@link Ext.form.ComboBox}).
14 * @constructor
15 * Creates a new TextField
16 * @param {Object} config Configuration options
17 */
18Ext.form.TextField = Ext.extend(Ext.form.Field,  {
19    /**
20     * @cfg {String} vtypeText A custom error message to display in place of the default message provided
21     * for the {@link #vtype} currently set for this field (defaults to '').  Only applies if vtype is set, else ignored.
22     */
23    /**
24     * @cfg {RegExp} stripCharsRe A JavaScript RegExp object used to strip unwanted content from the value before validation (defaults to null).
25     */
26    /**
27     * @cfg {Boolean} grow True if this field should automatically grow and shrink to its content
28     */
29    grow : false,
30    /**
31     * @cfg {Number} growMin The minimum width to allow when grow = true (defaults to 30)
32     */
33    growMin : 30,
34    /**
35     * @cfg {Number} growMax The maximum width to allow when grow = true (defaults to 800)
36     */
37    growMax : 800,
38    /**
39     * @cfg {String} vtype A validation type name as defined in {@link Ext.form.VTypes} (defaults to null)
40     */
41    vtype : null,
42    /**
43     * @cfg {RegExp} maskRe An input mask regular expression that will be used to filter keystrokes that don't match
44     * (defaults to null)
45     */
46    maskRe : null,
47    /**
48     * @cfg {Boolean} disableKeyFilter True to disable input keystroke filtering (defaults to false)
49     */
50    disableKeyFilter : false,
51    /**
52     * @cfg {Boolean} allowBlank False to validate that the value length > 0 (defaults to true)
53     */
54    allowBlank : true,
55    /**
56     * @cfg {Number} minLength Minimum input field length required (defaults to 0)
57     */
58    minLength : 0,
59    /**
60     * @cfg {Number} maxLength Maximum input field length allowed (defaults to Number.MAX_VALUE)
61     */
62    maxLength : Number.MAX_VALUE,
63    /**
64     * @cfg {String} minLengthText Error text to display if the minimum length validation fails (defaults to
65     * "The minimum length for this field is {minLength}")
66     */
67    minLengthText : "The minimum length for this field is {0}",
68    /**
69     * @cfg {String} maxLengthText Error text to display if the maximum length validation fails (defaults to
70     * "The maximum length for this field is {maxLength}")
71     */
72    maxLengthText : "The maximum length for this field is {0}",
73    /**
74     * @cfg {Boolean} selectOnFocus True to automatically select any existing field text when the field receives
75     * input focus (defaults to false)
76     */
77    selectOnFocus : false,
78    /**
79     * @cfg {String} blankText Error text to display if the allow blank validation fails (defaults to "This field is required")
80     */
81    blankText : "This field is required",
82    /**
83     * @cfg {Function} validator A custom validation function to be called during field validation (defaults to null).
84     * If specified, this function will be called only after the built-in validations ({@link #allowBlank}, {@link #minLength},
85     * {@link #maxLength}) and any configured {@link #vtype} all return true. This function will be passed the current field
86     * value and expected to return boolean true if the value is valid or a string error message if invalid.
87     */
88    validator : null,
89    /**
90     * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation (defaults to null).
91     * If available, this regex will be evaluated only after the basic validators all return true, and will be passed the
92     * current field value.  If the test fails, the field will be marked invalid using {@link #regexText}.
93     */
94    regex : null,
95    /**
96     * @cfg {String} regexText The error text to display if {@link #regex} is used and the test fails during
97     * validation (defaults to "")
98     */
99    regexText : "",
100    /**
101     * @cfg {String} emptyText The default text to place into an empty field (defaults to null). Note that this
102     * value will be submitted to the server if this field is enabled and configured with a {@link #name}.
103     */
104    emptyText : null,
105    /**
106     * @cfg {String} emptyClass The CSS class to apply to an empty field to style the {@link #emptyText} (defaults to
107     * 'x-form-empty-field').  This class is automatically added and removed as needed depending on the current field value.
108     */
109    emptyClass : 'x-form-empty-field',
110
111    /**
112     * @cfg {Boolean} enableKeyEvents True to enable the proxying of key events for the HTML input field (defaults to false)
113     */
114
115    initComponent : function(){
116        Ext.form.TextField.superclass.initComponent.call(this);
117        this.addEvents(
118            /**
119             * @event autosize
120             * Fires when the autosize function is triggered.  The field may or may not have actually changed size
121             * according to the default logic, but this event provides a hook for the developer to apply additional
122             * logic at runtime to resize the field if needed.
123             * @param {Ext.form.Field} this This text field
124             * @param {Number} width The new field width
125             */
126            'autosize',
127
128            /**
129             * @event keydown
130             * Keydown input field event. This event only fires if enableKeyEvents is set to true.
131             * @param {Ext.form.TextField} this This text field
132             * @param {Ext.EventObject} e
133             */
134            'keydown',
135            /**
136             * @event keyup
137             * Keyup input field event. This event only fires if enableKeyEvents is set to true.
138             * @param {Ext.form.TextField} this This text field
139             * @param {Ext.EventObject} e
140             */
141            'keyup',
142            /**
143             * @event keypress
144             * Keypress input field event. This event only fires if enableKeyEvents is set to true.
145             * @param {Ext.form.TextField} this This text field
146             * @param {Ext.EventObject} e
147             */
148            'keypress'
149        );
150    },
151
152    // private
153    initEvents : function(){
154        Ext.form.TextField.superclass.initEvents.call(this);
155        if(this.validationEvent == 'keyup'){
156            this.validationTask = new Ext.util.DelayedTask(this.validate, this);
157            this.el.on('keyup', this.filterValidation, this);
158        }
159        else if(this.validationEvent !== false){
160            this.el.on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
161        }
162        if(this.selectOnFocus || this.emptyText){
163            this.on("focus", this.preFocus, this);
164            this.el.on('mousedown', function(){
165                if(!this.hasFocus){
166                    this.el.on('mouseup', function(e){
167                        e.preventDefault();
168                    }, this, {single:true});
169                }
170            }, this);
171            if(this.emptyText){
172                this.on('blur', this.postBlur, this);
173                this.applyEmptyText();
174            }
175        }
176        if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Ext.form.VTypes[this.vtype+'Mask']))){
177            this.el.on("keypress", this.filterKeys, this);
178        }
179        if(this.grow){
180            this.el.on("keyup", this.onKeyUpBuffered,  this, {buffer:50});
181            this.el.on("click", this.autoSize,  this);
182        }
183
184        if(this.enableKeyEvents){
185            this.el.on("keyup", this.onKeyUp, this);
186            this.el.on("keydown", this.onKeyDown, this);
187            this.el.on("keypress", this.onKeyPress, this);
188        }
189    },
190
191    processValue : function(value){
192        if(this.stripCharsRe){
193            var newValue = value.replace(this.stripCharsRe, '');
194            if(newValue !== value){
195                this.setRawValue(newValue);
196                return newValue;
197            }
198        }
199        return value;
200    },
201
202    filterValidation : function(e){
203        if(!e.isNavKeyPress()){
204            this.validationTask.delay(this.validationDelay);
205        }
206    },
207   
208    //private
209    onDisable: function(){
210        Ext.form.TextField.superclass.onDisable.call(this);
211        if(Ext.isIE){
212            this.el.dom.unselectable = 'on';
213        }
214    },
215   
216    //private
217    onEnable: function(){
218        Ext.form.TextField.superclass.onEnable.call(this);
219        if(Ext.isIE){
220            this.el.dom.unselectable = '';
221        }
222    },
223
224    // private
225    onKeyUpBuffered : function(e){
226        if(!e.isNavKeyPress()){
227            this.autoSize();
228        }
229    },
230
231    // private
232    onKeyUp : function(e){
233        this.fireEvent('keyup', this, e);
234    },
235
236    // private
237    onKeyDown : function(e){
238        this.fireEvent('keydown', this, e);
239    },
240
241    // private
242    onKeyPress : function(e){
243        this.fireEvent('keypress', this, e);
244    },
245
246    /**
247     * Resets the current field value to the originally-loaded value and clears any validation messages.
248     * Also adds emptyText and emptyClass if the original value was blank.
249     */
250    reset : function(){
251        Ext.form.TextField.superclass.reset.call(this);
252        this.applyEmptyText();
253    },
254
255    applyEmptyText : function(){
256        if(this.rendered && this.emptyText && this.getRawValue().length < 1 && !this.hasFocus){
257            this.setRawValue(this.emptyText);
258            this.el.addClass(this.emptyClass);
259        }
260    },
261
262    // private
263    preFocus : function(){
264        if(this.emptyText){
265            if(this.el.dom.value == this.emptyText){
266                this.setRawValue('');
267            }
268            this.el.removeClass(this.emptyClass);
269        }
270        if(this.selectOnFocus){
271            this.el.dom.select();
272        }
273    },
274
275    // private
276    postBlur : function(){
277        this.applyEmptyText();
278    },
279
280    // private
281    filterKeys : function(e){
282        if(e.ctrlKey){
283            return;
284        }
285        var k = e.getKey();
286        if(Ext.isGecko && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
287            return;
288        }
289        var c = e.getCharCode(), cc = String.fromCharCode(c);
290        if(!Ext.isGecko && e.isSpecialKey() && !cc){
291            return;
292        }
293        if(!this.maskRe.test(cc)){
294            e.stopEvent();
295        }
296    },
297
298    setValue : function(v){
299        if(this.emptyText && this.el && v !== undefined && v !== null && v !== ''){
300            this.el.removeClass(this.emptyClass);
301        }
302        Ext.form.TextField.superclass.setValue.apply(this, arguments);
303        this.applyEmptyText();
304        this.autoSize();
305    },
306
307    /**
308     * Validates a value according to the field's validation rules and marks the field as invalid
309     * if the validation fails
310     * @param {Mixed} value The value to validate
311     * @return {Boolean} True if the value is valid, else false
312     */
313    validateValue : function(value){
314        if(value.length < 1 || value === this.emptyText){ // if it's blank
315             if(this.allowBlank){
316                 this.clearInvalid();
317                 return true;
318             }else{
319                 this.markInvalid(this.blankText);
320                 return false;
321             }
322        }
323        if(value.length < this.minLength){
324            this.markInvalid(String.format(this.minLengthText, this.minLength));
325            return false;
326        }
327        if(value.length > this.maxLength){
328            this.markInvalid(String.format(this.maxLengthText, this.maxLength));
329            return false;
330        }
331        if(this.vtype){
332            var vt = Ext.form.VTypes;
333            if(!vt[this.vtype](value, this)){
334                this.markInvalid(this.vtypeText || vt[this.vtype +'Text']);
335                return false;
336            }
337        }
338        if(typeof this.validator == "function"){
339            var msg = this.validator(value);
340            if(msg !== true){
341                this.markInvalid(msg);
342                return false;
343            }
344        }
345        if(this.regex && !this.regex.test(value)){
346            this.markInvalid(this.regexText);
347            return false;
348        }
349        return true;
350    },
351
352    /**
353     * Selects text in this field
354     * @param {Number} start (optional) The index where the selection should start (defaults to 0)
355     * @param {Number} end (optional) The index where the selection should end (defaults to the text length)
356     */
357    selectText : function(start, end){
358        var v = this.getRawValue();
359        var doFocus = false;
360        if(v.length > 0){
361            start = start === undefined ? 0 : start;
362            end = end === undefined ? v.length : end;
363            var d = this.el.dom;
364            if(d.setSelectionRange){
365                d.setSelectionRange(start, end);
366            }else if(d.createTextRange){
367                var range = d.createTextRange();
368                range.moveStart("character", start);
369                range.moveEnd("character", end-v.length);
370                range.select();
371            }
372            doFocus = Ext.isGecko || Ext.isOpera;
373        }else{
374            doFocus = true;
375        }
376        if(doFocus){
377            this.focus();
378        }
379    },
380
381    /**
382     * Automatically grows the field to accomodate the width of the text up to the maximum field width allowed.
383     * This only takes effect if grow = true, and fires the {@link #autosize} event.
384     */
385    autoSize : function(){
386        if(!this.grow || !this.rendered){
387            return;
388        }
389        if(!this.metrics){
390            this.metrics = Ext.util.TextMetrics.createInstance(this.el);
391        }
392        var el = this.el;
393        var v = el.dom.value;
394        var d = document.createElement('div');
395        d.appendChild(document.createTextNode(v));
396        v = d.innerHTML;
397        Ext.removeNode(d);
398        d = null;
399        v += "&#160;";
400        var w = Math.min(this.growMax, Math.max(this.metrics.getWidth(v) + /* add extra padding */ 10, this.growMin));
401        this.el.setWidth(w);
402        this.fireEvent("autosize", this, w);
403    }
404});
405Ext.reg('textfield', Ext.form.TextField);
Note: See TracBrowser for help on using the repository browser.