2 * @class Ext.core.Element
4 Ext.core.Element.addMethods({
6 * Looks at this node and then at parent nodes for a match of the passed simple selector (e.g. div.some-class or span:first-child)
7 * @param {String} selector The simple selector to test
8 * @param {Number/Mixed} maxDepth (optional) The max depth to search as a number or element (defaults to 50 || document.body)
9 * @param {Boolean} returnEl (optional) True to return a Ext.core.Element object instead of DOM node
10 * @return {HTMLElement} The matching DOM node (or null if no match was found)
12 findParent : function(simpleSelector, maxDepth, returnEl) {
18 maxDepth = maxDepth || 50;
19 if (isNaN(maxDepth)) {
20 stopEl = Ext.getDom(maxDepth);
21 maxDepth = Number.MAX_VALUE;
23 while (p && p.nodeType == 1 && depth < maxDepth && p != b && p != stopEl) {
24 if (Ext.DomQuery.is(p, simpleSelector)) {
25 return returnEl ? Ext.get(p) : p;
34 * Looks at parent nodes for a match of the passed simple selector (e.g. div.some-class or span:first-child)
35 * @param {String} selector The simple selector to test
36 * @param {Number/Mixed} maxDepth (optional) The max depth to
37 search as a number or element (defaults to 10 || document.body)
38 * @param {Boolean} returnEl (optional) True to return a Ext.core.Element object instead of DOM node
39 * @return {HTMLElement} The matching DOM node (or null if no match was found)
41 findParentNode : function(simpleSelector, maxDepth, returnEl) {
42 var p = Ext.fly(this.dom.parentNode, '_internal');
43 return p ? p.findParent(simpleSelector, maxDepth, returnEl) : null;
47 * Walks up the dom looking for a parent node that matches the passed simple selector (e.g. div.some-class or span:first-child).
48 * This is a shortcut for findParentNode() that always returns an Ext.core.Element.
49 * @param {String} selector The simple selector to test
50 * @param {Number/Mixed} maxDepth (optional) The max depth to
51 search as a number or element (defaults to 10 || document.body)
52 * @return {Ext.core.Element} The matching DOM node (or null if no match was found)
54 up : function(simpleSelector, maxDepth) {
55 return this.findParentNode(simpleSelector, maxDepth, true);
59 * Creates a {@link Ext.CompositeElement} for child nodes based on the passed CSS selector (the selector should not contain an id).
60 * @param {String} selector The CSS selector
61 * @return {CompositeElement/CompositeElement} The composite element
63 select : function(selector) {
64 return Ext.core.Element.select(selector, false, this.dom);
68 * Selects child nodes based on the passed CSS selector (the selector should not contain an id).
69 * @param {String} selector The CSS selector
70 * @return {Array} An array of the matched nodes
72 query : function(selector) {
73 return Ext.DomQuery.select(selector, this.dom);
77 * Selects a single child at any depth below this element based on the passed CSS selector (the selector should not contain an id).
78 * @param {String} selector The CSS selector
79 * @param {Boolean} returnDom (optional) True to return the DOM node instead of Ext.core.Element (defaults to false)
80 * @return {HTMLElement/Ext.core.Element} The child Ext.core.Element (or DOM node if returnDom = true)
82 down : function(selector, returnDom) {
83 var n = Ext.DomQuery.selectNode(selector, this.dom);
84 return returnDom ? n : Ext.get(n);
88 * Selects a single *direct* child based on the passed CSS selector (the selector should not contain an id).
89 * @param {String} selector The CSS selector
90 * @param {Boolean} returnDom (optional) True to return the DOM node instead of Ext.core.Element (defaults to false)
91 * @return {HTMLElement/Ext.core.Element} The child Ext.core.Element (or DOM node if returnDom = true)
93 child : function(selector, returnDom) {
99 id = id.replace(/[\.:]/g, "\\$0");
100 node = Ext.DomQuery.selectNode('#' + id + " > " + selector, me.dom);
101 return returnDom ? node : Ext.get(node);
105 * Gets the parent node for this element, optionally chaining up trying to match a selector
106 * @param {String} selector (optional) Find a parent node that matches the passed simple selector
107 * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.core.Element
108 * @return {Ext.core.Element/HTMLElement} The parent node or null
110 parent : function(selector, returnDom) {
111 return this.matchNode('parentNode', 'parentNode', selector, returnDom);
115 * Gets the next sibling, skipping text nodes
116 * @param {String} selector (optional) Find the next sibling that matches the passed simple selector
117 * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.core.Element
118 * @return {Ext.core.Element/HTMLElement} The next sibling or null
120 next : function(selector, returnDom) {
121 return this.matchNode('nextSibling', 'nextSibling', selector, returnDom);
125 * Gets the previous sibling, skipping text nodes
126 * @param {String} selector (optional) Find the previous sibling that matches the passed simple selector
127 * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.core.Element
128 * @return {Ext.core.Element/HTMLElement} The previous sibling or null
130 prev : function(selector, returnDom) {
131 return this.matchNode('previousSibling', 'previousSibling', selector, returnDom);
136 * Gets the first child, skipping text nodes
137 * @param {String} selector (optional) Find the next sibling that matches the passed simple selector
138 * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.core.Element
139 * @return {Ext.core.Element/HTMLElement} The first child or null
141 first : function(selector, returnDom) {
142 return this.matchNode('nextSibling', 'firstChild', selector, returnDom);
146 * Gets the last child, skipping text nodes
147 * @param {String} selector (optional) Find the previous sibling that matches the passed simple selector
148 * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.core.Element
149 * @return {Ext.core.Element/HTMLElement} The last child or null
151 last : function(selector, returnDom) {
152 return this.matchNode('previousSibling', 'lastChild', selector, returnDom);
155 matchNode : function(dir, start, selector, returnDom) {
160 var n = this.dom[start];
162 if (n.nodeType == 1 && (!selector || Ext.DomQuery.is(n, selector))) {
163 return !returnDom ? Ext.get(n) : n;