-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathshutdown-dialog.html
173 lines (154 loc) · 4.63 KB
/
shutdown-dialog.html
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
<template id="shutdown-dialog-template">
<style>
@import "css/style.css";
#prompt,
#restarting,
#shutting-down,
#shutdown-complete {
display: none;
}
:host([state="prompt"]) #prompt {
display: block;
}
:host([state="restarting"]) #restarting {
display: block;
}
:host([state="shutting-down"]) #shutting-down {
display: block;
}
:host([state="shutdown-complete"]) #shutdown-complete {
display: block;
}
</style>
<div id="prompt">
<h3>Shut Down TinyPilot Device?</h3>
<p>
Note that this will shut down <strong>TinyPilot</strong>, not the machine
to which it is attached.
</p>
<button id="confirm-shutdown" class="btn-danger" type="button">
Shut Down
</button>
<button id="confirm-restart" class="btn-danger" type="button">
Restart
</button>
<button id="cancel-shutdown" type="button">Close</button>
</div>
<div id="restarting">
<h3>Restarting TinyPilot Device</h3>
<progress-spinner></progress-spinner>
</div>
<div id="shutting-down">
<h3>Shutting Down TinyPilot Device</h3>
<progress-spinner></progress-spinner>
</div>
<div id="shutdown-complete">
<h3>Shutdown Complete</h3>
</div>
</template>
<script type="module">
import {
DialogClosedEvent,
DialogFailedEvent,
DialogCloseStateChangedEvent,
DialogVariantChangedEvent,
} from "/js/events.js";
import { shutdown } from "/js/controllers.js";
(function () {
const template = document.querySelector("#shutdown-dialog-template");
customElements.define(
"shutdown-dialog",
class extends HTMLElement {
_states = {
PROMPT: "prompt",
RESTARTING: "restarting",
SHUTTING_DOWN: "shutting-down",
SHUTDOWN_COMPLETE: "shutdown-complete",
};
_statesWithoutDialogClose = new Set([
this._states.RESTARTING,
this._states.SHUTTING_DOWN,
this._states.SHUTDOWN_COMPLETE,
]);
constructor() {
super();
}
connectedCallback() {
this.attachShadow({ mode: "open" }).appendChild(
template.content.cloneNode(true)
);
this._state = this._states.PROMPT;
this.shadowRoot
.getElementById("confirm-shutdown")
.addEventListener("click", () => {
this._doShutdown(/*restart=*/ false);
});
this.shadowRoot
.getElementById("confirm-restart")
.addEventListener("click", () => {
this._doShutdown(/*restart=*/ true);
});
this.shadowRoot
.getElementById("cancel-shutdown")
.addEventListener("click", () => {
this.dispatchEvent(new DialogClosedEvent());
});
}
get _state() {
return this.getAttribute("state");
}
set _state(newValue) {
this.setAttribute("state", newValue);
this.dispatchEvent(
new DialogCloseStateChangedEvent(
!this._statesWithoutDialogClose.has(newValue)
)
);
}
_doShutdown(restart) {
shutdown(restart)
.then(() => {
this._emitShutdownStartedEvent(restart);
if (restart) {
this._state = this._states.RESTARTING;
} else {
this._state = this._states.SHUTTING_DOWN;
// We can't tell when the system has fully shut down, but assume
// that it's done after 30 seconds.
setTimeout(() => {
this._state = this._states.SHUTDOWN_COMPLETE;
this.dispatchEvent(new DialogVariantChangedEvent("success"));
}, 30 * 1000);
}
})
.catch((error) => {
if (restart) {
this.dispatchEvent(
new DialogFailedEvent({
title: "Failed to Restart TinyPilot Device",
details: error,
})
);
} else {
this.dispatchEvent(
new DialogFailedEvent({
title: "Failed to Shut Down TinyPilot Device",
details: error,
})
);
}
});
}
_emitShutdownStartedEvent(restart) {
this.dispatchEvent(
new CustomEvent("shutdown-started", {
detail: { restart },
bubbles: true,
composed: true,
})
);
}
}
);
})();
</script>