3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
10 Ext.Element.addMethods(function(){
\r
11 var PARENTNODE = 'parentNode',
\r
12 NEXTSIBLING = 'nextSibling',
\r
13 PREVIOUSSIBLING = 'previousSibling',
\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
25 findParent : function(simpleSelector, maxDepth, returnEl){
\r
30 if(Ext.isGecko && Object.prototype.toString.call(p) == '[object XULElement]') {
\r
33 maxDepth = maxDepth || 50;
\r
34 if (isNaN(maxDepth)) {
\r
35 stopEl = Ext.getDom(maxDepth);
\r
36 maxDepth = Number.MAX_VALUE;
\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
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
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
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
69 up : function(simpleSelector, maxDepth){
\r
70 return this.findParentNode(simpleSelector, maxDepth, true);
\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
79 select : function(selector, unique){
\r
80 return Ext.Element.select(selector, unique, this.dom);
\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
88 query : function(selector, unique){
\r
89 return DQ.select(selector, this.dom);
\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
98 child : function(selector, returnDom){
\r
99 var n = DQ.selectNode(selector, this.dom);
\r
100 return returnDom ? n : GET(n);
\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
109 down : function(selector, returnDom){
\r
110 var n = DQ.selectNode(" > " + selector, this.dom);
\r
111 return returnDom ? n : GET(n);
\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
120 parent : function(selector, returnDom){
\r
121 return this.matchNode(PARENTNODE, PARENTNODE, selector, returnDom);
\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
130 next : function(selector, returnDom){
\r
131 return this.matchNode(NEXTSIBLING, NEXTSIBLING, selector, returnDom);
\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
140 prev : function(selector, returnDom){
\r
141 return this.matchNode(PREVIOUSSIBLING, PREVIOUSSIBLING, selector, returnDom);
\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
151 first : function(selector, returnDom){
\r
152 return this.matchNode(NEXTSIBLING, 'firstChild', selector, returnDom);
\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
161 last : function(selector, returnDom){
\r
162 return this.matchNode(PREVIOUSSIBLING, 'lastChild', selector, returnDom);
\r
165 matchNode : function(dir, start, selector, returnDom){
\r
166 var n = this.dom[start];
\r
168 if(n.nodeType == 1 && (!selector || DQ.is(n, selector))){
\r
169 return !returnDom ? GET(n) : n;
\r