-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdom.js
More file actions
123 lines (111 loc) · 2.99 KB
/
Copy pathdom.js
File metadata and controls
123 lines (111 loc) · 2.99 KB
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
/* eslint no-param-reassign: 0 */
/**
* get the hidden element width, height
* @param {HTMLElement} element dom
*/
export function getPopupElementSize(element) {
const originalDisplay = element.style.display
const originalVisibility = element.style.visibility
element.style.display = 'block'
element.style.visibility = 'hidden'
const styles = window.getComputedStyle(element)
const width = element.offsetWidth + parseInt(styles.marginLeft, 10) + parseInt(
styles.marginRight,
10,
)
const height = element.offsetHeight + parseInt(
styles.marginTop,
10,
) + parseInt(styles.marginBottom, 10)
element.style.display = originalDisplay
element.style.visibility = originalVisibility
return {
width,
height,
}
}
/**
* get the popup position
* @param {Element} el element
* @param {Element} elRelative relative element
* @param {Number} targetWidth target element's width
* @param {Number} targetHeight target element's height
* @param {Boolean} fixed
* @param {String} fixedPosition
* @param {Boolean} rtl
*/
export function getRelativePosition({
el,
elRelative,
targetWidth,
targetHeight,
fixed,
fixedPosition,
rtl,
}) {
let left = 0
let top = 0
let offsetX = 0
let offsetY = 0
const relativeRect = elRelative.getBoundingClientRect()
const documentWidth = document.documentElement.clientWidth
const documentHeight = document.documentElement.clientHeight
if (fixed) {
offsetX = window.pageXOffset + relativeRect.left
offsetY = window.pageYOffset + relativeRect.top
}
const calendarBounding = el.getBoundingClientRect()
const outOfBoundsRight = calendarBounding.right > window.innerWidth
const outOfBoundsBottom = calendarBounding.bottom > window.innerHeight
const fixedPositionRight = fixedPosition && fixedPosition.indexOf('right') !== -1
const fixedPositionTop = fixedPosition && fixedPosition.indexOf('top') !== -1
const setLeft = () => {
left = offsetX
}
const setRight = () => {
left = offsetX + relativeRect.width - targetWidth
}
const setBottom = () => {
top = offsetY + relativeRect.height
}
const setTop = () => {
top = offsetY - targetHeight
}
if (fixedPosition === '') {
if (outOfBoundsRight || rtl) {
setRight()
} else {
setLeft()
}
if (outOfBoundsBottom) {
setTop()
} else {
setBottom()
}
const hasRelativWidth = documentWidth - relativeRect.left < targetWidth
&& relativeRect.right < targetWidth
const hasRelativHeight = relativeRect.top <= targetHeight
&& documentHeight - relativeRect.bottom <= targetHeight
if (hasRelativWidth) {
left = offsetX - relativeRect.left + 1
}
if (hasRelativHeight) {
top = offsetY + documentHeight - relativeRect.top - targetHeight
}
} else {
if (fixedPositionRight) {
setRight()
} else {
setLeft()
}
if (fixedPositionTop) {
setTop()
} else {
setBottom()
}
}
return {
left: `${left}px`,
top: `${top}px`,
}
}