/** * SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop * SPDX-License-Identifier: GPL-3.0-or-later * @author Author: Sascha Nitsch (grumpydeveloper) * @internal **/ import { RTNode } from './rtnode'; import { RenderTree } from './rendertree'; import Formula from 'fparser'; /** * calculate new defintions based on formula */ export class RTCalc extends RTNode { /** name of the definition */ private target: string; /** expression to calculate */ private expression: string; /** * clone function * @return a copy of us */ clone(): RTCalc { return new RTCalc(this.root, this.target, this.expression); } /** * constructor * @param root our parent RenderTree * @param target name of the new definition * @param expression expression to calculate */ constructor(root: RenderTree, target: string, expression: string) { super(root); this.target = target; this.expression = expression; } /** * create RTCalc from given node into given RenderTree * @param root render tree to insert to * @param node input node * @returns new RTCalc node */ static fromElem(root: RenderTree, node: HTMLElement): RTCalc { return new RTCalc( root, node.getAttribute('name') || '', node.innerHTML ); } /** * render our element into target * @param target target to render into */ // eslint-disable-next-line @typescript-eslint/no-unused-vars render(target: HTMLElement): void { const exp = this.resolveString(this.expression); const fObj = new Formula(exp); const result = fObj.evaluate({}); this.root.set(this.target, result.toString()); } }