3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
10 Ext.Element.addMethods(function(){
11 var PARENTNODE = 'parentNode',
12 NEXTSIBLING = 'nextSibling',
13 PREVIOUSSIBLING = 'previousSibling',
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)
20 * @param {String} selector The simple selector to test
21 * @param {Number/Mixed} maxDepth (optional) The max depth to search as a number or element (defaults to 50 || document.body)
22 * @param {Boolean} returnEl (optional) True to return a Ext.Element object instead of DOM node
23 * @return {HTMLElement} The matching DOM node (or null if no match was found)
25 findParent : function(simpleSelector, maxDepth, returnEl){
30 if(Ext.isGecko && Object.prototype.toString.call(p) == '[object XULElement]') {
33 maxDepth = maxDepth || 50;
34 if (isNaN(maxDepth)) {
35 stopEl = Ext.getDom(maxDepth);
36 maxDepth = Number.MAX_VALUE;
38 while(p && p.nodeType == 1 && depth < maxDepth && p != b && p != stopEl){
39 if(DQ.is(p, simpleSelector)){
40 return returnEl ? GET(p) : p;
49 * Looks at parent nodes for a match of the passed simple selector (e.g. div.some-class or span:first-child)
50 * @param {String} selector The simple selector to test
51 * @param {Number/Mixed} maxDepth (optional) The max depth to
52 search as a number or element (defaults to 10 || document.body)
53 * @param {Boolean} returnEl (optional) True to return a Ext.Element object instead of DOM node
54 * @return {HTMLElement} The matching DOM node (or null if no match was found)
56 findParentNode : function(simpleSelector, maxDepth, returnEl){
57 var p = Ext.fly(this.dom.parentNode, '_internal');
58 return p ? p.findParent(simpleSelector, maxDepth, returnEl) : null;
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).
63 * This is a shortcut for findParentNode() that always returns an Ext.Element.
64 * @param {String} selector The simple selector to test
65 * @param {Number/Mixed} maxDepth (optional) The max depth to
66 search as a number or element (defaults to 10 || document.body)
67 * @return {Ext.Element} The matching DOM node (or null if no match was found)
69 up : function(simpleSelector, maxDepth){
70 return this.findParentNode(simpleSelector, maxDepth, true);
74 * Creates a {@link Ext.CompositeElement} for child nodes based on the passed CSS selector (the selector should not contain an id).
75 * @param {String} selector The CSS selector
76 * @return {CompositeElement/CompositeElementLite} The composite element
78 select : function(selector){
79 return Ext.Element.select(selector, this.dom);
83 * Selects child nodes based on the passed CSS selector (the selector should not contain an id).
84 * @param {String} selector The CSS selector
85 * @return {Array} An array of the matched nodes
87 query : function(selector){
88 return DQ.select(selector, this.dom);
92 * Selects a single child at any depth below this element based on the passed CSS selector (the selector should not contain an id).
93 * @param {String} selector The CSS selector
94 * @param {Boolean} returnDom (optional) True to return the DOM node instead of Ext.Element (defaults to false)
95 * @return {HTMLElement/Ext.Element} The child Ext.Element (or DOM node if returnDom = true)
97 child : function(selector, returnDom){
98 var n = DQ.selectNode(selector, this.dom);
99 return returnDom ? n : GET(n);
103 * Selects a single *direct* child based on the passed CSS selector (the selector should not contain an id).
104 * @param {String} selector The CSS selector
105 * @param {Boolean} returnDom (optional) True to return the DOM node instead of Ext.Element (defaults to false)
106 * @return {HTMLElement/Ext.Element} The child Ext.Element (or DOM node if returnDom = true)
108 down : function(selector, returnDom){
109 var n = DQ.selectNode(" > " + selector, this.dom);
110 return returnDom ? n : GET(n);
114 * Gets the parent node for this element, optionally chaining up trying to match a selector
115 * @param {String} selector (optional) Find a parent node that matches the passed simple selector
116 * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element
117 * @return {Ext.Element/HTMLElement} The parent node or null
119 parent : function(selector, returnDom){
120 return this.matchNode(PARENTNODE, PARENTNODE, selector, returnDom);
124 * Gets the next sibling, skipping text nodes
125 * @param {String} selector (optional) Find the next sibling that matches the passed simple selector
126 * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element
127 * @return {Ext.Element/HTMLElement} The next sibling or null
129 next : function(selector, returnDom){
130 return this.matchNode(NEXTSIBLING, NEXTSIBLING, selector, returnDom);
134 * Gets the previous sibling, skipping text nodes
135 * @param {String} selector (optional) Find the previous sibling that matches the passed simple selector
136 * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element
137 * @return {Ext.Element/HTMLElement} The previous sibling or null
139 prev : function(selector, returnDom){
140 return this.matchNode(PREVIOUSSIBLING, PREVIOUSSIBLING, selector, returnDom);
145 * Gets the first child, skipping text nodes
146 * @param {String} selector (optional) Find the next sibling that matches the passed simple selector
147 * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element
148 * @return {Ext.Element/HTMLElement} The first child or null
150 first : function(selector, returnDom){
151 return this.matchNode(NEXTSIBLING, 'firstChild', selector, returnDom);
155 * Gets the last child, skipping text nodes
156 * @param {String} selector (optional) Find the previous sibling that matches the passed simple selector
157 * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element
158 * @return {Ext.Element/HTMLElement} The last child or null
160 last : function(selector, returnDom){
161 return this.matchNode(PREVIOUSSIBLING, 'lastChild', selector, returnDom);
164 matchNode : function(dir, start, selector, returnDom){
165 var n = this.dom[start];
167 if(n.nodeType == 1 && (!selector || DQ.is(n, selector))){
168 return !returnDom ? GET(n) : n;