-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmlInterface.js
107 lines (102 loc) · 3.29 KB
/
htmlInterface.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
/* global play */
function createInfo(text) {
const textElement = document.createElement('div');
const appElement = document.getElementById('app');
textElement.textContent = text;
textElement.className = 'text';
appElement.appendChild(textElement);
}
function createChoice(text, choices) {
const appElement = document.getElementById('app');
const containerElement = document.createElement('div');
createInfo(text);
appElement.appendChild(containerElement);
const promise = new Promise((resolve) => {
const choiceButtons = [];
choices.forEach(({ id, label }, index) => {
const choiceButton = document.createElement('button');
choiceButton.textContent = label;
const eventListener = () => {
choiceButton.removeEventListener('click', eventListener);
appElement.removeChild(containerElement);
const choiceValueElement = document.createElement('div');
choiceValueElement.textContent = label;
appElement.appendChild(choiceValueElement);
resolve(id);
};
choiceButton.addEventListener('click', eventListener);
choiceButtons.push(choiceButton);
containerElement.appendChild(choiceButton);
if (index === 0) {
choiceButton.focus();
}
});
});
return promise;
}
function createInput(text, type) {
const appElement = document.getElementById('app');
createInfo(text);
const promise = new Promise((resolve) => {
const formElement = document.createElement('form');
const numberInput = document.createElement('input');
if (type) {
numberInput.setAttribute('type', type);
}
const enterButton = document.createElement('button');
enterButton.textContent = 'ENTER';
const eventListener = (event) => {
enterButton.removeEventListener('click', eventListener);
formElement.removeEventListener('submit', eventListener);
const { value } = numberInput;
resolve(value);
formElement.removeChild(numberInput);
formElement.removeChild(enterButton);
const numberValueElement = document.createElement('div');
numberValueElement.textContent = value;
formElement.appendChild(numberValueElement);
event.preventDefault();
};
formElement.addEventListener('submit', eventListener);
enterButton.addEventListener('click', eventListener);
formElement.appendChild(numberInput);
formElement.appendChild(enterButton);
appElement.appendChild(formElement);
numberInput.focus();
});
return promise;
}
const game = play();
function getInputs(input) {
const { done, value: output } = game.next(input);
if (!done) {
let promise;
switch (output.type) {
case play.PROMPT_TYPE.CHOICE: {
promise = createChoice(output.text, output.choices);
break;
}
case play.PROMPT_TYPE.NUMERIC: {
promise = createInput(output.text, 'number');
break;
}
case play.PROMPT_TYPE.STRING: {
promise = createInput(output.text);
break;
}
case play.PROMPT_TYPE.NONE:
default: {
createInfo(output.text);
promise = Promise.resolve();
break;
}
}
promise.then(getInputs);
window.scrollTo({
behavior: 'smooth',
left: 0,
top: document.body.getBoundingClientRect().height,
});
}
}
getInputs();