You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
8.6 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. */
  21. Ext.Element.addMethods(function(){
  22. var PARENTNODE = 'parentNode',
  23. NEXTSIBLING = 'nextSibling',
  24. PREVIOUSSIBLING = 'previousSibling',
  25. DQ = Ext.DomQuery,
  26. GET = Ext.get;
  27. return {
  28. <span id='Ext-Element-method-findParent'> /**
  29. </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)
  30. * @param {String} selector The simple selector to test
  31. * @param {Number/Mixed} maxDepth (optional) The max depth to search as a number or element (defaults to 50 || document.body)
  32. * @param {Boolean} returnEl (optional) True to return a Ext.Element object instead of DOM node
  33. * @return {HTMLElement} The matching DOM node (or null if no match was found)
  34. */
  35. findParent : function(simpleSelector, maxDepth, returnEl){
  36. var p = this.dom,
  37. b = document.body,
  38. depth = 0,
  39. stopEl;
  40. if(Ext.isGecko &amp;&amp; Object.prototype.toString.call(p) == '[object XULElement]') {
  41. return null;
  42. }
  43. maxDepth = maxDepth || 50;
  44. if (isNaN(maxDepth)) {
  45. stopEl = Ext.getDom(maxDepth);
  46. maxDepth = Number.MAX_VALUE;
  47. }
  48. while(p &amp;&amp; p.nodeType == 1 &amp;&amp; depth &lt; maxDepth &amp;&amp; p != b &amp;&amp; p != stopEl){
  49. if(DQ.is(p, simpleSelector)){
  50. return returnEl ? GET(p) : p;
  51. }
  52. depth++;
  53. p = p.parentNode;
  54. }
  55. return null;
  56. },
  57. <span id='Ext-Element-method-findParentNode'> /**
  58. </span> * Looks at parent nodes for a match of the passed simple selector (e.g. div.some-class or span:first-child)
  59. * @param {String} selector The simple selector to test
  60. * @param {Number/Mixed} maxDepth (optional) The max depth to
  61. search as a number or element (defaults to 10 || document.body)
  62. * @param {Boolean} returnEl (optional) True to return a Ext.Element object instead of DOM node
  63. * @return {HTMLElement} The matching DOM node (or null if no match was found)
  64. */
  65. findParentNode : function(simpleSelector, maxDepth, returnEl){
  66. var p = Ext.fly(this.dom.parentNode, '_internal');
  67. return p ? p.findParent(simpleSelector, maxDepth, returnEl) : null;
  68. },
  69. <span id='Ext-Element-method-up'> /**
  70. </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).
  71. * This is a shortcut for findParentNode() that always returns an Ext.Element.
  72. * @param {String} selector The simple selector to test
  73. * @param {Number/Mixed} maxDepth (optional) The max depth to
  74. search as a number or element (defaults to 10 || document.body)
  75. * @return {Ext.Element} The matching DOM node (or null if no match was found)
  76. */
  77. up : function(simpleSelector, maxDepth){
  78. return this.findParentNode(simpleSelector, maxDepth, true);
  79. },
  80. <span id='Ext-Element-method-select'> /**
  81. </span> * Creates a {@link Ext.CompositeElement} for child nodes based on the passed CSS selector (the selector should not contain an id).
  82. * @param {String} selector The CSS selector
  83. * @return {CompositeElement/CompositeElementLite} The composite element
  84. */
  85. select : function(selector){
  86. return Ext.Element.select(selector, this.dom);
  87. },
  88. <span id='Ext-Element-method-query'> /**
  89. </span> * Selects child nodes based on the passed CSS selector (the selector should not contain an id).
  90. * @param {String} selector The CSS selector
  91. * @return {Array} An array of the matched nodes
  92. */
  93. query : function(selector){
  94. return DQ.select(selector, this.dom);
  95. },
  96. <span id='Ext-Element-method-child'> /**
  97. </span> * Selects a single child at any depth below this element based on the passed CSS selector (the selector should not contain an id).
  98. * @param {String} selector The CSS selector
  99. * @param {Boolean} returnDom (optional) True to return the DOM node instead of Ext.Element (defaults to false)
  100. * @return {HTMLElement/Ext.Element} The child Ext.Element (or DOM node if returnDom = true)
  101. */
  102. child : function(selector, returnDom){
  103. var n = DQ.selectNode(selector, this.dom);
  104. return returnDom ? n : GET(n);
  105. },
  106. <span id='Ext-Element-method-down'> /**
  107. </span> * Selects a single *direct* child based on the passed CSS selector (the selector should not contain an id).
  108. * @param {String} selector The CSS selector
  109. * @param {Boolean} returnDom (optional) True to return the DOM node instead of Ext.Element (defaults to false)
  110. * @return {HTMLElement/Ext.Element} The child Ext.Element (or DOM node if returnDom = true)
  111. */
  112. down : function(selector, returnDom){
  113. var n = DQ.selectNode(&quot; &gt; &quot; + selector, this.dom);
  114. return returnDom ? n : GET(n);
  115. },
  116. <span id='Ext-Element-method-parent'> /**
  117. </span> * Gets the parent node for this element, optionally chaining up trying to match a selector
  118. * @param {String} selector (optional) Find a parent node that matches the passed simple selector
  119. * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element
  120. * @return {Ext.Element/HTMLElement} The parent node or null
  121. */
  122. parent : function(selector, returnDom){
  123. return this.matchNode(PARENTNODE, PARENTNODE, selector, returnDom);
  124. },
  125. <span id='Ext-Element-method-next'> /**
  126. </span> * Gets the next sibling, skipping text nodes
  127. * @param {String} selector (optional) Find the next sibling that matches the passed simple selector
  128. * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element
  129. * @return {Ext.Element/HTMLElement} The next sibling or null
  130. */
  131. next : function(selector, returnDom){
  132. return this.matchNode(NEXTSIBLING, NEXTSIBLING, selector, returnDom);
  133. },
  134. <span id='Ext-Element-method-prev'> /**
  135. </span> * 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. <span id='Ext-Element-method-first'> /**
  144. </span> * Gets the first child, skipping text nodes
  145. * @param {String} selector (optional) Find the next sibling that matches the passed simple selector
  146. * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element
  147. * @return {Ext.Element/HTMLElement} The first child or null
  148. */
  149. first : function(selector, returnDom){
  150. return this.matchNode(NEXTSIBLING, 'firstChild', selector, returnDom);
  151. },
  152. <span id='Ext-Element-method-last'> /**
  153. </span> * Gets the last child, skipping text nodes
  154. * @param {String} selector (optional) Find the previous sibling that matches the passed simple selector
  155. * @param {Boolean} returnDom (optional) True to return a raw dom node instead of an Ext.Element
  156. * @return {Ext.Element/HTMLElement} The last child or null
  157. */
  158. last : function(selector, returnDom){
  159. return this.matchNode(PREVIOUSSIBLING, 'lastChild', selector, returnDom);
  160. },
  161. matchNode : function(dir, start, selector, returnDom){
  162. var n = this.dom[start];
  163. while(n){
  164. if(n.nodeType == 1 &amp;&amp; (!selector || DQ.is(n, selector))){
  165. return !returnDom ? GET(n) : n;
  166. }
  167. n = n[dir];
  168. }
  169. return null;
  170. }
  171. };
  172. }());</pre>
  173. </body>
  174. </html>