Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / core / src / dom / Element.traversal.js
1 /**
2  * @class Ext.core.Element
3  */
4 Ext.core.Element.addMethods({
5     /**
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)
11      */
12     findParent : function(simpleSelector, maxDepth, returnEl) {
13         var p = this.dom,
14             b = document.body,
15             depth = 0,
16             stopEl;
17
18         maxDepth = maxDepth || 50;
19         if (isNaN(maxDepth)) {
20             stopEl = Ext.getDom(maxDepth);
21             maxDepth = Number.MAX_VALUE;
22         }
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;
26             }
27             depth++;
28             p = p.parentNode;
29         }
30         return null;
31     },
32     
33     /**
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)
40      */
41     findParentNode : function(simpleSelector, maxDepth, returnEl) {
42         var p = Ext.fly(this.dom.parentNode, '_internal');
43         return p ? p.findParent(simpleSelector, maxDepth, returnEl) : null;
44     },
45
46     /**
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)
53      */
54     up : function(simpleSelector, maxDepth) {
55         return this.findParentNode(simpleSelector, maxDepth, true);
56     },
57
58     /**
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
62      */
63     select : function(selector) {
64         return Ext.core.Element.select(selector, false,  this.dom);
65     },
66
67     /**
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
71      */
72     query : function(selector) {
73         return Ext.DomQuery.select(selector, this.dom);
74     },
75
76     /**
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)
81      */
82     down : function(selector, returnDom) {
83         var n = Ext.DomQuery.selectNode(selector, this.dom);
84         return returnDom ? n : Ext.get(n);
85     },
86
87     /**
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)
92      */
93     child : function(selector, returnDom) {
94         var node,
95             me = this,
96             id;
97         id = Ext.get(me).id;
98         // Escape . or :
99         id = id.replace(/[\.:]/g, "\\$0");
100         node = Ext.DomQuery.selectNode('#' + id + " > " + selector, me.dom);
101         return returnDom ? node : Ext.get(node);
102     },
103
104      /**
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
109      */
110     parent : function(selector, returnDom) {
111         return this.matchNode('parentNode', 'parentNode', selector, returnDom);
112     },
113
114      /**
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
119      */
120     next : function(selector, returnDom) {
121         return this.matchNode('nextSibling', 'nextSibling', selector, returnDom);
122     },
123
124     /**
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
129      */
130     prev : function(selector, returnDom) {
131         return this.matchNode('previousSibling', 'previousSibling', selector, returnDom);
132     },
133
134
135     /**
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
140      */
141     first : function(selector, returnDom) {
142         return this.matchNode('nextSibling', 'firstChild', selector, returnDom);
143     },
144
145     /**
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
150      */
151     last : function(selector, returnDom) {
152         return this.matchNode('previousSibling', 'lastChild', selector, returnDom);
153     },
154
155     matchNode : function(dir, start, selector, returnDom) {
156         if (!this.dom) {
157             return null;
158         }
159         
160         var n = this.dom[start];
161         while (n) {
162             if (n.nodeType == 1 && (!selector || Ext.DomQuery.is(n, selector))) {
163                 return !returnDom ? Ext.get(n) : n;
164             }
165             n = n[dir];
166         }
167         return null;
168     }
169 });