UNPKG

6.78 kBJavaScriptView Raw
1/**
2 * @copyright Valor Software
3 * @copyright Angular ng-bootstrap team
4 */
5// previous version:
6// https://github.com/angular-ui/bootstrap/blob/07c31d0731f7cb068a1932b8e01d2312b796b4ec/src/position/position.js
7// tslint:disable
8export var Positioning = (function () {
9 function Positioning() {
10 }
11 Positioning.prototype.position = function (element, round) {
12 if (round === void 0) { round = true; }
13 var elPosition;
14 var parentOffset = { width: 0, height: 0, top: 0, bottom: 0, left: 0, right: 0 };
15 if (this.getStyle(element, 'position') === 'fixed') {
16 elPosition = element.getBoundingClientRect();
17 }
18 else {
19 var offsetParentEl = this.offsetParent(element);
20 elPosition = this.offset(element, false);
21 if (offsetParentEl !== document.documentElement) {
22 parentOffset = this.offset(offsetParentEl, false);
23 }
24 parentOffset.top += offsetParentEl.clientTop;
25 parentOffset.left += offsetParentEl.clientLeft;
26 }
27 elPosition.top -= parentOffset.top;
28 elPosition.bottom -= parentOffset.top;
29 elPosition.left -= parentOffset.left;
30 elPosition.right -= parentOffset.left;
31 if (round) {
32 elPosition.top = Math.round(elPosition.top);
33 elPosition.bottom = Math.round(elPosition.bottom);
34 elPosition.left = Math.round(elPosition.left);
35 elPosition.right = Math.round(elPosition.right);
36 }
37 return elPosition;
38 };
39 Positioning.prototype.offset = function (element, round) {
40 if (round === void 0) { round = true; }
41 var elBcr = element.getBoundingClientRect();
42 var viewportOffset = {
43 top: window.pageYOffset - document.documentElement.clientTop,
44 left: window.pageXOffset - document.documentElement.clientLeft
45 };
46 var elOffset = {
47 height: elBcr.height || element.offsetHeight,
48 width: elBcr.width || element.offsetWidth,
49 top: elBcr.top + viewportOffset.top,
50 bottom: elBcr.bottom + viewportOffset.top,
51 left: elBcr.left + viewportOffset.left,
52 right: elBcr.right + viewportOffset.left
53 };
54 if (round) {
55 elOffset.height = Math.round(elOffset.height);
56 elOffset.width = Math.round(elOffset.width);
57 elOffset.top = Math.round(elOffset.top);
58 elOffset.bottom = Math.round(elOffset.bottom);
59 elOffset.left = Math.round(elOffset.left);
60 elOffset.right = Math.round(elOffset.right);
61 }
62 return elOffset;
63 };
64 Positioning.prototype.positionElements = function (hostElement, targetElement, placement, appendToBody) {
65 var hostElPosition = appendToBody ? this.offset(hostElement, false) : this.position(hostElement, false);
66 var shiftWidth = {
67 left: hostElPosition.left,
68 center: hostElPosition.left + hostElPosition.width / 2 - targetElement.offsetWidth / 2,
69 right: hostElPosition.left + hostElPosition.width
70 };
71 var shiftHeight = {
72 top: hostElPosition.top,
73 center: hostElPosition.top + hostElPosition.height / 2 - targetElement.offsetHeight / 2,
74 bottom: hostElPosition.top + hostElPosition.height
75 };
76 var targetElBCR = targetElement.getBoundingClientRect();
77 var placementPrimary = placement.split(' ')[0] || 'top';
78 var placementSecondary = placement.split(' ')[1] || 'center';
79 var targetElPosition = {
80 height: targetElBCR.height || targetElement.offsetHeight,
81 width: targetElBCR.width || targetElement.offsetWidth,
82 top: 0,
83 bottom: targetElBCR.height || targetElement.offsetHeight,
84 left: 0,
85 right: targetElBCR.width || targetElement.offsetWidth
86 };
87 switch (placementPrimary) {
88 case 'top':
89 targetElPosition.top = hostElPosition.top - targetElement.offsetHeight;
90 targetElPosition.bottom += hostElPosition.top - targetElement.offsetHeight;
91 targetElPosition.left = shiftWidth[placementSecondary];
92 targetElPosition.right += shiftWidth[placementSecondary];
93 break;
94 case 'bottom':
95 targetElPosition.top = shiftHeight[placementPrimary];
96 targetElPosition.bottom += shiftHeight[placementPrimary];
97 targetElPosition.left = shiftWidth[placementSecondary];
98 targetElPosition.right += shiftWidth[placementSecondary];
99 break;
100 case 'left':
101 targetElPosition.top = shiftHeight[placementSecondary];
102 targetElPosition.bottom += shiftHeight[placementSecondary];
103 targetElPosition.left = hostElPosition.left - targetElement.offsetWidth;
104 targetElPosition.right += hostElPosition.left - targetElement.offsetWidth;
105 break;
106 case 'right':
107 targetElPosition.top = shiftHeight[placementSecondary];
108 targetElPosition.bottom += shiftHeight[placementSecondary];
109 targetElPosition.left = shiftWidth[placementPrimary];
110 targetElPosition.right += shiftWidth[placementPrimary];
111 break;
112 }
113 targetElPosition.top = Math.round(targetElPosition.top);
114 targetElPosition.bottom = Math.round(targetElPosition.bottom);
115 targetElPosition.left = Math.round(targetElPosition.left);
116 targetElPosition.right = Math.round(targetElPosition.right);
117 return targetElPosition;
118 };
119 Positioning.prototype.getStyle = function (element, prop) { return window.getComputedStyle(element)[prop]; };
120 Positioning.prototype.isStaticPositioned = function (element) {
121 return (this.getStyle(element, 'position') || 'static') === 'static';
122 };
123 Positioning.prototype.offsetParent = function (element) {
124 var offsetParentEl = element.offsetParent || document.documentElement;
125 while (offsetParentEl && offsetParentEl !== document.documentElement && this.isStaticPositioned(offsetParentEl)) {
126 offsetParentEl = offsetParentEl.offsetParent;
127 }
128 return offsetParentEl || document.documentElement;
129 };
130 return Positioning;
131}());
132var positionService = new Positioning();
133export function positionElements(hostElement, targetElement, placement, appendToBody) {
134 var pos = positionService.positionElements(hostElement, targetElement, placement, appendToBody);
135 targetElement.style.top = pos.top + "px";
136 targetElement.style.left = pos.left + "px";
137}
138//# sourceMappingURL=ng-positioning.js.map
\No newline at end of file