forked from tiny-pilot/tinypilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps-dialog.html
More file actions
144 lines (125 loc) · 3.65 KB
/
https-dialog.html
File metadata and controls
144 lines (125 loc) · 3.65 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
<template id="https-dialog-template">
<style>
@import "css/style.css";
@import "css/toggle.css";
#loading,
#edit {
display: none;
}
:host([state="loading"]) #loading {
display: block;
}
:host([state="edit"]) #edit {
display: block;
}
.toggle-container {
display: flex;
align-items: center;
justify-content: center;
margin: 1.5em 0;
}
.toggle {
margin-right: 0.75rem;
}
</style>
<div id="loading">
<h3>Retrieving HTTPS Settings</h3>
<progress-spinner></progress-spinner>
</div>
<div id="edit">
<h3>Manage HTTPS Connection</h3>
<div class="toggle-container">
<label class="toggle">
<input type="checkbox" id="requires-https" />
<span class="toggle-slider"></span>
</label>
Require encrypted connection (HTTPS)
</div>
<button class="close-btn" type="button">Close</button>
</div>
</template>
<script type="module">
import {
DialogClosedEvent,
DialogFailedEvent,
DialogCloseStateChangedEvent,
SecureConnectionRequiredEvent,
} from "/js/events.js";
import { requiresHttps, setRequiresHttps } from "/js/controllers.js";
(function () {
const template = document.querySelector("#https-dialog-template");
customElements.define(
"https-dialog",
class extends HTMLElement {
_states = {
LOADING: "loading",
EDIT: "edit",
};
_statesWithoutDialogClose = new Set([this._states.LOADING]);
connectedCallback() {
this.attachShadow({ mode: "open" }).appendChild(
template.content.cloneNode(true)
);
this._elements = {
requiresHttpsCheckbox:
this.shadowRoot.querySelector("#requires-https"),
};
this.addEventListener("overlay-shown", () => this._initialize());
this.shadowRoot
.querySelectorAll(".close-btn")
.forEach((buttonElement) =>
buttonElement.addEventListener("click", () => {
this.dispatchEvent(new DialogClosedEvent());
})
);
this._elements.requiresHttpsCheckbox.addEventListener(
"input",
(event) => this._setRequiresHttps(event.target.checked)
);
}
get _state() {
return this.getAttribute("state");
}
set _state(newValue) {
this.setAttribute("state", newValue);
this.dispatchEvent(
new DialogCloseStateChangedEvent(
/*canBeClosed=*/ !this._statesWithoutDialogClose.has(newValue)
)
);
}
async _initialize() {
if (SecureConnectionRequiredEvent.isApplicable()) {
this.dispatchEvent(new SecureConnectionRequiredEvent());
return;
}
this._state = this._states.LOADING;
try {
this._elements.requiresHttpsCheckbox.checked =
await requiresHttps();
this._state = this._states.EDIT;
} catch (error) {
this.dispatchEvent(
new DialogFailedEvent({
title: "Failed to Load HTTPS Settings",
details: error,
})
);
}
}
async _setRequiresHttps(shouldBeRequired) {
try {
await setRequiresHttps(shouldBeRequired);
} catch (error) {
this.dispatchEvent(
new DialogFailedEvent({
title: "Failed to Change HTTPS Settings",
details: error,
})
);
}
}
}
);
})();
</script>