From 450f0ac2f171e8f687bc2d895518489c0405f350 Mon Sep 17 00:00:00 2001 From: jarves Date: Thu, 5 Oct 2023 01:58:44 -0500 Subject: [PATCH] ui: character_stats cleanup - avoid recreating new picker every click Signed-off-by: jarves --- ui/core/components/character_stats.tsx | 42 +++++++++++--------------- ui/core/components/input.tsx | 2 +- ui/core/components/number_picker.ts | 2 +- 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/ui/core/components/character_stats.tsx b/ui/core/components/character_stats.tsx index 8deb14f7b3..9323f59b41 100644 --- a/ui/core/components/character_stats.tsx +++ b/ui/core/components/character_stats.tsx @@ -103,13 +103,10 @@ export class CharacterStats extends Component { let bonusStatValue = bonusStats.getStat(stat); if (bonusStatValue == 0) { - valueElem.classList.remove('text-success', 'text-danger'); valueElem.classList.add('text-white'); } else if (bonusStatValue > 0) { - valueElem.classList.remove('text-white', 'text-danger'); valueElem.classList.add('text-success'); } else if (bonusStatValue < 0) { - valueElem.classList.remove('text-white', 'text-success'); valueElem.classList.add('text-danger'); } @@ -240,32 +237,27 @@ export class CharacterStats extends Component { ); - let popover = Popover.getOrCreateInstance(link, { + let popover: Popover | null = null; + + let picker = new NumberPicker(null, this.player, { + label: `Bonus ${statName}`, + extraCssClasses: ['mb-0'], + changedEvent: (player: Player) => player.bonusStatsChangeEmitter, + getValue: (player: Player) => player.getBonusStats().getStat(stat), + setValue: (eventID: EventID, player: Player, newValue: number) => { + const bonusStats = player.getBonusStats().withStat(stat, newValue); + player.setBonusStats(eventID, bonusStats); + popover?.hide(); + }, + }); + + popover = new Popover(link, { customClass: 'bonus-stats-popover', placement: 'right', fallbackPlacement: ['left'], sanitize: false, - content: -
- - -
, - }); - - link.addEventListener('shown.bs.popover', (event) => { - let popoverBody = document.querySelector('.popover.bonus-stats-popover .popover-body') as HTMLElement; - popoverBody.innerHTML = ''; - let picker = new NumberPicker(popoverBody, this.player, { - label: `Bonus ${statName}`, - extraCssClasses: ['mb-0'], - changedEvent: (player: Player) => player.bonusStatsChangeEmitter, - getValue: (player: Player) => player.getBonusStats().getStat(stat), - setValue: (eventID: EventID, player: Player, newValue: number) => { - const bonusStats = player.getBonusStats().withStat(stat, newValue); - player.setBonusStats(eventID, bonusStats); - popover.hide(); - }, - }); + html:true, + content: picker.rootElem, }); return link as HTMLElement; diff --git a/ui/core/components/input.tsx b/ui/core/components/input.tsx index 6dba0973f3..4b0e7729ca 100644 --- a/ui/core/components/input.tsx +++ b/ui/core/components/input.tsx @@ -47,7 +47,7 @@ export abstract class Input extends Component { readonly changeEmitter = new TypedEvent(); - constructor(parent: HTMLElement, cssClass: string, modObject: ModObject, config: InputConfig) { + constructor(parent: HTMLElement | null, cssClass: string, modObject: ModObject, config: InputConfig) { super(parent, 'input-root', config.rootElem); this.inputConfig = config; this.modObject = modObject; diff --git a/ui/core/components/number_picker.ts b/ui/core/components/number_picker.ts index 5098bf82b9..e84d3b6972 100644 --- a/ui/core/components/number_picker.ts +++ b/ui/core/components/number_picker.ts @@ -16,7 +16,7 @@ export class NumberPicker extends Input { private float: boolean; private positive: boolean; - constructor(parent: HTMLElement, modObject: ModObject, config: NumberPickerConfig) { + constructor(parent: HTMLElement | null, modObject: ModObject, config: NumberPickerConfig) { super(parent, 'number-picker-root', modObject, config); this.float = config.float || false; this.positive = config.positive || false;