Skip to content

Commit

Permalink
Provide buttons from server
Browse files Browse the repository at this point in the history
  • Loading branch information
marein committed Nov 2, 2024
1 parent 6cbe4d2 commit 91cfd5f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
12 changes: 4 additions & 8 deletions assets/js/Common/ConfirmationButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ customElements.define('confirmation-button', class extends HTMLElement {
connectedCallback() {
this._initialChildren = Array.from(this.children);
this._button = this.querySelector('button');
this._onDocumentClickHandler = this._onDocumentClick.bind(this);
this._onDocumentClick = e => !this.contains(e.target) && this.reset();

this._button?.addEventListener('click', this._onButtonClick.bind(this));
}

reset() {
document.removeEventListener('click', this._onDocumentClickHandler);
document.removeEventListener('click', this._onDocumentClick);
this.replaceChildren(...this._initialChildren);
this._button?.removeAttribute('disabled');
this._button?.classList.remove('btn-loading');
}

_onButtonClick(e) {
setTimeout(() => document.addEventListener('click', this._onDocumentClickHandler), 0);
setTimeout(() => document.addEventListener('click', this._onDocumentClick), 0);

this.replaceChildren(html`
<div class="row row-gap-2">
Expand Down Expand Up @@ -49,15 +49,11 @@ customElements.define('confirmation-button', class extends HTMLElement {
}

_onYesClick(e) {
document.removeEventListener('click', this._onDocumentClickHandler);
document.removeEventListener('click', this._onDocumentClick);
this.replaceChildren(...this._initialChildren);
this._button?.setAttribute('disabled', '');
this._button?.classList.add('btn-loading');

this.dispatchEvent(new CustomEvent('confirmation-button:yes'));
}

_onDocumentClick(e) {
!this.contains(e.target) && this.reset();
}
});
4 changes: 1 addition & 3 deletions assets/js/ConnectFour/AbortButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ customElements.define('connect-four-abort-button', class extends HTMLElement {
connectedCallback() {
this.replaceChildren(html`
<confirmation-button @confirmation-button:yes="${this._onConfirmed.bind(this)}">
<button id="abort-game" class="btn btn-outline-danger w-100">
${this.innerHTML}
</button>
${Array.from(this.children)}
</confirmation-button>
`);
}
Expand Down
4 changes: 1 addition & 3 deletions assets/js/ConnectFour/ResignButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ customElements.define('connect-four-resign-button', class extends HTMLElement {
connectedCallback() {
this.replaceChildren(html`
<confirmation-button @confirmation-button:yes="${this._onConfirmed.bind(this)}">
<button class="btn w-100">
${this.innerHTML}
</button>
${Array.from(this.children)}
</confirmation-button>
`);
}
Expand Down
8 changes: 6 additions & 2 deletions src/WebInterface/Presentation/Http/View/game.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@
</div>
<div class="col-6">
<connect-four-abort-button game-id="{{ game.gameId }}">
Abort
<button id="abort-game" class="btn btn-outline-danger w-100">
Abort
</button>
</connect-four-abort-button>
</div>
<div class="col-6">
<connect-four-resign-button game-id="{{ game.gameId }}">
Resign
<button class="btn w-100">
Resign
</button>
</connect-four-resign-button>
</div>
</div>
Expand Down

0 comments on commit 91cfd5f

Please sign in to comment.