Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
NebulaServices
GitHub Repository: NebulaServices/Nebula
Path: blob/main/src/utils/index.ts
976 views
1
type TType = "success" | "error" | "multiline";
2
type ToastPosition = "top"
3
| "top-start"
4
| "top-end"
5
| "center"
6
| "center-start"
7
| "center-end"
8
| "bottom"
9
| "bottom-start"
10
| "bottom-end";
11
12
interface Props {
13
TType: TType;
14
text: string;
15
class: string;
16
id?: string;
17
duration?: number;
18
emoji?: any;
19
position?: ToastPosition;
20
}
21
22
/**
23
* This allows us to call our toast notifs.
24
*
25
* @example
26
* import { toast } from "@utils/index";
27
* toast(".toastMessage");
28
*/
29
function toast(query: string) {
30
const wrapper = document.getElementById("toastwrapper") as HTMLDivElement;
31
wrapper.classList.remove("hidden");
32
//this is a really hacky solution for toast notifications LOL
33
const element = document.querySelector(query) as HTMLElement;
34
//click the element
35
element.click();
36
}
37
38
type Items = {
39
type: "id" | "class" | "generic",
40
val: string
41
}
42
43
type ElementDatasets = {
44
name: string,
45
val: string | undefined
46
}
47
48
class Elements {
49
/**
50
* An async generator function to get your objects quickly and easily.
51
*
52
* @example
53
* const items = selectElements(items: [{ type: "id", val: "iframe" }]);
54
* for await (const item of items) {
55
* console.log(item) // Perform some action on this item (OR pause and continue when needed!)
56
* }
57
*/
58
static async *select(items: Items[]) {
59
for (const item in items) {
60
switch (items[item].type) {
61
case "id":
62
yield document.getElementById(items[item].val) as HTMLElement;
63
break;
64
case "class":
65
yield document.getElementsByClassName(items[item].val);
66
break;
67
case "generic":
68
yield document.getElementsByName(items[item].val);
69
break;
70
}
71
}
72
};
73
74
static exists<RetType>(elem: any): RetType {
75
if (elem.value) return elem.value as RetType;
76
throw new Error(`Something is WRONG. The element doesn't exist!`);
77
}
78
79
static attachEvent<Element extends HTMLElement, EType extends keyof HTMLElementEventMap>(item: Element, event: EType, fn: (event?: Event) => unknown) {
80
item.addEventListener(event, fn);
81
}
82
83
static createCustomElement(name: string, fn: (datasets?: ElementDatasets[]) => unknown, datasets?: Omit<ElementDatasets, "val">[]) {
84
class CustomEl extends HTMLElement {
85
dat: ElementDatasets[] = [];
86
constructor() {
87
super();
88
if (datasets) {
89
datasets.forEach((data) => {
90
this.dat.push({ name: data.name, val: this.dataset[data.name] });
91
});
92
}
93
(async () => await fn(this.dat))();
94
}
95
}
96
97
if (customElements.get(name)) return log({ type: "error", bg: true, prefix: false, throw: false }, `An element with the name ${name} already exists! This WILL not work! And`);
98
99
log({ type: 'info', bg: false, prefix: true }, `Creating custom element with the name ${name}`);
100
customElements.define(name, CustomEl);
101
}
102
}
103
104
/**
105
* Allows use to turn a basic phrase into a full URL. Mainly used when ther user enters something in for use in UV/SJ
106
*
107
* @example
108
* import { search } from "@utils/index";
109
* search("YES", "https://www.google.com/search?q=%s");
110
*/
111
function search(input: string, template: string) {
112
try { return new URL(input).toString() } catch (_) {};
113
114
try {
115
const url = new URL(`http://${input}`);
116
if (url.hostname.includes(".")) return url.toString();
117
} catch (_) {};
118
119
return template.replace("%s", encodeURIComponent(input));
120
}
121
122
type LogOpts = { type: "normal" | "warn" | "info", bg: boolean, prefix: boolean } | { type: "error", bg: boolean, prefix: boolean, throw: boolean }
123
/**
124
* Custom built log function with styles applied.
125
*
126
* @example
127
* import { log } from "@utils/index";
128
* log("info", opts: { bg: true, prefix: false }, message: "This is an example"); // BG can be true or false when BG is false, most of the time it reverts back to normal styling (except for the "normal" mode). When prefix is true, this adds a prefix of the type of message used.
129
*/
130
const log = (type: LogOpts, message: string): void => {
131
const styles = {
132
warn: {
133
bg: {
134
color: "#ffffff",
135
bg: "#cc3300"
136
},
137
normal: "#ffffff"
138
},
139
error: {
140
bg: {
141
color: "#ffffff",
142
bg: "#cc0000"
143
},
144
normal: "#ffffff"
145
},
146
info: {
147
bg: {
148
color: "#ffffff",
149
bg: "#088F8F"
150
},
151
normal: "#088F8F"
152
},
153
normal: {
154
bg: {
155
color: "#ffffff",
156
bg: "#7967dd"
157
},
158
normal: "#7967dd"
159
}
160
}
161
switch(type.type) {
162
case "info":
163
console.info(`%c${type.prefix ? `Info: ${message}` : message}`, `${type.bg ? `color: ${styles.info.bg.color}; background-color: ${styles.info.bg.bg}; padding: 2px 10px; font-weight: bold;` : `color: ${styles.info.normal}; font-weight: bold;`}`);
164
break;
165
case "error":
166
if (type.throw) throw new Error(message);
167
console.error(`%c${type.prefix ? `Error: ${message}` : message }`, `${type.bg ? `color: ${styles.error.bg.color}; background-color: ${styles.error.bg.bg}; padding: 2px 10px;` : `color: ${styles.error.normal};`}`);
168
break;
169
case "warn":
170
console.warn(`%c${type.prefix ? `Warning: ${message}` : message}`, `${type.bg ? `color: ${styles.warn.bg.color}; background-color: ${styles.warn.bg.bg}; padding: 2px 10px;` : `color: ${styles.warn.normal};`}`);
171
break;
172
case "normal":
173
console.log(`%c${message}`, `${type.bg ? `color: ${styles.normal.bg.color}; background-color: ${styles.normal.bg.bg}; padding: 2px 10px; font-weight: bold;` : `color: ${styles.normal.normal}; font-weight: bold;`}`);
174
break;
175
}
176
};
177
178
179
export {
180
type TType,
181
type ToastPosition,
182
type Props,
183
toast,
184
search,
185
log,
186
Elements
187
};
188
189