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