PageRenderTime 66ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/VivoWebControls/sencha-touch-1.1.1/docs/source/Element.alignment.html

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