-
-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathwifi-dialog.html
More file actions
382 lines (352 loc) · 12 KB
/
wifi-dialog.html
File metadata and controls
382 lines (352 loc) · 12 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<!-- Remark about the terminology: in the UI, we decided to say `Wi-Fi` instead
of `WiFi`, as this is the official spelling. We are not super strict about
this for code symbols, however, mainly for simplicity. -->
<template id="wifi-template">
<style>
@import "css/style.css";
#initializing,
#prompt,
#changing,
#success-enabled,
#success-disabled {
display: none;
}
:host([state="initializing"]) #initializing,
:host([state="prompt"]) #prompt,
:host([state="changing"]) #changing,
:host([state="success-enabled"]) #success-enabled,
:host([state="success-disabled"]) #success-disabled {
display: block;
}
#input-error {
margin-top: 1rem;
}
label {
width: 8.5em;
text-align: right;
}
.input-container {
display: flex;
flex-direction: column;
align-items: start;
gap: 1em;
margin-top: 1.5em;
padding-left: 6em;
}
.input-row {
display: flex;
justify-content: center;
align-items: center;
gap: 0.5em;
}
.input-row input {
width: 20em;
}
.input-row select {
width: 21em;
}
.button-container {
margin-top: 1em;
}
</style>
<div id="initializing">
<h3>Retrieving Current Wi-Fi Settings</h3>
<div>
<progress-spinner></progress-spinner>
</div>
</div>
<div id="prompt">
<h3>Wi-Fi Credentials</h3>
<p>
Connect your TinyPilot device to a wireless network by entering your Wi-Fi
credentials.
</p>
<inline-message variant="warning" id="no-ethernet-warning">
<strong>Warning:</strong> You are currently connected to your TinyPilot
device using Wi-Fi. If you change your settings, you might get
disconnected. Consider connecting an Ethernet cable before proceeding.
</inline-message>
<div class="input-container">
<div class="input-row">
<label for="ssid-input">Network Name:</label>
<input
type="text"
id="ssid-input"
class="monospace"
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"
placeholder="Wi-Fi SSID"
/>
</div>
<div class="input-row">
<label for="psk-input">Password:</label>
<input
type="password"
id="psk-input"
class="monospace"
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"
placeholder="(hidden)"
/>
</div>
<div class="input-row">
<label for="country-code-input">Country:</label>
<select id="country-code-input">
<!-- Filled programmatically -->
</select>
</div>
</div>
<inline-message variant="error" id="input-error">
<strong>Invalid Wi-Fi Settings:</strong>
<span id="input-error-reason"><!-- Filled programmatically --></span>
</inline-message>
<div class="button-container">
<button id="disable-button" class="btn-danger" type="button">
Remove
</button>
<button id="enable-button" class="btn-success" type="button">Save</button>
<button class="close-button" type="button">Close</button>
</div>
</div>
<div id="changing">
<h3>Applying Changes</h3>
<progress-spinner></progress-spinner>
</div>
<div id="success-enabled">
<h3>Wi-Fi Credentials Saved</h3>
<p>
Your Wi-Fi credentials have been saved. When your TinyPilot device is in
range of the wireless network, it will automatically try to connect to it.
</p>
<p>
To monitor the Wi-Fi connection status, open the "Network Status" dialog.
</p>
<button id="network-status-button" class="btn-action" type="button">
Open Network Status
</button>
<button class="close-button" type="button">Close</button>
</div>
<div id="success-disabled">
<h3>Wi-Fi Credentials Removed</h3>
<p>
Your Wi-Fi credentials have been removed. Your TinyPilot device will no
longer try to connect to the wireless network.
</p>
<button class="close-button" type="button">Close</button>
</div>
</template>
<script type="module">
import {
DialogClosedEvent,
DialogFailedEvent,
DialogCloseStateChangedEvent,
DialogVariantChangedEvent,
} from "/js/events.js";
import {
getNetworkStatus,
getWifiSettings,
enableWifi,
disableWifi,
} from "/js/controllers.js";
import { COUNTRY_CODES } from "/js/wifi.js";
(function () {
const template = document.querySelector("#wifi-template");
customElements.define(
"wifi-dialog",
class extends HTMLElement {
_states = {
INITIALIZING: "initializing",
PROMPT: "prompt",
CHANGING: "changing",
SUCCESS_ENABLED: "success-enabled",
SUCCESS_DISABLED: "success-disabled",
};
_statesWithoutDialogClose = new Set([
this._states.INITIALIZING,
this._states.CHANGING,
]);
connectedCallback() {
this.attachShadow({ mode: "open" }).appendChild(
template.content.cloneNode(true)
);
this._elements = {
noEthernetWarning: this.shadowRoot.querySelector(
"#no-ethernet-warning"
),
inputError: this.shadowRoot.querySelector("#input-error"),
inputErrorReason: this.shadowRoot.querySelector(
"#input-error-reason"
),
ssidInput: this.shadowRoot.querySelector("#ssid-input"),
pskInput: this.shadowRoot.querySelector("#psk-input"),
countryCodeInput: this.shadowRoot.querySelector(
"#country-code-input"
),
enableButton: this.shadowRoot.querySelector("#enable-button"),
disableButton: this.shadowRoot.querySelector("#disable-button"),
networkStatusButton: this.shadowRoot.querySelector(
"#network-status-button"
),
};
this.addEventListener("overlay-shown", () => this._initialize());
[
this._elements.ssidInput,
this._elements.pskInput,
this._elements.countryCodeInput,
].forEach((el) =>
el.addEventListener("input", () => {
this._refreshButtons();
})
);
this._elements.enableButton.addEventListener("click", () => {
this._enable();
});
this._elements.disableButton.addEventListener("click", () => {
this._disable();
});
this._elements.networkStatusButton.addEventListener("click", () => {
this.dispatchEvent(new DialogClosedEvent());
const menuBar = document.getElementById("menu-bar");
menuBar.dispatchEvent(
new CustomEvent("network-status-dialog-requested", {
bubbles: true,
composed: true,
})
);
});
this.shadowRoot.querySelectorAll(".close-button").forEach((el) =>
el.addEventListener("click", () => {
this.dispatchEvent(new DialogClosedEvent());
})
);
Object.entries(COUNTRY_CODES)
.sort(([, nameA], [, nameB]) => nameA.localeCompare(nameB))
.forEach(([code, name]) =>
this._elements.countryCodeInput.add(
new Option(`${name} (${code})`, code)
)
);
}
get _state() {
return this.getAttribute("state");
}
set _state(newValue) {
this.setAttribute("state", newValue);
this.dispatchEvent(
new DialogCloseStateChangedEvent(
!this._statesWithoutDialogClose.has(newValue)
)
);
}
async _initialize() {
this._state = this._states.INITIALIZING;
let wifiSettings, networkStatus;
try {
[wifiSettings, networkStatus] = await Promise.all([
getWifiSettings(),
getNetworkStatus(),
]);
} catch (error) {
this.dispatchEvent(
new DialogFailedEvent({
title: "Failed to Retrieve Wi-Fi Settings",
details: error,
})
);
return;
}
this._elements.ssidInput.value = wifiSettings.ssid;
// Preselect the US country code as initial value, (a) as demo value,
// and (b) because it probably fits most users anyway.
this._elements.countryCodeInput.value =
wifiSettings.countryCode || "US";
this._elements.pskInput.value = "";
this._initialWiFiSettings = { psk: "", ...wifiSettings };
// Show ethernet warning when no ethernet interfaces are connected.
this._elements.noEthernetWarning.hide();
this._elements.inputError.hide();
const noEthernetConnected = networkStatus
.filter((iface) => iface.name.startsWith("eth"))
.every((iface) => !iface.isConnected);
if (noEthernetConnected) {
this._elements.noEthernetWarning.show();
}
this._refreshButtons();
this._state = this._states.PROMPT;
}
_refreshButtons() {
const hasInputChanged = [
[this._elements.ssidInput, this._initialWiFiSettings.ssid],
[this._elements.pskInput, this._initialWiFiSettings.psk],
[
this._elements.countryCodeInput,
this._initialWiFiSettings.countryCode,
],
].some(([el, initialValue]) => el.value !== initialValue);
const isMandatoryInputMissing = [
this._elements.ssidInput,
this._elements.countryCodeInput,
].some((el) => el.value === "");
// The “Enable” button is unavailable if the user hasn’t made any
// changes or if mandatory fields are still missing.
this._elements.enableButton.disabled =
!hasInputChanged || isMandatoryInputMissing;
// The “Disable” button is only available if there currently is a
// WiFi configuration (i.e., if there is an initial SSID value).
this._elements.disableButton.disabled =
!this._initialWiFiSettings.ssid;
}
async _enable() {
this._state = this._states.CHANGING;
try {
await enableWifi(
this._elements.countryCodeInput.value,
this._elements.ssidInput.value,
// The backend expects a value of `null` to indicate an open
// WiFi, not an empty string.
this._elements.pskInput.value || null
);
} catch (error) {
if (error.code === "INVALID_WIFI_SETTINGS") {
// Display validation errors inline in order to make it more
// convenient for the user to correct them.
this._elements.inputErrorReason.textContent = error.message;
this._elements.inputError.show();
this._state = this._states.PROMPT;
return;
}
this.dispatchEvent(
new DialogFailedEvent({
title: "Failed to Enable Wi-Fi",
details: error,
})
);
return;
}
this._state = this._states.SUCCESS_ENABLED;
this.dispatchEvent(new DialogVariantChangedEvent("success"));
}
async _disable() {
this._state = this._states.CHANGING;
try {
await disableWifi();
} catch (error) {
this.dispatchEvent(
new DialogFailedEvent({
title: "Failed to Disable Wi-Fi",
details: error,
})
);
return;
}
this._state = this._states.SUCCESS_DISABLED;
this.dispatchEvent(new DialogVariantChangedEvent("success"));
}
}
);
})();
</script>