/
position.js
158 lines (128 loc) · 5.95 KB
/
position.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
155
156
157
158
import hammerhead from '../deps/hammerhead';
import * as styleUtils from './style';
import * as domUtils from './dom';
import * as shared from './shared/position';
import AxisValues from '../../../shared/utils/values/axis-values';
export { isElementVisible, isIframeVisible } from './shared/visibility';
export const getElementRectangle = hammerhead.utils.position.getElementRectangle;
export const getOffsetPosition = hammerhead.utils.position.getOffsetPosition;
export const offsetToClientCoords = hammerhead.utils.position.offsetToClientCoords;
export const getClientDimensions = shared.getClientDimensions;
export const getElementFromPoint = shared.getElementFromPoint;
export const calcRelativePosition = shared.calcRelativePosition;
export function getIframeClientCoordinates (iframe) {
const { left, top } = getOffsetPosition(iframe);
const clientPosition = offsetToClientCoords({ x: left, y: top });
const iframeBorders = styleUtils.getBordersWidth(iframe);
const iframePadding = styleUtils.getElementPadding(iframe);
const iframeRectangleLeft = clientPosition.x + iframeBorders.left + iframePadding.left;
const iframeRectangleTop = clientPosition.y + iframeBorders.top + iframePadding.top;
return {
left: iframeRectangleLeft,
top: iframeRectangleTop,
right: iframeRectangleLeft + styleUtils.getWidth(iframe),
bottom: iframeRectangleTop + styleUtils.getHeight(iframe),
};
}
export function containsOffset (el, offsetX, offsetY) {
const dimensions = getClientDimensions(el);
const width = Math.max(el.scrollWidth, dimensions.width);
const height = Math.max(el.scrollHeight, dimensions.height);
const maxX = dimensions.scrollbar.right + dimensions.border.left + dimensions.border.right + width;
const maxY = dimensions.scrollbar.bottom + dimensions.border.top + dimensions.border.bottom + height;
return (typeof offsetX === 'undefined' || offsetX >= 0 && maxX >= offsetX) &&
(typeof offsetY === 'undefined' || offsetY >= 0 && maxY >= offsetY);
}
export function getEventAbsoluteCoordinates (ev) {
const el = ev.target || ev.srcElement;
const pageCoordinates = getEventPageCoordinates(ev);
const curDocument = domUtils.findDocument(el);
let xOffset = 0;
let yOffset = 0;
if (domUtils.isElementInIframe(curDocument.documentElement)) {
const currentIframe = domUtils.getIframeByElement(curDocument);
if (currentIframe) {
const iframeOffset = getOffsetPosition(currentIframe);
const iframeBorders = styleUtils.getBordersWidth(currentIframe);
xOffset = iframeOffset.left + iframeBorders.left;
yOffset = iframeOffset.top + iframeBorders.top;
}
}
return {
x: pageCoordinates.x + xOffset,
y: pageCoordinates.y + yOffset,
};
}
export function getEventPageCoordinates (ev) {
const curCoordObject = /^touch/.test(ev.type) && ev.targetTouches ? ev.targetTouches[0] || ev.changedTouches[0] : ev;
const bothPageCoordinatesAreZero = curCoordObject.pageX === 0 && curCoordObject.pageY === 0;
const notBothClientCoordinatesAreZero = curCoordObject.clientX !== 0 || curCoordObject.clientY !== 0;
if ((curCoordObject.pageX === null || bothPageCoordinatesAreZero && notBothClientCoordinatesAreZero) &&
curCoordObject.clientX !== null) {
const currentDocument = domUtils.findDocument(ev.target || ev.srcElement);
const html = currentDocument.documentElement;
const body = currentDocument.body;
return {
x: Math.round(curCoordObject.clientX + (html && html.scrollLeft || body && body.scrollLeft || 0) -
(html.clientLeft || 0)),
y: Math.round(curCoordObject.clientY + (html && html.scrollTop || body && body.scrollTop || 0) -
(html.clientTop || 0)),
};
}
return {
x: Math.round(curCoordObject.pageX),
y: Math.round(curCoordObject.pageY),
};
}
export function getIframePointRelativeToParentFrame (pos, iframeWin) {
const iframe = domUtils.findIframeByWindow(iframeWin);
const iframeOffset = getOffsetPosition(iframe);
const iframeBorders = styleUtils.getBordersWidth(iframe);
const iframePadding = styleUtils.getElementPadding(iframe);
return offsetToClientCoords({
x: pos.x + iframeOffset.left + iframeBorders.left + iframePadding.left,
y: pos.y + iframeOffset.top + iframeBorders.top + iframePadding.top,
});
}
export function clientToOffsetCoord (coords, currentDocument) {
const doc = currentDocument || document;
return {
x: coords.x + styleUtils.getScrollLeft(doc),
y: coords.y + styleUtils.getScrollTop(doc),
};
}
export function findCenter (el) {
const rectangle = getElementRectangle(el);
return {
x: Math.round(rectangle.left + rectangle.width / 2),
y: Math.round(rectangle.top + rectangle.height / 2),
};
}
export function getClientPosition (el) {
const { left, top } = getOffsetPosition(el);
const clientCoords = offsetToClientCoords({ x: left, y: top });
clientCoords.x = Math.round(clientCoords.x);
clientCoords.y = Math.round(clientCoords.y);
return clientCoords;
}
export function getElementClientRectangle (el) {
const rect = getElementRectangle(el);
const clientPos = offsetToClientCoords({
x: rect.left,
y: rect.top,
});
return {
height: rect.height,
left: clientPos.x,
top: clientPos.y,
width: rect.width,
};
}
export function isInRectangle ({ x, y }, rectangle) {
return x >= rectangle.left && x <= rectangle.right && y >= rectangle.top && y <= rectangle.bottom;
}
export function getWindowPosition () {
const x = window.screenLeft || window.screenX;
const y = window.screenTop || window.screenY;
return new AxisValues(x, y);
}