import { createElement } from "@/utils/helpers/index.js"; import { Component } from…
- User
import { createElement } from "@/utils/helpers/index.js"; import { Component } from "../../base/Component.js";
export class ProgressBar extends Component { #currentResolve = null; #progressBar = null; #animationEndHandler = null;
start(duration) { this.interrupt();
codereturn new Promise((resolve) => { this.#currentResolve = resolve; this.#progressBar.style.animationDuration = `${duration}ms`; this.#progressBar.classList.add('fill-animation'); if (this.#animationEndHandler) { this.#progressBar.removeEventListener('animationend', this.#animationEndHandler); this.#animationEndHandler = null; } this.#animationEndHandler = () => this.#resolve(true); this.#progressBar.addEventListener('animationend', this.#animationEndHandler, { once: true }); });}
#resolve(isFinished){ if (!this.#currentResolve) return;
codethis.#currentResolve({ isFinished: isFinished }); this.#currentResolve = null; this.reset(); if (this.#animationEndHandler) { this.#progressBar.removeEventListener('animationend', this.#animationEndHandler); this.#animationEndHandler = null; }}
interrupt() { this.#resolve(false); }
update(percentage) { this.#progressBar.style.width =
${percentage}%; }reset() { this.update(0); this.#progressBar.classList.remove('fill-animation'); this.#progressBar.style.animationDuration = '';
codethis.#progressBar.offsetWidth;}
onMounted() { this.#progressBar = this.element.querySelector('#progress-bar'); }
setupElement() { return createElement({ tag: 'div', attributes: { id: 'progress-container' }, children: [ { tag: 'div', attributes: { id: 'progress-bar' } } ] }); } } улучшить код
- Assistant