Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / core / core / Element.traversal.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.Element\r
9  */\r
10 Ext.Element.addMethods(function(){\r
11         var PARENTNODE = 'parentNode',\r
12                 NEXTSIBLING = 'nextSibling',\r
13                 PREVIOUSSIBLING = 'previousSibling',\r
14                 DQ = Ext.DomQuery,\r
15                 GET = Ext.get;\r
16         \r
17         return {\r
18                 /**\r
19              * 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)\r
20              * @param {String} selector The simple selector to test\r
21              * @param {Number/Mixed} maxDepth (optional) The max depth to search as a number or element (defaults to 50 || document.body)\r
22              * @param {Boolean} returnEl (optional) True to return a Ext.Element object instead of DOM node\r
23              * @return {HTMLElement} The matching DOM node (or null if no match was found)\r
24              */\r
25             findParent : function(simpleSelector, maxDepth, returnEl){\r
26                 var p = this.dom,\r
27                         b = document.body, \r
28                         depth = 0,                      \r
29                         stopEl;         \r
30             if(Ext.isGecko && Object.prototype.toString.call(p) == '[object XULElement]') {\r
31                 return null;\r
32             }\r
33                 maxDepth = maxDepth || 50;\r
34                 if (isNaN(maxDepth)) {\r
35                     stopEl = Ext.getDom(maxDepth);\r
36                     maxDepth = Number.MAX_VALUE;\r
37                 }\r
38                 while(p && p.nodeType == 1 && depth < maxDepth && p != b && p != stopEl){\r
39                     if(DQ.is(p, simpleSelector)){\r
40                         return returnEl ? GET(p) : p;\r
41                     }\r
42                     depth++;\r
43                     p = p.parentNode;\r
44                 }\r
45                 return null;\r
46             },\r
47         \r
48             /**\r
49              * Looks at parent nodes for a match of the passed simple selector (e.g. div.some-class or span:first-child)\r
50              * @param {String} selector The simple selector to test\r
51              * @param {Number/Mixed} maxDepth (optional) The max depth to\r
52                     search as a number or element (defaults to 10 || document.body)\r
53              * @param {Boolean} returnEl (optional) True to return a Ext.Element object instead of DOM node\r
54              * @return {HTMLElement} The matching DOM node (or null if no match was found)\r
55              */\r
56             findParentNode : function(simpleSelector, maxDepth, returnEl){\r
57                 var p = Ext.fly(this.dom.parentNode, '_internal');\r
58                 return p ? p.findParent(simpleSelector, maxDepth, returnEl) : null;\r
59             },\r
60         \r
61             /**\r
62              * Walks up the dom looking for a parent node that matches the passed simple selector (e.g. div.some-class or span:first-child).\r
63              * This is a shortcut for findParentNode() that always returns an Ext.Element.\r
64              * @param {String} selector The simple selector to test\r
65              * @param {Number/Mixed} maxDepth (optional) The max depth to\r
66                     search as a number or element (defaults to 10 || document.body)\r
67              * @return {Ext.Element} The matching DOM node (or null if no match was found)\r
68              */\r
69             up : function(simpleSelector, maxDepth){\r
70                 return this.findParentNode(simpleSelector, maxDepth, true);\r
71             },\r
72         \r
73             /**\r
74              * Creates a {@link Ext.CompositeElement} for child nodes based on the passed CSS selector (the selector should not contain an id).\r
75              * @param {String} selector The CSS selector\r
76              * @param {Boolean} unique (optional) True to create a unique Ext.Element for each child (defaults to false, which creates a single shared flyweight object)\r
77              * @return {CompositeElement/CompositeElementLite} The composite element\r
78              */\r
79             select : function(selector, unique){\r
80                 return Ext.Element.select(selector, unique, this.dom);\r
81             },\r
82         \r
83             /**\r
84              * Selects child nodes based on the passed CSS selector (the selector should not contain an id).\r
85              * @param {String} selector The CSS selector\r
86              * @return {Array} An array of the matched nodes\r
87              */\r
88             query : function(selector, unique){\r
89                 return DQ.select(selector, this.dom);\r
90             },\r
91         \r
92             /**\r
93              * Selects a single child at any depth below this element based on the passed CSS selector (the selector should not contain an id).\r
94              * @param {String} selector The CSS selector\r
95              * @param {Boolean} returnDom (optional) True to return the DOM node instead of Ext.Element (defaults to false)\r
96              * @return {HTMLElement/Ext.Element} The child Ext.Element (or DOM node if returnDom = true)\r
97              */\r
98             child : function(selector, returnDom){\r
99                 var n = DQ.selectNode(selector, this.dom);\r
100                 return returnDom ? n : GET(n);\r
101             },\r
102         \r
103             /**\r
104              * Selects a single *direct* child based on the passed CSS selector (the selector should not contain an id).\r
105              * @param {String} selector The CSS selector\r
106              * @param {Boolean} returnDom (optional) True to return the DOM node instead of Ext.Element (defaults to false)\r
107              * @return {HTMLElement/Ext.Element} The child Ext.Element (or DOM node if returnDom = true)\r
108              */\r
109             down : function(selector, returnDom){\r
110                 var n = DQ.selectNode(" > " + selector, this.dom);\r
111                 return returnDom ? n : GET(n);\r
112             },\r
113         \r
114                  /**\r
115              * Gets the parent node for this element, optionally chaining up trying to match a selector\r
116              * @param {String} selector (optional) Find a parent node that matches the passed simple selector\r
117              * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element\r
118              * @return {Ext.Element/HTMLElement} The parent node or null\r
119                  */\r
120             parent : function(selector, returnDom){\r
121                 return this.matchNode(PARENTNODE, PARENTNODE, selector, returnDom);\r
122             },\r
123         \r
124              /**\r
125              * Gets the next sibling, skipping text nodes\r
126              * @param {String} selector (optional) Find the next sibling that matches the passed simple selector\r
127              * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element\r
128              * @return {Ext.Element/HTMLElement} The next sibling or null\r
129                  */\r
130             next : function(selector, returnDom){\r
131                 return this.matchNode(NEXTSIBLING, NEXTSIBLING, selector, returnDom);\r
132             },\r
133         \r
134             /**\r
135              * Gets the previous sibling, skipping text nodes\r
136              * @param {String} selector (optional) Find the previous sibling that matches the passed simple selector\r
137              * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element\r
138              * @return {Ext.Element/HTMLElement} The previous sibling or null\r
139                  */\r
140             prev : function(selector, returnDom){\r
141                 return this.matchNode(PREVIOUSSIBLING, PREVIOUSSIBLING, selector, returnDom);\r
142             },\r
143         \r
144         \r
145             /**\r
146              * Gets the first child, skipping text nodes\r
147              * @param {String} selector (optional) Find the next sibling that matches the passed simple selector\r
148              * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element\r
149              * @return {Ext.Element/HTMLElement} The first child or null\r
150                  */\r
151             first : function(selector, returnDom){\r
152                 return this.matchNode(NEXTSIBLING, 'firstChild', selector, returnDom);\r
153             },\r
154         \r
155             /**\r
156              * Gets the last child, skipping text nodes\r
157              * @param {String} selector (optional) Find the previous sibling that matches the passed simple selector\r
158              * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element\r
159              * @return {Ext.Element/HTMLElement} The last child or null\r
160                  */\r
161             last : function(selector, returnDom){\r
162                 return this.matchNode(PREVIOUSSIBLING, 'lastChild', selector, returnDom);\r
163             },\r
164             \r
165             matchNode : function(dir, start, selector, returnDom){\r
166                 var n = this.dom[start];\r
167                 while(n){\r
168                     if(n.nodeType == 1 && (!selector || DQ.is(n, selector))){\r
169                         return !returnDom ? GET(n) : n;\r
170                     }\r
171                     n = n[dir];\r
172                 }\r
173                 return null;\r
174             }   \r
175     }\r
176 }());