1 | export class InstallButton extends HTMLElement {
|
2 | static preload() {
|
3 | import("./connect");
|
4 | }
|
5 | connectedCallback() {
|
6 | if (this.renderRoot) {
|
7 | return;
|
8 | }
|
9 | this.renderRoot = this.attachShadow({ mode: "open" });
|
10 | if (!InstallButton.isSupported || !InstallButton.isAllowed) {
|
11 | this.toggleAttribute("install-unsupported", true);
|
12 | this.renderRoot.innerHTML = !InstallButton.isAllowed
|
13 | ? "<slot name='not-allowed'>You can only install ESP devices on HTTPS websites or on the localhost.</slot>"
|
14 | : "<slot name='unsupported'>Your browser does not support installing things on ESP devices. Use Google Chrome or Microsoft Edge.</slot>";
|
15 | return;
|
16 | }
|
17 | this.toggleAttribute("install-supported", true);
|
18 | this.addEventListener("mouseover", InstallButton.preload);
|
19 | const slot = document.createElement("slot");
|
20 | slot.addEventListener("click", async (ev) => {
|
21 | ev.preventDefault();
|
22 | const mod = await import("./connect");
|
23 | mod.connect(this);
|
24 | });
|
25 | slot.name = "activate";
|
26 | const button = document.createElement("button");
|
27 | button.innerText = "CONNECT";
|
28 | slot.append(button);
|
29 | if ("adoptedStyleSheets" in Document.prototype &&
|
30 | "replaceSync" in CSSStyleSheet.prototype) {
|
31 | const sheet = new CSSStyleSheet();
|
32 |
|
33 | sheet.replaceSync(InstallButton.style);
|
34 |
|
35 | this.renderRoot.adoptedStyleSheets = [sheet];
|
36 | }
|
37 | else {
|
38 | const styleSheet = document.createElement("style");
|
39 | styleSheet.innerText = InstallButton.style;
|
40 | this.renderRoot.append(styleSheet);
|
41 | }
|
42 | this.renderRoot.append(slot);
|
43 | }
|
44 | }
|
45 | InstallButton.isSupported = "serial" in navigator;
|
46 | InstallButton.isAllowed = window.isSecureContext;
|
47 | InstallButton.style = `
|
48 | button {
|
49 | position: relative;
|
50 | cursor: pointer;
|
51 | font-size: 14px;
|
52 | padding: 8px 28px;
|
53 | color: var(--esp-tools-button-text-color, #fff);
|
54 | background-color: var(--esp-tools-button-color, #03a9f4);
|
55 | border: none;
|
56 | border-radius: 4px;
|
57 | box-shadow: 0 2px 2px 0 rgba(0,0,0,.14), 0 3px 1px -2px rgba(0,0,0,.12), 0 1px 5px 0 rgba(0,0,0,.2);
|
58 | }
|
59 | button::before {
|
60 | content: " ";
|
61 | position: absolute;
|
62 | top: 0;
|
63 | bottom: 0;
|
64 | left: 0;
|
65 | right: 0;
|
66 | opacity: 0.2;
|
67 | border-radius: 4px;
|
68 | }
|
69 | button:hover {
|
70 | box-shadow: 0 4px 8px 0 rgba(0,0,0,.14), 0 1px 7px 0 rgba(0,0,0,.12), 0 3px 1px -1px rgba(0,0,0,.2);
|
71 | }
|
72 | button:hover::before {
|
73 | background-color: rgba(255,255,255,.8);
|
74 | }
|
75 | button:focus {
|
76 | outline: none;
|
77 | }
|
78 | button:focus::before {
|
79 | background-color: white;
|
80 | }
|
81 | button:active::before {
|
82 | background-color: grey;
|
83 | }
|
84 | :host([active]) button {
|
85 | color: rgba(0, 0, 0, 0.38);
|
86 | background-color: rgba(0, 0, 0, 0.12);
|
87 | box-shadow: none;
|
88 | cursor: unset;
|
89 | pointer-events: none;
|
90 | }
|
91 | improv-wifi-launch-button {
|
92 | display: block;
|
93 | margin-top: 16px;
|
94 | }
|
95 | .hidden {
|
96 | display: none;
|
97 | }`;
|
98 | customElements.define("esp-web-install-button", InstallButton);
|