source: trunk/web/addons/job_monarch/lib/extjs-30/src/core/core/Element.traversal.js @ 625

Last change on this file since 625 was 625, checked in by ramonb, 15 years ago

lib/extjs-30:

  • new ExtJS 3.0
File size: 7.8 KB
Line 
1/*!
2 * Ext JS Library 3.0.0
3 * Copyright(c) 2006-2009 Ext JS, LLC
4 * licensing@extjs.com
5 * http://www.extjs.com/license
6 */
7/**
8 * @class Ext.Element
9 */
10Ext.Element.addMethods(function(){
11        var PARENTNODE = 'parentNode',
12                NEXTSIBLING = 'nextSibling',
13                PREVIOUSSIBLING = 'previousSibling',
14                DQ = Ext.DomQuery,
15                GET = Ext.get;
16       
17        return {
18                /**
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)
24             */
25            findParent : function(simpleSelector, maxDepth, returnEl){
26                var p = this.dom,
27                        b = document.body, 
28                        depth = 0,                     
29                        stopEl;         
30            if(Ext.isGecko && Object.prototype.toString.call(p) == '[object XULElement]') {
31                return null;
32            }
33                maxDepth = maxDepth || 50;
34                if (isNaN(maxDepth)) {
35                    stopEl = Ext.getDom(maxDepth);
36                    maxDepth = Number.MAX_VALUE;
37                }
38                while(p && p.nodeType == 1 && depth < maxDepth && p != b && p != stopEl){
39                    if(DQ.is(p, simpleSelector)){
40                        return returnEl ? GET(p) : p;
41                    }
42                    depth++;
43                    p = p.parentNode;
44                }
45                return null;
46            },
47       
48            /**
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)
55             */
56            findParentNode : function(simpleSelector, maxDepth, returnEl){
57                var p = Ext.fly(this.dom.parentNode, '_internal');
58                return p ? p.findParent(simpleSelector, maxDepth, returnEl) : null;
59            },
60       
61            /**
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)
68             */
69            up : function(simpleSelector, maxDepth){
70                return this.findParentNode(simpleSelector, maxDepth, true);
71            },
72       
73            /**
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             * @param {Boolean} unique (optional) True to create a unique Ext.Element for each child (defaults to false, which creates a single shared flyweight object)
77             * @return {CompositeElement/CompositeElementLite} The composite element
78             */
79            select : function(selector, unique){
80                return Ext.Element.select(selector, unique, this.dom);
81            },
82       
83            /**
84             * Selects child nodes based on the passed CSS selector (the selector should not contain an id).
85             * @param {String} selector The CSS selector
86             * @return {Array} An array of the matched nodes
87             */
88            query : function(selector, unique){
89                return DQ.select(selector, this.dom);
90            },
91       
92            /**
93             * Selects a single child at any depth below this element based on the passed CSS selector (the selector should not contain an id).
94             * @param {String} selector The CSS selector
95             * @param {Boolean} returnDom (optional) True to return the DOM node instead of Ext.Element (defaults to false)
96             * @return {HTMLElement/Ext.Element} The child Ext.Element (or DOM node if returnDom = true)
97             */
98            child : function(selector, returnDom){
99                var n = DQ.selectNode(selector, this.dom);
100                return returnDom ? n : GET(n);
101            },
102       
103            /**
104             * Selects a single *direct* child based on the passed CSS selector (the selector should not contain an id).
105             * @param {String} selector The CSS selector
106             * @param {Boolean} returnDom (optional) True to return the DOM node instead of Ext.Element (defaults to false)
107             * @return {HTMLElement/Ext.Element} The child Ext.Element (or DOM node if returnDom = true)
108             */
109            down : function(selector, returnDom){
110                var n = DQ.selectNode(" > " + selector, this.dom);
111                return returnDom ? n : GET(n);
112            },
113       
114                 /**
115             * Gets the parent node for this element, optionally chaining up trying to match a selector
116             * @param {String} selector (optional) Find a parent node that matches the passed simple selector
117             * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element
118             * @return {Ext.Element/HTMLElement} The parent node or null
119                 */
120            parent : function(selector, returnDom){
121                return this.matchNode(PARENTNODE, PARENTNODE, selector, returnDom);
122            },
123       
124             /**
125             * Gets the next sibling, skipping text nodes
126             * @param {String} selector (optional) Find the next sibling that matches the passed simple selector
127             * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element
128             * @return {Ext.Element/HTMLElement} The next sibling or null
129                 */
130            next : function(selector, returnDom){
131                return this.matchNode(NEXTSIBLING, NEXTSIBLING, selector, returnDom);
132            },
133       
134            /**
135             * Gets the previous sibling, skipping text nodes
136             * @param {String} selector (optional) Find the previous sibling that matches the passed simple selector
137             * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element
138             * @return {Ext.Element/HTMLElement} The previous sibling or null
139                 */
140            prev : function(selector, returnDom){
141                return this.matchNode(PREVIOUSSIBLING, PREVIOUSSIBLING, selector, returnDom);
142            },
143       
144       
145            /**
146             * Gets the first child, skipping text nodes
147             * @param {String} selector (optional) Find the next sibling that matches the passed simple selector
148             * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element
149             * @return {Ext.Element/HTMLElement} The first child or null
150                 */
151            first : function(selector, returnDom){
152                return this.matchNode(NEXTSIBLING, 'firstChild', selector, returnDom);
153            },
154       
155            /**
156             * Gets the last child, skipping text nodes
157             * @param {String} selector (optional) Find the previous sibling that matches the passed simple selector
158             * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element
159             * @return {Ext.Element/HTMLElement} The last child or null
160                 */
161            last : function(selector, returnDom){
162                return this.matchNode(PREVIOUSSIBLING, 'lastChild', selector, returnDom);
163            },
164           
165            matchNode : function(dir, start, selector, returnDom){
166                var n = this.dom[start];
167                while(n){
168                    if(n.nodeType == 1 && (!selector || DQ.is(n, selector))){
169                        return !returnDom ? GET(n) : n;
170                    }
171                    n = n[dir];
172                }
173                return null;
174            }   
175    }
176}());
Note: See TracBrowser for help on using the repository browser.