forked from public/foundryvtt-reve-de-dragon
Nouvelle fenêtre de jets de dés
This commit is contained in:
50
module/roll/roll-part-select.mjs
Normal file
50
module/roll/roll-part-select.mjs
Normal file
@@ -0,0 +1,50 @@
|
||||
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]
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user