Skip to content

Commit

Permalink
ui: character_stats cleanup
Browse files Browse the repository at this point in the history
 - avoid recreating new picker every click

Signed-off-by: jarves <jarveson@gmail.com>
  • Loading branch information
jarveson committed Oct 5, 2023
1 parent c9083ad commit 450f0ac
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
42 changes: 17 additions & 25 deletions ui/core/components/character_stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down Expand Up @@ -240,32 +237,27 @@ export class CharacterStats extends Component {
</a>
);

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<any>) => player.bonusStatsChangeEmitter,
getValue: (player: Player<any>) => player.getBonusStats().getStat(stat),
setValue: (eventID: EventID, player: Player<any>, 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:
<div className='input-root number-picker-root mb-0'>
<label className='form-label'>Bonus Health</label>
<input type='text' className='form-control number-picker-input' value={this.player.getBonusStats().getStat(stat)} />
</div>,
});

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<any>) => player.bonusStatsChangeEmitter,
getValue: (player: Player<any>) => player.getBonusStats().getStat(stat),
setValue: (eventID: EventID, player: Player<any>, newValue: number) => {
const bonusStats = player.getBonusStats().withStat(stat, newValue);
player.setBonusStats(eventID, bonusStats);
popover.hide();
},
});
html:true,
content: picker.rootElem,
});

return link as HTMLElement;
Expand Down
2 changes: 1 addition & 1 deletion ui/core/components/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export abstract class Input<ModObject, T, V = T> extends Component {

readonly changeEmitter = new TypedEvent<void>();

constructor(parent: HTMLElement, cssClass: string, modObject: ModObject, config: InputConfig<ModObject, T, V>) {
constructor(parent: HTMLElement | null, cssClass: string, modObject: ModObject, config: InputConfig<ModObject, T, V>) {
super(parent, 'input-root', config.rootElem);
this.inputConfig = config;
this.modObject = modObject;
Expand Down
2 changes: 1 addition & 1 deletion ui/core/components/number_picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class NumberPicker<ModObject> extends Input<ModObject, number> {
private float: boolean;
private positive: boolean;

constructor(parent: HTMLElement, modObject: ModObject, config: NumberPickerConfig<ModObject>) {
constructor(parent: HTMLElement | null, modObject: ModObject, config: NumberPickerConfig<ModObject>) {
super(parent, 'number-picker-root', modObject, config);
this.float = config.float || false;
this.positive = config.positive || false;
Expand Down

0 comments on commit 450f0ac

Please sign in to comment.