forked from Dogfalo/materialize
-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathutils.ts
286 lines (255 loc) · 7.95 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
import { Edges } from './edges';
import { Bounding } from './bounding';
/**
* Class with utilitary functions for global usage.
*/
export class Utils {
/** Specifies wether tab is pressed or not. */
static tabPressed: boolean = false;
/** Specifies wether there is a key pressed. */
static keyDown: boolean = false;
/**
* Key maps.
*/
static keys = {
TAB: ['Tab'],
ENTER: ['Enter'],
ESC: ['Escape', 'Esc'],
BACKSPACE: ['Backspace'],
ARROW_UP: ['ArrowUp', 'Up'],
ARROW_DOWN: ['ArrowDown', 'Down'],
ARROW_LEFT: ['ArrowLeft', 'Left'],
ARROW_RIGHT: ['ArrowRight', 'Right'],
DELETE: ['Delete', 'Del']
};
/**
* Detects when a key is pressed.
* @param e Event instance.
*/
static docHandleKeydown(e: KeyboardEvent) {
Utils.keyDown = true;
if ([...Utils.keys.TAB, ...Utils.keys.ARROW_DOWN, ...Utils.keys.ARROW_UP].includes(e.key)) {
Utils.tabPressed = true;
}
}
/**
* Detects when a key is released.
* @param e Event instance.
*/
static docHandleKeyup(e: KeyboardEvent) {
Utils.keyDown = false;
if ([...Utils.keys.TAB, ...Utils.keys.ARROW_DOWN, ...Utils.keys.ARROW_UP].includes(e.key)) {
Utils.tabPressed = false;
}
}
/**
* Detects when document is focused.
* @param e Event instance.
*/
/* eslint-disabled as of required event type condition check */
/* eslint-disable-next-line */
static docHandleFocus(e: FocusEvent) {
if (Utils.keyDown) {
document.body.classList.add('keyboard-focused');
}
}
/**
* Detects when document is not focused.
* @param e Event instance.
*/
/* eslint-disabled as of required event type condition check */
/* eslint-disable-next-line */
static docHandleBlur(e: FocusEvent) {
document.body.classList.remove('keyboard-focused');
}
/**
* Generates a unique string identifier.
*/
static guid(): string {
const s4 = (): string => {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
};
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
/**
* Checks for exceeded edges
* @param container Container element.
* @param bounding Bounding rect.
* @param offset Element offset.
*/
static checkWithinContainer(container: HTMLElement, bounding: Bounding, offset: number): Edges {
const edges = {
top: false,
right: false,
bottom: false,
left: false
};
const containerRect = container.getBoundingClientRect();
// If body element is smaller than viewport, use viewport height instead.
const containerBottom =
container === document.body
? Math.max(containerRect.bottom, window.innerHeight)
: containerRect.bottom;
const scrollLeft = container.scrollLeft;
const scrollTop = container.scrollTop;
const scrolledX = bounding.left - scrollLeft;
const scrolledY = bounding.top - scrollTop;
// Check for container and viewport for each edge
if (scrolledX < containerRect.left + offset || scrolledX < offset) {
edges.left = true;
}
if (
scrolledX + bounding.width > containerRect.right - offset ||
scrolledX + bounding.width > window.innerWidth - offset
) {
edges.right = true;
}
if (scrolledY < containerRect.top + offset || scrolledY < offset) {
edges.top = true;
}
if (
scrolledY + bounding.height > containerBottom - offset ||
scrolledY + bounding.height > window.innerHeight - offset
) {
edges.bottom = true;
}
return edges;
}
/**
* Checks if element can be aligned in multiple directions.
* @param el Element to be inspected.
* @param container Container element.
* @param bounding Bounding rect.
* @param offset Element offset.
*/
static checkPossibleAlignments(
el: HTMLElement,
container: HTMLElement,
bounding: Bounding,
offset: number
) {
const canAlign: {
top: boolean;
right: boolean;
bottom: boolean;
left: boolean;
spaceOnTop: number;
spaceOnRight: number;
spaceOnBottom: number;
spaceOnLeft: number;
} = {
top: true,
right: true,
bottom: true,
left: true,
spaceOnTop: null,
spaceOnRight: null,
spaceOnBottom: null,
spaceOnLeft: null
};
const containerAllowsOverflow = getComputedStyle(container).overflow === 'visible';
const containerRect = container.getBoundingClientRect();
const containerHeight = Math.min(containerRect.height, window.innerHeight);
const containerWidth = Math.min(containerRect.width, window.innerWidth);
const elOffsetRect = el.getBoundingClientRect();
const scrollLeft = container.scrollLeft;
const scrollTop = container.scrollTop;
const scrolledX = bounding.left - scrollLeft;
const scrolledYTopEdge = bounding.top - scrollTop;
const scrolledYBottomEdge = bounding.top + elOffsetRect.height - scrollTop;
// Check for container and viewport for left
canAlign.spaceOnRight = !containerAllowsOverflow
? containerWidth - (scrolledX + bounding.width)
: window.innerWidth - (elOffsetRect.left + bounding.width);
if (canAlign.spaceOnRight < 0) {
canAlign.left = false;
}
// Check for container and viewport for Right
canAlign.spaceOnLeft = !containerAllowsOverflow
? scrolledX - bounding.width + elOffsetRect.width
: elOffsetRect.right - bounding.width;
if (canAlign.spaceOnLeft < 0) {
canAlign.right = false;
}
// Check for container and viewport for Top
canAlign.spaceOnBottom = !containerAllowsOverflow
? containerHeight - (scrolledYTopEdge + bounding.height + offset)
: window.innerHeight - (elOffsetRect.top + bounding.height + offset);
if (canAlign.spaceOnBottom < 0) {
canAlign.top = false;
}
// Check for container and viewport for Bottom
canAlign.spaceOnTop = !containerAllowsOverflow
? scrolledYBottomEdge - (bounding.height - offset)
: elOffsetRect.bottom - (bounding.height + offset);
if (canAlign.spaceOnTop < 0) {
canAlign.bottom = false;
}
return canAlign;
}
/**
* Retrieves target element id from trigger.
* @param trigger Trigger element.
*/
static getIdFromTrigger(trigger: HTMLElement): string {
let id = trigger.dataset.target;
if (!id) {
id = trigger.getAttribute('href');
return id ? id.slice(1) : '';
}
return id;
}
/**
* Retrieves document scroll postion from top.
*/
static getDocumentScrollTop(): number {
return window.scrollY || document.documentElement.scrollTop || document.body.scrollTop || 0;
}
/**
* Retrieves document scroll postion from left.
*/
static getDocumentScrollLeft(): number {
return window.scrollX || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
}
/**
* Fires the given function after a certain ammount of time.
* @param func Function to be fired.
* @param wait Wait time.
* @param options Additional options.
*/
static throttle(
func: (Function: object) => void,
wait: number,
options: Partial<{ leading: boolean; trailing: boolean }> = {}
) {
let context: object,
args: IArguments,
result,
timeout = null,
previous = 0;
const later = () => {
previous = options.leading === false ? 0 : new Date().getTime();
timeout = null;
result = func.apply(context, args);
context = args = null;
};
return (...args) => {
const now = new Date().getTime();
if (!previous && options.leading === false) previous = now;
const remaining = wait - (now - previous);
context = this;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
}
}