diff --git a/module/applications/sheets/rollTables/rollTable.mjs b/module/applications/sheets/rollTables/rollTable.mjs index f0fc7df6..6590c464 100644 --- a/module/applications/sheets/rollTables/rollTable.mjs +++ b/module/applications/sheets/rollTables/rollTable.mjs @@ -1,24 +1,101 @@ -//Setting RollTable export default class DhRollTableSheet extends foundry.applications.sheets.RollTableSheet { + static DEFAULT_OPTIONS = { + ...super.DEFAULT_OPTIONS, + classes:['daggerheart', 'sheet', 'dh-style'], + actions: { + addAltFormula: DhRollTableSheet.#onAddAltFormula, + removeAltFormula: DhRollTableSheet.#onRemoveAltFormula + } + }; + static buildParts() { const { footer, ...parts } = super.PARTS; - return { + const test = { ...parts, summary: { template: 'systems/daggerheart/templates/sheets/rollTable/summary.hbs' }, footer - } + }; + return test; } - + static PARTS = DhRollTableSheet.buildParts(); - - static actions = { - addAltFormula: DhRollTableSheet.#onAddAltFormula, - removeAltForuma: DhRollTableSheet.#onRemoveAltFormula - }; - //Add formulafield - static async #onAddAltFormula(event, target) {} + async _preparePartContext(partId, context, options) { + context = await super._preparePartContext(partId, context, options); - //Remove formulafield - static async #onRemoveAltFormula(event, target) {} + switch (partId) { + case 'summary': + context.flagFields = this.daggerheartFlag.schema.fields; + context.flagData = this.daggerheartFlag; + const formulas =[]; + formulas.push({ + index: 0, + key: this.daggerheartFlag.formulaName, //Stored in flags as discussed + formula: context.source.formula, //Settinng default formula as part of first element + formulaInputName: "formula", + keyInputName: "flags.daggerheart.formulaName" + }); + this.daggerheartFlag.altFormula.forEach((alt,i) =>{ + formulas.push({ + index: i+1, + key: alt.key, + formula: alt.formula, + formulaInputName:`flags.daggerheart.altFormula.${i}.formula`, + keyInputName: `flags.daggerheart.altFormula.${i}.key` + }); + }); + context.formulaList=formulas; + context.isListView=formulas.length>1; //Condition to show list view only if more than one entry + break; + } + + return context; + } + + async _preRender(context, options) { + await super._preRender(context, options); + + if (!options.internalReferesh) + this.daggerheartFlag = new game.system.api.data.DhRollTable(this.document.flags.daggerheart); + } + + /** @override */ + async _processSubmitData(event, form, submitData, options) { + //submitData.flags.daggerheart = this.daggerheartFlag.toObject(); caused render headaches + + super._processSubmitData(event, form, submitData, options); + } + + static async #onAddAltFormula(_event, target) { + const currentAltFormula=this.daggerheartFlag.altFormula; + await this.daggerheartFlag.updateSource({ + altFormula:[...currentAltFormula,{key:"",formula:""}] + }); + this.render({ internalRefresh: true }); + } + + static async #onRemoveAltFormula(_event, target) { + const visualIndex = parseInt(target.dataset.index); + const currentAltFormula=this.daggerheartFlag.altFormula; + if(visualIndex===0) {//If deleting formula at [0] index + if(currentAltFormula.length>0) { + const newCore = currentAltFormula[0]; + const newAlt = currentAltFormula.slice(1); + await this.document.update({formula: newCore.formula}); + await this.daggerheartFlag.updateSource({ + formulaName:newCore.key, + altFormula:newAlt + }); + } else { + await this.document.update({ formula: "" }); + await this.daggerheartFlag.updateSource({ formulaName: "" }); + } + } else { + const arrayIndex = visualIndex - 1; + await this.daggerheartFlag.updateSource({ + altFormula: currentAltFormula.filter((_, i) => i !== arrayIndex) + }); + } + this.render({ internalRefresh: true }); + } } diff --git a/module/data/_module.mjs b/module/data/_module.mjs index 0a476ee9..cc0df5e9 100644 --- a/module/data/_module.mjs +++ b/module/data/_module.mjs @@ -1,6 +1,7 @@ export { default as DhCombat } from './combat.mjs'; export { default as DhCombatant } from './combatant.mjs'; export { default as DhTagTeamRoll } from './tagTeamRoll.mjs'; +export { default as DhRollTable } from './rollTable.mjs'; export * as countdowns from './countdowns.mjs'; export * as actions from './action/_module.mjs'; diff --git a/module/data/rollTable.mjs b/module/data/rollTable.mjs new file mode 100644 index 00000000..b1e90f51 --- /dev/null +++ b/module/data/rollTable.mjs @@ -0,0 +1,27 @@ +import FormulaField from './fields/formulaField.mjs'; + +//Extra definitions for RollTable +export default class DhRollTable extends foundry.abstract.TypeDataModel { + static defineSchema() { + const fields = foundry.data.fields; + + return { + formulaName: new fields.StringField({ + // This is to give a name to go together with the core.formula + required: true, + nullable: false, + initial: 'Formula' // Should be a translation + }), + altFormula: new fields.ArrayField( + new fields.SchemaField({ + key: new fields.StringField({ + required: false, + nullable: false, + blank: true + }), + formula: new FormulaField() + }) + ) + }; + } +} diff --git a/templates/sheets/rollTable/summary.hbs b/templates/sheets/rollTable/summary.hbs index 5d90266a..2592eb1c 100644 --- a/templates/sheets/rollTable/summary.hbs +++ b/templates/sheets/rollTable/summary.hbs @@ -1,6 +1,43 @@
{{formGroup fields.description value=source.description rootId=rootId}} - {{formGroup fields.formula value=source.formula placeholder=formulaPlaceholder rootId=rootId}} +
+ + Formula + + + + {{#if isListView}} + {{#each formulaList as |row|}} +
+ {{formGroup ../flagFields.formulaName + value=row.key + name=row.keyInputName + placeholder="Name" + }} + {{formGroup ../fields.formula + value=row.formula + name=row.formulaInputName + placeholder="Formula" + }} + + + +
+ {{/each}} + {{else}} +
+ + {{formGroup fields.formula + value=source.formula + placeholder=formulaPlaceholder + rootId=rootId + }} +
+ {{/if}} +
{{formGroup fields.replacement value=source.replacement rootId=rootId}} {{formGroup fields.displayRoll value=source.displayRoll rootId=rootId}}
\ No newline at end of file