generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbackRegistry.js
172 lines (153 loc) · 4.81 KB
/
callbackRegistry.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { back, forward, reload } from '@aegisjsproject/router/router.js';
let isRegistrationOpen = true;
export const closeRegistration = () => isRegistrationOpen = false;
const $$ = (selector, base = document) => base.querySelectorAll(selector);
const $ = (selector, base = document) => base.querySelector(selector);
export const FUNCS = {
debug: {
log: 'aegis:debug:log',
info: 'aegis:debug:info',
warn: 'aegis:debug:warn',
error: 'aegis:debug:error',
},
navigate: {
back: 'aegis:navigate:back',
forward: 'aegis:navigate:forward',
reload: 'aegis:navigate:reload',
link: 'aegis:navigate:go',
popup: 'aegis:navigate:popup',
},
ui: {
print: 'aegis:ui:print',
remove: 'aegis:ui:remove',
hide: 'aegis:ui:hide',
unhide: 'aegis:ui:unhide',
showModal: 'aegis:ui:showModal',
closeModal: 'aegis:ui:closeModal',
showPopover: 'aegis:ui:showPopover',
hidePopover: 'aegis:ui:hidePopover',
togglePopover: 'aegis:ui:togglePopover',
enable: 'aegis:ui:enable',
disable: 'aegis:ui:disable',
scrollTo: 'aegis:ui:scrollTo',
prevent: 'aegis:ui:prevent',
},
};
const handlers = new Map([
[FUNCS.debug.log, console.log],
[FUNCS.debug.warn, console.warn],
[FUNCS.debug.error, console.error],
[FUNCS.debug.info, console.info],
[FUNCS.navigate.back, back],
[FUNCS.navigate.forward, forward],
[FUNCS.navigate.reload, reload],
[FUNCS.navigate.link, event => {
if (event.isTrusted) {
event.preventDefault();
location.href = event.currentTarget.dataset.url;
}
}],
[FUNCS.navigate.popup, event => {
if (event.isTrusted) {
event.preventDefault();
window.open(event.currentTarget.dataset.url);
}
}],
[FUNCS.ui.hide, ({ currentTarget }) => {
$$(currentTarget.dataset.hideSelector).forEach(el => el.hidden = true);
}],
[FUNCS.ui.unhide, ({ currentTarget }) => {
$$(currentTarget.dataset.unhideSelector).forEach(el => el.hidden = false);
}],
[FUNCS.ui.disable, ({ currentTarget }) => {
$$(currentTarget.dataset.disableSelector).forEach(el => el.disabled = true);
}],
[FUNCS.ui.enable, ({ currentTarget }) => {
$$(currentTarget.dataset.enableSelector).forEach(el => el.disabled = false);
}],
[FUNCS.ui.remove, ({ currentTarget }) => {
$$(currentTarget.dataset.removeSelector).forEach(el => el.remove());
}],
[FUNCS.ui.scrollTo, ({ currentTarget }) => {
const target = $(currentTarget.dataset.scrollToSelector);
if (target instanceof Element) {
target.scrollIntoView({
behavior: matchMedia('(prefers-reduced-motion: reduce)').matches
? 'instant'
: 'smooth',
});
}
}],
[FUNCS.ui.showModal, ({ currentTarget }) => {
const target = $(currentTarget.dataset.showModalSelector);
if (target instanceof HTMLDialogElement) {
target.showModal();
}
}],
[FUNCS.ui.closeModal, ({ currentTarget }) => {
const target = $(currentTarget.dataset.closeModalSelector);
if (target instanceof HTMLDialogElement) {
target.close();
}
}],
[FUNCS.ui.showPopover, ({ currentTarget }) => {
const target = $(currentTarget.dataset.showPopoverSelector);
if (target instanceof HTMLElement) {
target.showPopover();
}
}],
[FUNCS.ui.hidePopover, ({ currentTarget }) => {
const target = $(currentTarget.dataset.hidePopoverSelector);
if (target instanceof HTMLElement) {
target.hidePopover();
}
}],
[FUNCS.ui.togglePopover, ({ currentTarget }) => {
const target = $(currentTarget.dataset.togglePopoverSelector);
if (target instanceof HTMLElement) {
target.togglePopover();
}
}],
[FUNCS.ui.print, () => globalThis.print()],
[FUNCS.ui.prevent, event => event.preventDefault()],
]);
export const listCallbacks = () => Object.freeze(Array.from(handlers.keys()));
export const hasCallback = name => handlers.has(name);
export const getCallback = name => handlers.get(name);
export function callCallback(name, ...args) {
if (handlers.has(name)) {
return handlers.get(name).apply(this || globalThis, args);
} else {
throw new Error(`No ${name} function registered.`);
}
}
export function createCallback(callback) {
return registerCallback('aegis:callback:' + crypto.randomUUID(), callback);
}
export function registerCallback(name, callback) {
if (typeof name !== 'string' || name.length === 0) {
throw new TypeError('Callback name must be a string.');
} if (! (callback instanceof Function)) {
throw new TypeError('Callback must be a function.');
} else if (isRegistrationOpen) {
if (handlers.has(name)) {
throw new Error(`Handler "${name}" is already registered.`);
} else {
handlers.set(name, callback);
return name;
}
} else {
return name;
}
}
export function getHost(target) {
if (target instanceof Event) {
return getHost(target.currentTarget);
} else if (target instanceof Document) {
return target;
} else if (target instanceof Element) {
return getHost(target.getRootNode());
} else if (target instanceof ShadowRoot) {
return target.host;
}
}