|
| 1 | +import {html} from 'uhtml/node.js' |
| 2 | + |
| 3 | +customElements.define('connect-four-animated-game', class extends HTMLElement { |
| 4 | + #plainElement; #moveElements; #abortController; |
| 5 | + |
| 6 | + connectedCallback() { |
| 7 | + this.#plainElement = html`<div class="gp-cf-game__field"></div>`; |
| 8 | + this.#moveElements = Array.from(this.querySelectorAll('[data-move]')).sort((a, b) => { |
| 9 | + return parseInt(a.dataset.move) - parseInt(b.dataset.move); |
| 10 | + }); |
| 11 | + this.#abortController = new AbortController(); |
| 12 | + |
| 13 | + if (typeof AbortSignal?.timeout !== 'function' || typeof AbortSignal?.any !== 'function') { |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + this.addEventListener('mouseenter', this.#onMouseEnter); |
| 18 | + this.addEventListener('mouseleave', this.#onMouseLeave); |
| 19 | + } |
| 20 | + |
| 21 | + disconnectedCallback() { |
| 22 | + this.#abortController.abort(); |
| 23 | + } |
| 24 | + |
| 25 | + #onMouseEnter = () => { |
| 26 | + this.#abortController = new AbortController(); |
| 27 | + this.#runAnimation(); |
| 28 | + } |
| 29 | + |
| 30 | + #onMouseLeave = () => { |
| 31 | + this.#abortController.abort(); |
| 32 | + this.#showFinalState(); |
| 33 | + } |
| 34 | + |
| 35 | + #runAnimation = async () => { |
| 36 | + this.#clear(); |
| 37 | + |
| 38 | + for (const [i, moveElement] of this.#moveElements.entries()) { |
| 39 | + this.#moveElements.forEach(e => { |
| 40 | + e.classList.remove('gp-cf-game__field--highlight', 'gp-cf-game__field--current') |
| 41 | + }); |
| 42 | + moveElement.classList.add('gp-cf-game__field--highlight', 'gp-cf-game__field--current'); |
| 43 | + this.querySelector('[data-move="' + moveElement.dataset.move + '"]').replaceWith(moveElement); |
| 44 | + |
| 45 | + const isLastMove = i === this.#moveElements.length - 1; |
| 46 | + isLastMove && this.#showFinalState(); |
| 47 | + |
| 48 | + try { |
| 49 | + await new Promise((resolve, reject) => { |
| 50 | + AbortSignal.any([this.#abortController.signal, AbortSignal.timeout(isLastMove ? 1500 : 350)]) |
| 51 | + .addEventListener('abort', reject); |
| 52 | + }); |
| 53 | + } catch { |
| 54 | + if (this.#abortController.signal.aborted) return; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + this.#runAnimation(); |
| 59 | + } |
| 60 | + |
| 61 | + #clear() { |
| 62 | + this.#moveElements.forEach(e => { |
| 63 | + e.classList.remove('gp-cf-game__field--highlight', 'gp-cf-game__field--current'); |
| 64 | + const clone = this.#plainElement.cloneNode(); |
| 65 | + clone.dataset.move = e.dataset.move; |
| 66 | + clone.dataset.win = e.dataset.win; |
| 67 | + this.querySelector('[data-move="' + e.dataset.move + '"]').replaceWith(clone); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + #showFinalState() { |
| 72 | + this.#moveElements.forEach((e, k) => { |
| 73 | + e.classList.remove('gp-cf-game__field--highlight', 'gp-cf-game__field--current'); |
| 74 | + k === this.#moveElements.length - 1 && e.classList.add('gp-cf-game__field--current'); |
| 75 | + e.hasAttribute('data-win') && e.classList.add('gp-cf-game__field--highlight'); |
| 76 | + this.querySelector('[data-move="' + e.dataset.move + '"]').replaceWith(e); |
| 77 | + }); |
| 78 | + } |
| 79 | +}); |
0 commit comments