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.

431 lines
16 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({
  22. <span id='Ext-Element-method-getAnchorXY'> /**
  23. </span> * Gets the x,y coordinates specified by the anchor position on the element.
  24. * @param {String} anchor (optional) The specified anchor position (defaults to &quot;c&quot;). See {@link #alignTo}
  25. * for details on supported anchor positions.
  26. * @param {Boolean} local (optional) True to get the local (element top/left-relative) anchor position instead
  27. * of page coordinates
  28. * @param {Object} size (optional) An object containing the size to use for calculating anchor position
  29. * {width: (target width), height: (target height)} (defaults to the element's current size)
  30. * @return {Array} [x, y] An array containing the element's x and y coordinates
  31. */
  32. getAnchorXY : function(anchor, local, s){
  33. //Passing a different size is useful for pre-calculating anchors,
  34. //especially for anchored animations that change the el size.
  35. anchor = (anchor || &quot;tl&quot;).toLowerCase();
  36. s = s || {};
  37. var me = this,
  38. vp = me.dom == document.body || me.dom == document,
  39. w = s.width || vp ? Ext.lib.Dom.getViewWidth() : me.getWidth(),
  40. h = s.height || vp ? Ext.lib.Dom.getViewHeight() : me.getHeight(),
  41. xy,
  42. r = Math.round,
  43. o = me.getXY(),
  44. scroll = me.getScroll(),
  45. extraX = vp ? scroll.left : !local ? o[0] : 0,
  46. extraY = vp ? scroll.top : !local ? o[1] : 0,
  47. hash = {
  48. c : [r(w * 0.5), r(h * 0.5)],
  49. t : [r(w * 0.5), 0],
  50. l : [0, r(h * 0.5)],
  51. r : [w, r(h * 0.5)],
  52. b : [r(w * 0.5), h],
  53. tl : [0, 0],
  54. bl : [0, h],
  55. br : [w, h],
  56. tr : [w, 0]
  57. };
  58. xy = hash[anchor];
  59. return [xy[0] + extraX, xy[1] + extraY];
  60. },
  61. <span id='Ext-Element-method-anchorTo'> /**
  62. </span> * Anchors an element to another element and realigns it when the window is resized.
  63. * @param {Mixed} element The element to align to.
  64. * @param {String} position The position to align to.
  65. * @param {Array} offsets (optional) Offset the positioning by [x, y]
  66. * @param {Boolean/Object} animate (optional) True for the default animation or a standard Element animation config object
  67. * @param {Boolean/Number} monitorScroll (optional) True to monitor body scroll and reposition. If this parameter
  68. * is a number, it is used as the buffer delay (defaults to 50ms).
  69. * @param {Function} callback (optional) The function to call after the animation finishes
  70. * @return {Ext.Element} this
  71. */
  72. anchorTo : function(el, alignment, offsets, animate, monitorScroll, callback){
  73. var me = this,
  74. dom = me.dom,
  75. scroll = !Ext.isEmpty(monitorScroll),
  76. action = function(){
  77. Ext.fly(dom).alignTo(el, alignment, offsets, animate);
  78. Ext.callback(callback, Ext.fly(dom));
  79. },
  80. anchor = this.getAnchor();
  81. // previous listener anchor, remove it
  82. this.removeAnchor();
  83. Ext.apply(anchor, {
  84. fn: action,
  85. scroll: scroll
  86. });
  87. Ext.EventManager.onWindowResize(action, null);
  88. if(scroll){
  89. Ext.EventManager.on(window, 'scroll', action, null,
  90. {buffer: !isNaN(monitorScroll) ? monitorScroll : 50});
  91. }
  92. action.call(me); // align immediately
  93. return me;
  94. },
  95. <span id='Ext-Element-method-removeAnchor'> /**
  96. </span> * Remove any anchor to this element. See {@link #anchorTo}.
  97. * @return {Ext.Element} this
  98. */
  99. removeAnchor : function(){
  100. var me = this,
  101. anchor = this.getAnchor();
  102. if(anchor &amp;&amp; anchor.fn){
  103. Ext.EventManager.removeResizeListener(anchor.fn);
  104. if(anchor.scroll){
  105. Ext.EventManager.un(window, 'scroll', anchor.fn);
  106. }
  107. delete anchor.fn;
  108. }
  109. return me;
  110. },
  111. // private
  112. getAnchor : function(){
  113. var data = Ext.Element.data,
  114. dom = this.dom;
  115. if (!dom) {
  116. return;
  117. }
  118. var anchor = data(dom, '_anchor');
  119. if(!anchor){
  120. anchor = data(dom, '_anchor', {});
  121. }
  122. return anchor;
  123. },
  124. <span id='Ext-Element-method-getAlignToXY'> /**
  125. </span> * Gets the x,y coordinates to align this element with another element. See {@link #alignTo} for more info on the
  126. * supported position values.
  127. * @param {Mixed} element The element to align to.
  128. * @param {String} position (optional, defaults to &quot;tl-bl?&quot;) The position to align to.
  129. * @param {Array} offsets (optional) Offset the positioning by [x, y]
  130. * @return {Array} [x, y]
  131. */
  132. getAlignToXY : function(el, p, o){
  133. el = Ext.get(el);
  134. if(!el || !el.dom){
  135. throw &quot;Element.alignToXY with an element that doesn't exist&quot;;
  136. }
  137. o = o || [0,0];
  138. p = (!p || p == &quot;?&quot; ? &quot;tl-bl?&quot; : (!(/-/).test(p) &amp;&amp; p !== &quot;&quot; ? &quot;tl-&quot; + p : p || &quot;tl-bl&quot;)).toLowerCase();
  139. var me = this,
  140. d = me.dom,
  141. a1,
  142. a2,
  143. x,
  144. y,
  145. //constrain the aligned el to viewport if necessary
  146. w,
  147. h,
  148. r,
  149. dw = Ext.lib.Dom.getViewWidth() -10, // 10px of margin for ie
  150. dh = Ext.lib.Dom.getViewHeight()-10, // 10px of margin for ie
  151. p1y,
  152. p1x,
  153. p2y,
  154. p2x,
  155. swapY,
  156. swapX,
  157. doc = document,
  158. docElement = doc.documentElement,
  159. docBody = doc.body,
  160. scrollX = (docElement.scrollLeft || docBody.scrollLeft || 0)+5,
  161. scrollY = (docElement.scrollTop || docBody.scrollTop || 0)+5,
  162. c = false, //constrain to viewport
  163. p1 = &quot;&quot;,
  164. p2 = &quot;&quot;,
  165. m = p.match(/^([a-z]+)-([a-z]+)(\?)?$/);
  166. if(!m){
  167. throw &quot;Element.alignTo with an invalid alignment &quot; + p;
  168. }
  169. p1 = m[1];
  170. p2 = m[2];
  171. c = !!m[3];
  172. //Subtract the aligned el's internal xy from the target's offset xy
  173. //plus custom offset to get the aligned el's new offset xy
  174. a1 = me.getAnchorXY(p1, true);
  175. a2 = el.getAnchorXY(p2, false);
  176. x = a2[0] - a1[0] + o[0];
  177. y = a2[1] - a1[1] + o[1];
  178. if(c){
  179. w = me.getWidth();
  180. h = me.getHeight();
  181. r = el.getRegion();
  182. //If we are at a viewport boundary and the aligned el is anchored on a target border that is
  183. //perpendicular to the vp border, allow the aligned el to slide on that border,
  184. //otherwise swap the aligned el to the opposite border of the target.
  185. p1y = p1.charAt(0);
  186. p1x = p1.charAt(p1.length-1);
  187. p2y = p2.charAt(0);
  188. p2x = p2.charAt(p2.length-1);
  189. swapY = ((p1y==&quot;t&quot; &amp;&amp; p2y==&quot;b&quot;) || (p1y==&quot;b&quot; &amp;&amp; p2y==&quot;t&quot;));
  190. swapX = ((p1x==&quot;r&quot; &amp;&amp; p2x==&quot;l&quot;) || (p1x==&quot;l&quot; &amp;&amp; p2x==&quot;r&quot;));
  191. if (x + w &gt; dw + scrollX) {
  192. x = swapX ? r.left-w : dw+scrollX-w;
  193. }
  194. if (x &lt; scrollX) {
  195. x = swapX ? r.right : scrollX;
  196. }
  197. if (y + h &gt; dh + scrollY) {
  198. y = swapY ? r.top-h : dh+scrollY-h;
  199. }
  200. if (y &lt; scrollY){
  201. y = swapY ? r.bottom : scrollY;
  202. }
  203. }
  204. return [x,y];
  205. },
  206. <span id='Ext-Element-method-alignTo'> /**
  207. </span> * Aligns this element with another element relative to the specified anchor points. If the other element is the
  208. * document it aligns it to the viewport.
  209. * The position parameter is optional, and can be specified in any one of the following formats:
  210. * &lt;ul&gt;
  211. * &lt;li&gt;&lt;b&gt;Blank&lt;/b&gt;: Defaults to aligning the element's top-left corner to the target's bottom-left corner (&quot;tl-bl&quot;).&lt;/li&gt;
  212. * &lt;li&gt;&lt;b&gt;One anchor (deprecated)&lt;/b&gt;: The passed anchor position is used as the target element's anchor point.
  213. * The element being aligned will position its top-left corner (tl) to that point. &lt;i&gt;This method has been
  214. * deprecated in favor of the newer two anchor syntax below&lt;/i&gt;.&lt;/li&gt;
  215. * &lt;li&gt;&lt;b&gt;Two anchors&lt;/b&gt;: If two values from the table below are passed separated by a dash, the first value is used as the
  216. * element's anchor point, and the second value is used as the target's anchor point.&lt;/li&gt;
  217. * &lt;/ul&gt;
  218. * In addition to the anchor points, the position parameter also supports the &quot;?&quot; character. If &quot;?&quot; is passed at the end of
  219. * the position string, the element will attempt to align as specified, but the position will be adjusted to constrain to
  220. * the viewport if necessary. Note that the element being aligned might be swapped to align to a different position than
  221. * that specified in order to enforce the viewport constraints.
  222. * Following are all of the supported anchor positions:
  223. &lt;pre&gt;
  224. Value Description
  225. ----- -----------------------------
  226. tl The top left corner (default)
  227. t The center of the top edge
  228. tr The top right corner
  229. l The center of the left edge
  230. c In the center of the element
  231. r The center of the right edge
  232. bl The bottom left corner
  233. b The center of the bottom edge
  234. br The bottom right corner
  235. &lt;/pre&gt;
  236. Example Usage:
  237. &lt;pre&gt;&lt;code&gt;
  238. // align el to other-el using the default positioning (&quot;tl-bl&quot;, non-constrained)
  239. el.alignTo(&quot;other-el&quot;);
  240. // align the top left corner of el with the top right corner of other-el (constrained to viewport)
  241. el.alignTo(&quot;other-el&quot;, &quot;tr?&quot;);
  242. // align the bottom right corner of el with the center left edge of other-el
  243. el.alignTo(&quot;other-el&quot;, &quot;br-l?&quot;);
  244. // align the center of el with the bottom left corner of other-el and
  245. // adjust the x position by -6 pixels (and the y position by 0)
  246. el.alignTo(&quot;other-el&quot;, &quot;c-bl&quot;, [-6, 0]);
  247. &lt;/code&gt;&lt;/pre&gt;
  248. * @param {Mixed} element The element to align to.
  249. * @param {String} position (optional, defaults to &quot;tl-bl?&quot;) The position to align to.
  250. * @param {Array} offsets (optional) Offset the positioning by [x, y]
  251. * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object
  252. * @return {Ext.Element} this
  253. */
  254. alignTo : function(element, position, offsets, animate){
  255. var me = this;
  256. return me.setXY(me.getAlignToXY(element, position, offsets),
  257. me.preanim &amp;&amp; !!animate ? me.preanim(arguments, 3) : false);
  258. },
  259. // private ==&gt; used outside of core
  260. adjustForConstraints : function(xy, parent, offsets){
  261. return this.getConstrainToXY(parent || document, false, offsets, xy) || xy;
  262. },
  263. // private ==&gt; used outside of core
  264. getConstrainToXY : function(el, local, offsets, proposedXY){
  265. var os = {top:0, left:0, bottom:0, right: 0};
  266. return function(el, local, offsets, proposedXY){
  267. el = Ext.get(el);
  268. offsets = offsets ? Ext.applyIf(offsets, os) : os;
  269. var vw, vh, vx = 0, vy = 0;
  270. if(el.dom == document.body || el.dom == document){
  271. vw =Ext.lib.Dom.getViewWidth();
  272. vh = Ext.lib.Dom.getViewHeight();
  273. }else{
  274. vw = el.dom.clientWidth;
  275. vh = el.dom.clientHeight;
  276. if(!local){
  277. var vxy = el.getXY();
  278. vx = vxy[0];
  279. vy = vxy[1];
  280. }
  281. }
  282. var s = el.getScroll();
  283. vx += offsets.left + s.left;
  284. vy += offsets.top + s.top;
  285. vw -= offsets.right;
  286. vh -= offsets.bottom;
  287. var vr = vx + vw,
  288. vb = vy + vh,
  289. xy = proposedXY || (!local ? this.getXY() : [this.getLeft(true), this.getTop(true)]),
  290. x = xy[0], y = xy[1],
  291. offset = this.getConstrainOffset(),
  292. w = this.dom.offsetWidth + offset,
  293. h = this.dom.offsetHeight + offset;
  294. // only move it if it needs it
  295. var moved = false;
  296. // first validate right/bottom
  297. if((x + w) &gt; vr){
  298. x = vr - w;
  299. moved = true;
  300. }
  301. if((y + h) &gt; vb){
  302. y = vb - h;
  303. moved = true;
  304. }
  305. // then make sure top/left isn't negative
  306. if(x &lt; vx){
  307. x = vx;
  308. moved = true;
  309. }
  310. if(y &lt; vy){
  311. y = vy;
  312. moved = true;
  313. }
  314. return moved ? [x, y] : false;
  315. };
  316. }(),
  317. // el = Ext.get(el);
  318. // offsets = Ext.applyIf(offsets || {}, {top : 0, left : 0, bottom : 0, right : 0});
  319. // var me = this,
  320. // doc = document,
  321. // s = el.getScroll(),
  322. // vxy = el.getXY(),
  323. // vx = offsets.left + s.left,
  324. // vy = offsets.top + s.top,
  325. // vw = -offsets.right,
  326. // vh = -offsets.bottom,
  327. // vr,
  328. // vb,
  329. // xy = proposedXY || (!local ? me.getXY() : [me.getLeft(true), me.getTop(true)]),
  330. // x = xy[0],
  331. // y = xy[1],
  332. // w = me.dom.offsetWidth, h = me.dom.offsetHeight,
  333. // moved = false; // only move it if it needs it
  334. //
  335. //
  336. // if(el.dom == doc.body || el.dom == doc){
  337. // vw += Ext.lib.Dom.getViewWidth();
  338. // vh += Ext.lib.Dom.getViewHeight();
  339. // }else{
  340. // vw += el.dom.clientWidth;
  341. // vh += el.dom.clientHeight;
  342. // if(!local){
  343. // vx += vxy[0];
  344. // vy += vxy[1];
  345. // }
  346. // }
  347. // // first validate right/bottom
  348. // if(x + w &gt; vx + vw){
  349. // x = vx + vw - w;
  350. // moved = true;
  351. // }
  352. // if(y + h &gt; vy + vh){
  353. // y = vy + vh - h;
  354. // moved = true;
  355. // }
  356. // // then make sure top/left isn't negative
  357. // if(x &lt; vx){
  358. // x = vx;
  359. // moved = true;
  360. // }
  361. // if(y &lt; vy){
  362. // y = vy;
  363. // moved = true;
  364. // }
  365. // return moved ? [x, y] : false;
  366. // },
  367. // private, used internally
  368. getConstrainOffset : function(){
  369. return 0;
  370. },
  371. <span id='Ext-Element-method-getCenterXY'> /**
  372. </span> * Calculates the x, y to center this element on the screen
  373. * @return {Array} The x, y values [x, y]
  374. */
  375. getCenterXY : function(){
  376. return this.getAlignToXY(document, 'c-c');
  377. },
  378. <span id='Ext-Element-method-center'> /**
  379. </span> * Centers the Element in either the viewport, or another Element.
  380. * @param {Mixed} centerIn (optional) The element in which to center the element.
  381. */
  382. center : function(centerIn){
  383. return this.alignTo(centerIn || document, 'c-c');
  384. }
  385. });
  386. </pre>
  387. </body>
  388. </html>