|
1 |
| -class WebpackDevServerProgress extends HTMLElement { |
2 |
| - constructor() { |
3 |
| - super(); |
4 |
| - this.attachShadow({ mode: "open" }); |
5 |
| - this.maxDashOffset = -219.99078369140625; |
6 |
| - this.animationTimer = null; |
| 1 | +export function isProgressSupported() { |
| 2 | + return "customElements" in self && !!HTMLElement.prototype.attachShadow; |
| 3 | +} |
| 4 | + |
| 5 | +export function defineProgressElement() { |
| 6 | + if (customElements.get("wds-progress")) { |
| 7 | + return; |
7 | 8 | }
|
8 | 9 |
|
9 |
| - #reset() { |
10 |
| - clearTimeout(this.animationTimer); |
11 |
| - this.animationTimer = null; |
| 10 | + class WebpackDevServerProgress extends HTMLElement { |
| 11 | + constructor() { |
| 12 | + super(); |
| 13 | + this.attachShadow({ mode: "open" }); |
| 14 | + this.maxDashOffset = -219.99078369140625; |
| 15 | + this.animationTimer = null; |
| 16 | + } |
12 | 17 |
|
13 |
| - const typeAttr = this.getAttribute("type")?.toLowerCase(); |
14 |
| - this.type = typeAttr === "circular" ? "circular" : "linear"; |
| 18 | + #reset() { |
| 19 | + clearTimeout(this.animationTimer); |
| 20 | + this.animationTimer = null; |
15 | 21 |
|
16 |
| - const innerHTML = |
17 |
| - this.type === "circular" |
18 |
| - ? WebpackDevServerProgress.#circularTemplate() |
19 |
| - : WebpackDevServerProgress.#linearTemplate(); |
20 |
| - this.shadowRoot.innerHTML = innerHTML; |
| 22 | + const typeAttr = this.getAttribute("type")?.toLowerCase(); |
| 23 | + this.type = typeAttr === "circular" ? "circular" : "linear"; |
21 | 24 |
|
22 |
| - this.initialProgress = Number(this.getAttribute("progress")) ?? 0; |
| 25 | + const innerHTML = |
| 26 | + this.type === "circular" |
| 27 | + ? WebpackDevServerProgress.#circularTemplate() |
| 28 | + : WebpackDevServerProgress.#linearTemplate(); |
| 29 | + this.shadowRoot.innerHTML = innerHTML; |
23 | 30 |
|
24 |
| - this.#update(this.initialProgress); |
25 |
| - } |
| 31 | + this.initialProgress = Number(this.getAttribute("progress")) ?? 0; |
26 | 32 |
|
27 |
| - static #circularTemplate() { |
28 |
| - return ` |
| 33 | + this.#update(this.initialProgress); |
| 34 | + } |
| 35 | + |
| 36 | + static #circularTemplate() { |
| 37 | + return ` |
29 | 38 | <style>
|
30 | 39 | :host {
|
31 | 40 | width: 200px;
|
@@ -88,10 +97,10 @@ class WebpackDevServerProgress extends HTMLElement {
|
88 | 97 | </text>
|
89 | 98 | </svg>
|
90 | 99 | `;
|
91 |
| - } |
| 100 | + } |
92 | 101 |
|
93 |
| - static #linearTemplate() { |
94 |
| - return ` |
| 102 | + static #linearTemplate() { |
| 103 | + return ` |
95 | 104 | <style>
|
96 | 105 | :host {
|
97 | 106 | position: fixed;
|
@@ -125,80 +134,71 @@ class WebpackDevServerProgress extends HTMLElement {
|
125 | 134 | </style>
|
126 | 135 | <div id="progress"></div>
|
127 | 136 | `;
|
128 |
| - } |
129 |
| - |
130 |
| - connectedCallback() { |
131 |
| - this.#reset(); |
132 |
| - } |
133 |
| - |
134 |
| - static get observedAttributes() { |
135 |
| - return ["progress", "type"]; |
136 |
| - } |
| 137 | + } |
137 | 138 |
|
138 |
| - attributeChangedCallback(name, oldValue, newValue) { |
139 |
| - if (name === "progress") { |
140 |
| - this.#update(Number(newValue)); |
141 |
| - } else if (name === "type") { |
| 139 | + connectedCallback() { |
142 | 140 | this.#reset();
|
143 | 141 | }
|
144 |
| - } |
145 | 142 |
|
146 |
| - #update(percent) { |
147 |
| - const element = this.shadowRoot.querySelector("#progress"); |
148 |
| - if (this.type === "circular") { |
149 |
| - const path = this.shadowRoot.querySelector("path"); |
150 |
| - const value = this.shadowRoot.querySelector("#percent-value"); |
151 |
| - const offset = ((100 - percent) / 100) * this.maxDashOffset; |
152 |
| - |
153 |
| - path.style.strokeDashoffset = offset; |
154 |
| - value.textContent = percent; |
155 |
| - } else { |
156 |
| - element.style.width = `${percent}%`; |
| 143 | + static get observedAttributes() { |
| 144 | + return ["progress", "type"]; |
157 | 145 | }
|
158 | 146 |
|
159 |
| - if (percent >= 100) { |
160 |
| - this.#hide(); |
161 |
| - } else if (percent > 0) { |
162 |
| - this.#show(); |
| 147 | + attributeChangedCallback(name, oldValue, newValue) { |
| 148 | + if (name === "progress") { |
| 149 | + this.#update(Number(newValue)); |
| 150 | + } else if (name === "type") { |
| 151 | + this.#reset(); |
| 152 | + } |
163 | 153 | }
|
164 |
| - } |
165 |
| - |
166 |
| - #show() { |
167 |
| - const element = this.shadowRoot.querySelector("#progress"); |
168 |
| - element.classList.remove("hidden"); |
169 |
| - } |
170 | 154 |
|
171 |
| - #hide() { |
172 |
| - const element = this.shadowRoot.querySelector("#progress"); |
173 |
| - if (this.type === "circular") { |
174 |
| - element.classList.add("disappear"); |
175 |
| - element.addEventListener( |
176 |
| - "animationend", |
177 |
| - () => { |
178 |
| - element.classList.add("hidden"); |
179 |
| - this.#update(0); |
180 |
| - }, |
181 |
| - { once: true }, |
182 |
| - ); |
183 |
| - } else if (this.type === "linear") { |
184 |
| - element.classList.add("disappear"); |
185 |
| - this.animationTimer = setTimeout(() => { |
186 |
| - element.classList.remove("disappear"); |
187 |
| - element.classList.add("hidden"); |
188 |
| - element.style.width = "0%"; |
189 |
| - this.animationTimer = null; |
190 |
| - }, 800); |
| 155 | + #update(percent) { |
| 156 | + const element = this.shadowRoot.querySelector("#progress"); |
| 157 | + if (this.type === "circular") { |
| 158 | + const path = this.shadowRoot.querySelector("path"); |
| 159 | + const value = this.shadowRoot.querySelector("#percent-value"); |
| 160 | + const offset = ((100 - percent) / 100) * this.maxDashOffset; |
| 161 | + |
| 162 | + path.style.strokeDashoffset = offset; |
| 163 | + value.textContent = percent; |
| 164 | + } else { |
| 165 | + element.style.width = `${percent}%`; |
| 166 | + } |
| 167 | + |
| 168 | + if (percent >= 100) { |
| 169 | + this.#hide(); |
| 170 | + } else if (percent > 0) { |
| 171 | + this.#show(); |
| 172 | + } |
191 | 173 | }
|
192 |
| - } |
193 |
| -} |
194 | 174 |
|
195 |
| -export function isProgressSupported() { |
196 |
| - return "customElements" in window && !!HTMLElement.prototype.attachShadow; |
197 |
| -} |
| 175 | + #show() { |
| 176 | + const element = this.shadowRoot.querySelector("#progress"); |
| 177 | + element.classList.remove("hidden"); |
| 178 | + } |
198 | 179 |
|
199 |
| -export function defineProgressElement() { |
200 |
| - if (customElements.get("wds-progress")) { |
201 |
| - return; |
| 180 | + #hide() { |
| 181 | + const element = this.shadowRoot.querySelector("#progress"); |
| 182 | + if (this.type === "circular") { |
| 183 | + element.classList.add("disappear"); |
| 184 | + element.addEventListener( |
| 185 | + "animationend", |
| 186 | + () => { |
| 187 | + element.classList.add("hidden"); |
| 188 | + this.#update(0); |
| 189 | + }, |
| 190 | + { once: true }, |
| 191 | + ); |
| 192 | + } else if (this.type === "linear") { |
| 193 | + element.classList.add("disappear"); |
| 194 | + this.animationTimer = setTimeout(() => { |
| 195 | + element.classList.remove("disappear"); |
| 196 | + element.classList.add("hidden"); |
| 197 | + element.style.width = "0%"; |
| 198 | + this.animationTimer = null; |
| 199 | + }, 800); |
| 200 | + } |
| 201 | + } |
202 | 202 | }
|
203 | 203 |
|
204 | 204 | customElements.define("wds-progress", WebpackDevServerProgress);
|
|
0 commit comments