-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathutils.ts
315 lines (272 loc) · 8.61 KB
/
utils.ts
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { Rect, Axis, ElementX, ScrollAxis, IContainer } from './interfaces';
import { containerInstance } from './constants';
export const getIntersection = (rect1: Rect, rect2: Rect) => {
return {
left: Math.max(rect1.left, rect2.left),
top: Math.max(rect1.top, rect2.top),
right: Math.min(rect1.right, rect2.right),
bottom: Math.min(rect1.bottom, rect2.bottom),
};
};
export const getIntersectionOnAxis = (rect1: Rect, rect2: Rect, axis: Axis) => {
if (axis === 'x') {
return {
left: Math.max(rect1.left, rect2.left),
top: rect1.top,
right: Math.min(rect1.right, rect2.right),
bottom: rect1.bottom,
};
} else {
return {
left: rect1.left,
top: Math.max(rect1.top, rect2.top),
right: rect1.right,
bottom: Math.min(rect1.bottom, rect2.bottom),
};
}
};
export const getContainerRect = (element: HTMLElement): Rect => {
const _rect = element.getBoundingClientRect();
const rect = {
left: _rect.left,
right: _rect.right,
top: _rect.top,
bottom: _rect.bottom,
};
if (hasBiggerChild(element, 'x') && !isScrollingOrHidden(element, 'x')) {
const width = rect.right - rect.left;
rect.right = rect.right + element.scrollWidth - width;
}
if (hasBiggerChild(element, 'y') && !isScrollingOrHidden(element, 'y')) {
const height = rect.bottom - rect.top;
rect.bottom = rect.bottom + element.scrollHeight - height;
}
return rect;
};
export const getScrollingAxis = (element: HTMLElement): ScrollAxis | null => {
const style = window.getComputedStyle(element);
const overflow = style['overflow'];
const general = overflow === 'auto' || overflow === 'scroll';
if (general) return ScrollAxis.xy;
const overFlowX = style[`overflow-x` as any];
const xScroll = overFlowX === 'auto' || overFlowX === 'scroll';
const overFlowY = style[`overflow-y` as any];
const yScroll = overFlowY === 'auto' || overFlowY === 'scroll';
if (xScroll && yScroll) return ScrollAxis.xy;
if (xScroll) return ScrollAxis.x;
if (yScroll) return ScrollAxis.y;
return null;
};
export const isScrolling = (element: HTMLElement, axis: Axis) => {
const style = window.getComputedStyle(element);
const overflow = style['overflow'];
const overFlowAxis = style[`overflow-${axis}` as any];
const general = overflow === 'auto' || overflow === 'scroll';
const dimensionScroll = overFlowAxis === 'auto' || overFlowAxis === 'scroll';
return general || dimensionScroll;
};
export const isScrollingOrHidden = (element: HTMLElement, axis: Axis) => {
const style = window.getComputedStyle(element);
const overflow = style['overflow'];
const overFlowAxis = style[`overflow-${axis}` as any];
const general = overflow === 'auto' || overflow === 'scroll' || overflow === 'hidden';
const dimensionScroll = overFlowAxis === 'auto' || overFlowAxis === 'scroll' || overFlowAxis === 'hidden';
return general || dimensionScroll;
};
export const hasBiggerChild = (element: HTMLElement, axis: Axis) => {
if (axis === 'x') {
return element.scrollWidth > element.clientWidth;
} else {
return element.scrollHeight > element.clientHeight;
}
};
export const hasScrollBar = (element: HTMLElement, axis: Axis) => {
return hasBiggerChild(element, axis) && isScrolling(element, axis);
};
export const getVisibleRect = (element: HTMLElement, elementRect: Rect) => {
let currentElement = element;
let rect = elementRect || getContainerRect(element);
currentElement = element.parentElement!;
while (currentElement) {
if (hasBiggerChild(currentElement, 'x') && isScrollingOrHidden(currentElement, 'x')) {
rect = getIntersectionOnAxis(rect, currentElement.getBoundingClientRect(), 'x');
}
if (hasBiggerChild(currentElement, 'y') && isScrollingOrHidden(currentElement, 'y')) {
rect = getIntersectionOnAxis(rect, currentElement.getBoundingClientRect(), 'y');
}
currentElement = currentElement.parentElement!;
}
return rect;
};
export const getParentRelevantContainerElement = (element: Element, relevantContainers: IContainer[]) => {
let current: ElementX = element as ElementX;
while (current) {
if ((current as ElementX)[containerInstance]) {
const container = current[containerInstance];
if (relevantContainers.some(p => p === container)) {
return container;
}
}
current = current.parentElement as ElementX;
}
return null;
}
export const listenScrollParent = (element: HTMLElement, clb: () => void) => {
let scrollers: HTMLElement[] = [];
setScrollers();
function setScrollers() {
let currentElement = element;
while (currentElement) {
if (isScrolling(currentElement, 'x') || isScrolling(currentElement, 'y')) {
scrollers.push(currentElement);
}
currentElement = currentElement.parentElement!;
}
}
function dispose() {
stop();
scrollers = null!;
};
function start() {
if (scrollers) {
scrollers.forEach(p => p.addEventListener('scroll', clb));
window.addEventListener('scroll', clb);
}
}
function stop() {
if (scrollers) {
scrollers.forEach(p => p.removeEventListener('scroll', clb));
window.removeEventListener('scroll', clb);
}
}
return {
dispose,
start,
stop
};
};
export const hasParent = (element: HTMLElement, parent: HTMLElement) => {
let current: HTMLElement | null = element;
while (current) {
if (current === parent) {
return true;
}
current = current.parentElement;
}
return false;
};
export const getParent = (element: Element | null, selector: string) => {
let current: Element | null = element;
while (current) {
if (current.matches(selector)) {
return current;
}
current = current.parentElement;
}
return null;
};
export const hasClass = (element: HTMLElement, cls: string) => {
return (
element.className
.split(' ')
.map(p => p)
.indexOf(cls) > -1
);
};
export const addClass = (element: Element | null | undefined, cls: string) => {
if (element) {
const classes = element.className.split(' ').filter(p => p);
if (classes.indexOf(cls) === -1) {
classes.unshift(cls);
element.className = classes.join(' ');
}
}
};
export const removeClass = (element: HTMLElement, cls: string) => {
if (element) {
const classes = element.className.split(' ').filter(p => p && p !== cls);
element.className = classes.join(' ');
}
};
export const debounce = (fn: Function, delay: number, immediate: boolean) => {
let timer: any = null;
return (...params: any[]) => {
if (timer) {
clearTimeout(timer);
}
if (immediate && !timer) {
fn.call(null, ...params);
} else {
timer = setTimeout(() => {
timer = null;
fn.call(null, ...params);
}, delay);
}
};
};
export const removeChildAt = (parent: HTMLElement, index: number) => {
return parent.removeChild(parent.children[index]);
};
export const addChildAt = (parent: HTMLElement, child: HTMLElement, index: number) => {
if (index >= parent.children.length) {
parent.appendChild(child);
} else {
parent.insertBefore(child, parent.children[index]);
}
};
export const isMobile = () => {
if (typeof window !== 'undefined') {
if (
window.navigator.userAgent.match(/Android/i) ||
window.navigator.userAgent.match(/webOS/i) ||
window.navigator.userAgent.match(/iPhone/i) ||
window.navigator.userAgent.match(/iPad/i) ||
window.navigator.userAgent.match(/iPod/i) ||
window.navigator.userAgent.match(/BlackBerry/i) ||
window.navigator.userAgent.match(/Windows Phone/i)
) {
return true;
} else {
return false;
}
}
return false;
};
export const clearSelection = () => {
if (window.getSelection) {
if (window.getSelection().empty) {
// Chrome
window.getSelection().empty();
} else if (window.getSelection().removeAllRanges) {
// Firefox
window.getSelection().removeAllRanges();
}
} else if ((window.document as any).selection) {
// IE?
(window.document as any).selection.empty();
}
};
export const getElementCursor = (element: Element | null) => {
if (element) {
const style = window.getComputedStyle(element);
if (style) {
return style.cursor;
}
}
return null;
};
export const getDistanceToParent = (parent: HTMLElement, child: HTMLElement): number | null => {
let current: Element | null = child;
let dist = 0;
while (current) {
if (current === parent) {
return dist;
}
dist++;
current = current.parentElement;
}
return null;
}
export function isVisible(rect: Rect): boolean {
return !(rect.bottom <= rect.top || rect.right <= rect.left);
}