source: trunk/web/addons/job_monarch/lib/extjs/source/widgets/form/Field.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: 22.9 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.Field
11 * @extends Ext.BoxComponent
12 * Base class for form fields that provides default event handling, sizing, value handling and other functionality.
13 * @constructor
14 * Creates a new Field
15 * @param {Object} config Configuration options
16 */
17Ext.form.Field = Ext.extend(Ext.BoxComponent,  {
18    /**
19     * @cfg {String} fieldLabel The label text to display next to this field (defaults to '')
20     * <p><b>A Field's label is not by default rendered as part of the Field's structure.
21     * The label is rendered by the {@link Ext.layout.FormLayout form layout} layout manager
22     * of the {@link Ext.form.Container Container} to which the Field is added.</b></p>
23     */
24    /**
25     * @cfg {String} labelStyle A CSS style specification to apply directly to this field's label (defaults to the
26     * container's labelStyle value if set, or ''). For example, <code>labelStyle: 'font-weight:bold;'</code>.
27     */
28    /**
29     * @cfg {String} labelSeparator The standard separator to display after the text of each form label (defaults
30     * to the value of {@link Ext.layout.FormLayout#labelSeparator}, which is a colon ':' by default).  To display
31     * no separator for this field's label specify empty string ''.
32     */
33    /**
34     * @cfg {Boolean} hideLabel True to completely hide the label element (defaults to false)
35     */
36    /**
37     * @cfg {String} clearCls The CSS class used to provide field clearing (defaults to 'x-form-clear-left')
38     */
39    /**
40     * @cfg {String} itemCls An additional CSS class to apply to the wrapper's form item element of this field (defaults
41     * to the container's itemCls value if set, or '').  Since it is applied to the item wrapper, it allows you to write
42     * standard CSS rules that can apply to the field, the label (if specified) or any other element within the markup for
43     * the field. NOTE: this will not have any effect on fields that are not part of a form. Example use:
44     * <pre><code>
45// Apply a style to the field's label:
46&lt;style>
47    .required .x-form-item-label {font-weight:bold;color:red;}
48&lt;/style>
49
50new Ext.FormPanel({
51        height: 100,
52        renderTo: document.body,
53        items: [{
54                xtype: 'textfield',
55                fieldLabel: 'Name',
56                itemCls: 'required' //this label will be styled
57        },{
58                xtype: 'textfield',
59                fieldLabel: 'Favorite Color'
60        }]
61});
62</code></pre>
63     */
64    /**
65     * @cfg {String} inputType The type attribute for input fields -- e.g. radio, text, password, file (defaults
66     * to "text"). The types "file" and "password" must be used to render those field types currently -- there are
67     * no separate Ext components for those. Note that if you use <tt>inputType:'file'</tt>, {@link #emptyText}
68     * is not supported and should be avoided.
69     */
70    /**
71     * @cfg {Number} tabIndex The tabIndex for this field. Note this only applies to fields that are rendered,
72     * not those which are built via applyTo (defaults to undefined).
73     */
74    /**
75     * @cfg {Mixed} value A value to initialize this field with (defaults to undefined).
76     */
77    /**
78     * @cfg {String} name The field's HTML name attribute (defaults to "").
79     */
80    /**
81     * @cfg {String} cls A custom CSS class to apply to the field's underlying element (defaults to "").
82     */
83
84    /**
85     * @cfg {String} invalidClass The CSS class to use when marking a field invalid (defaults to "x-form-invalid")
86     */
87    invalidClass : "x-form-invalid",
88    /**
89     * @cfg {String} invalidText The error text to use when marking a field invalid and no message is provided
90     * (defaults to "The value in this field is invalid")
91     */
92    invalidText : "The value in this field is invalid",
93    /**
94     * @cfg {String} focusClass The CSS class to use when the field receives focus (defaults to "x-form-focus")
95     */
96    focusClass : "x-form-focus",
97    /**
98     * @cfg {String/Boolean} validationEvent The event that should initiate field validation. Set to false to disable
99      automatic validation (defaults to "keyup").
100     */
101    validationEvent : "keyup",
102    /**
103     * @cfg {Boolean} validateOnBlur Whether the field should validate when it loses focus (defaults to true).
104     */
105    validateOnBlur : true,
106    /**
107     * @cfg {Number} validationDelay The length of time in milliseconds after user input begins until validation
108     * is initiated (defaults to 250)
109     */
110    validationDelay : 250,
111    /**
112     * @cfg {String/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to
113     * {tag: "input", type: "text", size: "20", autocomplete: "off"})
114     */
115    defaultAutoCreate : {tag: "input", type: "text", size: "20", autocomplete: "off"},
116    /**
117     * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field")
118     */
119    fieldClass : "x-form-field",
120    /**
121     * @cfg {String} msgTarget The location where error text should display.  Should be one of the following values
122     * (defaults to 'qtip'):
123     *<pre>
124Value         Description
125-----------   ----------------------------------------------------------------------
126qtip          Display a quick tip when the user hovers over the field
127title         Display a default browser title attribute popup
128under         Add a block div beneath the field containing the error text
129side          Add an error icon to the right of the field with a popup on hover
130[element id]  Add the error text directly to the innerHTML of the specified element
131</pre>
132     */
133    msgTarget : 'qtip',
134    /**
135     * @cfg {String} msgFx <b>Experimental</b> The effect used when displaying a validation message under the field
136     * (defaults to 'normal').
137     */
138    msgFx : 'normal',
139    /**
140     * @cfg {Boolean} readOnly True to mark the field as readOnly in HTML (defaults to false) -- Note: this only
141     * sets the element's readOnly DOM attribute.
142     */
143    readOnly : false,
144    /**
145     * @cfg {Boolean} disabled True to disable the field (defaults to false).
146     * <p>Be aware that conformant with the <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.12.1">HTML specification</a>,
147     * disabled Fields will not be {@link Ext.form.BasicForm#submit submitted}.</p>
148     */
149    disabled : false,
150
151    // private
152    isFormField : true,
153
154    // private
155    hasFocus : false,
156
157        // private
158        initComponent : function(){
159        Ext.form.Field.superclass.initComponent.call(this);
160        this.addEvents(
161            /**
162             * @event focus
163             * Fires when this field receives input focus.
164             * @param {Ext.form.Field} this
165             */
166            'focus',
167            /**
168             * @event blur
169             * Fires when this field loses input focus.
170             * @param {Ext.form.Field} this
171             */
172            'blur',
173            /**
174             * @event specialkey
175             * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed.  You can check
176             * {@link Ext.EventObject#getKey} to determine which key was pressed.
177             * @param {Ext.form.Field} this
178             * @param {Ext.EventObject} e The event object
179             */
180            'specialkey',
181            /**
182             * @event change
183             * Fires just before the field blurs if the field value has changed.
184             * @param {Ext.form.Field} this
185             * @param {Mixed} newValue The new value
186             * @param {Mixed} oldValue The original value
187             */
188            'change',
189            /**
190             * @event invalid
191             * Fires after the field has been marked as invalid.
192             * @param {Ext.form.Field} this
193             * @param {String} msg The validation message
194             */
195            'invalid',
196            /**
197             * @event valid
198             * Fires after the field has been validated with no errors.
199             * @param {Ext.form.Field} this
200             */
201            'valid'
202        );
203    },
204
205    /**
206     * Returns the name attribute of the field if available
207     * @return {String} name The field name
208     */
209    getName: function(){
210         return this.rendered && this.el.dom.name ? this.el.dom.name : (this.hiddenName || '');
211    },
212
213    // private
214    onRender : function(ct, position){
215        Ext.form.Field.superclass.onRender.call(this, ct, position);
216        if(!this.el){
217            var cfg = this.getAutoCreate();
218            if(!cfg.name){
219                cfg.name = this.name || this.id;
220            }
221            if(this.inputType){
222                cfg.type = this.inputType;
223            }
224            this.el = ct.createChild(cfg, position);
225        }
226        var type = this.el.dom.type;
227        if(type){
228            if(type == 'password'){
229                type = 'text';
230            }
231            this.el.addClass('x-form-'+type);
232        }
233        if(this.readOnly){
234            this.el.dom.readOnly = true;
235        }
236        if(this.tabIndex !== undefined){
237            this.el.dom.setAttribute('tabIndex', this.tabIndex);
238        }
239
240        this.el.addClass([this.fieldClass, this.cls]);
241    },
242
243    // private
244    initValue : function(){
245        if(this.value !== undefined){
246            this.setValue(this.value);
247        }else if(this.el.dom.value.length > 0 && this.el.dom.value != this.emptyText){
248            this.setValue(this.el.dom.value);
249        }
250        // reference to original value for reset
251        this.originalValue = this.getValue();
252    },
253
254    /**
255     * Returns true if this field has been changed since it was originally loaded and is not disabled.
256     */
257    isDirty : function() {
258        if(this.disabled) {
259            return false;
260        }
261        return String(this.getValue()) !== String(this.originalValue);
262    },
263
264    // private
265    afterRender : function(){
266        Ext.form.Field.superclass.afterRender.call(this);
267        this.initEvents();
268        this.initValue();
269    },
270
271    // private
272    fireKey : function(e){
273        if(e.isSpecialKey()){
274            this.fireEvent("specialkey", this, e);
275        }
276    },
277
278    /**
279     * Resets the current field value to the originally loaded value and clears any validation messages
280     */
281    reset : function(){
282        this.setValue(this.originalValue);
283        this.clearInvalid();
284    },
285
286    // private
287    initEvents : function(){
288        this.el.on(Ext.isIE || Ext.isSafari3 ? "keydown" : "keypress", this.fireKey,  this);
289        this.el.on("focus", this.onFocus,  this);
290
291        // fix weird FF/Win editor issue when changing OS window focus
292        var o = this.inEditor && Ext.isWindows && Ext.isGecko ? {buffer:10} : null;
293        this.el.on("blur", this.onBlur,  this, o);
294    },
295
296    // private
297    onFocus : function(){
298        if(this.focusClass){
299            this.el.addClass(this.focusClass);
300        }
301        if(!this.hasFocus){
302            this.hasFocus = true;
303            this.startValue = this.getValue();
304            this.fireEvent("focus", this);
305        }
306    },
307
308    // private
309    beforeBlur : Ext.emptyFn,
310
311    // private
312    onBlur : function(){
313        this.beforeBlur();
314        if(this.focusClass){
315            this.el.removeClass(this.focusClass);
316        }
317        this.hasFocus = false;
318        if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
319            this.validate();
320        }
321        var v = this.getValue();
322        if(String(v) !== String(this.startValue)){
323            this.fireEvent('change', this, v, this.startValue);
324        }
325        this.fireEvent("blur", this);
326    },
327
328    /**
329     * Returns whether or not the field value is currently valid
330     * @param {Boolean} preventMark True to disable marking the field invalid
331     * @return {Boolean} True if the value is valid, else false
332     */
333    isValid : function(preventMark){
334        if(this.disabled){
335            return true;
336        }
337        var restore = this.preventMark;
338        this.preventMark = preventMark === true;
339        var v = this.validateValue(this.processValue(this.getRawValue()));
340        this.preventMark = restore;
341        return v;
342    },
343
344    /**
345     * Validates the field value
346     * @return {Boolean} True if the value is valid, else false
347     */
348    validate : function(){
349        if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
350            this.clearInvalid();
351            return true;
352        }
353        return false;
354    },
355
356    // protected - should be overridden by subclasses if necessary to prepare raw values for validation
357    processValue : function(value){
358        return value;
359    },
360
361    // private
362    // Subclasses should provide the validation implementation by overriding this
363    validateValue : function(value){
364        return true;
365    },
366
367    /**
368     * Mark this field as invalid, using {@link #msgTarget} to determine how to display the error and
369     * applying {@link #invalidClass} to the field's element.
370     * @param {String} msg (optional) The validation message (defaults to {@link #invalidText})
371     */
372    markInvalid : function(msg){
373        if(!this.rendered || this.preventMark){ // not rendered
374            return;
375        }
376        this.el.addClass(this.invalidClass);
377        msg = msg || this.invalidText;
378
379        switch(this.msgTarget){
380            case 'qtip':
381                this.el.dom.qtip = msg;
382                this.el.dom.qclass = 'x-form-invalid-tip';
383                if(Ext.QuickTips){ // fix for floating editors interacting with DND
384                    Ext.QuickTips.enable();
385                }
386                break;
387            case 'title':
388                this.el.dom.title = msg;
389                break;
390            case 'under':
391                if(!this.errorEl){
392                    var elp = this.getErrorCt();
393                    if(!elp){ // field has no container el
394                        this.el.dom.title = msg;
395                        break;
396                    }
397                    this.errorEl = elp.createChild({cls:'x-form-invalid-msg'});
398                    this.errorEl.setWidth(elp.getWidth(true)-20);
399                }
400                this.errorEl.update(msg);
401                Ext.form.Field.msgFx[this.msgFx].show(this.errorEl, this);
402                break;
403            case 'side':
404                if(!this.errorIcon){
405                    var elp = this.getErrorCt();
406                    if(!elp){ // field has no container el
407                        this.el.dom.title = msg;
408                        break;
409                    }
410                    this.errorIcon = elp.createChild({cls:'x-form-invalid-icon'});
411                }
412                this.alignErrorIcon();
413                this.errorIcon.dom.qtip = msg;
414                this.errorIcon.dom.qclass = 'x-form-invalid-tip';
415                this.errorIcon.show();
416                this.on('resize', this.alignErrorIcon, this);
417                break;
418            default:
419                var t = Ext.getDom(this.msgTarget);
420                t.innerHTML = msg;
421                t.style.display = this.msgDisplay;
422                break;
423        }
424        this.fireEvent('invalid', this, msg);
425    },
426
427    // private
428    getErrorCt : function(){
429        return this.el.findParent('.x-form-element', 5, true) || // use form element wrap if available
430            this.el.findParent('.x-form-field-wrap', 5, true);   // else direct field wrap
431    },
432
433    // private
434    alignErrorIcon : function(){
435        this.errorIcon.alignTo(this.el, 'tl-tr', [2, 0]);
436    },
437
438    /**
439     * Clear any invalid styles/messages for this field
440     */
441    clearInvalid : function(){
442        if(!this.rendered || this.preventMark){ // not rendered
443            return;
444        }
445        this.el.removeClass(this.invalidClass);
446        switch(this.msgTarget){
447            case 'qtip':
448                this.el.dom.qtip = '';
449                break;
450            case 'title':
451                this.el.dom.title = '';
452                break;
453            case 'under':
454                if(this.errorEl){
455                    Ext.form.Field.msgFx[this.msgFx].hide(this.errorEl, this);
456                }
457                break;
458            case 'side':
459                if(this.errorIcon){
460                    this.errorIcon.dom.qtip = '';
461                    this.errorIcon.hide();
462                    this.un('resize', this.alignErrorIcon, this);
463                }
464                break;
465            default:
466                var t = Ext.getDom(this.msgTarget);
467                t.innerHTML = '';
468                t.style.display = 'none';
469                break;
470        }
471        this.fireEvent('valid', this);
472    },
473
474    /**
475     * Returns the raw data value which may or may not be a valid, defined value.  To return a normalized value see {@link #getValue}.
476     * @return {Mixed} value The field value
477     */
478    getRawValue : function(){
479        var v = this.rendered ? this.el.getValue() : Ext.value(this.value, '');
480        if(v === this.emptyText){
481            v = '';
482        }
483        return v;
484    },
485
486    /**
487     * Returns the normalized data value (undefined or emptyText will be returned as '').  To return the raw value see {@link #getRawValue}.
488     * @return {Mixed} value The field value
489     */
490    getValue : function(){
491        if(!this.rendered) {
492            return this.value;
493        }
494        var v = this.el.getValue();
495        if(v === this.emptyText || v === undefined){
496            v = '';
497        }
498        return v;
499    },
500
501    /**
502     * Sets the underlying DOM field's value directly, bypassing validation.  To set the value with validation see {@link #setValue}.
503     * @param {Mixed} value The value to set
504     * @return {Mixed} value The field value that is set
505     */
506    setRawValue : function(v){
507        return this.el.dom.value = (v === null || v === undefined ? '' : v);
508    },
509
510    /**
511     * Sets a data value into the field and validates it.  To set the value directly without validation see {@link #setRawValue}.
512     * @param {Mixed} value The value to set
513     */
514    setValue : function(v){
515        this.value = v;
516        if(this.rendered){
517            this.el.dom.value = (v === null || v === undefined ? '' : v);
518            this.validate();
519        }
520    },
521
522    // private
523    adjustSize : function(w, h){
524        var s = Ext.form.Field.superclass.adjustSize.call(this, w, h);
525        s.width = this.adjustWidth(this.el.dom.tagName, s.width);
526        return s;
527    },
528
529    // private
530    adjustWidth : function(tag, w){
531        tag = tag.toLowerCase();
532        if(typeof w == 'number' && !Ext.isSafari){
533            if(Ext.isIE && (tag == 'input' || tag == 'textarea')){
534                if(tag == 'input' && !Ext.isStrict){
535                    return this.inEditor ? w : w - 3;
536                }
537                if(tag == 'input' && Ext.isStrict){
538                    return w - (Ext.isIE6 ? 4 : 1);
539                }
540                if(tag == 'textarea' && Ext.isStrict){
541                    return w-2;
542                }
543            }else if(Ext.isOpera && Ext.isStrict){
544                if(tag == 'input'){
545                    return w + 2;
546                }
547                if(tag == 'textarea'){
548                    return w-2;
549                }
550            }
551        }
552        return w;
553    }
554
555    /**
556     * @cfg {Boolean} autoWidth @hide
557     */
558    /**
559     * @cfg {Boolean} autoHeight @hide
560     */
561
562    /**
563     * @cfg {String} autoEl @hide
564     */
565});
566
567Ext.form.MessageTargets = {
568    'qtip' : {
569        mark: function(f){
570            this.el.dom.qtip = msg;
571            this.el.dom.qclass = 'x-form-invalid-tip';
572            if(Ext.QuickTips){ // fix for floating editors interacting with DND
573                Ext.QuickTips.enable();
574            }
575        },
576        clear: function(f){
577            this.el.dom.qtip = '';
578        }
579    },
580    'title' : {
581        mark: function(f){
582            this.el.dom.title = msg;
583        },
584        clear: function(f){
585            this.el.dom.title = '';
586        }
587    },
588    'under' : {
589        mark: function(f){
590            if(!this.errorEl){
591                var elp = this.getErrorCt();
592                if(!elp){ // field has no container el
593                    this.el.dom.title = msg;
594                    return;
595                }
596                this.errorEl = elp.createChild({cls:'x-form-invalid-msg'});
597                this.errorEl.setWidth(elp.getWidth(true)-20);
598            }
599            this.errorEl.update(msg);
600            Ext.form.Field.msgFx[this.msgFx].show(this.errorEl, this);
601        },
602        clear: function(f){
603            if(this.errorEl){
604                Ext.form.Field.msgFx[this.msgFx].hide(this.errorEl, this);
605            }else{
606                this.el.dom.title = '';
607            }
608        }
609    },
610    'side' : {
611        mark: function(f){
612            if(!this.errorIcon){
613                var elp = this.getErrorCt();
614                if(!elp){ // field has no container el
615                    this.el.dom.title = msg;
616                    return;
617                }
618                this.errorIcon = elp.createChild({cls:'x-form-invalid-icon'});
619            }
620            this.alignErrorIcon();
621            this.errorIcon.dom.qtip = msg;
622            this.errorIcon.dom.qclass = 'x-form-invalid-tip';
623            this.errorIcon.show();
624            this.on('resize', this.alignErrorIcon, this);
625        },
626        clear: function(f){
627            if(this.errorIcon){
628                this.errorIcon.dom.qtip = '';
629                this.errorIcon.hide();
630                this.un('resize', this.alignErrorIcon, this);
631            }else{
632                this.el.dom.title = '';
633            }
634        }
635    },
636    'around' : {
637        mark: function(f){
638
639        },
640        clear: function(f){
641
642        }
643    }
644};
645
646
647// anything other than normal should be considered experimental
648Ext.form.Field.msgFx = {
649    normal : {
650        show: function(msgEl, f){
651            msgEl.setDisplayed('block');
652        },
653
654        hide : function(msgEl, f){
655            msgEl.setDisplayed(false).update('');
656        }
657    },
658
659    slide : {
660        show: function(msgEl, f){
661            msgEl.slideIn('t', {stopFx:true});
662        },
663
664        hide : function(msgEl, f){
665            msgEl.slideOut('t', {stopFx:true,useDisplay:true});
666        }
667    },
668
669    slideRight : {
670        show: function(msgEl, f){
671            msgEl.fixDisplay();
672            msgEl.alignTo(f.el, 'tl-tr');
673            msgEl.slideIn('l', {stopFx:true});
674        },
675
676        hide : function(msgEl, f){
677            msgEl.slideOut('l', {stopFx:true,useDisplay:true});
678        }
679    }
680};
681Ext.reg('field', Ext.form.Field);
Note: See TracBrowser for help on using the repository browser.