Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug removing tabs in gear picker, and also clean up callbacks to … #1930

Merged
merged 1 commit into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions ui/core/components/component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export abstract class Component {
private disposeCallbacks: Array<() => void> = [];
private disposed: boolean = false;

readonly rootElem: HTMLElement;

constructor(parentElem: HTMLElement | null, rootCssClass: string, rootElem?: HTMLElement) {
Expand All @@ -8,4 +11,18 @@ export abstract class Component {
parentElem.appendChild(this.rootElem);
}
}

addOnDisposeCallback(callback: () => void) {
this.disposeCallbacks.push(callback);
}

dispose() {
if (this.disposed) {
return;
}
this.disposed = true;

this.disposeCallbacks.forEach(callback => callback());
this.disposeCallbacks = [];
}
}
15 changes: 10 additions & 5 deletions ui/core/components/gear_picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ class SelectorModal extends Popup {
};

this.player.gearChangeEmitter.on(updateGemIcon);
this.addOnDisposeCallback(() => this.player.gearChangeEmitter.off(updateGemIcon));
updateGemIcon();
});
});
Expand Down Expand Up @@ -450,6 +451,7 @@ class SelectorModal extends Popup {
<li class="nav-item">
<a
class="nav-link selector-modal-item-tab ${selected ? 'active' : ''}"
data-content-id="${tabContentId}"
data-bs-toggle="tab"
data-bs-target="#${tabContentId}"
type="button"
Expand Down Expand Up @@ -715,8 +717,9 @@ class SelectorModal extends Popup {
}
});
};
updateSelected();
this.player.gearChangeEmitter.on(updateSelected);
this.addOnDisposeCallback(() => this.player.gearChangeEmitter.off(updateSelected));
updateSelected();

const applyFilters = () => {
let validItemElems = listItemElems;
Expand Down Expand Up @@ -798,9 +801,11 @@ class SelectorModal extends Popup {

this.player.sim.phaseChangeEmitter.on(applyFilters);
this.player.sim.filtersChangeEmitter.on(applyFilters);
this.player.gearChangeEmitter.on(() => {
applyFilters();
updateSelected();
this.player.gearChangeEmitter.on(applyFilters);
this.addOnDisposeCallback(() => {
this.player.sim.phaseChangeEmitter.off(applyFilters);
this.player.sim.filtersChangeEmitter.off(applyFilters);
this.player.gearChangeEmitter.off(applyFilters);
});

applyFilters();
Expand All @@ -811,7 +816,7 @@ class SelectorModal extends Popup {
.filter(tab => tab.dataset.label.includes(labelSubstring));

const contentElems = tabElems
.map(tabElem => document.getElementById(tabElem.getAttribute('href').substring(1)))
.map(tabElem => document.getElementById(tabElem.dataset.contentId!.substring(1)))
.filter(tabElem => Boolean(tabElem));

tabElems.forEach(elem => elem.parentElement.remove());
Expand Down
6 changes: 5 additions & 1 deletion ui/core/components/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export class Popup extends Component {
}

$(this.rootElem).bPopup({
onClose: () => this.rootElem.remove(),
onClose: () => {
this.rootElem.remove();
this.dispose();
},
});
}

Expand All @@ -36,5 +39,6 @@ export class Popup extends Component {
close() {
$(this.rootElem).bPopup().close();
this.rootElem.remove();
this.dispose();
}
}