-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
89 lines (77 loc) · 2.49 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
//https://stackoverflow.com/a/35385518/18704284
function htmlToElement(html) {
var template = document.createElement('template');
html = html.trim(); // Never return a text node of whitespace as the result
template.innerHTML = html;
return template.content.firstChild;
}
function call1(func) {
let result;
return () => (result ??= [func()])[0];
}
//https://stackoverflow.com/a/22114687/18704284
function copy(src) {
var dst = new ArrayBuffer(src.byteLength);
new Uint8Array(dst).set(new Uint8Array(src));
return dst;
}
let prevA
function download(file, filename) {
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
if(prevA) document.body.removeChild(prevA)
var a = prevA = document.createElement("a")
const url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
window.URL.revokeObjectURL(url);
}, 0);
}
}
function downloadUrl(url, filename) {
if(prevA) document.body.removeChild(prevA)
var a = prevA = document.createElement("a")
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
}
function dateToStr(date) {
return date.getUTCDate().toString().padStart(2, '0')
+ '.' + (date.getUTCMonth()+1).toString().padStart(2, '0')
+ '.' + date.getUTCFullYear().toString().padStart(4, '0')
}
function parseDate(str) {
if(!str) return
const dateRegex = /^(\d\d)\.(\d\d)\.(\d\d\d\d)$/
const r = str.match(dateRegex)
if(!r) return
const d = r[1], m = r[2], y = r[3];
const date = new Date(y, m-1, d)
if(date instanceof Date && isFinite(date)) return date;
}
function addClick(element, callback) {
function keydownHandler(event) {
if(event.keyCode === 32) {
event.preventDefault();
}
else if (event.keyCode === 13) {
event.preventDefault();
callback(event);
}
}
function keyupHandler(event) {
if(event.keyCode !== 32) return
event.preventDefault();
callback(event);
}
element.addEventListener('keydown', keydownHandler)
element.addEventListener('keyup', keyupHandler)
element.addEventListener('click', (e) => { callback(e); element.blur() })
element.setAttribute('tabindex', '0')
element.setAttribute('data-custom-button', '')
}