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