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