-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
147 lines (135 loc) · 6.1 KB
/
common.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
// ###
// # #
// # ### ## # ## # ### # ##
// # # # # # # # # # # # ## #
// # # # # # # # # # # # # #
// # # # # # # # # # # # # # #
// ### ### # # # # ### # #
/**
* A class that provides common functions.
*/
class Common {
// # # ### #
// # # # #
// # ## ### # # ### ### # ## # # ##
// ### # # # # #### # # # # # #### # ##
// # # # # # # # ## # # # # # ##
// # ## # # # # # ## # ### # # ##
/**
* Formats the time portion of the date.
* @param {Date} time The time to display.
* @returns {string} The formatted time.
*/
static formatTime(time) {
return (time.getHours() === 0 ? "12" : time.getHours() > 12 ? (time.getHours() - 12).toString() : time.getHours().toString()) + ":" + (time.getMinutes() < 10 ? "0" : "") + time.getMinutes().toString() + " " + (time.getHours() < 12 ? "AM" : "PM");
}
// # # ### #
// # # # # # #
// # ## ### # # ### ### # # ### ### ##
// ### # # # # #### # # # # # # # # # ##
// # # # # # # # ## # # # # ## # ##
// # ## # # # # # ## ### # # ## ##
/**
* Formats the date to show in the user's time zone.
* @param {Date} time The date and time to display.
* @returns {string} The formatted date and time.
*/
static formatDate(time) {
const now = new Date(),
today = new Date(now);
today.setMilliseconds(0);
today.setSeconds(0);
today.setMinutes(0);
today.setHours(0);
const date = new Date(time);
date.setMilliseconds(0);
date.setSeconds(0);
date.setMinutes(0);
date.setHours(0);
switch (date.getTime() - today.getTime()) {
case 0:
return "Today " + Common.formatTime(time);
case 86400000:
return "Tomorrow " + Common.formatTime(time);
case -86400000:
return "Yesterday " + Common.formatTime(time);
default:
return ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][time.getMonth()] + " " + time.getDate().toString() + " " + time.getFullYear().toString() + " " + Common.formatTime(time);
}
}
// # # ## #### #
// # # # # #
// ### ### # # # ### ### ## ## ### ##
// # # # #### # # # # # # # # # # ##
// # # # # # # # # # # # # # # ##
// # # ## # # ### #### # # ## ## ### ##
/**
* HTML-encodes a string.
* @param {string} str The string.
* @returns {string} The encoded string.
*/
static htmlEncode(str) {
return str.replace(/&/gim, ">").replace(/</gim, "<").replace(/[\u0080-\uFFFF]/gim, (i) => `&#${i.charCodeAt(0)};`);
}
// ## # ### # ### # ### ## #
// # # # # # # # # # #
// # ## ### ### # # ### ### ### # ### ### ## # ## # # ### # ### ### ##
// # # # # # # # # # # # # # # # # # # # # # # ## #### # # # # # # # ##
// # # # # ## # # # # # ## # # ## # # # # # # # ## # # # # # # ## # ##
// ### ## # # ### ### # # ## # # ### # # ## ## # ## # # ### ### # # ## ##
// #
/**
* Loads data from an API into an element.
* @param {string} api The API to load data from.
* @param {string} querySelector The query selector to fill the data into.
* @param {function} template The template function.
* @returns {Promise} A promise that resolves when the data has been loaded.
*/
static loadDataIntoTemplate(api, querySelector, template) {
var el = document.querySelector(querySelector);
el.innerHTML = "<div class=\"loading\">Loading...</div>";
return fetch(api).then((res) => res.json()).then((data) => {
el.innerHTML = "";
if (Array.isArray(data)) {
data.forEach((item) => {
el.insertAdjacentHTML("beforeend", template(item));
});
} else {
el.innerHTML = template(data);
}
});
}
// # ### # #
// # # # # #
// ### ### ### ### ## # # ### ### ## # # ### ### ## # # ### ###
// # # # # # # # # # # #### # # # # # ## # # # # # # # # # # # #
// # # ## # # # # # # # # # # # ## # # # ## # # # # # # # # #
// # # # # # ### ## # # ### # # ## # # # # ## ### # # ###
// ###
/**
* Gives a random background.
* @return {string} The random background.
*/
static randomBackground() {
return Common.backgrounds[Math.floor(Math.random() * Common.backgrounds.length)] + ".jpg";
}
}
Common.backgrounds = [
"backfire",
"blizzard",
"burning indika",
"centrifuge",
"foundry",
"hive",
"junebug",
"keg party",
"labyrinth",
"mesa",
"roundabout",
"sub rosa",
"syrinx",
"terminal",
"turnstile",
"vault",
"wraith"
];