PageRenderTime 45ms CodeModel.GetById 19ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/ext-4.0.7/docs/source/Element.traversal.html

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