-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog.js
104 lines (87 loc) · 2.46 KB
/
dialog.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
// @ts-check
import { assert } from './lib/assert.js';
import { scale, matrixStyle } from './lib/matrix2d.js';
export const createDialogBox = () => {
const element = document.createComment('');
/** @type {HTMLElement?} */
let showing = null;
/** @type {HTMLElement?} */
let waning = null;
/** @type {HTMLElement?} */
let waxing = null;
const logElement = () => {
const { parentNode } = element;
assert(parentNode !== null);
if (waxing !== null) {
console.warn(
'Log dropped because multiple logs in a single turn:',
waxing.innerHTML,
);
parentNode.removeChild(waxing);
waxing = null;
}
const dialogElement = document.createElement('div');
dialogElement.className = 'dialog panel';
dialogElement.style.transform = 'scale(0)';
parentNode.insertBefore(dialogElement, element);
waxing = dialogElement;
return dialogElement;
};
/**
* @param {string} html
*/
const logHTML = html => {
logElement().innerHTML = html;
};
/** @param {string} message */
const log = message => {
const dialogElement = logElement();
dialogElement.innerText = message;
};
const close = () => {
if (waning !== null) {
const { parentNode } = waning;
assert(parentNode !== null);
parentNode.removeChild(waning);
waning = null;
}
if (showing !== null) {
waning = showing;
showing = null;
}
};
const tock = () => {
if (waning !== null) {
const { parentNode } = element;
assert(parentNode !== null);
parentNode.removeChild(waning);
waning = null;
}
if (waxing !== null) {
if (showing !== null) {
waning = showing;
showing = null;
}
showing = waxing;
waxing = null;
}
};
/** @param {import('./progress.js').Progress} progress */
const animate = progress => {
if (progress.linear >= 1) return;
if (waxing !== null) {
waxing.style.transform = matrixStyle(scale(progress.sinusoidal));
waxing.style.transformOrigin = 'bottom center';
}
if (showing !== null) {
showing.style.transform = '';
}
if (waning !== null) {
waning.style.transform = matrixStyle(scale(1 - progress.sinusoidal));
waning.style.transformOrigin = 'top center';
}
};
const controller = { logHTML, log, animate, close, tock };
return { element, controller };
};
/** @typedef {ReturnType<createDialogBox>['controller']} DialogController */