import {InitiativeTrackerUtil, UtilConditions} from "../../initiativetracker/initiativetracker-utils.js"; import {InitiativeTrackerConditionCustomEdit} from "./dmscreen-initiativetracker-conditioncustom.js"; import {InitiativeTrackerConditionUtil} from "./dmscreen-initiativetracker-condition.js"; class _UtilConditionsCustomView { static $getBtnCondition ({comp, cbSubmit, cbClick}) { const $btn = $(``) .on("click", evt => { cbClick({ name: comp._state.name, color: comp._state.color, turns: comp._state.turns, }); if (evt.shiftKey && EventUtil.isCtrlMetaKey(evt)) return cbSubmit({turns: 10}); if (EventUtil.isCtrlMetaKey(evt)) return cbSubmit({turns: 1}); if (evt.shiftKey) return cbSubmit({turns: null}); }); comp._addHookBase("color", () => $btn.css({"background-color": `${comp._state.color}`}))(); comp._addHookBase("name", () => $btn.text(comp._state.name || "\u00A0"))(); return $btn; } } class _RenderableCollectionConditionsCustomView extends RenderableCollectionGenericRows { constructor ( { comp, $wrpRows, rdState, cbDoSubmit, }, ) { super(comp, "conditionsCustom", $wrpRows); this._rdState = rdState; this._cbDoSubmit = cbDoSubmit; } _$getWrpRow () { return $(`
`); } /* -------------------------------------------- */ _populateRow ({comp, $wrpRow, entity}) { _UtilConditionsCustomView.$getBtnCondition({ comp, cbClick: ({name, color, turns}) => { this._comp._state.name = name; this._comp._state.color = color; this._comp._state.turns = turns; }, cbSubmit: ({turns}) => { this._comp._state.turns = turns; this._cbDoSubmit({rdState: this._rdState}); }, }).appendTo($wrpRow); } } export class InitiativeTrackerConditionAdd extends BaseComponent { static _RenderState = class { constructor () { this.cbDoClose = null; } }; constructor ({conditionsCustom}) { super(); this.__state.conditionsCustom = conditionsCustom; } getConditionsCustom () { return MiscUtil.copyFast(this._state.conditionsCustom); } async pGetShowModalResults () { const rdState = new this.constructor._RenderState(); const {$modalInner, doClose, pGetResolved} = UiUtil.getShowModal({ isMinHeight0: true, isHeaderBorder: true, title: "Add Condition", $titleSplit: this._render_$getBtnEditCustom({rdState}), }); rdState.cbDoClose = doClose; $$($modalInner)` ${this._render_$getStgConditionsStandard({rdState})}
${this._render_$getStgConditionsCustom({rdState})} ${this._render_$getStgIpts({rdState})} ${this._render_$getStgSubmit({rdState})} `; return pGetResolved(); } _render_$getBtnEditCustom ({rdState}) { return $(``) .on("click", async () => { const compEdit = new InitiativeTrackerConditionCustomEdit({conditionsCustom: MiscUtil.copyFast(this._state.conditionsCustom)}); await compEdit.pGetShowModalResults(); this._state.conditionsCustom = compEdit.getConditionsCustom(); }); } _render_$getStgConditionsStandard ({rdState}) { const $wrps = InitiativeTrackerUtil.CONDITIONS .map(cond => { const $btn = _UtilConditionsCustomView.$getBtnCondition({ comp: BaseComponent.fromObject({ name: cond.name, color: cond.color, turns: cond.turns, }, "*"), cbClick: ({name, color, turns}) => { this._state.name = name; this._state.color = color; }, cbSubmit: ({turns}) => { this._state.turns = turns; this._doSubmit({rdState}); }, }); return $$`
${$btn}
`; }); return $$`
${$wrps}
`; } _render_$getStgConditionsCustom ({rdState}) { const $wrpRows = $(`
`); const compRows = new _RenderableCollectionConditionsCustomView({ comp: this, $wrpRows, rdState, cbDoSubmit: this._doSubmit.bind(this), }); this._addHookBase("conditionsCustom", () => compRows.render())(); const $stg = $$`
${$wrpRows}
`; this._addHookBase("conditionsCustom", () => $stg.toggleVe(!!this._state.conditionsCustom.length))(); return $stg; } _render_$getStgIpts ({rdState}) { const $iptName = ComponentUiUtil.$getIptStr(this, "name", {html: ``}) .on("keydown", evt => { if (evt.key !== "Enter") return; $iptName.trigger("change"); this._doSubmit({rdState}); }); const $iptColor = ComponentUiUtil.$getIptColor(this, "color", {html: ``}); const $iptTurns = ComponentUiUtil.$getIptInt(this, "turns", null, {isAllowNull: true, fallbackOnNaN: null, html: ``}) .on("keydown", evt => { if (evt.key !== "Enter") return; $iptTurns.trigger("change"); this._doSubmit({rdState}); }); const $btnSave = $(``) .click(() => { this._state.conditionsCustom = [ ...this._state.conditionsCustom, InitiativeTrackerConditionUtil.getNewRowState({ name: this._state.name, color: this._state.color, turns: this._state.turns, }), ]; }); return $$`
Name
Color
Duration
 
${$iptName}
${$iptColor}
${$iptTurns}
${$btnSave}
`; } _render_$getStgSubmit ({rdState}) { const $btnAdd = $(``) .click(() => this._doSubmit({rdState})); return $$`
${$btnAdd}
`; } _doSubmit ({rdState}) { rdState.cbDoClose( true, UtilConditions.getDefaultState({ name: this._state.name, color: this._state.color, turns: this._state.turns, }), ); } _getDefaultState () { return { name: "", color: MiscUtil.randomColor(), turns: null, conditionsCustom: [], }; } }