Upgrade to ExtJS 3.1.1 - Released 02/08/2010
[extjs.git] / src / core / core / Element.traversal.js
1 /*!
2  * Ext JS Library 3.1.1
3  * Copyright(c) 2006-2010 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              * @return {CompositeElement/CompositeElementLite} The composite element\r
77              */\r
78             select : function(selector){\r
79                 return Ext.Element.select(selector, this.dom);\r
80             },\r
81         \r
82             /**\r
83              * Selects child nodes based on the passed CSS selector (the selector should not contain an id).\r
84              * @param {String} selector The CSS selector\r
85              * @return {Array} An array of the matched nodes\r
86              */\r
87             query : function(selector){\r
88                 return DQ.select(selector, this.dom);\r
89             },\r
90         \r
91             /**\r
92              * Selects a single child at any depth below this element based on the passed CSS selector (the selector should not contain an id).\r
93              * @param {String} selector The CSS selector\r
94              * @param {Boolean} returnDom (optional) True to return the DOM node instead of Ext.Element (defaults to false)\r
95              * @return {HTMLElement/Ext.Element} The child Ext.Element (or DOM node if returnDom = true)\r
96              */\r
97             child : function(selector, returnDom){\r
98                 var n = DQ.selectNode(selector, this.dom);\r
99                 return returnDom ? n : GET(n);\r
100             },\r
101         \r
102             /**\r
103              * Selects a single *direct* child based on the passed CSS selector (the selector should not contain an id).\r
104              * @param {String} selector The CSS selector\r
105              * @param {Boolean} returnDom (optional) True to return the DOM node instead of Ext.Element (defaults to false)\r
106              * @return {HTMLElement/Ext.Element} The child Ext.Element (or DOM node if returnDom = true)\r
107              */\r
108             down : function(selector, returnDom){\r
109                 var n = DQ.selectNode(" > " + selector, this.dom);\r
110                 return returnDom ? n : GET(n);\r
111             },\r
112         \r
113                  /**\r
114              * Gets the parent node for this element, optionally chaining up trying to match a selector\r
115              * @param {String} selector (optional) Find a parent node that matches the passed simple selector\r
116              * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element\r
117              * @return {Ext.Element/HTMLElement} The parent node or null\r
118                  */\r
119             parent : function(selector, returnDom){\r
120                 return this.matchNode(PARENTNODE, PARENTNODE, selector, returnDom);\r
121             },\r
122         \r
123              /**\r
124              * Gets the next sibling, skipping text nodes\r
125              * @param {String} selector (optional) Find the next sibling that matches the passed simple selector\r
126              * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element\r
127              * @return {Ext.Element/HTMLElement} The next sibling or null\r
128                  */\r
129             next : function(selector, returnDom){\r
130                 return this.matchNode(NEXTSIBLING, NEXTSIBLING, selector, returnDom);\r
131             },\r
132         \r
133             /**\r
134              * Gets the previous sibling, skipping text nodes\r
135              * @param {String} selector (optional) Find the previous sibling that matches the passed simple selector\r
136              * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element\r
137              * @return {Ext.Element/HTMLElement} The previous sibling or null\r
138                  */\r
139             prev : function(selector, returnDom){\r
140                 return this.matchNode(PREVIOUSSIBLING, PREVIOUSSIBLING, selector, returnDom);\r
141             },\r
142         \r
143         \r
144             /**\r
145              * Gets the first child, skipping text nodes\r
146              * @param {String} selector (optional) Find the next sibling that matches the passed simple selector\r
147              * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element\r
148              * @return {Ext.Element/HTMLElement} The first child or null\r
149                  */\r
150             first : function(selector, returnDom){\r
151                 return this.matchNode(NEXTSIBLING, 'firstChild', selector, returnDom);\r
152             },\r
153         \r
154             /**\r
155              * Gets the last child, skipping text nodes\r
156              * @param {String} selector (optional) Find the previous sibling that matches the passed simple selector\r
157              * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element\r
158              * @return {Ext.Element/HTMLElement} The last child or null\r
159                  */\r
160             last : function(selector, returnDom){\r
161                 return this.matchNode(PREVIOUSSIBLING, 'lastChild', selector, returnDom);\r
162             },\r
163             \r
164             matchNode : function(dir, start, selector, returnDom){\r
165                 var n = this.dom[start];\r
166                 while(n){\r
167                     if(n.nodeType == 1 && (!selector || DQ.is(n, selector))){\r
168                         return !returnDom ? GET(n) : n;\r
169                     }\r
170                     n = n[dir];\r
171                 }\r
172                 return null;\r
173             }   \r
174     }\r
175 }());