📄 component.js
字号:
if(typeof this.autoEl == 'string'){
this.el = document.createElement(this.autoEl);
}else{
var div = document.createElement('div');
Ext.DomHelper.overwrite(div, this.autoEl);
this.el = div.firstChild;
}
if (!this.el.id) {
this.el.id = this.getId();
}
}
if(this.el){
this.el = Ext.get(this.el);
if(this.allowDomMove !== false){
ct.dom.insertBefore(this.el.dom, position);
}
}
},
// private
getAutoCreate : function(){
var cfg = typeof this.autoCreate == "object" ?
this.autoCreate : Ext.apply({}, this.defaultAutoCreate);
if(this.id && !cfg.id){
cfg.id = this.id;
}
return cfg;
},
// private
afterRender : Ext.emptyFn,
/**
* Destroys this component by purging any event listeners, removing the component's element from the DOM,
* removing the component from its {@link Ext.Container} (if applicable) and unregistering it from
* {@link Ext.ComponentMgr}. Destruction is generally handled automatically by the framework and this method
* should usually not need to be called directly.
*/
destroy : function(){
if(this.fireEvent("beforedestroy", this) !== false){
this.beforeDestroy();
if(this.rendered){
this.el.removeAllListeners();
this.el.remove();
if(this.actionMode == "container"){
this.container.remove();
}
}
this.onDestroy();
Ext.ComponentMgr.unregister(this);
this.fireEvent("destroy", this);
this.purgeListeners();
}
},
// private
beforeDestroy : Ext.emptyFn,
// private
onDestroy : Ext.emptyFn,
/**
* Returns the underlying {@link Ext.Element}.
* @return {Ext.Element} The element
*/
getEl : function(){
return this.el;
},
/**
* Returns the id of this component.
* @return {String}
*/
getId : function(){
return this.id || (this.id = "ext-comp-" + (++Ext.Component.AUTO_ID));
},
/**
* Returns the item id of this component.
* @return {String}
*/
getItemId : function(){
return this.itemId || this.getId();
},
/**
* Try to focus this component.
* @param {Boolean} selectText (optional) If applicable, true to also select the text in this component
* @param {Boolean/Number} delay (optional) Delay the focus this number of milliseconds (true for 10 milliseconds)
* @return {Ext.Component} this
*/
focus : function(selectText, delay){
if(delay){
this.focus.defer(typeof delay == 'number' ? delay : 10, this, [selectText, false]);
return;
}
if(this.rendered){
this.el.focus();
if(selectText === true){
this.el.dom.select();
}
}
return this;
},
// private
blur : function(){
if(this.rendered){
this.el.blur();
}
return this;
},
/**
* Disable this component.
* @return {Ext.Component} this
*/
disable : function(){
if(this.rendered){
this.onDisable();
}
this.disabled = true;
this.fireEvent("disable", this);
return this;
},
// private
onDisable : function(){
this.getActionEl().addClass(this.disabledClass);
this.el.dom.disabled = true;
},
/**
* Enable this component.
* @return {Ext.Component} this
*/
enable : function(){
if(this.rendered){
this.onEnable();
}
this.disabled = false;
this.fireEvent("enable", this);
return this;
},
// private
onEnable : function(){
this.getActionEl().removeClass(this.disabledClass);
this.el.dom.disabled = false;
},
/**
* Convenience function for setting disabled/enabled by boolean.
* @param {Boolean} disabled
*/
setDisabled : function(disabled){
this[disabled ? "disable" : "enable"]();
},
/**
* Show this component.
* @return {Ext.Component} this
*/
show: function(){
if(this.fireEvent("beforeshow", this) !== false){
this.hidden = false;
if(this.autoRender){
this.render(typeof this.autoRender == 'boolean' ? Ext.getBody() : this.autoRender);
}
if(this.rendered){
this.onShow();
}
this.fireEvent("show", this);
}
return this;
},
// private
onShow : function(){
if(this.hideParent){
this.container.removeClass('x-hide-' + this.hideMode);
}else{
this.getActionEl().removeClass('x-hide-' + this.hideMode);
}
},
/**
* Hide this component.
* @return {Ext.Component} this
*/
hide: function(){
if(this.fireEvent("beforehide", this) !== false){
this.hidden = true;
if(this.rendered){
this.onHide();
}
this.fireEvent("hide", this);
}
return this;
},
// private
onHide : function(){
if(this.hideParent){
this.container.addClass('x-hide-' + this.hideMode);
}else{
this.getActionEl().addClass('x-hide-' + this.hideMode);
}
},
/**
* Convenience function to hide or show this component by boolean.
* @param {Boolean} visible True to show, false to hide
* @return {Ext.Component} this
*/
setVisible: function(visible){
if(visible) {
this.show();
}else{
this.hide();
}
return this;
},
/**
* Returns true if this component is visible.
*/
isVisible : function(){
return this.rendered && this.getActionEl().isVisible();
},
/**
* Clone the current component using the original config values passed into this instance by default.
* @param {Object} overrides A new config containing any properties to override in the cloned version.
* An id property can be passed on this object, otherwise one will be generated to avoid duplicates.
* @return {Ext.Component} clone The cloned copy of this component
*/
cloneConfig : function(overrides){
overrides = overrides || {};
var id = overrides.id || Ext.id();
var cfg = Ext.applyIf(overrides, this.initialConfig);
cfg.id = id; // prevent dup id
return new this.constructor(cfg);
},
/**
* Gets the xtype for this component as registered with {@link Ext.ComponentMgr}. For a list of all
* available xtypes, see the {@link Ext.Component} header. Example usage:
* <pre><code>
var t = new Ext.form.TextField();
alert(t.getXType()); // alerts 'textfield'
</code></pre>
* @return {String} The xtype
*/
getXType : function(){
return this.constructor.xtype;
},
/**
* <p>Tests whether or not this Component is of a specific xtype. This can test whether this Component is descended
* from the xtype (default) or whether it is directly of the xtype specified (shallow = true).</p>
* <p><b>If using your own subclasses, be aware that a Component must register its own xtype
* to participate in determination of inherited xtypes.</b></p>
* <p>For a list of all available xtypes, see the {@link Ext.Component} header.</p>
* <p>Example usage:</p>
* <pre><code>
var t = new Ext.form.TextField();
var isText = t.isXType('textfield'); // true
var isBoxSubclass = t.isXType('box'); // true, descended from BoxComponent
var isBoxInstance = t.isXType('box', true); // false, not a direct BoxComponent instance
</code></pre>
* @param {String} xtype The xtype to check for this Component
* @param {Boolean} shallow (optional) False to check whether this Component is descended from the xtype (this is
* the default), or true to check whether this Component is directly of the specified xtype.
*/
isXType : function(xtype, shallow){
//assume a string by default
if (typeof xtype == 'function'){
xtype = xtype.xtype; //handle being passed the class, eg. Ext.Component
}else if (typeof xtype == 'object'){
xtype = xtype.constructor.xtype; //handle being passed an instance
}
return !shallow ? ('/' + this.getXTypes() + '/').indexOf('/' + xtype + '/') != -1 : this.constructor.xtype == xtype;
},
/**
* <p>Returns this Component's xtype hierarchy as a slash-delimited string. For a list of all
* available xtypes, see the {@link Ext.Component} header.</p>
* <p><b>If using your own subclasses, be aware that a Component must register its own xtype
* to participate in determination of inherited xtypes.</b></p>
* <p>Example usage:</p>
* <pre><code>
var t = new Ext.form.TextField();
alert(t.getXTypes()); // alerts 'component/box/field/textfield'
</pre></code>
* @return {String} The xtype hierarchy string
*/
getXTypes : function(){
var tc = this.constructor;
if(!tc.xtypes){
var c = [], sc = this;
while(sc && sc.constructor.xtype){
c.unshift(sc.constructor.xtype);
sc = sc.constructor.superclass;
}
tc.xtypeChain = c;
tc.xtypes = c.join('/');
}
return tc.xtypes;
},
/**
* Find a container above this component at any level by a custom function. If the passed function returns
* true, the container will be returned. The passed function is called with the arguments (container, this component).
* @param {Function} fcn
* @param {Object} scope (optional)
* @return {Ext.Container} The first Container for which the custom function returns true
*/
findParentBy: function(fn) {
for (var p = this.ownerCt; (p != null) && !fn(p, this); p = p.ownerCt);
return p || null;
},
/**
* Find a container above this component at any level by xtype or class
* @param {String/Class} xtype The xtype string for a component, or the class of the component directly
* @return {Ext.Container} The first Container which matches the given xtype or class
*/
findParentByType: function(xtype) {
return typeof xtype == 'function' ?
this.findParentBy(function(p){
return p.constructor === xtype;
}) :
this.findParentBy(function(p){
return p.constructor.xtype === xtype;
});
},
// internal function for auto removal of assigned event handlers on destruction
mon : function(item, ename, fn, scope, opt){
if(!this.mons){
this.mons = [];
this.on('beforedestroy', function(){
for(var i= 0, len = this.mons.length; i < len; i++){
var m = this.mons[i];
m.item.un(m.ename, m.fn, m.scope);
}
}, this);
}
this.mons.push({
item: item, ename: ename, fn: fn, scope: scope
});
item.on(ename, fn, scope, opt);
}
});
Ext.reg('component', Ext.Component);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -