/*
* Based on:
* Ext JS Library 1.1.1
* Copyright(c) 2006-2007, Ext JS, LLC.
*
* Originally Released Under LGPL - original licence link has changed is not relivant.
*
* Fork - LGPL
* <script type="text/javascript">
*/
/**
* @class Roo.form.Field
* @extends Roo.BoxComponent
* Base class for form fields that provides default event handling, sizing, value handling and other functionality.
* @constructor
* Creates a new Field
* @param {Object} config Configuration options
*/
Roo.form.Field = function(config){
Roo.form.Field.superclass.constructor.call(this, config);
};
Roo.extend(Roo.form.Field, Roo.BoxComponent, {
/**
* @cfg {String} fieldLabel Label to use when rendering a form.
*/
/**
* @cfg {String} qtip Mouse over tip
*/
/**
* @cfg {String} invalidClass The CSS class to use when marking a field invalid (defaults to "x-form-invalid")
*/
invalidClass : "x-form-invalid",
/**
* @cfg {String} invalidText The error text to use when marking a field invalid and no message is provided (defaults to "The value in this field is invalid")
*/
invalidText : "The value in this field is invalid",
/**
* @cfg {String} focusClass The CSS class to use when the field receives focus (defaults to "x-form-focus")
*/
focusClass : "x-form-focus",
/**
* @cfg {String/Boolean} validationEvent The event that should initiate field validation. Set to false to disable
automatic validation (defaults to "keyup").
*/
validationEvent : "keyup",
/**
* @cfg {Boolean} validateOnBlur Whether the field should validate when it loses focus (defaults to true).
*/
validateOnBlur : true,
/**
* @cfg {Number} validationDelay The length of time in milliseconds after user input begins until validation is initiated (defaults to 250)
*/
validationDelay : 250,
/**
* @cfg {String/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to
* {tag: "input", type: "text", size: "20", autocomplete: "off"})
*/
defaultAutoCreate : {tag: "input", type: "text", size: "20", autocomplete: "new-password"},
/**
* @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field")
*/
fieldClass : "x-form-field",
/**
* @cfg {String} msgTarget The location where error text should display. Should be one of the following values (defaults to 'qtip'):
*<pre>
Value Description
----------- ----------------------------------------------------------------------
qtip Display a quick tip when the user hovers over the field
title Display a default browser title attribute popup
under Add a block div beneath the field containing the error text
side Add an error icon to the right of the field with a popup on hover
[element id] Add the error text directly to the innerHTML of the specified element
</pre>
*/
msgTarget : 'qtip',
/**
* @cfg {String} msgFx <b>Experimental</b> The effect used when displaying a validation message under the field (defaults to 'normal').
*/
msgFx : 'normal',
/**
* @cfg {Boolean} readOnly True to mark the field as readOnly in HTML (defaults to false) -- Note: this only sets the element's readOnly DOM attribute.
*/
readOnly : false,
/**
* @cfg {Boolean} disabled True to disable the field (defaults to false).
*/
disabled : false,
/**
* @cfg {String} inputType The type attribute for input fields -- e.g. radio, text, password (defaults to "text").
*/
inputType : undefined,
/**
* @cfg {Number} tabIndex The tabIndex for this field. Note this only applies to fields that are rendered, not those which are built via applyTo (defaults to undefined).
*/
tabIndex : undefined,
// private
isFormField : true,
// private
hasFocus : false,
/**
* @property {Roo.Element} fieldEl
* Element Containing the rendered Field (with label etc.)
*/
/**
* @cfg {Mixed} value A value to initialize this field with.
*/
value : undefined,
/**
* @cfg {String} name The field's HTML name attribute.
*/
/**
* @cfg {String} cls A CSS class to apply to the field's underlying element.
*/
// private
loadedValue : false,
// private ??
initComponent : function(){
Roo.form.Field.superclass.initComponent.call(this);
this.addEvents({
/**
* @event focus
* Fires when this field receives input focus.
* @param {Roo.form.Field} this
*/
focus : true,
/**
* @event blur
* Fires when this field loses input focus.
* @param {Roo.form.Field} this
*/
blur : true,
/**
* @event specialkey
* Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed. You can check
* {@link Roo.EventObject#getKey} to determine which key was pressed.
* @param {Roo.form.Field} this
* @param {Roo.EventObject} e The event object
*/
specialkey : true,
/**
* @event change
* Fires just before the field blurs if the field value has changed.
* @param {Roo.form.Field} this
* @param {Mixed} newValue The new value
* @param {Mixed} oldValue The original value
*/
change : true,
/**
* @event invalid
* Fires after the field has been marked as invalid.
* @param {Roo.form.Field} this
* @param {String} msg The validation message
*/
invalid : true,
/**
* @event valid
* Fires after the field has been validated with no errors.
* @param {Roo.form.Field} this
*/
valid : true,
/**
* @event keyup
* Fires after the key up
* @param {Roo.form.Field} this
* @param {Roo.EventObject} e The event Object
*/
keyup : true
});
},
/**
* Returns the name attribute of the field if available
* @return {String} name The field name
*/
getName: function(){
return this.rendered && this.el.dom.name ? this.el.dom.name : (this.hiddenName || '');
},
// private
onRender : function(ct, position){
Roo.form.Field.superclass.onRender.call(this, ct, position);
if(!this.el){
var cfg = this.getAutoCreate();
if(!cfg.name){
cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
}
if (!cfg.name.length) {
delete cfg.name;
}
if(this.inputType){
cfg.type = this.inputType;
}
this.el = ct.createChild(cfg, position);
}
var type = this.el.dom.type;
if(type){
if(type == 'password'){
type = 'text';
}
this.el.addClass('x-form-'+type);
}
if(this.readOnly){
this.el.dom.readOnly = true;
}
if(this.tabIndex !== undefined){
this.el.dom.setAttribute('tabIndex', this.tabIndex);
}
this.el.addClass([this.fieldClass, this.cls]);
this.initValue();
},
/**
* Apply the behaviors of this component to an existing element. <b>This is used instead of render().</b>
* @param {String/HTMLElement/Element} el The id of the node, a DOM node or an existing Element
* @return {Roo.form.Field} this
*/
applyTo : function(target){
this.allowDomMove = false;
this.el = Roo.get(target);
this.render(this.el.dom.parentNode);
return this;
},
// private
initValue : function(){
if(this.value !== undefined){
this.setValue(this.value);
}else if(this.el.dom.value.length > 0){
this.setValue(this.el.dom.value);
}
},
/**
* Returns true if this field has been changed since it was originally loaded and is not disabled.
* DEPRICATED - it never worked well - use hasChanged/resetHasChanged.
*/
isDirty : function() {
if(this.disabled) {
return false;
}
return String(this.getValue()) !== String(this.originalValue);
},
/**
* stores the current value in loadedValue
*/
resetHasChanged : function()
{
this.loadedValue = String(this.getValue());
},
/**
* checks the current value against the 'loaded' value.
* Note - will return false if 'resetHasChanged' has not been called first.
*/
hasChanged : function()
{
if(this.disabled || this.readOnly) {
return false;
}
return this.loadedValue !== false && String(this.getValue()) !== this.loadedValue;
},
// private
afterRender : function(){
Roo.form.Field.superclass.afterRender.call(this);
this.initEvents();
},
// private
fireKey : function(e){
//Roo.log('field ' + e.getKey());
if(e.isNavKeyPress()){
this.fireEvent("specialkey", this, e);
}
},
/**
* Resets the current field value to the originally loaded value and clears any validation messages
*/
reset : function(){
this.setValue(this.resetValue);
this.originalValue = this.getValue();
this.clearInvalid();
},
// private
initEvents : function(){
// safari killled keypress - so keydown is now used..
this.el.on("keydown" , this.fireKey, this);
this.el.on("focus", this.onFocus, this);
this.el.on("blur", this.onBlur, this);
this.el.relayEvent('keyup', this);
// reference to original value for reset
this.originalValue = this.getValue();
this.resetValue = this.getValue();
},
// private
onFocus : function(){
if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
this.el.addClass(this.focusClass);
}
if(!this.hasFocus){
this.hasFocus = true;
this.startValue = this.getValue();
this.fireEvent("focus", this);
}
},
beforeBlur : Roo.emptyFn,
// private
onBlur : function(){
this.beforeBlur();
if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
this.el.removeClass(this.focusClass);
}
this.hasFocus = false;
if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
this.validate();
}
var v = this.getValue();
if(String(v) !== String(this.startValue)){
this.fireEvent('change', this, v, this.startValue);
}
this.fireEvent("blur", this);
},
/**
* Returns whether or not the field value is currently valid
* @param {Boolean} preventMark True to disable marking the field invalid
* @return {Boolean} True if the value is valid, else false
*/
isValid : function(preventMark){
if(this.disabled){
return true;
}
var restore = this.preventMark;
this.preventMark = preventMark === true;
var v = this.validateValue(this.processValue(this.getRawValue()));
this.preventMark = restore;
return v;
},
/**
* Validates the field value
* @return {Boolean} True if the value is valid, else false
*/
validate : function(){
if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
this.clearInvalid();
return true;
}
return false;
},
processValue : function(value){
return value;
},
// private
// Subclasses should provide the validation implementation by overriding this
validateValue : function(value){
return true;
},
/**
* Mark this field as invalid
* @param {String} msg The validation message
*/
markInvalid : function(msg){
if(!this.rendered || this.preventMark){ // not rendered
return;
}
var obj = (typeof(this.combo) != 'undefined') ? this.combo : this; // fix the combox array!!
obj.el.addClass(this.invalidClass);
msg = msg || this.invalidText;
switch(this.msgTarget){
case 'qtip':
obj.el.dom.qtip = msg;
obj.el.dom.qclass = 'x-form-invalid-tip';
if(Roo.QuickTips){ // fix for floating editors interacting with DND
Roo.QuickTips.enable();
}
break;
case 'title':
this.el.dom.title = msg;
break;
case 'under':
if(!this.errorEl){
var elp = this.el.findParent('.x-form-element', 5, true);
this.errorEl = elp.createChild({cls:'x-form-invalid-msg'});
this.errorEl.setWidth(elp.getWidth(true)-20);
}
this.errorEl.update(msg);
Roo.form.Field.msgFx[this.msgFx].show(this.errorEl, this);
break;
case 'side':
if(!this.errorIcon){
var elp = this.el.findParent('.x-form-element', 5, true);
this.errorIcon = elp.createChild({cls:'x-form-invalid-icon'});
}
this.alignErrorIcon();
this.errorIcon.dom.qtip = msg;
this.errorIcon.dom.qclass = 'x-form-invalid-tip';
this.errorIcon.show();
this.on('resize', this.alignErrorIcon, this);
break;
default:
var t = Roo.getDom(this.msgTarget);
t.innerHTML = msg;
t.style.display = this.msgDisplay;
break;
}
this.fireEvent('invalid', this, msg);
},
// private
alignErrorIcon : function(){
this.errorIcon.alignTo(this.el, 'tl-tr', [2, 0]);
},
/**
* Clear any invalid styles/messages for this field
*/
clearInvalid : function(){
if(!this.rendered || this.preventMark){ // not rendered
return;
}
var obj = (typeof(this.combo) != 'undefined') ? this.combo : this; // fix the combox array!!
obj.el.removeClass(this.invalidClass);
switch(this.msgTarget){
case 'qtip':
obj.el.dom.qtip = '';
break;
case 'title':
this.el.dom.title = '';
break;
case 'under':
if(this.errorEl){
Roo.form.Field.msgFx[this.msgFx].hide(this.errorEl, this);
}
break;
case 'side':
if(this.errorIcon){
this.errorIcon.dom.qtip = '';
this.errorIcon.hide();
this.un('resize', this.alignErrorIcon, this);
}
break;
default:
var t = Roo.getDom(this.msgTarget);
t.innerHTML = '';
t.style.display = 'none';
break;
}
this.fireEvent('valid', this);
},
/**
* Returns the raw data value which may or may not be a valid, defined value. To return a normalized value see {@link #getValue}.
* @return {Mixed} value The field value
*/
getRawValue : function(){
var v = this.el.getValue();
return v;
},
/**
* Returns the normalized data value (undefined or emptyText will be returned as ''). To return the raw value see {@link #getRawValue}.
* @return {Mixed} value The field value
*/
getValue : function(){
var v = this.el.getValue();
return v;
},
/**
* Sets the underlying DOM field's value directly, bypassing validation. To set the value with validation see {@link #setValue}.
* @param {Mixed} value The value to set
*/
setRawValue : function(v){
return this.el.dom.value = (v === null || v === undefined ? '' : v);
},
/**
* Sets a data value into the field and validates it. To set the value directly without validation see {@link #setRawValue}.
* @param {Mixed} value The value to set
*/
setValue : function(v){
this.value = v;
if(this.rendered){
this.el.dom.value = (v === null || v === undefined ? '' : v);
this.validate();
}
},
adjustSize : function(w, h){
var s = Roo.form.Field.superclass.adjustSize.call(this, w, h);
s.width = this.adjustWidth(this.el.dom.tagName, s.width);
return s;
},
adjustWidth : function(tag, w){
tag = tag.toLowerCase();
if(typeof w == 'number' && Roo.isStrict && !Roo.isSafari){
if(Roo.isIE && (tag == 'input' || tag == 'textarea')){
if(tag == 'input'){
return w + 2;
}
if(tag == 'textarea'){
return w-2;
}
}else if(Roo.isOpera){
if(tag == 'input'){
return w + 2;
}
if(tag == 'textarea'){
return w-2;
}
}
}
return w;
}
});
// anything other than normal should be considered experimental
Roo.form.Field.msgFx = {
normal : {
show: function(msgEl, f){
msgEl.setDisplayed('block');
},
hide : function(msgEl, f){
msgEl.setDisplayed(false).update('');
}
},
slide : {
show: function(msgEl, f){
msgEl.slideIn('t', {stopFx:true});
},
hide : function(msgEl, f){
msgEl.slideOut('t', {stopFx:true,useDisplay:true});
}
},
slideRight : {
show: function(msgEl, f){
msgEl.fixDisplay();
msgEl.alignTo(f.el, 'tl-tr');
msgEl.slideIn('l', {stopFx:true});
},
hide : function(msgEl, f){
msgEl.slideOut('l', {stopFx:true,useDisplay:true});
}
}
};