-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
99 lines (83 loc) · 2.21 KB
/
main.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
const NUM = 'number'
const STR = 'string'
let stdout = '';
let timer;
Module({
onAbort(reason) {
console.error(`WASM aborted: ${reason}`)
},
print(data) {
if (data) {
clearTimeout(timer);
stdout = `${stdout}${data}`;
timer = setTimeout(() => {
console.dir(stdout);
stdout = '';
}, 5);
}
},
printErr(data) {
if (data) {
console.log('stderr: ', data)
}
},
})
.then(({ccall, FS, IDBFS}) => {
ccall('pib_init', NUM, [STR], []);
const version = ccall('pib_exec', STR, [STR], [`phpversion();`]);
console.log('PHP VERSION: ', version);
const loadingAlert = document.querySelector('.loading');
const form = document.querySelector('form')
const fields = form.elements;
const {
sourceDT,
date,
time,
format,
output,
customDT,
nowDT,
} = fields;
loadingAlert.remove();
form.classList.remove('disabled');
const intObs = new IntersectionObserver((entries) => {
entries.forEach(entry => {
const isStuck = entry.intersectionRatio < 1
form.classList.toggle('is-stuck', isStuck);
})
}, {
rootMargin: "-1px 100% 100% 100%",
threshold: [0.99999, 1],
});
intObs.observe(form);
const updateOnInput = () => {
if (date.value && time.value && format.value) {
const php = `(new DateTimeImmutable('${date.value} ${time.value}'))->format('${format.value}');`;
const formatted = ccall('pib_exec', STR, [STR], [php]);
output.value = formatted;
}
}
setInterval(() => {
if (sourceDT.value === 'now') {
const now = new Date()
const nowDate = now.toISOString().split('T')[0]
const nowTime = now.toTimeString().split(' ')[0]
date.value = nowDate;
time.value = nowTime;
updateOnInput();
}
}, 1000);
date.addEventListener('input', updateOnInput);
time.addEventListener('input', updateOnInput);
format.addEventListener('input', updateOnInput);
customDT.addEventListener('input', () => {
date.value = '';
date.removeAttribute('readonly');
time.value = '';
time.removeAttribute('readonly');
});
nowDT.addEventListener('input', () => {
date.setAttribute('readonly', '');
time.setAttribute('readonly', '')
})
})