-
Notifications
You must be signed in to change notification settings - Fork 9
/
captivity.js
306 lines (272 loc) · 11.2 KB
/
captivity.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause
import PageBuilder from "./pagebuilder.js";
class Captivity {
constructor(bridge) {
const loading = document.getElementById('loading');
this._bridge = bridge;
this._panels = [];
this._builder = new PageBuilder('div', undefined, document.body);
// Initial location of the transcript is the body. This is changed later
// when it becomes a panel.
this._transcript = PageBuilder.add_transcript(document.body, true);
this._bridge.receiveObjectCallback = command => {
this._transcribe(command);
return Object.assign(command, {"confirm": "Captivity"});
};
this._panelHolder = new PageBuilder(this._builder.add_node('div'));
const focusInput = this._panel_basic(this._new_panel(
"Basic and Focus"));
this._panel_login(this._new_panel("Dummy Login"));
this._panel_demonstrations(this._new_panel("Web View Demonstrations"));
this._panel_page_builder(this._new_panel("Page Builder Samples"));
this._panel_diagnostic(this._new_panel("Diagnostic"));
this._new_panel(null, this._transcript);
this._selectors_build();
loading.firstChild.textContent = "Captivity";
this._builder.into(loading, false);
// The back end for the focus command must do whatever is needed to
// cause the keyboard to be displayed:
//
// - For Android, there's a few lines of system service code, which
// are in the captivewebview Activity class, in the focusWebView
// method.
// - On iOS, there's just a call to the view.becomeFirstResponder
// method.
//
// This never worked on iOS until it was changed to be in an
// asynchronous block, like it is now. At some point even that stopped
// working on iOS.
// Most of it works on Catalyst. The focussed field does receive
// keyboard input. However, the cursor isn't shown in the field until
// the user types something.
this._send({"command": "ready"})
.then(() => {
focusInput.focus();
this._send({"command": "focus"});
});
}
_new_panel(label, panel) {
if (panel === undefined) {
panel = new PageBuilder('fieldset');
}
panel.add_classes('inactive');
if (label !== null) {
const legend = panel.add_node('legend');
legend.append(label);
}
this._panelHolder.node.append(panel.node);
this._panels.push(panel);
return panel;
}
_panel_basic(panel) {
const buttonA = panel.add_button("Command A");
const buttonJSFetch = panel.add_button("JS Fetch");
const buttonBridgedFetch = panel.add_button("Bridged Fetch");
const focusInput = panel.add_input('focus', "Default focussed:");
panel.add_node('span', "User interfaces: ");
const buttonSecondary = panel.add_button("Secondary");
const buttonSpinner = panel.add_button("Spinner");
buttonA.addEventListener('click', () => {
this._send({"command": "A"});
focusInput.value = "";
});
buttonJSFetch.addEventListener('click', (event) => {
event.target.disabled = true;
fetch(focusInput.value)
.then(response => {
this._transcribe({ fetchResponse:`${response}` });
event.target.disabled = false;
})
.catch(error => {
this._transcribe({
fetchError: `${error}`,
name: error.name,
message: error.message,
cause: error.cause
});
event.target.disabled = false;
});
});
buttonBridgedFetch.addEventListener('click', (event) => {
event.target.disabled = true;
let resource = focusInput.value;
if (resource === "") {
resource = "https://example.com";
}
this._send({command: "fetch", parameters: {
resource: resource
}})
.then(response => {
event.target.disabled = false;
})
.catch(error => {
event.target.disabled = false;
});
});
buttonSecondary.addEventListener('click', () => {
this._send(
{"command": "load", "parameters": {"page": "Secondary"}});
});
buttonSpinner.addEventListener('click', () => {
this._send(
{"command": "load", "parameters": {"page": "Spinner"}});
});
return focusInput.inputNode;
}
_panel_login(panel) {
// Chrome logs a warning if a password type input isn't within a form.
const form = new PageBuilder(panel.add_node('form'));
const plainInput = form.add_input('plain', "Plain Input:");
const secretInput = form.add_input(
'secret', "Secret Input:", true, "password");
const buttonLogIn = form.add_button("Log in");
buttonLogIn.onclick = () => {
this._send({
"command": "login",
"plain":plainInput.value,
"secret":secretInput.value
});
plainInput.value = "";
secretInput.value = "";
};
return form;
}
_panel_demonstrations(panel) {
panel.add_node(
'div', "These pages open in this Activity or View Controller.");
panel.add_anchor("three.html", "Three JS for WebGL");
panel.add_anchor("grid.html", "Grid with changing colour");
panel.add_anchor("embeddedSVG.html", "Embedded SVG Images");
panel.add_anchor("camera.html", "Camera");
panel.add_anchor("speech.html", "Speech");
const speechButton = panel.add_button("Load Speech");
speechButton.addEventListener('click', () => {
this._send({"load": "speech.html"});
})
}
_panel_page_builder(panel) {
const buttonProgress = panel.add_button("Start");
const progress = panel.add_progress();
let interval;
buttonProgress.addEventListener('click', () => {
// The progress value mustn't be set before the progress bar is
// visible. If it is, the step width sizes will be calculated too
// large and the bar will be wider than its container.
if (progress.value === undefined) {
progress.value = [0, 0, 0, 0];
}
if (interval === undefined) {
interval = setInterval(() => {
const valueArray = progress.value.slice();
let increment = 7 * valueArray.length;
let filled = 0;
function array_increment() {
for(const index of valueArray.keys()) {
valueArray[index] += increment;
if (valueArray[index] > 100) {
increment = valueArray[index] - 100;
valueArray[index] = 100;
filled += 1;
}
else {
increment = 0;
break;
}
}
}
array_increment();
if (increment > 0) {
for(const index of valueArray.keys()) {
valueArray[index] = 0;
}
array_increment();
}
progress.value = valueArray;
progress.set_step_colour(
filled > 1 ? '#33dd33' : // Green.
filled < 1 ? '#cc3232' : // Red.
'#e7b416' // Amber.
);
}, 500);
buttonProgress.firstChild.nodeValue = "Stop";
}
else {
clearInterval(interval);
interval = undefined;
buttonProgress.firstChild.nodeValue = "Resume";
}
});
}
_panel_diagnostic(panel) {
panel.add_node('div', "Dark mode.").classList.add('dark-mode-only');
panel.add_node('div', "Light mode.").classList.add('light-mode-only');
const innerHeightDiv = new PageBuilder(panel.add_node('div'));
innerHeightDiv.add_node('span', "Window innerHeight:");
const innerHeightSpan = innerHeightDiv.add_node('span');
const innerHeightText = document.createTextNode("innerHeight here");
innerHeightSpan.append(innerHeightText);
const innerWidthDiv = new PageBuilder(panel.add_node('div'));
innerWidthDiv.add_node('span', "Window innerWidth:");
const innerWidthSpan = innerWidthDiv.add_node('span');
const innerWidthText = document.createTextNode("innerWidth here");
innerWidthSpan.append(innerWidthText);
panel.add_input('dummy', "Tap to show keyboard:")
const check_size = () => {
innerWidthText.nodeValue = String(window.innerWidth);
innerHeightText.nodeValue = String(window.innerHeight);
};
window.addEventListener('resize', check_size);
const sizeButton = panel.add_button("Get");
sizeButton.addEventListener('click', check_size);
PageBuilder.prevent_focus(sizeButton);
check_size();
}
_selectors_build() {
// const holder = new PageBuilder(this._panelHolder.add_node('div'));
const holder = new PageBuilder(this._builder.add_node('div'));
holder.add_classes('selectors')
this._selectors = [];
for (const targetIndex of this._panels.keys()) {
const div = new PageBuilder(holder.add_node('div'));
const button = div.add_button(
targetIndex == this._panels.length - 1 ? "log" :
String(targetIndex + 1));
button.onclick = () => this._select(targetIndex);
this._selectors.push(button);
}
this._select(0);
}
_select(index) {
for (const [panelIndex, panel] of this._panels.entries()) {
panel.node.classList.toggle('inactive', panelIndex !== index);
this._selectors[panelIndex].classList.toggle(
'selected', panelIndex === index);
}
if (index === 0) {
const focusInput = document.getElementById("focus");
focusInput.focus();
}
}
_transcribe(message) {
this._transcript.add(JSON.stringify(message, undefined, 4), 'pre');
}
_send(object_) {
return (
this._bridge ?
this._bridge.sendObject(object_) :
Promise.reject(new Error("No Bridge!"))
).then(response => {
this._transcribe(response);
return response;
})
.catch(error => {
this._transcribe(error);
return error;
});
}
}
export default function(bridge) {
new Captivity(bridge);
return null;
}