-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
216 lines (174 loc) · 5.05 KB
/
helper.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
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
const copyToClipboard = ($selector, callback) => {
$selector.select();
document.execCommand("copy");
$selector.blur();
if (typeof callback == "function") {
callback();
}
};
const getCurrentFullDate = () => {
const now = new Date();
const currentDate = Intl.DateTimeFormat("en-us", {
dateStyle: "long",
}).format(now);
return currentDate;
};
const getTimestamp = () => {
const now = new Date();
return now.getTime();
};
const getDateTime = (date) => {
let hours = date.getHours();
let minutes = date.getMinutes();
let ampm = hours >= 12 ? "pm" : "am";
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? "0" + minutes : minutes;
let time = hours + ":" + minutes + " " + ampm;
return time.toUpperCase();
};
const newLine = (multiplier = 1) => {
let newLine = "";
for (let index = 0; index < multiplier; index++) {
newLine = newLine + "\n";
}
return newLine;
};
const closeWindow = () => {
window.close();
};
const goBack = () => {
window.history.back();
};
const daylightLyrics = [
"Wide awake, getting half past zero",
"It's getting heated so I leave the windows open (leave the windows open)",
"Preoccupied with a late night B-roll",
"Right now, laying here alone is heaven (here alone is heaven)",
"And I've been a hero",
"Helpless",
"I'm in hell",
"And I've cried",
"Up and down in these hallways",
"Blamed myself",
"Bad luck, I don't wanna be home at midnight",
"Sun's up, I don't really wanna fight the daylight",
"I don't care if you moved on",
"I'm not laying in bed with a fucked up head",
"I'm not laying in bed with a fucked up",
"Sun-dried on the backyard patio",
"Drunk eyes",
"'Cause I didn't give it a home run",
"Yeah, yeah",
"You're hiding on the FM radio",
"I sing along just to sing my thoughts at someone",
"Yeah, yeah",
"And I've been a hero",
"Helpless",
"I'm in hell",
"And I've cried",
"Up and down in these hallways",
"Blamed myself",
"Bad luck, I don't wanna be home at midnight",
"Sun's up, I don't really wanna fight the daylight",
"I don't care if you moved on",
"I'm not laying in bed with a fucked up head",
"I'm not laying in bed with a fucked up",
];
const decodeVariable = (string) => {
if (typeof string !== "string") {
return;
}
const randomNumber = Math.floor(Math.random() * daylightLyrics.length);
const today = new Date();
const currentTime = getDateTime(today);
string = string.replaceAll("[current_date]", getCurrentFullDate());
string = string.replaceAll("[current_time]", currentTime);
string = string.replaceAll("[daylight]", daylightLyrics[randomNumber]);
return string;
};
const downloadAsTextFile = (
contents,
fileName = "daylight - " + getCurrentFullDate()
) => {
if (contents.length < 1) {
return;
}
const element = document.createElement("a");
element.setAttribute(
"href",
"data:text/plain;charset=utf-8," + encodeURIComponent(contents)
);
element.setAttribute("download", fileName);
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
};
const dialog = (
options = {
content: "",
type: "alert",
confirmCallback: null,
cancelCallback: null,
}
) => {
const $dialog = document.querySelector("#dialog");
const $dialogBody = document.querySelector("#dialog-body");
const $dialogConfirm = document.querySelector("#dialog-confirm");
const $dialogCancel = document.querySelector("#dialog-cancel");
showElement($dialogCancel);
if (options.type == "alert") {
hideElement($dialogCancel);
}
const confirmHandler = () => {
console.log("Confirmed.");
if (typeof options.confirmCallback == "function") {
options.confirmCallback();
}
removeDialogActionsEventListeners();
};
const cancelHandler = () => {
console.log("Cancelled.");
if (typeof options.cancelCallback == "function") {
options.cancelCallback();
}
removeDialogActionsEventListeners();
};
const removeDialogActionsEventListeners = () => {
$dialogConfirm.removeEventListener("click", confirmHandler, false);
$dialogCancel.removeEventListener("click", cancelHandler, false);
};
$dialogBody.innerHTML = options.content;
$dialog.showModal();
$dialogConfirm.addEventListener("click", confirmHandler);
$dialogCancel.addEventListener("click", cancelHandler);
};
const hideElement = ($element) => {
$element.style.display = "none";
};
const showElement = ($element) => {
$element.style.display = "";
};
const resizeTextareaHandler = (e) => {
const $this = e.target ?? e;
$this.style.height = `auto`;
$this.style.height = `${$this.scrollHeight}px`;
$this.style.resize = `none`;
};
const autoResizeTextarea = ($textarea) => {
resizeTextareaHandler($textarea);
$textarea.addEventListener("input", resizeTextareaHandler, false);
};
export {
copyToClipboard,
getCurrentFullDate,
getTimestamp,
newLine,
closeWindow,
goBack,
decodeVariable,
downloadAsTextFile,
dialog,
autoResizeTextarea,
};