forked from tiny-pilot/tinypilot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
error-dialog.html
85 lines (77 loc) · 2.52 KB
/
error-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
<template id="error-dialog-template">
<style>
@import "css/style.css";
#details {
padding: 0.5rem;
max-height: 50vh;
overflow: auto;
opacity: 0.7;
border: 1px solid black;
background-color: white;
text-align: left;
font-size: 0.8em;
white-space: pre-wrap;
overflow-wrap: break-word;
}
.details-label {
color: var(--brand-red);
font-weight: 600;
text-align: left;
font-size: 0.9em;
margin: 0.25rem 0.4rem;
}
</style>
<h3 id="title"></h3>
<p id="message"></p>
<div id="details-container">
<div class="details-label">Details:</div>
<pre id="details" class="monospace"></pre>
</div>
<button id="close">Close</button>
</template>
<script>
(function () {
const doc = (document._currentScript || document.currentScript)
.ownerDocument;
const template = doc.querySelector("#error-dialog-template");
customElements.define(
"error-dialog",
class extends HTMLElement {
DEFAULT_MESSAGE = "Please refresh the page and try again.";
connectedCallback() {
this.attachShadow({ mode: "open" });
this.shadowRoot.appendChild(template.content.cloneNode(true));
this.close = this.close.bind(this);
this.shadowRoot
.getElementById("close")
.addEventListener("click", this.close);
}
/**
* @param title A concise summary of the error.
* @param message (string, optional) A user-friendly and helpful message
* that ideally gives the user some guidance what to do
* now. Defaults to a generic `DEFAULT_MESSAGE`.
* @param details (string|Error, optional) The technical error details,
* e.g. the original error message from the API or
* library call.
*/
setup({ title, message = this.DEFAULT_MESSAGE, details = "" }) {
this.shadowRoot.getElementById("title").innerText = title;
this.shadowRoot.getElementById("message").innerText = message;
this.shadowRoot.getElementById("details").innerText = details;
this.shadowRoot.getElementById(
"details-container"
).style.display = details ? "block" : "none";
}
close() {
this.dispatchEvent(
new CustomEvent("dialog-closed", {
bubbles: true,
composed: true,
})
);
}
}
);
})();
</script>