Files
foundryvtt-reve-de-dragon/module/roll/roll-part-select.mjs

51 lines
1.5 KiB
JavaScript

import { Grammar } from "../grammar.js"
import { RollPart } from "./roll-part.mjs"
export class RollPartSelect extends RollPart {
restore(rollData) {
this.setCurrent(rollData, { key: this.getSaved(rollData)?.key })
}
store(rollData, targetData) {
this.setSaved(targetData, { key: this.getCurrent(rollData).key })
}
choices(refs) { return [] }
getAjustements(rollData) {
const current = this.getCurrent(rollData)
if (current) {
return [{ label: current.label, diff: current.value }]
}
return []
}
selectByKey(rollData, key = undefined, defValue = undefined) {
const refs = this.getRefs(rollData)
const choices = this.choices(refs) ??[]
const current = this.getCurrent(rollData)
const newChoice = (choices.length == 0)
? { key: '', value: defValue }
: this.$getSelectedChoice(choices, key ?? current?.key ?? refs.key ?? '')
this.setCurrent(rollData, newChoice)
return newChoice
}
$getSelectedChoice(choices, key) {
const potential = choices.filter(it => Grammar.includesLowerCaseNoAccent(it.key, key))
switch (potential.length) {
case 0:
// ui.notifications.warn(`Aucun choix de ${this.name} pour ${key}`)
return choices[0]
case 1:
return potential[0]
default:
const selected = potential.find(it => Grammar.equalsInsensitive(it.key, key))
// ui.notifications.info(`Plusieurs choix de ${this.name} pour ${key}, le premier est utilisé`)
return selected ?? potential[0]
}
}
}