forked from tiny-pilot/tinypilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshare-logs-button.html
More file actions
214 lines (182 loc) · 6.15 KB
/
share-logs-button.html
File metadata and controls
214 lines (182 loc) · 6.15 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
<template id="share-logs-button-template">
<style>
@import "css/style.css";
#get-url-button,
#get-url-button progress-spinner,
#display-link {
display: none;
}
#get-url-button span {
/* To hide the initial button label, we use the `visibility` attribute,
so that the button keeps its original width when showing the progress
spinner. */
visibility: hidden;
}
:host([state="initial"]) #get-url-button,
:host([state="loading"]) #get-url-button {
display: block;
}
:host([state="initial"]) #get-url-button span {
visibility: visible;
}
:host([state="loading"]) #get-url-button progress-spinner {
display: block;
}
:host([state="display-link"]) #display-link {
display: flex;
}
button {
position: relative;
margin: 0;
}
#get-url-button progress-spinner {
--size: 1.5em;
--thickness: 5px;
--color-circle: var(--brand-creme-light);
--color-rotor: var(--brand-blue-dark);
position: absolute;
top: 0.4em;
left: 50%;
right: 50%;
margin-left: calc(-0.5 * var(--size));
}
#display-link {
align-items: stretch;
}
.url-container {
display: flex;
align-items: center;
background-color: #dbdbdb;
padding: 0 0.5em;
border: 1px solid #666;
border-right: none; /* Visually collapse with left button border */
border-top-left-radius: 0.25em;
border-bottom-left-radius: 0.25em;
}
.url-container a {
color: #000;
align-items: center;
}
#copy-button {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
#copy-button img {
width: 1.2em;
margin-bottom: -0.2em;
}
#copy-button span {
display: none;
}
#copy-button.copied span {
display: inline;
}
#copy-button.copied img {
display: none;
}
</style>
<button id="get-url-button" class="btn-action">
<span>Get Shareable URL</span>
<progress-spinner></progress-spinner>
</button>
<div id="display-link">
<div class="url-container">
<a href="" target="_blank" rel="noopener noreferrer">
<!-- Filled via JS -->
</a>
</div>
<button id="copy-button" class="btn-action" title="Copy Link to Clipboard">
<img src="/img/clipboard-icon.svg" alt="Copy" />
<span>Copied!</span>
</button>
</div>
</template>
<script type="module">
import { copyElementTextToClipboard } from "/js/clipboard.js";
import { textToShareableUrl } from "/js/controllers.js";
(function () {
const template = document.querySelector("#share-logs-button-template");
customElements.define(
"share-logs-button",
class extends HTMLElement {
_states = {
INITIAL: "initial",
LOADING: "loading",
DISPLAY_LINK: "display-link",
};
connectedCallback() {
this.attachShadow({ mode: "open" }).appendChild(
template.content.cloneNode(true)
);
this._elements = {
getUrlButton: this.shadowRoot.querySelector("#get-url-button"),
urlLink: this.shadowRoot.querySelector("#display-link a"),
copyButton: this.shadowRoot.querySelector("#copy-button"),
};
this._elements.getUrlButton.addEventListener("click", () => {
this._handleRequestShareableUrl();
});
this._elements.copyButton.addEventListener("click", () => {
this._handleCopyToClipboard();
});
this._state = this._states.INITIAL;
}
set _state(newValue) {
this.setAttribute("state", newValue);
this._elements.getUrlButton.disabled =
newValue === this._states.LOADING;
}
/**
* A callback function that returns the logs text when the user requests
* a shareable URL.
* @callback getLogsTextCallback
* @returns {string}
*
* @param {getLogsTextCallback} getLogsTextCb
*/
initialize(getLogsTextCb) {
this.getLogsText = getLogsTextCb;
this._state = this._states.INITIAL;
}
async _handleRequestShareableUrl() {
this._state = this._states.LOADING;
try {
const url = await textToShareableUrl(this.getLogsText());
// For the displayed URL, strip the protocol prefix, to make it
// look a bit slimmer.
this._elements.urlLink.textContent = url
.replace("http://", "")
.replace("https://", "");
this._elements.urlLink.setAttribute("href", url);
this._elements.urlLink.setAttribute("title", url);
} catch (error) {
console.error("Failed to upload logs: " + error);
// In case of failure, we just reset the button, since we cannot
// display a detailed error message inline. It should be obvious
// to the user that they need to try again, and the original error
// message probably wouldn’t help them much.
this.initialize(this.getLogsText); // Reset component.
return;
}
this._state = this._states.DISPLAY_LINK;
}
_handleCopyToClipboard() {
// Workaround: we display the URL without protocol part in the UI, but
// we still want to copy the full, canonical URL to the clipboard.
// To do that, we briefly replace the display URL with the canonical
// one for the duration of the “copy to clipboard” operation. The user
// shouldn’t notice anything about this.
const previousDisplayText = this._elements.urlLink.textContent;
this._elements.urlLink.textContent =
this._elements.urlLink.getAttribute("href");
copyElementTextToClipboard(this._elements.urlLink);
this._elements.urlLink.textContent = previousDisplayText;
this._elements.copyButton.classList.add("copied");
setTimeout(() => {
this._elements.copyButton.classList.remove("copied");
}, 2000);
}
}
);
})();
</script>