/* This file is part of Ext JS 4.2 Copyright (c) 2011-2013 Sencha Inc Contact: http://www.sencha.com/contact GNU General Public License Usage This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact. Build date: 2013-05-16 14:36:50 (f9be68accb407158ba2b1be2c226a6ce1f649314) */ /** * Base class for all Ext components. * * The Component base class has built-in support for basic hide/show and enable/disable and size control behavior. * * ## xtypes * * Every component has a specific xtype, which is its Ext-specific type name, along with methods for checking the xtype * like {@link #getXType} and {@link #isXType}. See the [Component Guide][1] for more information on xtypes and the * Component hierarchy. * * ## Finding components * * All Components are registered with the {@link Ext.ComponentManager} on construction so that they can be referenced at * any time via {@link Ext#getCmp Ext.getCmp}, passing the {@link #id}. * * Additionally the {@link Ext.ComponentQuery} provides a CSS-selectors-like way to look up components by their xtype * and many other attributes. For example the following code will find all textfield components inside component with * `id: 'myform'`: * * Ext.ComponentQuery.query('#myform textfield'); * * ## Extending Ext.Component * * All subclasses of Component may participate in the automated Ext component * lifecycle of creation, rendering and destruction which is provided by the {@link Ext.container.Container Container} * class. Components may be added to a Container through the {@link Ext.container.Container#cfg-items items} config option * at the time the Container is created, or they may be added dynamically via the * {@link Ext.container.Container#method-add add} method. * * All user-developed visual widgets that are required to participate in automated lifecycle and size management should * subclass Component. * * See the Creating new UI controls chapter in [Component Guide][1] for details on how and to either extend * or augment Ext JS base classes to create custom Components. * * ## The Ext.Component class by itself * * Usually one doesn't need to instantiate the Ext.Component class. There are subclasses which implement * specialized use cases, covering most application needs. However it is possible to instantiate a base * Component, and it can be rendered to document, or handled by layouts as the child item of a Container: * * @example * Ext.create('Ext.Component', { * html: 'Hello world!', * width: 300, * height: 200, * padding: 20, * style: { * color: '#FFFFFF', * backgroundColor:'#000000' * }, * renderTo: Ext.getBody() * }); * * The Component above creates its encapsulating `div` upon render, and use the configured HTML as content. More complex * internal structure may be created using the {@link #renderTpl} configuration, although to display database-derived * mass data, it is recommended that an ExtJS data-backed Component such as a {@link Ext.view.View View}, * {@link Ext.grid.Panel GridPanel}, or {@link Ext.tree.Panel TreePanel} be used. * * [1]: #!/guide/components */ Ext.define('Ext.Component', { /* Begin Definitions */ alias: ['widget.component', 'widget.box'], extend: 'Ext.AbstractComponent', uses: [ 'Ext.util.DelayedTask', 'Ext.Layer', 'Ext.resizer.Resizer', 'Ext.util.ComponentDragger' ], mixins: { floating: 'Ext.util.Floating' }, statics: { // Collapse/expand directions DIRECTION_TOP: 'top', DIRECTION_RIGHT: 'right', DIRECTION_BOTTOM: 'bottom', DIRECTION_LEFT: 'left', VERTICAL_DIRECTION_Re: /^(?:top|bottom)$/, // RegExp whih specifies characters in an xtype which must be translated to '-' when generating auto IDs. // This includes dot, comma and whitespace INVALID_ID_CHARS_Re: /[\.,\s]/g }, /* End Definitions */ /** * @cfg {Boolean/Object} resizable * Specify as `true` to apply a {@link Ext.resizer.Resizer Resizer} to this Component after rendering. * * May also be specified as a config object to be passed to the constructor of {@link Ext.resizer.Resizer Resizer} * to override any defaults. By default the Component passes its minimum and maximum size, and uses * `{@link Ext.resizer.Resizer#dynamic}: false` */ /** * @cfg {String} resizeHandles * A valid {@link Ext.resizer.Resizer} handles config string. Only applies when resizable = true. */ resizeHandles: 'all', /** * @cfg {Boolean} [autoScroll=false] * `true` to use overflow:'auto' on the components layout element and show scroll bars automatically when necessary, * `false` to clip any overflowing content. * This should not be combined with {@link #overflowX} or {@link #overflowY}. */ /** * @cfg {String} overflowX * Possible values are: * * `'auto'` to enable automatic horizontal scrollbar (overflow-x: 'auto'). * * `'scroll'` to always enable horizontal scrollbar (overflow-x: 'scroll'). * The default is overflow-x: 'hidden'. This should not be combined with {@link #autoScroll}. */ /** * @cfg {String} overflowY * Possible values are: * * `'auto'` to enable automatic vertical scrollbar (overflow-y: 'auto'). * * `'scroll'` to always enable vertical scrollbar (overflow-y: 'scroll'). * The default is overflow-y: 'hidden'. This should not be combined with {@link #autoScroll}. */ /** * @cfg {Boolean} floating * Specify as true to float the Component outside of the document flow using CSS absolute positioning. * * Components such as {@link Ext.window.Window Window}s and {@link Ext.menu.Menu Menu}s are floating by default. * * Floating Components that are programatically {@link Ext.Component#method-render rendered} will register * themselves with the global {@link Ext.WindowManager ZIndexManager} * * ### Floating Components as child items of a Container * * A floating Component may be used as a child item of a Container. This just allows the floating Component to seek * a ZIndexManager by examining the ownerCt chain. * * When configured as floating, Components acquire, at render time, a {@link Ext.ZIndexManager ZIndexManager} which * manages a stack of related floating Components. The ZIndexManager brings a single floating Component to the top * of its stack when the Component's {@link #toFront} method is called. * * The ZIndexManager is found by traversing up the {@link #ownerCt} chain to find an ancestor which itself is * floating. This is so that descendant floating Components of floating _Containers_ (Such as a ComboBox dropdown * within a Window) can have its zIndex managed relative to any siblings, but always **above** that floating * ancestor Container. * * If no floating ancestor is found, a floating Component registers itself with the default {@link Ext.WindowManager * ZIndexManager}. * * Floating components _do not participate in the Container's layout_. Because of this, they are not rendered until * you explicitly {@link #method-show} them. * * After rendering, the ownerCt reference is deleted, and the {@link #floatParent} property is set to the found * floating ancestor Container. If no floating ancestor Container was found the {@link #floatParent} property will * not be set. */ floating: false, /** * @cfg {String} [defaultAlign="tl-bl?"] * The default {@link Ext.util.Positionable#getAlignToXY Ext.Element#getAlignToXY} anchor position value for this menu * relative to its element of origin. Used in conjunction with {@link #showBy}. */ defaultAlign: 'tl-bl?', /** * @cfg {Boolean} toFrontOnShow * True to automatically call {@link #toFront} when the {@link #method-show} method is called on an already visible, * floating component. */ toFrontOnShow: true, /** * @cfg {Ext.util.Region/Ext.Element} constrainTo * A {@link Ext.util.Region Region} (or an element from which a Region measurement will be read) which is used * to constrain the component. Only applies when the component is floating. */ /** * @cfg {Object/String} constraintInsets * An object or a string (in TRBL order) specifying insets from the configured {@link #constrainTo constrain region} * within which this component must be constrained when positioning or sizing. * example: * * constraintInsets: '10 10 10 10' // Constrain with 10px insets from parent */ /** * @property {Ext.ZIndexManager} zIndexManager * Only present for {@link #floating} Components after they have been rendered. * * A reference to the ZIndexManager which is managing this Component's z-index. * * The {@link Ext.ZIndexManager ZIndexManager} maintains a stack of floating Component z-indices, and also provides * a single modal mask which is insert just beneath the topmost visible modal floating Component. * * Floating Components may be {@link #toFront brought to the front} or {@link #toBack sent to the back} of the * z-index stack. * * This defaults to the global {@link Ext.WindowManager ZIndexManager} for floating Components that are * programatically {@link Ext.Component#method-render rendered}. * * For {@link #floating} Components which are added to a Container, the ZIndexManager is acquired from the first * ancestor Container found which is floating. If no floating ancestor is found, the global {@link Ext.WindowManager ZIndexManager} is * used. * * See {@link #floating} and {@link #zIndexParent} * @readonly */ /** * @property {Ext.Container} floatParent * **Only present for {@link #floating} Components which were inserted as child items of Containers.** * * There are other similar relationships such as the {@link Ext.button.Button button} which activates a {@link Ext.button.Button#cfg-menu menu}, or the * {@link Ext.menu.Item menu item} which activated a {@link Ext.menu.Item#cfg-menu submenu}, or the * {@link Ext.grid.column.Column column header} which activated the column menu. * * These differences are abstracted away by the {@link #up} method. * * Floating Components that are programatically {@link Ext.Component#method-render rendered} will not have a `floatParent` * property. * * See {@link #floating} and {@link #zIndexManager} * @readonly */ /** * @property {Ext.Container} zIndexParent * Only present for {@link #floating} Components which were inserted as child items of Containers, and which have a floating * Container in their containment ancestry. * * For {@link #floating} Components which are child items of a Container, the zIndexParent will be a floating * ancestor Container which is responsible for the base z-index value of all its floating descendants. It provides * a {@link Ext.ZIndexManager ZIndexManager} which provides z-indexing services for all its descendant floating * Components. * * Floating Components that are programatically {@link Ext.Component#method-render rendered} will not have a `zIndexParent` * property. * * For example, the dropdown {@link Ext.view.BoundList BoundList} of a ComboBox which is in a Window will have the * Window as its `zIndexParent`, and will always show above that Window, wherever the Window is placed in the z-index stack. * * See {@link #floating} and {@link #zIndexManager} * @readonly */ /** * @cfg {Boolean/Object} [draggable=false] * Specify as true to make a {@link #floating} Component draggable using the Component's encapsulating element as * the drag handle. * * This may also be specified as a config object for the {@link Ext.util.ComponentDragger ComponentDragger} which is * instantiated to perform dragging. * * For example to create a Component which may only be dragged around using a certain internal element as the drag * handle, use the delegate option: * * new Ext.Component({ * constrain: true, * floating: true, * style: { * backgroundColor: '#fff', * border: '1px solid black' * }, * html: '
The content
', * draggable: { * delegate: 'h1' * } * }).show(); */ /** * @cfg {Boolean} [formBind=false] * When inside FormPanel, any component configured with `formBind: true` will * be enabled/disabled depending on the validity state of the form. * See {@link Ext.form.Panel} for more information and example. */ /** * @cfg {Number/String} [columnWidth=undefined] * Defines the column width inside {@link Ext.layout.container.Column column layout}. * * Can be specified as a number or as a percentage. */ /** * @cfg {"north"/"south"/"east"/"west"/"center"} [region=undefined] * Defines the region inside {@link Ext.layout.container.Border border layout}. * * Possible values: * * - north - Positions component at top. * - south - Positions component at bottom. * - east - Positions component at right. * - west - Positions component at left. * - center - Positions component at the remaining space. * There **must** be a component with `region: "center"` in every border layout. */ hideMode: 'display', offsetsCls: Ext.baseCSSPrefix + 'hide-offsets', bubbleEvents: [], defaultComponentLayoutType: 'autocomponent', //renderTpl: new Ext.XTemplate( // 'this
Component. Some Containers may
* delegate focus to a descendant Component ({@link Ext.window.Window Window}s can do this through their
* {@link Ext.window.Window#defaultFocus defaultFocus} config option.
*/
focus: function(selectText, delay, callback, scope) {
var me = this,
focusEl,
focusElDom,
containerScrollTop;
// If delay is wanted, queue a call to this function.
if (delay) {
if (!me.focusTask) {
// One global DelayedTask to assign focus
// So that the last focus call wins.
Ext.Component.prototype.focusTask = new Ext.util.DelayedTask(me.focus);
}
me.focusTask.delay(Ext.isNumber(delay) ? delay : 10, null, me, [selectText, false, callback, scope]);
return me;
}
// An immediate focus call must cancel any outstanding delayed focus calls.
if (me.focusTask) {
me.focusTask.cancel();
}
if (me.rendered && !me.isDestroyed && me.isVisible(true) && (focusEl = me.getFocusEl())) {
// getFocusEl might return a Component if a Container wishes to delegate focus to a descendant.
// Window can do this via its defaultFocus configuration which can reference a Button.
if (focusEl.isComponent) {
return focusEl.focus(selectText, delay);
}
// If it was an Element with a dom property
if ((focusElDom = focusEl.dom)) {
// Not a natural focus holding element, add a tab index to make it programatically focusable.
if (focusEl.needsTabIndex()) {
focusElDom.tabIndex = -1;
}
if (me.floating) {
containerScrollTop = me.container.dom.scrollTop;
}
// Focus the element.
// The focusEl has a DOM focus listener on it which invokes the Component's onFocus method
// to perform Component-specific focus processing
focusEl.focus();
if (selectText === true) {
focusElDom.select();
}
// Call the callback when focus is done
Ext.callback(callback, scope);
}
// Focusing a floating Component brings it to the front of its stack.
// this is performed by its zIndexManager. Pass preventFocus true to avoid recursion.
if (me.floating) {
me.toFront(true);
if (containerScrollTop !== undefined) {
me.container.dom.scrollTop = containerScrollTop;
}
}
}
return me;
},
/**
* Cancel any deferred focus on this component
* @protected
*/
cancelFocus: function() {
var task = this.focusTask;
if (task) {
task.cancel();
}
},
// @private
blur: function() {
var focusEl;
if (this.rendered && (focusEl = this.getFocusEl())) {
focusEl.blur();
}
return this;
},
getEl: function() {
return this.el;
},
// Deprecate 5.0
getResizeEl: function() {
return this.el;
},
// Deprecate 5.0
getPositionEl: function() {
return this.el;
},
// Deprecate 5.0
getActionEl: function() {
return this.el;
},
// Deprecate 5.0
getVisibilityEl: function() {
return this.el;
},
/*
* @protected
* Used by {@link Ext.ComponentQuery ComponentQuery}, and the {@link Ext.AbstractComponent#up up} method to find the
* owning Component in the linkage hierarchy.
*
* By default this returns the Container which contains this Component.
*
* This may be overriden by Component authors who implement ownership hierarchies which are not
* based upon ownerCt, such as BoundLists being owned by Fields or Menus being owned by Buttons.
*/
getRefOwner: function() {
return this.ownerCt || this.floatParent;
},
/**
* @protected
* Implements an upward event bubbling policy. By default a Component bubbles events up to its {@link #getRefOwner reference owner}.
*
* Component subclasses may implement a different bubbling strategy by overriding this method.
*/
getBubbleTarget: function() {
return this.getRefOwner();
},
// @private
getContentTarget: function() {
return this.el;
},
/**
* 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(),
cfg = Ext.applyIf(overrides, this.initialConfig),
self;
cfg.id = id;
self = Ext.getClass(this);
// prevent dup id
return new self(cfg);
},
/**
* Gets the xtype for this component as registered with {@link Ext.ComponentManager}. For a list of all available
* xtypes, see the {@link Ext.Component} header. Example usage:
*
* var t = new Ext.form.field.Text();
* alert(t.getXType()); // alerts 'textfield'
*
* @return {String} The xtype
*/
getXType: function() {
return this.self.xtype;
},
/**
* Find a container above this component at any level by a custom function. If the passed function returns true, the
* container will be returned.
*
* See also the {@link Ext.Component#up up} method.
*
* @param {Function} fn The custom function to call with the arguments (container, this component).
* @return {Ext.container.Container} The first Container for which the custom function returns true
*/
findParentBy: function(fn) {
var p;
// Iterate up the ownerCt chain until there's no ownerCt, or we find an ancestor which matches using the selector function.
for (p = this.getBubbleTarget(); p && !fn(p, this); p = p.getBubbleTarget()) {
// do nothing
}
return p || null;
},
/**
* Find a container above this component at any level by xtype or class
*
* See also the {@link Ext.Component#up up} method.
*
* @param {String/Ext.Class} xtype The xtype string for a component, or the class of the component directly
* @return {Ext.container.Container} The first Container which matches the given xtype or class
*/
findParentByType: function(xtype) {
return Ext.isFunction(xtype) ?
this.findParentBy(function(p) {
return p.constructor === xtype;
})
:
this.up(xtype);
},
/**
* Bubbles up the component/container heirarchy, calling the specified function with each component. The scope
* (*this*) of function call will be the scope provided or the current component. The arguments to the function will
* be the args provided or the current component. If the function returns false at any point, the bubble is stopped.
*
* @param {Function} fn The function to call
* @param {Object} [scope] The scope of the function. Defaults to current node.
* @param {Array} [args] The args to call the function with. Defaults to passing the current component.
* @return {Ext.Component} this
*/
bubble: function(fn, scope, args) {
var p = this;
while (p) {
if (fn.apply(scope || p, args || [p]) === false) {
break;
}
p = p.getBubbleTarget();
}
return this;
},
getProxy: function() {
var me = this,
target;
if (!me.proxy) {
target = Ext.getBody();
me.proxy = me.el.createProxy(Ext.baseCSSPrefix + 'proxy-el', target, true);
}
return me.proxy;
},
/*
* For more information on the hierarchy events, see the note for the
* hierarchyEventSource observer defined in the onClassCreated callback.
*
* This functionality is contained in Component (as opposed to Container)
* because a Component can be the ownerCt for a floating component (loadmask),
* and the loadmask needs to know when its owner is shown/hidden via the
* hierarchyEventSource so that its hidden state can be synchronized.
*
* TODO: merge this functionality with Ext.globalEvents
*/
fireHierarchyEvent: function (ename) {
this.hierarchyEventSource.fireEvent(ename, this);
},
onAdded: function() {
this.callParent(arguments);
if (this.hierarchyEventSource.hasListeners.added) {
this.fireHierarchyEvent('added');
}
}
}, function () {
/*
* The observer below is used to be able to detect showing/hiding at various levels
* in the hierarchy. While it's not particularly expensive to bubble an event up,
* cascading an event down can be quite costly.
*
* The main usage for this is to do with floating components. For example, the load mask
* is a floating component. The component it is masking may be inside several containers.
* As such, we need to know when component is hidden, either directly, or via a parent
* container being hidden. We can subscribe to these events and filter out the appropriate
* container.
*/
this.hierarchyEventSource = this.prototype.hierarchyEventSource = new Ext.util.Observable({ events: {
hide: true,
show: true,
collapse: true,
expand: true,
added: true
}});
});