[76] | 1 | /*! |
---|
| 2 | * Ext JS Library 3.4.0 |
---|
| 3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
| 4 | * licensing@sencha.com |
---|
| 5 | * http://www.sencha.com/license |
---|
| 6 | */ |
---|
| 7 | /** |
---|
| 8 | * @class Ext.Element |
---|
| 9 | */ |
---|
| 10 | Ext.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 | * @return {CompositeElement/CompositeElementLite} The composite element |
---|
| 77 | */ |
---|
| 78 | select : function(selector){ |
---|
| 79 | return Ext.Element.select(selector, this.dom); |
---|
| 80 | }, |
---|
| 81 | |
---|
| 82 | /** |
---|
| 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 |
---|
| 86 | */ |
---|
| 87 | query : function(selector){ |
---|
| 88 | return DQ.select(selector, this.dom); |
---|
| 89 | }, |
---|
| 90 | |
---|
| 91 | /** |
---|
| 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) |
---|
| 96 | */ |
---|
| 97 | child : function(selector, returnDom){ |
---|
| 98 | var n = DQ.selectNode(selector, this.dom); |
---|
| 99 | return returnDom ? n : GET(n); |
---|
| 100 | }, |
---|
| 101 | |
---|
| 102 | /** |
---|
| 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) |
---|
| 107 | */ |
---|
| 108 | down : function(selector, returnDom){ |
---|
| 109 | var n = DQ.selectNode(" > " + selector, this.dom); |
---|
| 110 | return returnDom ? n : GET(n); |
---|
| 111 | }, |
---|
| 112 | |
---|
| 113 | /** |
---|
| 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 |
---|
| 118 | */ |
---|
| 119 | parent : function(selector, returnDom){ |
---|
| 120 | return this.matchNode(PARENTNODE, PARENTNODE, selector, returnDom); |
---|
| 121 | }, |
---|
| 122 | |
---|
| 123 | /** |
---|
| 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 |
---|
| 128 | */ |
---|
| 129 | next : function(selector, returnDom){ |
---|
| 130 | return this.matchNode(NEXTSIBLING, NEXTSIBLING, selector, returnDom); |
---|
| 131 | }, |
---|
| 132 | |
---|
| 133 | /** |
---|
| 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 |
---|
| 138 | */ |
---|
| 139 | prev : function(selector, returnDom){ |
---|
| 140 | return this.matchNode(PREVIOUSSIBLING, PREVIOUSSIBLING, selector, returnDom); |
---|
| 141 | }, |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | /** |
---|
| 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 |
---|
| 149 | */ |
---|
| 150 | first : function(selector, returnDom){ |
---|
| 151 | return this.matchNode(NEXTSIBLING, 'firstChild', selector, returnDom); |
---|
| 152 | }, |
---|
| 153 | |
---|
| 154 | /** |
---|
| 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 |
---|
| 159 | */ |
---|
| 160 | last : function(selector, returnDom){ |
---|
| 161 | return this.matchNode(PREVIOUSSIBLING, 'lastChild', selector, returnDom); |
---|
| 162 | }, |
---|
| 163 | |
---|
| 164 | matchNode : function(dir, start, selector, returnDom){ |
---|
| 165 | var n = this.dom[start]; |
---|
| 166 | while(n){ |
---|
| 167 | if(n.nodeType == 1 && (!selector || DQ.is(n, selector))){ |
---|
| 168 | return !returnDom ? GET(n) : n; |
---|
| 169 | } |
---|
| 170 | n = n[dir]; |
---|
| 171 | } |
---|
| 172 | return null; |
---|
| 173 | } |
---|
| 174 | }; |
---|
| 175 | }()); |
---|