-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
154 lines (138 loc) · 3.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* global Window, Document */
function getMiddleOfRect(rect) {
const x = rect.left + rect.width / 2;
const y = rect.top + rect.height / 2;
return [x, y];
}
function getBoundaryRect(rect, columns, rows) {
const column = rect.width / columns;
const row = rect.height / rows;
const left = rect.left + column;
const top = rect.top + row;
const right = rect.right - column;
const bottom = rect.bottom - row;
return {
left,
top,
right,
bottom
};
}
function getPositionForBoundary(rect, boundaryRect) {
const point = getMiddleOfRect(rect);
const [x, y] = point;
const position = [];
if (y <= boundaryRect.top) {
position.push('top');
} else if (y >= boundaryRect.bottom) {
position.push('bottom');
} else {
position.push('middle');
}
if (x <= boundaryRect.left) {
position.push('left');
} else if (x >= boundaryRect.right) {
position.push('right');
} else {
position.push('center');
}
return position.join(' ');
}
export function getPosition(element, container, columns, rows) {
const elementRect = element.getBoundingClientRect();
const containerRect = getNormalisedRect(container);
const boundaryRect = getBoundaryRect(containerRect, columns, rows);
return getPositionForBoundary(elementRect, boundaryRect);
}
function getNormalisedRect(object) {
if (object instanceof Window) {
return {
top: 0,
left: 0,
right: object.innerWidth,
bottom: object.innerHeight,
width: object.innerWidth,
height: object.innerHeight
};
} else if (object instanceof Document) {
return object.documentElement.getBoundingClientRect();
} else {
return object.getBoundingClientRect();
}
}
export function getCoords(position, element, reference) {
if (!element.offsetParent) {
return [0, 0];
}
const elementRect = element.getBoundingClientRect();
const referenceRect = reference.getBoundingClientRect();
const offsetRect = element.offsetParent.getBoundingClientRect();
const elementHeight = elementRect.height;
const elementWidth = elementRect.width;
const point = getMiddleOfRect(referenceRect);
const referenceCenter = point[0] - elementWidth / 2;
const referenceMiddle = point[1] - elementHeight / 2;
const referenceTop = referenceRect.top;
const referenceLeft = referenceRect.left;
const referenceRight = referenceLeft + referenceRect.width;
const referenceBottom = referenceTop + referenceRect.height;
const offsetScrollTop = element.offsetParent.scrollTop;
const offsetScrollLeft = element.offsetParent.scrollLeft;
const offsetTop = offsetRect.top * -1;
const offsetLeft = offsetRect.left * -1;
let x;
let y;
switch (position) {
case 'top left':
x = referenceLeft;
y = referenceTop - elementHeight;
break;
case 'top center':
x = referenceCenter;
y = referenceTop - elementHeight;
break;
case 'top right':
x = referenceRight - elementWidth;
y = referenceTop - elementHeight;
break;
case 'right top':
x = referenceRight;
y = referenceTop;
break;
case 'right middle':
x = referenceRight;
y = referenceMiddle;
break;
case 'right bottom':
x = referenceRight;
y = referenceBottom - elementHeight;
break;
case 'bottom left':
x = referenceLeft;
y = referenceBottom;
break;
case 'bottom center':
x = referenceCenter;
y = referenceBottom;
break;
case 'bottom right':
x = referenceRight - elementWidth;
y = referenceBottom;
break;
case 'left top':
x = referenceLeft - elementWidth;
y = referenceTop;
break;
case 'left middle':
x = referenceLeft - elementWidth;
y = referenceMiddle;
break;
case 'left bottom':
x = referenceLeft - elementWidth;
y = referenceBottom - elementHeight;
break;
}
x += offsetLeft + offsetScrollLeft;
y += offsetTop + offsetScrollTop;
return [x, y];
}