forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
183 lines (157 loc) · 4.99 KB
/
popup.js
File metadata and controls
183 lines (157 loc) · 4.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import extend from './extend'
import { position as eventPosition } from './event'
export function getAnchorPosition (el, offset) {
let
{top, left, right, bottom} = el.getBoundingClientRect(),
a = {
top,
left,
width: el.offsetWidth,
height: el.offsetHeight
}
if (offset) {
a.top -= offset[1]
a.left -= offset[0]
if (bottom) {
bottom += offset[1]
}
if (right) {
right += offset[0]
}
a.width += offset[0]
a.height += offset[1]
}
a.right = right || a.left + a.width
a.bottom = bottom || a.top + a.height
a.middle = a.left + ((a.right - a.left) / 2)
a.center = a.top + ((a.bottom - a.top) / 2)
return a
}
export function getTargetPosition (el) {
return {
top: 0,
center: el.offsetHeight / 2,
bottom: el.offsetHeight,
left: 0,
middle: el.offsetWidth / 2,
right: el.offsetWidth
}
}
export function getOverlapMode (anchor, target, median) {
if ([anchor, target].indexOf(median) >= 0) return 'auto'
if (anchor === target) return 'inclusive'
return 'exclusive'
}
export function getPositions (anchor, target) {
const
a = extend({}, anchor),
t = extend({}, target)
const positions = {
x: ['left', 'right'].filter(p => p !== t.horizontal),
y: ['top', 'bottom'].filter(p => p !== t.vertical)
}
const overlap = {
x: getOverlapMode(a.horizontal, t.horizontal, 'middle'),
y: getOverlapMode(a.vertical, t.vertical, 'center')
}
positions.x.splice(overlap.x === 'auto' ? 0 : 1, 0, 'middle')
positions.y.splice(overlap.y === 'auto' ? 0 : 1, 0, 'center')
if (overlap.y !== 'auto') {
a.vertical = a.vertical === 'top' ? 'bottom' : 'top'
if (overlap.y === 'inclusive') {
t.vertical = t.vertical
}
}
if (overlap.x !== 'auto') {
a.horizontal = a.horizontal === 'left' ? 'right' : 'left'
if (overlap.y === 'inclusive') {
t.horizontal = t.horizontal
}
}
return {
positions: positions,
anchorPos: a
}
}
export function repositionIfNeeded (anchor, target, selfOrigin, anchorOrigin, targetPosition) {
const {positions, anchorPos} = getPositions(anchorOrigin, selfOrigin)
if (targetPosition.top < 0 || targetPosition.top + target.bottom > window.innerHeight) {
let newTop = anchor[anchorPos.vertical] - target[positions.y[0]]
if (newTop + target.bottom <= window.innerHeight) {
targetPosition.top = newTop
}
else {
newTop = anchor[anchorPos.vertical] - target[positions.y[1]]
if (newTop + target.bottom <= window.innerHeight) {
targetPosition.top = newTop
}
}
}
if (targetPosition.left < 0 || targetPosition.left + target.right > window.innerWidth) {
let newLeft = anchor[anchorPos.horizontal] - target[positions.x[0]]
if (newLeft + target.right <= window.innerWidth) {
targetPosition.left = newLeft
}
else {
newLeft = anchor[anchorPos.horizontal] - target[positions.x[1]]
if (newLeft + target.right <= window.innerWidth) {
targetPosition.left = newLeft
}
}
}
return targetPosition
}
export function parseHorizTransformOrigin (pos) {
return pos === 'middle' ? 'center' : pos
}
export function setPosition ({el, animate, anchorEl, anchorOrigin, selfOrigin, maxHeight, event, anchorClick, touchPosition, offset}) {
let anchor
el.style.maxHeight = maxHeight || '65vh'
if (event && (!anchorClick || touchPosition)) {
const {top, left} = eventPosition(event)
anchor = {top, left, width: 1, height: 1, right: left + 1, center: top, middle: left, bottom: top + 1}
}
else {
anchor = getAnchorPosition(anchorEl, offset)
}
let target = getTargetPosition(el)
let targetPosition = {
top: anchor[anchorOrigin.vertical] - target[selfOrigin.vertical],
left: anchor[anchorOrigin.horizontal] - target[selfOrigin.horizontal]
}
targetPosition = repositionIfNeeded(anchor, target, selfOrigin, anchorOrigin, targetPosition)
el.style.top = Math.max(0, targetPosition.top) + 'px'
el.style.left = Math.max(0, targetPosition.left) + 'px'
if (animate) {
const directions = targetPosition.top < anchor.top ? ['up', 'down'] : ['down', 'up']
el.classList.add(`animate-popup-${directions[0]}`)
el.classList.remove(`animate-popup-${directions[1]}`)
}
}
export function positionValidator (pos) {
let parts = pos.split(' ')
if (parts.length !== 2) {
return false
}
if (!['top', 'center', 'bottom'].includes(parts[0])) {
console.error('Anchor/Self position must start with one of top/center/bottom')
return false
}
if (!['left', 'middle', 'right'].includes(parts[1])) {
console.error('Anchor/Self position must end with one of left/middle/right')
return false
}
return true
}
export function offsetValidator (val) {
if (!val) { return true }
if (val.length !== 2) { return false }
if (typeof val[0] !== 'number' || typeof val[1] !== 'number') {
return false
}
return true
}
export function parsePosition (pos) {
let parts = pos.split(' ')
return {vertical: parts[0], horizontal: parts[1]}
}