Roo/bootstrap/Input.js
[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 -- Depricated - use Invalid Text
211      */
212     regexText : "",
213     
214     /**
215      * @cfg {String} invalidText The error text to display if {@link #validator} test fails during validation (defaults to "")
216      */
217     invalidText : "",
218     
219     
220     
221     autocomplete: false,
222     
223     
224     fieldLabel : '',
225     inputType : 'text',
226     
227     name : false,
228     placeholder: false,
229     before : false,
230     after : false,
231     size : false,
232     hasFocus : false,
233     preventMark: false,
234     isFormField : true,
235     value : '',
236     labelWidth : 2,
237     labelAlign : false,
238     readOnly : false,
239     align : false,
240     formatedValue : false,
241     forceFeedback : false,
242     
243     indicatorpos : 'left',
244     
245     labellg : 0,
246     labelmd : 0,
247     labelsm : 0,
248     labelxs : 0,
249     
250     parentLabelAlign : function()
251     {
252         var parent = this;
253         while (parent.parent()) {
254             parent = parent.parent();
255             if (typeof(parent.labelAlign) !='undefined') {
256                 return parent.labelAlign;
257             }
258         }
259         return 'left';
260         
261     },
262     
263     getAutoCreate : function()
264     {
265         var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
266         
267         var id = Roo.id();
268         
269         var cfg = {};
270         
271         if(this.inputType != 'hidden'){
272             cfg.cls = 'form-group' //input-group
273         }
274         
275         var input =  {
276             tag: 'input',
277             id : id,
278             type : this.inputType,
279             value : this.value,
280             cls : 'form-control',
281             placeholder : this.placeholder || '',
282             autocomplete : this.autocomplete || 'new-password'
283         };
284         
285         if(this.align){
286             input.style = (typeof(input.style) == 'undefined') ? ('text-align:' + this.align) : (input.style + 'text-align:' + this.align);
287         }
288         
289         if(this.maxLength && this.maxLength != Number.MAX_VALUE){
290             input.maxLength = this.maxLength;
291         }
292         
293         if (this.disabled) {
294             input.disabled=true;
295         }
296         
297         if (this.readOnly) {
298             input.readonly=true;
299         }
300         
301         if (this.name) {
302             input.name = this.name;
303         }
304         
305         if (this.size) {
306             input.cls += ' input-' + this.size;
307         }
308         
309         var settings=this;
310         ['xs','sm','md','lg'].map(function(size){
311             if (settings[size]) {
312                 cfg.cls += ' col-' + size + '-' + settings[size];
313             }
314         });
315         
316         var inputblock = input;
317         
318         var feedback = {
319             tag: 'span',
320             cls: 'glyphicon form-control-feedback'
321         };
322             
323         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
324             
325             inputblock = {
326                 cls : 'has-feedback',
327                 cn :  [
328                     input,
329                     feedback
330                 ] 
331             };  
332         }
333         
334         if (this.before || this.after) {
335             
336             inputblock = {
337                 cls : 'input-group',
338                 cn :  [] 
339             };
340             
341             if (this.before && typeof(this.before) == 'string') {
342                 
343                 inputblock.cn.push({
344                     tag :'span',
345                     cls : 'roo-input-before input-group-addon',
346                     html : this.before
347                 });
348             }
349             if (this.before && typeof(this.before) == 'object') {
350                 this.before = Roo.factory(this.before);
351                 
352                 inputblock.cn.push({
353                     tag :'span',
354                     cls : 'roo-input-before input-group-' +
355                         (this.before.xtype == 'Button' ? 'btn' : 'addon')  //?? what about checkboxes - that looks like a bit of a hack thought? 
356                 });
357             }
358             
359             inputblock.cn.push(input);
360             
361             if (this.after && typeof(this.after) == 'string') {
362                 inputblock.cn.push({
363                     tag :'span',
364                     cls : 'roo-input-after input-group-addon',
365                     html : this.after
366                 });
367             }
368             if (this.after && typeof(this.after) == 'object') {
369                 this.after = Roo.factory(this.after);
370                 
371                 inputblock.cn.push({
372                     tag :'span',
373                     cls : 'roo-input-after input-group-' +
374                         (this.after.xtype == 'Button' ? 'btn' : 'addon')  //?? what about checkboxes - that looks like a bit of a hack thought? 
375                 });
376             }
377             
378             if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
379                 inputblock.cls += ' has-feedback';
380                 inputblock.cn.push(feedback);
381             }
382         };
383         
384         if (align ==='left' && this.fieldLabel.length) {
385             
386             cfg.cls += ' roo-form-group-label-left';
387             
388             cfg.cn = [
389                 {
390                     tag : 'i',
391                     cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
392                     tooltip : 'This field is required'
393                 },
394                 {
395                     tag: 'label',
396                     'for' :  id,
397                     cls : 'control-label',
398                     html : this.fieldLabel
399
400                 },
401                 {
402                     cls : "", 
403                     cn: [
404                         inputblock
405                     ]
406                 }
407             ];
408             
409             var labelCfg = cfg.cn[1];
410             var contentCfg = cfg.cn[2];
411             
412             if(this.indicatorpos == 'right'){
413                 cfg.cn = [
414                     {
415                         tag: 'label',
416                         'for' :  id,
417                         cls : 'control-label',
418                         html : this.fieldLabel
419
420                     },
421                     {
422                         tag : 'i',
423                         cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
424                         tooltip : 'This field is required'
425                     },
426                     {
427                         cls : "",
428                         cn: [
429                             inputblock
430                         ]
431                     }
432
433                 ];
434                 
435                 labelCfg = cfg.cn[0];
436                 contentCfg = cfg.cn[2];
437             
438             }
439             
440             if(this.labelWidth > 12){
441                 labelCfg.style = "width: " + this.labelWidth + 'px';
442             }
443             
444             if(this.labelWidth < 13 && this.labelmd == 0){
445                 this.labelmd = this.labelWidth;
446             }
447             
448             if(this.labellg > 0){
449                 labelCfg.cls += ' col-lg-' + this.labellg;
450                 contentCfg.cls += ' col-lg-' + (12 - this.labellg);
451             }
452             
453             if(this.labelmd > 0){
454                 labelCfg.cls += ' col-md-' + this.labelmd;
455                 contentCfg.cls += ' col-md-' + (12 - this.labelmd);
456             }
457             
458             if(this.labelsm > 0){
459                 labelCfg.cls += ' col-sm-' + this.labelsm;
460                 contentCfg.cls += ' col-sm-' + (12 - this.labelsm);
461             }
462             
463             if(this.labelxs > 0){
464                 labelCfg.cls += ' col-xs-' + this.labelxs;
465                 contentCfg.cls += ' col-xs-' + (12 - this.labelxs);
466             }
467             
468             
469         } else if ( this.fieldLabel.length) {
470                 
471             cfg.cn = [
472                 {
473                     tag : 'i',
474                     cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
475                     tooltip : 'This field is required'
476                 },
477                 {
478                     tag: 'label',
479                    //cls : 'input-group-addon',
480                     html : this.fieldLabel
481
482                 },
483
484                inputblock
485
486            ];
487            
488            if(this.indicatorpos == 'right'){
489                 
490                 cfg.cn = [
491                     {
492                         tag: 'label',
493                        //cls : 'input-group-addon',
494                         html : this.fieldLabel
495
496                     },
497                     {
498                         tag : 'i',
499                         cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
500                         tooltip : 'This field is required'
501                     },
502
503                    inputblock
504
505                ];
506
507             }
508
509         } else {
510             
511             cfg.cn = [
512
513                     inputblock
514
515             ];
516                 
517                 
518         };
519         
520         if (this.parentType === 'Navbar' &&  this.parent().bar) {
521            cfg.cls += ' navbar-form';
522         }
523         
524         if (this.parentType === 'NavGroup') {
525            cfg.cls += ' navbar-form';
526            cfg.tag = 'li';
527         }
528         
529         return cfg;
530         
531     },
532     /**
533      * return the real input element.
534      */
535     inputEl: function ()
536     {
537         return this.el.select('input.form-control',true).first();
538     },
539     
540     tooltipEl : function()
541     {
542         return this.inputEl();
543     },
544     
545     indicatorEl : function()
546     {
547         var indicator = this.el.select('i.roo-required-indicator',true).first();
548         
549         if(!indicator){
550             return false;
551         }
552         
553         return indicator;
554         
555     },
556     
557     setDisabled : function(v)
558     {
559         var i  = this.inputEl().dom;
560         if (!v) {
561             i.removeAttribute('disabled');
562             return;
563             
564         }
565         i.setAttribute('disabled','true');
566     },
567     initEvents : function()
568     {
569           
570         this.inputEl().on("keydown" , this.fireKey,  this);
571         this.inputEl().on("focus", this.onFocus,  this);
572         this.inputEl().on("blur", this.onBlur,  this);
573         
574         this.inputEl().relayEvent('keyup', this);
575         
576         this.indicator = this.indicatorEl();
577         
578         if(this.indicator){
579             this.indicator.setVisibilityMode(Roo.Element.DISPLAY);
580             this.indicator.hide();
581         }
582  
583         // reference to original value for reset
584         this.originalValue = this.getValue();
585         //Roo.form.TextField.superclass.initEvents.call(this);
586         if(this.validationEvent == 'keyup'){
587             this.validationTask = new Roo.util.DelayedTask(this.validate, this);
588             this.inputEl().on('keyup', this.filterValidation, this);
589         }
590         else if(this.validationEvent !== false){
591             this.inputEl().on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
592         }
593         
594         if(this.selectOnFocus){
595             this.on("focus", this.preFocus, this);
596             
597         }
598         if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
599             this.inputEl().on("keypress", this.filterKeys, this);
600         } else {
601             this.inputEl().relayEvent('keypress', this);
602         }
603        /* if(this.grow){
604             this.el.on("keyup", this.onKeyUp,  this, {buffer:50});
605             this.el.on("click", this.autoSize,  this);
606         }
607         */
608         if(this.inputEl().is('input[type=password]') && Roo.isSafari){
609             this.inputEl().on('keydown', this.SafariOnKeyDown, this);
610         }
611         
612         if (typeof(this.before) == 'object') {
613             this.before.render(this.el.select('.roo-input-before',true).first());
614         }
615         if (typeof(this.after) == 'object') {
616             this.after.render(this.el.select('.roo-input-after',true).first());
617         }
618         
619         
620     },
621     filterValidation : function(e){
622         if(!e.isNavKeyPress()){
623             this.validationTask.delay(this.validationDelay);
624         }
625     },
626      /**
627      * Validates the field value
628      * @return {Boolean} True if the value is valid, else false
629      */
630     validate : function(){
631         //if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
632         if(this.disabled || this.validateValue(this.getRawValue())){
633             this.markValid();
634             return true;
635         }
636         
637         this.markInvalid();
638         return false;
639     },
640     
641     
642     /**
643      * Validates a value according to the field's validation rules and marks the field as invalid
644      * if the validation fails
645      * @param {Mixed} value The value to validate
646      * @return {Boolean} True if the value is valid, else false
647      */
648     validateValue : function(value){
649         if(value.length < 1)  { // if it's blank
650             if(this.allowBlank){
651                 return true;
652             }            
653             return this.inputEl().hasClass('hide') ? true : false;
654         }
655         
656         if(value.length < this.minLength){
657             return false;
658         }
659         if(value.length > this.maxLength){
660             return false;
661         }
662         if(this.vtype){
663             var vt = Roo.form.VTypes;
664             if(!vt[this.vtype](value, this)){
665                 return false;
666             }
667         }
668         if(typeof this.validator == "function"){
669             var msg = this.validator(value);
670             if(msg !== true){
671                 return false;
672             }
673             if (typeof(msg) == 'string') {
674                 this.invalidText = msg;
675             }
676         }
677         
678         if(this.regex && !this.regex.test(value)){
679             return false;
680         }
681         
682         return true;
683     },
684
685     
686     
687      // private
688     fireKey : function(e){
689         //Roo.log('field ' + e.getKey());
690         if(e.isNavKeyPress()){
691             this.fireEvent("specialkey", this, e);
692         }
693     },
694     focus : function (selectText){
695         if(this.rendered){
696             this.inputEl().focus();
697             if(selectText === true){
698                 this.inputEl().dom.select();
699             }
700         }
701         return this;
702     } ,
703     
704     onFocus : function(){
705         if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
706            // this.el.addClass(this.focusClass);
707         }
708         if(!this.hasFocus){
709             this.hasFocus = true;
710             this.startValue = this.getValue();
711             this.fireEvent("focus", this);
712         }
713     },
714     
715     beforeBlur : Roo.emptyFn,
716
717     
718     // private
719     onBlur : function(){
720         this.beforeBlur();
721         if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
722             //this.el.removeClass(this.focusClass);
723         }
724         this.hasFocus = false;
725         if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
726             this.validate();
727         }
728         var v = this.getValue();
729         if(String(v) !== String(this.startValue)){
730             this.fireEvent('change', this, v, this.startValue);
731         }
732         this.fireEvent("blur", this);
733     },
734     
735     /**
736      * Resets the current field value to the originally loaded value and clears any validation messages
737      */
738     reset : function(){
739         this.setValue(this.originalValue);
740         this.validate();
741     },
742      /**
743      * Returns the name of the field
744      * @return {Mixed} name The name field
745      */
746     getName: function(){
747         return this.name;
748     },
749      /**
750      * Returns the normalized data value (undefined or emptyText will be returned as '').  To return the raw value see {@link #getRawValue}.
751      * @return {Mixed} value The field value
752      */
753     getValue : function(){
754         
755         var v = this.inputEl().getValue();
756         
757         return v;
758     },
759     /**
760      * Returns the raw data value which may or may not be a valid, defined value.  To return a normalized value see {@link #getValue}.
761      * @return {Mixed} value The field value
762      */
763     getRawValue : function(){
764         var v = this.inputEl().getValue();
765         
766         return v;
767     },
768     
769     /**
770      * Sets the underlying DOM field's value directly, bypassing validation.  To set the value with validation see {@link #setValue}.
771      * @param {Mixed} value The value to set
772      */
773     setRawValue : function(v){
774         return this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
775     },
776     
777     selectText : function(start, end){
778         var v = this.getRawValue();
779         if(v.length > 0){
780             start = start === undefined ? 0 : start;
781             end = end === undefined ? v.length : end;
782             var d = this.inputEl().dom;
783             if(d.setSelectionRange){
784                 d.setSelectionRange(start, end);
785             }else if(d.createTextRange){
786                 var range = d.createTextRange();
787                 range.moveStart("character", start);
788                 range.moveEnd("character", v.length-end);
789                 range.select();
790             }
791         }
792     },
793     
794     /**
795      * Sets a data value into the field and validates it.  To set the value directly without validation see {@link #setRawValue}.
796      * @param {Mixed} value The value to set
797      */
798     setValue : function(v){
799         this.value = v;
800         if(this.rendered){
801             this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
802             this.validate();
803         }
804     },
805     
806     /*
807     processValue : function(value){
808         if(this.stripCharsRe){
809             var newValue = value.replace(this.stripCharsRe, '');
810             if(newValue !== value){
811                 this.setRawValue(newValue);
812                 return newValue;
813             }
814         }
815         return value;
816     },
817   */
818     preFocus : function(){
819         
820         if(this.selectOnFocus){
821             this.inputEl().dom.select();
822         }
823     },
824     filterKeys : function(e){
825         var k = e.getKey();
826         if(!Roo.isIE && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
827             return;
828         }
829         var c = e.getCharCode(), cc = String.fromCharCode(c);
830         if(Roo.isIE && (e.isSpecialKey() || !cc)){
831             return;
832         }
833         if(!this.maskRe.test(cc)){
834             e.stopEvent();
835         }
836     },
837      /**
838      * Clear any invalid styles/messages for this field
839      */
840     clearInvalid : function(){
841         
842         if(!this.el || this.preventMark){ // not rendered
843             return;
844         }
845         
846      
847         this.el.removeClass(this.invalidClass);
848         
849         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
850             
851             var feedback = this.el.select('.form-control-feedback', true).first();
852             
853             if(feedback){
854                 this.el.select('.form-control-feedback', true).first().removeClass(this.invalidFeedbackClass);
855             }
856             
857         }
858         
859         this.fireEvent('valid', this);
860     },
861     
862      /**
863      * Mark this field as valid
864      */
865     markValid : function()
866     {
867         if(!this.el  || this.preventMark){ // not rendered...
868             return;
869         }
870         
871         this.el.removeClass([this.invalidClass, this.validClass]);
872         
873         var feedback = this.el.select('.form-control-feedback', true).first();
874             
875         if(feedback){
876             this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
877         }
878
879         if(this.disabled){
880             return;
881         }
882         
883         if(this.allowBlank && !this.getRawValue().length){
884             return;
885         }
886         
887         if(this.indicator){
888             this.indicator.hide();
889         }
890         
891         this.el.addClass(this.validClass);
892         
893         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank && (this.getValue().length || this.forceFeedback)){
894             
895             var feedback = this.el.select('.form-control-feedback', true).first();
896             
897             if(feedback){
898                 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
899                 this.el.select('.form-control-feedback', true).first().addClass([this.validFeedbackClass]);
900             }
901             
902         }
903         
904         this.fireEvent('valid', this);
905     },
906     
907      /**
908      * Mark this field as invalid
909      * @param {String} msg The validation message
910      */
911     markInvalid : function(msg)
912     {
913         if(!this.el  || this.preventMark){ // not rendered
914             return;
915         }
916         
917         this.el.removeClass([this.invalidClass, this.validClass]);
918         
919         var feedback = this.el.select('.form-control-feedback', true).first();
920             
921         if(feedback){
922             this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
923         }
924
925         if(this.disabled){
926             return;
927         }
928         
929         if(this.allowBlank && !this.getRawValue().length){
930             return;
931         }
932         
933         if(this.indicator){
934             this.indicator.show();
935         }
936         
937         this.el.addClass(this.invalidClass);
938         
939         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
940             
941             var feedback = this.el.select('.form-control-feedback', true).first();
942             
943             if(feedback){
944                 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
945                 
946                 if(this.getValue().length || this.forceFeedback){
947                     this.el.select('.form-control-feedback', true).first().addClass([this.invalidFeedbackClass]);
948                 }
949                 
950             }
951             
952         }
953         
954         this.fireEvent('invalid', this, msg);
955     },
956     // private
957     SafariOnKeyDown : function(event)
958     {
959         // this is a workaround for a password hang bug on chrome/ webkit.
960         if (this.inputEl().dom.type != 'password') {
961             return;
962         }
963         
964         var isSelectAll = false;
965         
966         if(this.inputEl().dom.selectionEnd > 0){
967             isSelectAll = (this.inputEl().dom.selectionEnd - this.inputEl().dom.selectionStart - this.getValue().length == 0) ? true : false;
968         }
969         if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
970             event.preventDefault();
971             this.setValue('');
972             return;
973         }
974         
975         if(isSelectAll  && event.getCharCode() > 31 && !event.ctrlKey) { // not backspace and delete key (or ctrl-v)
976             
977             event.preventDefault();
978             // this is very hacky as keydown always get's upper case.
979             //
980             var cc = String.fromCharCode(event.getCharCode());
981             this.setValue( event.shiftKey ?  cc : cc.toLowerCase());
982             
983         }
984     },
985     adjustWidth : function(tag, w){
986         tag = tag.toLowerCase();
987         if(typeof w == 'number' && Roo.isStrict && !Roo.isSafari){
988             if(Roo.isIE && (tag == 'input' || tag == 'textarea')){
989                 if(tag == 'input'){
990                     return w + 2;
991                 }
992                 if(tag == 'textarea'){
993                     return w-2;
994                 }
995             }else if(Roo.isOpera){
996                 if(tag == 'input'){
997                     return w + 2;
998                 }
999                 if(tag == 'textarea'){
1000                     return w-2;
1001                 }
1002             }
1003         }
1004         return w;
1005     }
1006     
1007 });
1008
1009