beercss 3.6.13 → 3.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,162 @@
1
+ import { updateDialog } from "./elements/dialogs";
2
+ import { updateMenu } from "./elements/menus";
3
+ import { updatePage } from "./elements/pages";
4
+ import { updateSnackbar } from "./elements/snackbars";
5
+
6
+ const _emptyNodeList = [] as unknown as NodeListOf<Element>;
7
+
8
+ export function isTouchable(): boolean {
9
+ return window.matchMedia("(pointer: coarse)").matches;
10
+ }
11
+
12
+ export function isDark(): boolean {
13
+ return window.matchMedia("(prefers-color-scheme: dark)").matches;
14
+ }
15
+
16
+ export async function wait(milliseconds: number) {
17
+ await new Promise((resolve) => setTimeout(resolve, milliseconds));
18
+ }
19
+
20
+ export function guid(): string {
21
+ return "fxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c: string) => {
22
+ const r = (Math.random() * 16) | 0;
23
+ const v = c === "x" ? r : (r & 0x3) | 0x8;
24
+ return v.toString(16);
25
+ });
26
+ }
27
+
28
+ export function query(selector: string | Element | null, element?: Element | null): Element | null {
29
+ try {
30
+ return (typeof selector === "string")
31
+ ? (element ?? document).querySelector(selector)
32
+ : selector;
33
+ } catch {
34
+ return null;
35
+ }
36
+ }
37
+
38
+ export function queryAll(selector: string | NodeListOf<Element> | null, element?: Element | null): NodeListOf<Element> {
39
+ try {
40
+ return (typeof selector === "string")
41
+ ? (element ?? document).querySelectorAll(selector)
42
+ : selector ?? _emptyNodeList;
43
+ } catch {
44
+ return _emptyNodeList;
45
+ }
46
+ }
47
+
48
+ export function hasClass(element: Element | null, name: string): boolean {
49
+ return element?.classList.contains(name) ?? false;
50
+ }
51
+
52
+ export function hasTag(element: Element | null, name: string): boolean {
53
+ return element?.tagName?.toLowerCase() === name;
54
+ }
55
+
56
+ export function hasType(element: HTMLInputElement | null, name: string): boolean {
57
+ return element?.type?.toLowerCase() === name;
58
+ }
59
+
60
+ export function addClass(element: Element | null | NodeListOf<Element>, name: string) {
61
+ if (element instanceof NodeList) for(let i=0; i<element.length; i++) element[i].classList.add(name);
62
+ else element?.classList.add(name);
63
+ }
64
+
65
+ export function removeClass(element: Element | null | NodeListOf<Element>, name: string) {
66
+ if (element instanceof NodeList) for(let i=0; i<element.length; i++) element[i].classList.remove(name);
67
+ else element?.classList.remove(name);
68
+ }
69
+
70
+ export function on(element: Element | null, name: string, callback: any, useCapture: boolean = true) {
71
+ if (element?.addEventListener) element.addEventListener(name, callback, useCapture);
72
+ }
73
+
74
+ export function off(element: Element | null, name: string, callback: any, useCapture: boolean = true) {
75
+ if (element?.removeEventListener) element.removeEventListener(name, callback, useCapture);
76
+ }
77
+
78
+ export function insertBefore(newElement: Element, element: Element | null) {
79
+ element?.parentNode?.insertBefore(newElement, element);
80
+ }
81
+
82
+ export function prev(element: Element): Element | null {
83
+ return element?.previousElementSibling;
84
+ }
85
+
86
+ export function next(element: Element): Element | null {
87
+ return element?.nextElementSibling;
88
+ }
89
+
90
+ export function parent(element: Element): Element | null {
91
+ return element?.parentElement;
92
+ }
93
+
94
+ export function create(htmlAttributesAsJson: any): HTMLElement {
95
+ const element = document.createElement("div");
96
+ for (let i = 0, keys = Object.keys(htmlAttributesAsJson), n = keys.length; i < n; i++) {
97
+ const key = keys[i];
98
+ const value = htmlAttributesAsJson[key] as string;
99
+ element.setAttribute(key, value);
100
+ }
101
+ return element;
102
+ }
103
+
104
+ export function blurActiveElement() {
105
+ (document.activeElement as HTMLElement)?.blur();
106
+ }
107
+
108
+ export function queryAllDataUi(id: string): NodeListOf<Element> {
109
+ return queryAll("[data-ui=\"#"+id+"\"]");
110
+ }
111
+
112
+ export function queryDataUi(id: string): Element | null {
113
+ return query("[data-ui=\"#"+id+"\"]");
114
+ }
115
+
116
+ export function updateAllClickable(element: Element) {
117
+ if (element.id && hasClass(element, "page")) element = queryDataUi(element.id) ?? element;
118
+
119
+ const container = parent(element);
120
+ if (!hasClass(container, "tabs") && !hasClass(container, "tabbed") && !hasTag(container, "nav")) return;
121
+
122
+ const as = queryAll("a", container);
123
+ for(let i=0; i<as.length; i++) removeClass(as[i], "active");
124
+ addClass(element, "active");
125
+ }
126
+
127
+ export async function run(from: Element, to: Element | null, options?: any, e?: Event): Promise<void> {
128
+ if (!to) {
129
+ to = query(from.getAttribute("data-ui"));
130
+ if (!to) return;
131
+ }
132
+
133
+ updateAllClickable(from);
134
+
135
+ if (hasTag(to, "dialog")) {
136
+ await updateDialog(from, to as HTMLDialogElement);
137
+ return;
138
+ }
139
+
140
+ if (hasTag(to, "menu")) {
141
+ updateMenu(from, to as HTMLMenuElement, e);
142
+ return;
143
+ }
144
+
145
+ if (hasClass(to, "snackbar")) {
146
+ updateSnackbar(to, options as number);
147
+ return;
148
+ }
149
+
150
+ if (hasClass(to, "page")) {
151
+ updatePage(to);
152
+ return;
153
+ }
154
+
155
+ if (hasClass(to, "active")) {
156
+ removeClass(from, "active");
157
+ removeClass(to, "active");
158
+ return;
159
+ }
160
+
161
+ addClass(to, "active");
162
+ }