sync
[roojs1] / Roo / bootstrap / Input.js
1 /*
2  * - LGPL
3  *
4  * Input
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.Input
10  * @extends Roo.bootstrap.Component
11  * Bootstrap Input class
12  * @cfg {Boolean} disabled is it disabled
13  * @cfg {String} inputType button | checkbox | email | file | hidden | image | number | password | radio | range | reset | search | submit | text
14  * @cfg {String} name name of the input
15  * @cfg {string} fieldLabel - the label associated
16  * @cfg {string} placeholder - placeholder to put in text.
17  * @cfg {string}  before - input group add on before
18  * @cfg {string} after - input group add on after
19  * @cfg {string} size - (lg|sm) or leave empty..
20  * @cfg {Number} xs colspan out of 12 for mobile-sized screens
21  * @cfg {Number} sm colspan out of 12 for tablet-sized screens
22  * @cfg {Number} md colspan out of 12 for computer-sized screens
23  * @cfg {Number} lg colspan out of 12 for large computer-sized screens
24  * @cfg {string} value default value of the input
25  * @cfg {Number} labelWidth set the width of label 
26  * @cfg {Number} labellg set the width of label (1-12)
27  * @cfg {Number} labelmd set the width of label (1-12)
28  * @cfg {Number} labelsm set the width of label (1-12)
29  * @cfg {Number} labelxs set the width of label (1-12)
30  * @cfg {String} labelAlign (top|left)
31  * @cfg {Boolean} readOnly Specifies that the field should be read-only
32  * @cfg {String} autocomplete - default is new-password see: https://developers.google.com/web/fundamentals/input/form/label-and-name-inputs?hl=en
33  * @cfg {String} indicatorpos (left|right) default left
34
35  * @cfg {String} align (left|center|right) Default left
36  * @cfg {Boolean} forceFeedback (true|false) Default false
37  * 
38  * 
39  * 
40  * 
41  * @constructor
42  * Create a new Input
43  * @param {Object} config The config object
44  */
45
46 Roo.bootstrap.Input = function(config){
47     
48     Roo.bootstrap.Input.superclass.constructor.call(this, config);
49     
50     this.addEvents({
51         /**
52          * @event focus
53          * Fires when this field receives input focus.
54          * @param {Roo.form.Field} this
55          */
56         focus : true,
57         /**
58          * @event blur
59          * Fires when this field loses input focus.
60          * @param {Roo.form.Field} this
61          */
62         blur : true,
63         /**
64          * @event specialkey
65          * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed.  You can check
66          * {@link Roo.EventObject#getKey} to determine which key was pressed.
67          * @param {Roo.form.Field} this
68          * @param {Roo.EventObject} e The event object
69          */
70         specialkey : true,
71         /**
72          * @event change
73          * Fires just before the field blurs if the field value has changed.
74          * @param {Roo.form.Field} this
75          * @param {Mixed} newValue The new value
76          * @param {Mixed} oldValue The original value
77          */
78         change : true,
79         /**
80          * @event invalid
81          * Fires after the field has been marked as invalid.
82          * @param {Roo.form.Field} this
83          * @param {String} msg The validation message
84          */
85         invalid : true,
86         /**
87          * @event valid
88          * Fires after the field has been validated with no errors.
89          * @param {Roo.form.Field} this
90          */
91         valid : true,
92          /**
93          * @event keyup
94          * Fires after the key up
95          * @param {Roo.form.Field} this
96          * @param {Roo.EventObject}  e The event Object
97          */
98         keyup : true
99     });
100 };
101
102 Roo.extend(Roo.bootstrap.Input, Roo.bootstrap.Component,  {
103      /**
104      * @cfg {String/Boolean} validationEvent The event that should initiate field validation. Set to false to disable
105       automatic validation (defaults to "keyup").
106      */
107     validationEvent : "keyup",
108      /**
109      * @cfg {Boolean} validateOnBlur Whether the field should validate when it loses focus (defaults to true).
110      */
111     validateOnBlur : true,
112     /**
113      * @cfg {Number} validationDelay The length of time in milliseconds after user input begins until validation is initiated (defaults to 250)
114      */
115     validationDelay : 250,
116      /**
117      * @cfg {String} focusClass The CSS class to use when the field receives focus (defaults to "x-form-focus")
118      */
119     focusClass : "x-form-focus",  // not needed???
120     
121        
122     /**
123      * @cfg {String} invalidClass The CSS class to use when marking a field invalid (defaults to "x-form-invalid")
124      */
125     invalidClass : "has-warning",
126     
127     /**
128      * @cfg {String} validClass The CSS class to use when marking a field valid (defaults to "x-form-invalid")
129      */
130     validClass : "has-success",
131     
132     /**
133      * @cfg {Boolean} hasFeedback (true|false) default true
134      */
135     hasFeedback : true,
136     
137     /**
138      * @cfg {String} invalidFeedbackIcon The CSS class to use when create feedback icon (defaults to "x-form-invalid")
139      */
140     invalidFeedbackClass : "glyphicon-warning-sign",
141     
142     /**
143      * @cfg {String} validFeedbackIcon The CSS class to use when create feedback icon (defaults to "x-form-invalid")
144      */
145     validFeedbackClass : "glyphicon-ok",
146     
147     /**
148      * @cfg {Boolean} selectOnFocus True to automatically select any existing field text when the field receives input focus (defaults to false)
149      */
150     selectOnFocus : false,
151     
152      /**
153      * @cfg {String} maskRe An input mask regular expression that will be used to filter keystrokes that don't match (defaults to null)
154      */
155     maskRe : null,
156        /**
157      * @cfg {String} vtype A validation type name as defined in {@link Roo.form.VTypes} (defaults to null)
158      */
159     vtype : null,
160     
161       /**
162      * @cfg {Boolean} disableKeyFilter True to disable input keystroke filtering (defaults to false)
163      */
164     disableKeyFilter : false,
165     
166        /**
167      * @cfg {Boolean} disabled True to disable the field (defaults to false).
168      */
169     disabled : false,
170      /**
171      * @cfg {Boolean} allowBlank False to validate that the value length > 0 (defaults to true)
172      */
173     allowBlank : true,
174     /**
175      * @cfg {String} blankText Error text to display if the allow blank validation fails (defaults to "This field is required")
176      */
177     blankText : "Please complete this mandatory field",
178     
179      /**
180      * @cfg {Number} minLength Minimum input field length required (defaults to 0)
181      */
182     minLength : 0,
183     /**
184      * @cfg {Number} maxLength Maximum input field length allowed (defaults to Number.MAX_VALUE)
185      */
186     maxLength : Number.MAX_VALUE,
187     /**
188      * @cfg {String} minLengthText Error text to display if the minimum length validation fails (defaults to "The minimum length for this field is {minLength}")
189      */
190     minLengthText : "The minimum length for this field is {0}",
191     /**
192      * @cfg {String} maxLengthText Error text to display if the maximum length validation fails (defaults to "The maximum length for this field is {maxLength}")
193      */
194     maxLengthText : "The maximum length for this field is {0}",
195   
196     
197     /**
198      * @cfg {Function} validator A custom validation function to be called during field validation (defaults to null).
199      * If available, this function will be called only after the basic validators all return true, and will be passed the
200      * current field value and expected to return boolean true if the value is valid or a string error message if invalid.
201      */
202     validator : null,
203     /**
204      * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation (defaults to null).
205      * If available, this regex will be evaluated only after the basic validators all return true, and will be passed the
206      * current field value.  If the test fails, the field will be marked invalid using {@link #regexText}.
207      */
208     regex : null,
209     /**
210      * @cfg {String} regexText The error text to display if {@link #regex} is used and the test fails during validation (defaults to "")
211      */
212     regexText : "",
213     
214     autocomplete: false,
215     
216     
217     fieldLabel : '',
218     inputType : 'text',
219     
220     name : false,
221     placeholder: false,
222     before : false,
223     after : false,
224     size : false,
225     hasFocus : false,
226     preventMark: false,
227     isFormField : true,
228     value : '',
229     labelWidth : 2,
230     labelAlign : false,
231     readOnly : false,
232     align : false,
233     formatedValue : false,
234     forceFeedback : false,
235     
236     indicatorpos : 'left',
237     
238     labellg : 0,
239     labelmd : 0,
240     labelsm : 0,
241     labelxs : 0,
242     
243     parentLabelAlign : function()
244     {
245         var parent = this;
246         while (parent.parent()) {
247             parent = parent.parent();
248             if (typeof(parent.labelAlign) !='undefined') {
249                 return parent.labelAlign;
250             }
251         }
252         return 'left';
253         
254     },
255     
256     getAutoCreate : function()
257     {
258         var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
259         
260         var id = Roo.id();
261         
262         var cfg = {};
263         
264         if(this.inputType != 'hidden'){
265             cfg.cls = 'form-group' //input-group
266         }
267         
268         var input =  {
269             tag: 'input',
270             id : id,
271             type : this.inputType,
272             value : this.value,
273             cls : 'form-control',
274             placeholder : this.placeholder || '',
275             autocomplete : this.autocomplete || 'new-password'
276         };
277         
278         if(this.align){
279             input.style = (typeof(input.style) == 'undefined') ? ('text-align:' + this.align) : (input.style + 'text-align:' + this.align);
280         }
281         
282         if(this.maxLength && this.maxLength != Number.MAX_VALUE){
283             input.maxLength = this.maxLength;
284         }
285         
286         if (this.disabled) {
287             input.disabled=true;
288         }
289         
290         if (this.readOnly) {
291             input.readonly=true;
292         }
293         
294         if (this.name) {
295             input.name = this.name;
296         }
297         
298         if (this.size) {
299             input.cls += ' input-' + this.size;
300         }
301         
302         var settings=this;
303         ['xs','sm','md','lg'].map(function(size){
304             if (settings[size]) {
305                 cfg.cls += ' col-' + size + '-' + settings[size];
306             }
307         });
308         
309         var inputblock = input;
310         
311         var feedback = {
312             tag: 'span',
313             cls: 'glyphicon form-control-feedback'
314         };
315             
316         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
317             
318             inputblock = {
319                 cls : 'has-feedback',
320                 cn :  [
321                     input,
322                     feedback
323                 ] 
324             };  
325         }
326         
327         if (this.before || this.after) {
328             
329             inputblock = {
330                 cls : 'input-group',
331                 cn :  [] 
332             };
333             
334             if (this.before && typeof(this.before) == 'string') {
335                 
336                 inputblock.cn.push({
337                     tag :'span',
338                     cls : 'roo-input-before input-group-addon',
339                     html : this.before
340                 });
341             }
342             if (this.before && typeof(this.before) == 'object') {
343                 this.before = Roo.factory(this.before);
344                 
345                 inputblock.cn.push({
346                     tag :'span',
347                     cls : 'roo-input-before input-group-' +
348                         (this.before.xtype == 'Button' ? 'btn' : 'addon')  //?? what about checkboxes - that looks like a bit of a hack thought? 
349                 });
350             }
351             
352             inputblock.cn.push(input);
353             
354             if (this.after && typeof(this.after) == 'string') {
355                 inputblock.cn.push({
356                     tag :'span',
357                     cls : 'roo-input-after input-group-addon',
358                     html : this.after
359                 });
360             }
361             if (this.after && typeof(this.after) == 'object') {
362                 this.after = Roo.factory(this.after);
363                 
364                 inputblock.cn.push({
365                     tag :'span',
366                     cls : 'roo-input-after input-group-' +
367                         (this.after.xtype == 'Button' ? 'btn' : 'addon')  //?? what about checkboxes - that looks like a bit of a hack thought? 
368                 });
369             }
370             
371             if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
372                 inputblock.cls += ' has-feedback';
373                 inputblock.cn.push(feedback);
374             }
375         };
376         
377         if (align ==='left' && this.fieldLabel.length) {
378             
379             cfg.cn = [
380                 {
381                     tag : 'i',
382                     cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
383                     tooltip : 'This field is required'
384                 },
385                 {
386                     tag: 'label',
387                     'for' :  id,
388                     cls : 'control-label',
389                     html : this.fieldLabel
390
391                 },
392                 {
393                     cls : "", 
394                     cn: [
395                         inputblock
396                     ]
397                 }
398             ];
399             
400             var labelCfg = cfg.cn[1];
401             var contentCfg = cfg.cn[2];
402             
403             if(this.indicatorpos == 'right'){
404                 cfg.cn = [
405                     {
406                         tag: 'label',
407                         'for' :  id,
408                         cls : 'control-label',
409                         html : this.fieldLabel
410
411                     },
412                     {
413                         tag : 'i',
414                         cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
415                         tooltip : 'This field is required'
416                     },
417                     {
418                         cls : "",
419                         cn: [
420                             inputblock
421                         ]
422                     }
423
424                 ];
425                 
426                 labelCfg = cfg.cn[0];
427                 contentCfg = cfg.cn[2];
428             
429             }
430             
431             if(this.labelWidth > 12){
432                 labelCfg.style = "width: " + this.labelWidth + 'px';
433             }
434             
435             if(this.labelWidth < 13 && this.labelmd == 0){
436                 this.labelmd = this.labelWidth;
437             }
438             
439             if(this.labellg > 0){
440                 labelCfg.cls += ' col-lg-' + this.labellg;
441                 contentCfg.cls += ' col-lg-' + (12 - this.labellg);
442             }
443             
444             if(this.labelmd > 0){
445                 labelCfg.cls += ' col-md-' + this.labelmd;
446                 contentCfg.cls += ' col-md-' + (12 - this.labelmd);
447             }
448             
449             if(this.labelsm > 0){
450                 labelCfg.cls += ' col-sm-' + this.labelsm;
451                 contentCfg.cls += ' col-sm-' + (12 - this.labelsm);
452             }
453             
454             if(this.labelxs > 0){
455                 labelCfg.cls += ' col-xs-' + this.labelxs;
456                 contentCfg.cls += ' col-xs-' + (12 - this.labelxs);
457             }
458             
459             
460         } else if ( this.fieldLabel.length) {
461                 
462             cfg.cn = [
463                 {
464                     tag : 'i',
465                     cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
466                     tooltip : 'This field is required'
467                 },
468                 {
469                     tag: 'label',
470                    //cls : 'input-group-addon',
471                     html : this.fieldLabel
472
473                 },
474
475                inputblock
476
477            ];
478            
479            if(this.indicatorpos == 'right'){
480                 
481                 cfg.cn = [
482                     {
483                         tag: 'label',
484                        //cls : 'input-group-addon',
485                         html : this.fieldLabel
486
487                     },
488                     {
489                         tag : 'i',
490                         cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
491                         tooltip : 'This field is required'
492                     },
493
494                    inputblock
495
496                ];
497
498             }
499
500         } else {
501             
502             cfg.cn = [
503
504                     inputblock
505
506             ];
507                 
508                 
509         };
510         
511         if (this.parentType === 'Navbar' &&  this.parent().bar) {
512            cfg.cls += ' navbar-form';
513         }
514         
515         if (this.parentType === 'NavGroup') {
516            cfg.cls += ' navbar-form';
517            cfg.tag = 'li';
518         }
519         
520         return cfg;
521         
522     },
523     /**
524      * return the real input element.
525      */
526     inputEl: function ()
527     {
528         return this.el.select('input.form-control',true).first();
529     },
530     
531     tooltipEl : function()
532     {
533         return this.inputEl();
534     },
535     
536     indicatorEl : function()
537     {
538         var indicator = this.el.select('i.roo-required-indicator',true).first();
539         
540         if(!indicator){
541             return false;
542         }
543         
544         return indicator;
545         
546     },
547     
548     setDisabled : function(v)
549     {
550         var i  = this.inputEl().dom;
551         if (!v) {
552             i.removeAttribute('disabled');
553             return;
554             
555         }
556         i.setAttribute('disabled','true');
557     },
558     initEvents : function()
559     {
560           
561         this.inputEl().on("keydown" , this.fireKey,  this);
562         this.inputEl().on("focus", this.onFocus,  this);
563         this.inputEl().on("blur", this.onBlur,  this);
564         
565         this.inputEl().relayEvent('keyup', this);
566         
567         this.indicator = this.indicatorEl();
568         
569         if(this.indicator){
570             this.indicator.setVisibilityMode(Roo.Element.DISPLAY);
571             this.indicator.hide();
572         }
573  
574         // reference to original value for reset
575         this.originalValue = this.getValue();
576         //Roo.form.TextField.superclass.initEvents.call(this);
577         if(this.validationEvent == 'keyup'){
578             this.validationTask = new Roo.util.DelayedTask(this.validate, this);
579             this.inputEl().on('keyup', this.filterValidation, this);
580         }
581         else if(this.validationEvent !== false){
582             this.inputEl().on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
583         }
584         
585         if(this.selectOnFocus){
586             this.on("focus", this.preFocus, this);
587             
588         }
589         if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
590             this.inputEl().on("keypress", this.filterKeys, this);
591         } else {
592             this.inputEl().relayEvent('keypress', this);
593         }
594        /* if(this.grow){
595             this.el.on("keyup", this.onKeyUp,  this, {buffer:50});
596             this.el.on("click", this.autoSize,  this);
597         }
598         */
599         if(this.inputEl().is('input[type=password]') && Roo.isSafari){
600             this.inputEl().on('keydown', this.SafariOnKeyDown, this);
601         }
602         
603         if (typeof(this.before) == 'object') {
604             this.before.render(this.el.select('.roo-input-before',true).first());
605         }
606         if (typeof(this.after) == 'object') {
607             this.after.render(this.el.select('.roo-input-after',true).first());
608         }
609         
610         
611     },
612     filterValidation : function(e){
613         if(!e.isNavKeyPress()){
614             this.validationTask.delay(this.validationDelay);
615         }
616     },
617      /**
618      * Validates the field value
619      * @return {Boolean} True if the value is valid, else false
620      */
621     validate : function(){
622         //if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
623         if(this.disabled || this.validateValue(this.getRawValue())){
624             this.markValid();
625             return true;
626         }
627         
628         this.markInvalid();
629         return false;
630     },
631     
632     
633     /**
634      * Validates a value according to the field's validation rules and marks the field as invalid
635      * if the validation fails
636      * @param {Mixed} value The value to validate
637      * @return {Boolean} True if the value is valid, else false
638      */
639     validateValue : function(value){
640         if(value.length < 1)  { // if it's blank
641             if(this.allowBlank){
642                 return true;
643             }
644             return false;
645         }
646         
647         if(value.length < this.minLength){
648             return false;
649         }
650         if(value.length > this.maxLength){
651             return false;
652         }
653         if(this.vtype){
654             var vt = Roo.form.VTypes;
655             if(!vt[this.vtype](value, this)){
656                 return false;
657             }
658         }
659         if(typeof this.validator == "function"){
660             var msg = this.validator(value);
661             if(msg !== true){
662                 return false;
663             }
664         }
665         
666         if(this.regex && !this.regex.test(value)){
667             return false;
668         }
669         
670         return true;
671     },
672
673     
674     
675      // private
676     fireKey : function(e){
677         //Roo.log('field ' + e.getKey());
678         if(e.isNavKeyPress()){
679             this.fireEvent("specialkey", this, e);
680         }
681     },
682     focus : function (selectText){
683         if(this.rendered){
684             this.inputEl().focus();
685             if(selectText === true){
686                 this.inputEl().dom.select();
687             }
688         }
689         return this;
690     } ,
691     
692     onFocus : function(){
693         if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
694            // this.el.addClass(this.focusClass);
695         }
696         if(!this.hasFocus){
697             this.hasFocus = true;
698             this.startValue = this.getValue();
699             this.fireEvent("focus", this);
700         }
701     },
702     
703     beforeBlur : Roo.emptyFn,
704
705     
706     // private
707     onBlur : function(){
708         this.beforeBlur();
709         if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
710             //this.el.removeClass(this.focusClass);
711         }
712         this.hasFocus = false;
713         if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
714             this.validate();
715         }
716         var v = this.getValue();
717         if(String(v) !== String(this.startValue)){
718             this.fireEvent('change', this, v, this.startValue);
719         }
720         this.fireEvent("blur", this);
721     },
722     
723     /**
724      * Resets the current field value to the originally loaded value and clears any validation messages
725      */
726     reset : function(){
727         this.setValue(this.originalValue);
728         this.validate();
729     },
730      /**
731      * Returns the name of the field
732      * @return {Mixed} name The name field
733      */
734     getName: function(){
735         return this.name;
736     },
737      /**
738      * Returns the normalized data value (undefined or emptyText will be returned as '').  To return the raw value see {@link #getRawValue}.
739      * @return {Mixed} value The field value
740      */
741     getValue : function(){
742         
743         var v = this.inputEl().getValue();
744         
745         return v;
746     },
747     /**
748      * Returns the raw data value which may or may not be a valid, defined value.  To return a normalized value see {@link #getValue}.
749      * @return {Mixed} value The field value
750      */
751     getRawValue : function(){
752         var v = this.inputEl().getValue();
753         
754         return v;
755     },
756     
757     /**
758      * Sets the underlying DOM field's value directly, bypassing validation.  To set the value with validation see {@link #setValue}.
759      * @param {Mixed} value The value to set
760      */
761     setRawValue : function(v){
762         return this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
763     },
764     
765     selectText : function(start, end){
766         var v = this.getRawValue();
767         if(v.length > 0){
768             start = start === undefined ? 0 : start;
769             end = end === undefined ? v.length : end;
770             var d = this.inputEl().dom;
771             if(d.setSelectionRange){
772                 d.setSelectionRange(start, end);
773             }else if(d.createTextRange){
774                 var range = d.createTextRange();
775                 range.moveStart("character", start);
776                 range.moveEnd("character", v.length-end);
777                 range.select();
778             }
779         }
780     },
781     
782     /**
783      * Sets a data value into the field and validates it.  To set the value directly without validation see {@link #setRawValue}.
784      * @param {Mixed} value The value to set
785      */
786     setValue : function(v){
787         this.value = v;
788         if(this.rendered){
789             this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
790             this.validate();
791         }
792     },
793     
794     /*
795     processValue : function(value){
796         if(this.stripCharsRe){
797             var newValue = value.replace(this.stripCharsRe, '');
798             if(newValue !== value){
799                 this.setRawValue(newValue);
800                 return newValue;
801             }
802         }
803         return value;
804     },
805   */
806     preFocus : function(){
807         
808         if(this.selectOnFocus){
809             this.inputEl().dom.select();
810         }
811     },
812     filterKeys : function(e){
813         var k = e.getKey();
814         if(!Roo.isIE && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
815             return;
816         }
817         var c = e.getCharCode(), cc = String.fromCharCode(c);
818         if(Roo.isIE && (e.isSpecialKey() || !cc)){
819             return;
820         }
821         if(!this.maskRe.test(cc)){
822             e.stopEvent();
823         }
824     },
825      /**
826      * Clear any invalid styles/messages for this field
827      */
828     clearInvalid : function(){
829         
830         if(!this.el || this.preventMark){ // not rendered
831             return;
832         }
833         
834         if(this.indicator){
835             this.indicator.hide();
836         }
837         
838         this.el.removeClass(this.invalidClass);
839         
840         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
841             
842             var feedback = this.el.select('.form-control-feedback', true).first();
843             
844             if(feedback){
845                 this.el.select('.form-control-feedback', true).first().removeClass(this.invalidFeedbackClass);
846             }
847             
848         }
849         
850         this.fireEvent('valid', this);
851     },
852     
853      /**
854      * Mark this field as valid
855      */
856     markValid : function()
857     {
858         if(!this.el  || this.preventMark){ // not rendered
859             return;
860         }
861         
862         this.el.removeClass([this.invalidClass, this.validClass]);
863         
864         var feedback = this.el.select('.form-control-feedback', true).first();
865             
866         if(feedback){
867             this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
868         }
869
870         if(this.disabled){
871             return;
872         }
873         
874         if(this.allowBlank && !this.getRawValue().length){
875             return;
876         }
877         
878         if(this.indicator){
879             this.indicator.hide();
880         }
881         
882         this.el.addClass(this.validClass);
883         
884         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank && (this.getValue().length || this.forceFeedback)){
885             
886             var feedback = this.el.select('.form-control-feedback', true).first();
887             
888             if(feedback){
889                 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
890                 this.el.select('.form-control-feedback', true).first().addClass([this.validFeedbackClass]);
891             }
892             
893         }
894         
895         this.fireEvent('valid', this);
896     },
897     
898      /**
899      * Mark this field as invalid
900      * @param {String} msg The validation message
901      */
902     markInvalid : function(msg)
903     {
904         if(!this.el  || this.preventMark){ // not rendered
905             return;
906         }
907         
908         this.el.removeClass([this.invalidClass, this.validClass]);
909         
910         var feedback = this.el.select('.form-control-feedback', true).first();
911             
912         if(feedback){
913             this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
914         }
915
916         if(this.disabled){
917             return;
918         }
919         
920         if(this.allowBlank && !this.getRawValue().length){
921             return;
922         }
923         
924         if(this.indicator){
925             this.indicator.show();
926         }
927         
928         this.el.addClass(this.invalidClass);
929         
930         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
931             
932             var feedback = this.el.select('.form-control-feedback', true).first();
933             
934             if(feedback){
935                 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
936                 
937                 if(this.getValue().length || this.forceFeedback){
938                     this.el.select('.form-control-feedback', true).first().addClass([this.invalidFeedbackClass]);
939                 }
940                 
941             }
942             
943         }
944         
945         this.fireEvent('invalid', this, msg);
946     },
947     // private
948     SafariOnKeyDown : function(event)
949     {
950         // this is a workaround for a password hang bug on chrome/ webkit.
951         if (this.inputEl().dom.type != 'password') {
952             return;
953         }
954         
955         var isSelectAll = false;
956         
957         if(this.inputEl().dom.selectionEnd > 0){
958             isSelectAll = (this.inputEl().dom.selectionEnd - this.inputEl().dom.selectionStart - this.getValue().length == 0) ? true : false;
959         }
960         if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
961             event.preventDefault();
962             this.setValue('');
963             return;
964         }
965         
966         if(isSelectAll  && event.getCharCode() > 31 && !event.ctrlKey) { // not backspace and delete key (or ctrl-v)
967             
968             event.preventDefault();
969             // this is very hacky as keydown always get's upper case.
970             //
971             var cc = String.fromCharCode(event.getCharCode());
972             this.setValue( event.shiftKey ?  cc : cc.toLowerCase());
973             
974         }
975     },
976     adjustWidth : function(tag, w){
977         tag = tag.toLowerCase();
978         if(typeof w == 'number' && Roo.isStrict && !Roo.isSafari){
979             if(Roo.isIE && (tag == 'input' || tag == 'textarea')){
980                 if(tag == 'input'){
981                     return w + 2;
982                 }
983                 if(tag == 'textarea'){
984                     return w-2;
985                 }
986             }else if(Roo.isOpera){
987                 if(tag == 'input'){
988                     return w + 2;
989                 }
990                 if(tag == 'textarea'){
991                     return w-2;
992                 }
993             }
994         }
995         return w;
996     }
997     
998 });
999
1000