-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.ts
166 lines (147 loc) · 4.43 KB
/
utils.ts
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
const DEBUG = false;
function logDebug(action, vars = {}) {
if (DEBUG)
console.log(" -> " + action, vars);
}
function hasClass(element, clss) {
const classes = new Set(element.className.split(/\s+/));
return classes.has(clss);
}
function addClass(element, clss) {
const classes = new Set(element.className.split(/\s+/));
classes.add(clss);
element.className = Array.from(classes).join(" ");
}
function rmClass(element, clss) {
const classes = new Set(element.className.split(/\s+/));
if (classes.has(clss))
classes.delete(clss);
element.className = Array.from(classes).join(" ");
}
function switchClass(element, clss, condition) {
const classes = new Set(element.className.split(/\s+/));
if (classes.has(clss))
classes.delete(clss);
if (condition)
classes.add(clss);
element.className = Array.from(classes).join(" ");
}
function fixImage(url, replacement=null) {
if (/image_not_available/.test(url)) {
if (replacement)
replacement.style.display = "block";
return '';
}
if (replacement)
replacement.style.display = "none";
return url.replace(/^http:/, '');
}
function formatNumber(x) {
return (x + "")
.replace(/(.)(.{3})$/, '<span class="shifted">$1</span>$2');
}
const monthNames = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
function formatMonth(dat) {
const d = new Date(dat);
return monthNames[new Date(dat).getMonth()] + " " + dat.slice(0, 4);
}
// Lighten colors function copied from Chris Coyier https://css-tricks.com/snippets/javascript/lighten-darken-color/
function lightenColor(col, amt=50) {
var usePound = false;
if (col[0] === "#") {
col = col.slice(1);
usePound = true;
}
var num = parseInt(col,16);
var r = (num >> 16) + amt;
if (r > 255) r = 255;
else if (r < 0) r = 0;
var b = ((num >> 8) & 0x00FF) + amt;
if (b > 255) b = 255;
else if (b < 0) b = 0;
var g = (num & 0x0000FF) + amt;
if (g > 255) g = 255;
else if (g < 0) g = 0;
return (usePound?"#":"") + (g | (b << 8) | (r << 16)).toString(16);
}
function meanArray(arr) {
return arr.reduce((a, b) => a + b, 0) / arr.length;
}
function divWidth(divId) {
return document.getElementById(divId).getBoundingClientRect().width;
}
function divHeight(divId) {
return document.getElementById(divId).getBoundingClientRect().height;
}
function isTouchDevice() {
return (('ontouchstart' in window) ||
(navigator.maxTouchPoints > 0) ||
(navigator["msMaxTouchPoints"] > 0));
}
function webGLSupport() {
try {
var canvas = document.createElement('canvas');
return !!window.WebGLRenderingContext &&
(canvas.getContext('webgl') || canvas.getContext('experimental-webgl'));
} catch(e) {
return false;
}
};
function rotatePosition(pos, angle) {
return {
x: pos.x * Math.cos(-angle) - pos.y * Math.sin(-angle),
y: pos.y * Math.cos(-angle) + pos.x * Math.sin(-angle)
};
};
function useWebWorker(script, inputData, callback) {
const worker_blob = new Blob([script], { type: "application/javascript" });
const worker_url = URL.createObjectURL(worker_blob);
const worker = new Worker(worker_url);
worker.onmessage = ({ data }) => {
callback(data);
worker.terminate();
};
worker.postMessage(inputData);
};
function uncompress(compressed, method, callback) {
useWebWorker(`
importScripts("${window.location.origin}/pako_inflate.min.js");
self.onmessage = async (evt) => {
const file = evt.data;
const decompressed = pako.${method}(file, {to: "string"});
self.postMessage(decompressed);
};
`, compressed, callback);
};
function buildComicsList(data, callback) {
useWebWorker(`
self.onmessage = async (evt) => {
const data = evt.data;
const lightenColor = ${lightenColor.toString()};
const list = data.comics.map(
x => '<li id="comic-' + x.id + '"' +
(data.color
? ' style="color: ' + lightenColor(data.creatorsRoles[x.role]) + '"'
: ""
) + (data.selectedComic && x.id === data.selectedComic.id
? ' class="selected"'
: ""
) + '>' + x.title + '</li>'
);
self.postMessage(list);
};
`, data, callback);
}
export {
logDebug,
hasClass, addClass, rmClass, switchClass,
fixImage,
formatNumber, formatMonth,
lightenColor,
meanArray,
divWidth, divHeight,
isTouchDevice, webGLSupport,
rotatePosition,
uncompress,
buildComicsList
};