From fd3f7d7d04379528cbc3abe2c7404eedf7f60f8a Mon Sep 17 00:00:00 2001 From: Stepan Kiryakov Date: Fri, 24 Jan 2025 18:32:12 +0400 Subject: [PATCH] update Signed-off-by: Stepan Kiryakov --- .../mathlive/keyboards/math-keyboard.ts | 151 ++++++----- .../mathlive/keyboards/matrix-keyboard.ts | 220 ++++++++-------- .../link-dialog/link-dialog.component.ts | 6 +- .../formula-configuration.component.ts | 23 +- .../modules/formulas/models/formula-tree.ts | 247 ++++++++++++++++++ .../formulas.ts | 47 +--- .../src/interface/formulas.interface.ts | 26 +- interfaces/src/models/schema.ts | 25 ++ 8 files changed, 524 insertions(+), 221 deletions(-) create mode 100644 frontend/src/app/modules/formulas/models/formula-tree.ts rename frontend/src/app/modules/formulas/{formula-configuration => models}/formulas.ts (70%) diff --git a/frontend/src/app/modules/common/mathlive/keyboards/math-keyboard.ts b/frontend/src/app/modules/common/mathlive/keyboards/math-keyboard.ts index 70a10ce4bf..d3df58d23d 100644 --- a/frontend/src/app/modules/common/mathlive/keyboards/math-keyboard.ts +++ b/frontend/src/app/modules/common/mathlive/keyboards/math-keyboard.ts @@ -109,59 +109,59 @@ export const mathKeyboard = { "|#0|", "\\sqrt[#0]{#0}", { - class: 'small', + class: "small", latex: "\\log_{#0}#0", shift: "\\ln", variants: [ - { class: 'small', latex: "\\log_{#0}#0" }, - { class: 'small', latex: "\\ln#0" }, - { class: 'small', latex: "\\log_{10}#0" }, + { class: "small", latex: "\\log_{#0}#0" }, + { class: "small", latex: "\\ln#0" }, + { class: "small", latex: "\\log_{10}#0" }, ] }, { latex: "\\exponentialE", shift: "\\exp", variants: [ - { class: 'small', latex: "\\exponentialE" }, - { class: 'small', latex: "\\exp\\left(#0\\right)" }, - { class: 'small', latex: "\\times10^{#0}" }, + { class: "small", latex: "\\exponentialE" }, + { class: "small", latex: "\\exp\\left(#0\\right)" }, + { class: "small", latex: "\\times10^{#0}" }, ] }, "\\lim_{#0}", { - class: 'small', + class: "small", latex: "\\sum_{#0}^{#0}#0", shift: "\\Sigma", variants: [ - { class: 'small', latex: "\\sum_{#0}^{#0}#0" }, - { class: 'small', latex: "\\sum #0" }, + { class: "small", latex: "\\sum_{#0}^{#0}#0" }, + { class: "small", latex: "\\sum #0" }, ] }, { - class: 'small', + class: "small", latex: "\\prod_{#0}^{#0}#0", shift: "\\Pi", variants: [ - { class: 'small', latex: "\\prod_{#0}^{#0}#0", }, - { class: 'small', latex: "\\prod#0", }, + { class: "small", latex: "\\prod_{#0}^{#0}#0", }, + { class: "small", latex: "\\prod#0", }, ] }, { - class: 'small', + class: "small", latex: "\\int_{#0}^{#0}#0", shift: "\\smallint", variants: [ - { class: 'small', latex: "\\int_{#0}^{#0}#0" }, - { class: 'small', latex: "\\int#0" }, - { class: 'small', latex: "\\iint #0" }, - { class: 'small', latex: "\\iiint#0" }, - { class: 'small', latex: "\\oint#0" }, - { class: 'small', latex: "\\intclockwise#0" }, - { class: 'small', latex: "\\varointclockwise#0" }, - { class: 'small', latex: "\\ointctrclockwise#0" }, - { class: 'small', latex: "\\intctrclockwise#0" }, - { class: 'small', latex: "\\oiint#0" }, - { class: 'small', latex: "\\oiiint#0" }, + { class: "small", latex: "\\int_{#0}^{#0}#0" }, + { class: "small", latex: "\\int#0" }, + { class: "small", latex: "\\iint #0" }, + { class: "small", latex: "\\iiint#0" }, + { class: "small", latex: "\\oint#0" }, + { class: "small", latex: "\\intclockwise#0" }, + { class: "small", latex: "\\varointclockwise#0" }, + { class: "small", latex: "\\ointctrclockwise#0" }, + { class: "small", latex: "\\intctrclockwise#0" }, + { class: "small", latex: "\\oiint#0" }, + { class: "small", latex: "\\oiiint#0" }, ] }, ], @@ -213,7 +213,7 @@ export const mathKeyboard = { ] }, { - class: 'small', + class: "small", latex: "\\mathrm{abs}\\left(#0\\right)", }, { @@ -323,34 +323,34 @@ export const mathKeyboard = { latex: "\\sin", shift: "\\sin^{-1}", variants: [ - { class: 'small', latex: "\\sin" }, - { class: 'small', latex: "\\sinh" }, - { class: 'small', latex: "\\sin^{-1}" }, - { class: 'small', latex: "\\arsinh" }, + { class: "small", latex: "\\sin" }, + { class: "small", latex: "\\sinh" }, + { class: "small", latex: "\\sin^{-1}" }, + { class: "small", latex: "\\arsinh" }, ] }, { latex: "\\cos", shift: "\\cos^{-1}", variants: [ - { class: 'small', latex: "\\cos" }, - { class: 'small', latex: "\\cosh" }, - { class: 'small', latex: "\\cos^{-1}" }, - { class: 'small', latex: "\\arcosh" }, + { class: "small", latex: "\\cos" }, + { class: "small", latex: "\\cosh" }, + { class: "small", latex: "\\cos^{-1}" }, + { class: "small", latex: "\\arcosh" }, ] }, { latex: "\\tan", shift: "\\tan^{-1}", variants: [ - { class: 'small', latex: "\\tan" }, - { class: 'small', latex: "\\tg" }, - { class: 'small', latex: "\\tan^{-1}" }, - { class: 'small', latex: "\\tanh" }, - { class: 'small', latex: "\\artanh" }, - { class: 'small', latex: "\\arctan" }, - { class: 'small', latex: "\\arctg" }, - { class: 'small', latex: "\\cot" }, + { class: "small", latex: "\\tan" }, + { class: "small", latex: "\\tg" }, + { class: "small", latex: "\\tan^{-1}" }, + { class: "small", latex: "\\tanh" }, + { class: "small", latex: "\\artanh" }, + { class: "small", latex: "\\arctan" }, + { class: "small", latex: "\\arctg" }, + { class: "small", latex: "\\cot" }, ] }, "\\Delta", @@ -368,8 +368,8 @@ export const mathKeyboard = { latex: "f(#0)", shift: "x_{i}", variants: [ - { class: 'small', latex: "f(#0)" }, - { class: 'small', latex: "g(#0)" }, + { class: "small", latex: "f(#0)" }, + { class: "small", latex: "g(#0)" }, { latex: "x^{n}" }, { latex: "x^{#0}" }, { latex: "x_{n}" }, @@ -407,39 +407,60 @@ export const mathKeyboard = { ] }, { - latex: '+', - class: 'action', - command: ['performWithFeedback', 'addRowAfter'], + latex: "+", + aside: "New Line", + class: "action", + command: ["performWithFeedback", "addRowAfter"], shift: { - latex: '-', - class: 'action', - command: ['performWithFeedback', 'removeRow'], + latex: "-", + class: "action", + aside: "Delete Line", + command: ["performWithFeedback", "removeRow"], }, + variants: [ + { + latex: "+", + aside: "New Line", + class: "small", + command: ["performWithFeedback", "addRowAfter"], + }, + { + latex: "-", + aside: "Delete Line", + class: "small", + command: ["performWithFeedback", "removeRow"], + }, + ] }, { - label: "Text", - class: 'action', - command: ['switchMode', 'text'], + label: "T", + aside: "Text Mode", + class: "action", + command: ["switchMode", "text"], shift: { - label: "Math", - class: 'small action', - command: ['switchMode', 'math'], + label: "M", + aside: "Math Mode", + class: "small action", + command: ["switchMode", "math"], }, variants: [ { - label: "Text", - class: 'small', - command: ['switchMode', 'text'], + label: "T", + class: "small", + aside: "Math Mode", + command: ["switchMode", "text"], }, { - label: "Math", - class: 'small', - command: ['switchMode', 'math'], + label: "M", + class: "small", + aside: "Math Mode", + command: ["switchMode", "math"], }, { - label: "LaTeX", - class: 'small', - command: ['switchMode', 'latex'], + label: "L", + class: "small", + aside: "LaTeX Mode", + command: ["switchMode", "latex"], }, ] }, diff --git a/frontend/src/app/modules/common/mathlive/keyboards/matrix-keyboard.ts b/frontend/src/app/modules/common/mathlive/keyboards/matrix-keyboard.ts index ff786114f7..8f1f5ea783 100644 --- a/frontend/src/app/modules/common/mathlive/keyboards/matrix-keyboard.ts +++ b/frontend/src/app/modules/common/mathlive/keyboards/matrix-keyboard.ts @@ -4,248 +4,248 @@ export const matrixKeyboard = { rows: [ [ { - label: '1x1', + label: "1x1", latex: "\\begin{pmatrix}#0\\end{pmatrix}", shift: "[#0]", variants: [ { latex: "\\ddots", insert: "\\begin{matrix}#0\\end{matrix}" }, - { latex: '(\\ddots)', insert: "\\begin{pmatrix}#0\\end{pmatrix}" }, - { latex: '\\lbrack\\ddots\\rbrack', insert: "\\begin{bmatrix}#0\\end{bmatrix}" }, - { latex: '\\vert\\ddots\\vert', insert: "\\begin{vmatrix}#0\\end{vmatrix}" }, - { latex: '\\lbrace\\ddots\\rbrace', insert: "\\begin{Bmatrix}#0\\end{Bmatrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0\\end{Bmatrix}" }, ] }, { - label: '1x2', + label: "1x2", latex: "\\begin{pmatrix}#0 & #0\\end{pmatrix}", shift: "[#0]", variants: [ { latex: "\\ddots", insert: "\\begin{matrix}#0 & #0\\end{matrix}" }, - { latex: '(\\ddots)', insert: "\\begin{pmatrix}#0 & #0\\end{pmatrix}" }, - { latex: '\\lbrack\\ddots\\rbrack', insert: "\\begin{bmatrix}#0 & #0\\end{bmatrix}" }, - { latex: '\\vert\\ddots\\vert', insert: "\\begin{vmatrix}#0 & #0\\end{vmatrix}" }, - { latex: '\\lbrace\\ddots\\rbrace', insert: "\\begin{Bmatrix}#0 & #0\\end{Bmatrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0 & #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0 & #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0 & #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0 & #0\\end{Bmatrix}" }, ] }, { - label: '1x3', + label: "1x3", latex: "\\begin{pmatrix}#0 & #0 & #0\\end{pmatrix}", shift: "[#0]", variants: [ { latex: "\\ddots", insert: "\\begin{matrix}#0 & #0 & #0\\end{matrix}" }, - { latex: '(\\ddots)', insert: "\\begin{pmatrix}#0 & #0 & #0\\end{pmatrix}" }, - { latex: '\\lbrack\\ddots\\rbrack', insert: "\\begin{bmatrix}#0 & #0 & #0\\end{bmatrix}" }, - { latex: '\\vert\\ddots\\vert', insert: "\\begin{vmatrix}#0 & #0 & #0\\end{vmatrix}" }, - { latex: '\\lbrace\\ddots\\rbrace', insert: "\\begin{Bmatrix}#0 & #0 & #0\\end{Bmatrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0 & #0 & #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0 & #0 & #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0 & #0 & #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0 & #0 & #0\\end{Bmatrix}" }, ] }, { - label: '1x4', + label: "1x4", latex: "\\begin{pmatrix}#0 & #0 & #0 & #0\\end{pmatrix}", shift: "[#0]", variants: [ { latex: "\\ddots", insert: "\\begin{matrix}#0 & #0 & #0 & #0\\end{matrix}" }, - { latex: '(\\ddots)', insert: "\\begin{pmatrix}#0 & #0 & #0 & #0\\end{pmatrix}" }, - { latex: '\\lbrack\\ddots\\rbrack', insert: "\\begin{bmatrix}#0 & #0 & #0 & #0\\end{bmatrix}" }, - { latex: '\\vert\\ddots\\vert', insert: "\\begin{vmatrix}#0 & #0 & #0 & #0\\end{vmatrix}" }, - { latex: '\\lbrace\\ddots\\rbrace', insert: "\\begin{Bmatrix}#0 & #0 & #0 & #0\\end{Bmatrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0 & #0 & #0 & #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0 & #0 & #0 & #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0 & #0 & #0 & #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0 & #0 & #0 & #0\\end{Bmatrix}" }, ] }, { - latex: '\\cdots', - command: ['performWithFeedback', 'addRowAfter'], - aside: 'Add Row After', + latex: "\\cdots", + command: ["performWithFeedback", "addRowAfter"], + aside: "Add Row After", shift: "\\cdots", variants: [ { - latex: '\\cdots', - command: ['performWithFeedback', 'addRowAfter'], - aside: 'Add Row After', + latex: "\\cdots", + command: ["performWithFeedback", "addRowAfter"], + aside: "Add Row After", }, { - latex: '\\cdots', - command: ['performWithFeedback', 'addRowBefore'], - aside: 'Add Row Before', + latex: "\\cdots", + command: ["performWithFeedback", "addRowBefore"], + aside: "Add Row Before", } ] } ], [ { - label: '2x1', - latex: '\\begin{pmatrix}#0\\\\ #0\\end{pmatrix}', + label: "2x1", + latex: "\\begin{pmatrix}#0\\\\ #0\\end{pmatrix}", shift: "[#0]", variants: [ - { latex: "\\ddots", insert: '\\begin{matrix}#0\\\\ #0\\end{matrix}' }, - { latex: '(\\ddots)', insert: '\\begin{pmatrix}#0\\\\ #0\\end{pmatrix}' }, - { latex: '\\lbrack\\ddots\\rbrack', insert: '\\begin{bmatrix}#0\\\\ #0\\end{bmatrix}' }, - { latex: '\\vert\\ddots\\vert', insert: '\\begin{vmatrix}#0\\\\ #0\\end{vmatrix}' }, - { latex: '\\lbrace\\ddots\\rbrace', insert: '\\begin{Bmatrix}#0\\\\ #0\\end{Bmatrix}' }, + { latex: "\\ddots", insert: "\\begin{matrix}#0\\\\ #0\\end{matrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0\\\\ #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0\\\\ #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0\\\\ #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0\\\\ #0\\end{Bmatrix}" }, ] }, { - label: '2x2', + label: "2x2", latex: "\\begin{pmatrix}#0 & #0\\\\ #0 & #0\\end{pmatrix}", shift: "[#0]", variants: [ { latex: "\\ddots", insert: "\\begin{matrix}#0 & #0\\\\ #0 & #0\\end{matrix}" }, - { latex: '(\\ddots)', insert: "\\begin{pmatrix}#0 & #0\\\\ #0 & #0\\end{pmatrix}" }, - { latex: '\\lbrack\\ddots\\rbrack', insert: "\\begin{bmatrix}#0 & #0\\\\ #0 & #0\\end{bmatrix}" }, - { latex: '\\vert\\ddots\\vert', insert: "\\begin{vmatrix}#0 & #0\\\\ #0 & #0\\end{vmatrix}" }, - { latex: '\\lbrace\\ddots\\rbrace', insert: "\\begin{Bmatrix}#0 & #0\\\\ #0 & #0\\end{Bmatrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0 & #0\\\\ #0 & #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0 & #0\\\\ #0 & #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0 & #0\\\\ #0 & #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0 & #0\\\\ #0 & #0\\end{Bmatrix}" }, ] }, { - label: '2x3', + label: "2x3", latex: "\\begin{pmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\end{pmatrix}", shift: "[#0]", variants: [ { latex: "\\ddots", insert: "\\begin{matrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\end{matrix}" }, - { latex: '(\\ddots)', insert: "\\begin{pmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\end{pmatrix}" }, - { latex: '\\lbrack\\ddots\\rbrack', insert: "\\begin{bmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\end{bmatrix}" }, - { latex: '\\vert\\ddots\\vert', insert: "\\begin{vmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\end{vmatrix}" }, - { latex: '\\lbrace\\ddots\\rbrace', insert: "\\begin{Bmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\end{Bmatrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\end{Bmatrix}" }, ] }, { - label: '2x4', + label: "2x4", latex: "\\begin{pmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{pmatrix}", shift: "[#0]", variants: [ { latex: "\\ddots", insert: "\\begin{matrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{matrix}" }, - { latex: '(\\ddots)', insert: "\\begin{pmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{pmatrix}" }, - { latex: '\\lbrack\\ddots\\rbrack', insert: "\\begin{bmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{bmatrix}" }, - { latex: '\\vert\\ddots\\vert', insert: "\\begin{vmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{vmatrix}" }, - { latex: '\\lbrace\\ddots\\rbrace', insert: "\\begin{Bmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{Bmatrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{Bmatrix}" }, ] }, { - latex: '\\vdots', - command: ['performWithFeedback', 'addColumnAfter'], - aside: 'Add Column After', + latex: "\\vdots", + command: ["performWithFeedback", "addColumnAfter"], + aside: "Add Column After", shift: "\\vdots", variants: [ { - latex: '\\vdots', - command: ['performWithFeedback', 'addColumnAfter'], - aside: 'Add Column After', + latex: "\\vdots", + command: ["performWithFeedback", "addColumnAfter"], + aside: "Add Column After", }, { - latex: '\\vdots', - command: ['performWithFeedback', 'addColumnBefore'], - aside: 'Add Column Before', + latex: "\\vdots", + command: ["performWithFeedback", "addColumnBefore"], + aside: "Add Column Before", } ] } ], [ { - label: '3x1', - latex: '\\begin{pmatrix}#0\\\\ #0\\\\ #0\\end{pmatrix}', + label: "3x1", + latex: "\\begin{pmatrix}#0\\\\ #0\\\\ #0\\end{pmatrix}", shift: "[#0]", variants: [ - { latex: "\\ddots", insert: '\\begin{matrix}#0\\\\ #0\\\\ #0\\end{matrix}' }, - { latex: '(\\ddots)', insert: '\\begin{pmatrix}#0\\\\ #0\\\\ #0\\end{pmatrix}' }, - { latex: '\\lbrack\\ddots\\rbrack', insert: '\\begin{bmatrix}#0\\\\ #0\\\\ #0\\end{bmatrix}' }, - { latex: '\\vert\\ddots\\vert', insert: '\\begin{vmatrix}#0\\\\ #0\\\\ #0\\end{vmatrix}' }, - { latex: '\\lbrace\\ddots\\rbrace', insert: '\\begin{Bmatrix}#0\\\\ #0\\\\ #0\\end{Bmatrix}' }, + { latex: "\\ddots", insert: "\\begin{matrix}#0\\\\ #0\\\\ #0\\end{matrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0\\\\ #0\\\\ #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0\\\\ #0\\\\ #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0\\\\ #0\\\\ #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0\\\\ #0\\\\ #0\\end{Bmatrix}" }, ] }, { - label: '3x2', + label: "3x2", latex: "\\begin{pmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{pmatrix}", shift: "[#0]", variants: [ { latex: "\\ddots", insert: "\\begin{matrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{matrix}" }, - { latex: '(\\ddots)', insert: "\\begin{pmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{pmatrix}" }, - { latex: '\\lbrack\\ddots\\rbrack', insert: "\\begin{bmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{bmatrix}" }, - { latex: '\\vert\\ddots\\vert', insert: "\\begin{vmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{vmatrix}" }, - { latex: '\\lbrace\\ddots\\rbrace', insert: "\\begin{Bmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{Bmatrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{Bmatrix}" }, ] }, { - label: '3x3', + label: "3x3", latex: "\\begin{pmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{pmatrix}", shift: "[#0]", variants: [ { latex: "\\ddots", insert: "\\begin{matrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{matrix}" }, - { latex: '(\\ddots)', insert: "\\begin{pmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{pmatrix}" }, - { latex: '\\lbrack\\ddots\\rbrack', insert: "\\begin{bmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{bmatrix}" }, - { latex: '\\vert\\ddots\\vert', insert: "\\begin{vmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{vmatrix}" }, - { latex: '\\lbrace\\ddots\\rbrace', insert: "\\begin{Bmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{Bmatrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{Bmatrix}" }, ] }, { - label: '3x4', + label: "3x4", latex: "\\begin{pmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{pmatrix}", shift: "[#0]", variants: [ { latex: "\\ddots", insert: "\\begin{matrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{matrix}" }, - { latex: '(\\ddots)', insert: "\\begin{pmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{pmatrix}" }, - { latex: '\\lbrack\\ddots\\rbrack', insert: "\\begin{bmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{bmatrix}" }, - { latex: '\\vert\\ddots\\vert', insert: "\\begin{vmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{vmatrix}" }, - { latex: '\\lbrace\\ddots\\rbrace', insert: "\\begin{Bmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{Bmatrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{Bmatrix}" }, ] }, { - latex: '\\cdots', - aside: 'Delelte Row', - command: ['performWithFeedback', 'removeRow'], + latex: "\\cdots", + aside: "Delelte Row", + command: ["performWithFeedback", "removeRow"], } ], [ { - label: '4x1', - latex: '\\begin{pmatrix}#0\\\\ #0\\\\ #0\\\\ #0\\end{pmatrix}', + label: "4x1", + latex: "\\begin{pmatrix}#0\\\\ #0\\\\ #0\\\\ #0\\end{pmatrix}", shift: "[#0]", variants: [ - { latex: "\\ddots", insert: '\\begin{matrix}#0\\\\ #0\\\\ #0\\\\ #0\\end{matrix}' }, - { latex: '(\\ddots)', insert: '\\begin{pmatrix}#0\\\\ #0\\\\ #0\\\\ #0\\end{pmatrix}' }, - { latex: '\\lbrack\\ddots\\rbrack', insert: '\\begin{bmatrix}#0\\\\ #0\\\\ #0\\\\ #0\\end{bmatrix}' }, - { latex: '\\vert\\ddots\\vert', insert: '\\begin{vmatrix}#0\\\\ #0\\\\ #0\\\\ #0\\end{vmatrix}' }, - { latex: '\\lbrace\\ddots\\rbrace', insert: '\\begin{Bmatrix}#0\\\\ #0\\\\ #0\\\\ #0\\end{Bmatrix}' }, + { latex: "\\ddots", insert: "\\begin{matrix}#0\\\\ #0\\\\ #0\\\\ #0\\end{matrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0\\\\ #0\\\\ #0\\\\ #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0\\\\ #0\\\\ #0\\\\ #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0\\\\ #0\\\\ #0\\\\ #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0\\\\ #0\\\\ #0\\\\ #0\\end{Bmatrix}" }, ] }, { - label: '4x2', + label: "4x2", latex: "\\begin{pmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{pmatrix}", shift: "[#0]", variants: [ { latex: "\\ddots", insert: "\\begin{matrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{matrix}" }, - { latex: '(\\ddots)', insert: "\\begin{pmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{pmatrix}" }, - { latex: '\\lbrack\\ddots\\rbrack', insert: "\\begin{bmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{bmatrix}" }, - { latex: '\\vert\\ddots\\vert', insert: "\\begin{vmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{vmatrix}" }, - { latex: '\\lbrace\\ddots\\rbrace', insert: "\\begin{Bmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{Bmatrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0 & #0\\\\ #0 & #0\\\\ #0 & #0\\\\ #0 & #0\\end{Bmatrix}" }, ] }, { - label: '4x3', + label: "4x3", latex: "\\begin{pmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{pmatrix}", shift: "[#0]", variants: [ { latex: "\\ddots", insert: "\\begin{matrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{matrix}" }, - { latex: '(\\ddots)', insert: "\\begin{pmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{pmatrix}" }, - { latex: '\\lbrack\\ddots\\rbrack', insert: "\\begin{bmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{bmatrix}" }, - { latex: '\\vert\\ddots\\vert', insert: "\\begin{vmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{vmatrix}" }, - { latex: '\\lbrace\\ddots\\rbrace', insert: "\\begin{Bmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{Bmatrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\\\ #0 & #0 & #0\\end{Bmatrix}" }, ] }, { - label: '4x4', + label: "4x4", latex: "\\begin{pmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{pmatrix}", shift: "[#0]", variants: [ { latex: "\\ddots", insert: "\\begin{matrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{matrix}" }, - { latex: '(\\ddots)', insert: "\\begin{pmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{pmatrix}" }, - { latex: '\\lbrack\\ddots\\rbrack', insert: "\\begin{bmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{bmatrix}" }, - { latex: '\\vert\\ddots\\vert', insert: "\\begin{vmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{vmatrix}" }, - { latex: '\\lbrace\\ddots\\rbrace', insert: "\\begin{Bmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{Bmatrix}" }, + { latex: "(\\ddots)", insert: "\\begin{pmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{pmatrix}" }, + { latex: "\\lbrack\\ddots\\rbrack", insert: "\\begin{bmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{bmatrix}" }, + { latex: "\\vert\\ddots\\vert", insert: "\\begin{vmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{vmatrix}" }, + { latex: "\\lbrace\\ddots\\rbrace", insert: "\\begin{Bmatrix}#0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\\\ #0 & #0 & #0 & #0\\end{Bmatrix}" }, ] }, { - latex: '\\vdots', - aside: 'Delelte Column', - command: ['performWithFeedback', 'removeColumn'], + latex: "\\vdots", + aside: "Delete Column", + command: ["performWithFeedback", "removeColumn"], } ] ] diff --git a/frontend/src/app/modules/formulas/dialogs/link-dialog/link-dialog.component.ts b/frontend/src/app/modules/formulas/dialogs/link-dialog/link-dialog.component.ts index dcd57642af..7c0594fd47 100644 --- a/frontend/src/app/modules/formulas/dialogs/link-dialog/link-dialog.component.ts +++ b/frontend/src/app/modules/formulas/dialogs/link-dialog/link-dialog.component.ts @@ -2,7 +2,7 @@ import { Component } from '@angular/core'; import { DialogService, DynamicDialogConfig, DynamicDialogRef } from 'primeng/dynamicdialog'; import { TreeListData, TreeListItem, TreeListView } from 'src/app/modules/common/tree-graph/tree-list'; import { FieldData } from 'src/app/modules/common/models/schema-node'; -import { FormulaLink } from '../../formula-configuration/formulas'; +import { IFormulaLink } from '@guardian/interfaces'; @Component({ selector: 'link-dialog', @@ -33,7 +33,7 @@ export class LinkDialog { } else { this.type = 'schema'; } - const link: FormulaLink = this.config.data?.link; + const link: IFormulaLink = this.config.data?.link; if (link) { this.type = link.type; this.entityId = link.entityId; @@ -109,7 +109,7 @@ export class LinkDialog { public onSubmit(): void { if (this.itemId) { - const result: FormulaLink = { + const result: IFormulaLink = { type: this.type, entityId: this.entityId, item: this.itemId diff --git a/frontend/src/app/modules/formulas/formula-configuration/formula-configuration.component.ts b/frontend/src/app/modules/formulas/formula-configuration/formula-configuration.component.ts index 9f3b9a87f9..5c95c9f033 100644 --- a/frontend/src/app/modules/formulas/formula-configuration/formula-configuration.component.ts +++ b/frontend/src/app/modules/formulas/formula-configuration/formula-configuration.component.ts @@ -1,15 +1,16 @@ import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; -import { Schema, UserPermissions } from '@guardian/interfaces'; +import { IFormulaItem, FormulaItemType, IFormulaLink, Schema, UserPermissions } from '@guardian/interfaces'; import { forkJoin, Subscription } from 'rxjs'; import { ProfileService } from 'src/app/services/profile.service'; import { FormulasService } from 'src/app/services/formulas.service'; import { CustomConfirmDialogComponent } from '../../common/custom-confirm-dialog/custom-confirm-dialog.component'; import { DialogService } from 'primeng/dynamicdialog'; -import { FormulaItem, FormulaItemType, FormulaLink, Formulas } from './formulas'; import { FormControl, FormGroup, Validators } from '@angular/forms'; import { MathLiveComponent } from '../../common/mathlive/mathlive.component'; import { LinkDialog } from '../dialogs/link-dialog/link-dialog.component'; +import { Formulas } from '../models/formulas'; +import { FormulasTree } from '../models/formula-tree'; @Component({ selector: 'app-formula-configuration', @@ -208,6 +209,14 @@ export class FormulaConfigurationComponent implements OnInit { description: value.description, config }; + + + const test = new FormulasTree(); + test.setFormulas([item]); + test.setSchemas(this.schemas); + test.update(); + debugger; + this.formulasService .updateFormula(item) .subscribe((data) => { @@ -289,7 +298,7 @@ export class FormulaConfigurationComponent implements OnInit { }); } - public onLink(item: FormulaItem) { + public onLink(item: IFormulaItem) { const dialogRef = this.dialogService.open(LinkDialog, { showHeader: false, width: '800px', @@ -303,14 +312,14 @@ export class FormulaConfigurationComponent implements OnInit { ] }, }); - dialogRef.onClose.subscribe((result: FormulaLink | null) => { + dialogRef.onClose.subscribe((result: IFormulaLink | null) => { if (result) { item.link = result } }); } - public getEntityName(link: FormulaLink): string { + public getEntityName(link: IFormulaLink): string { if (link.type === 'schema') { return this.schemasMap.get(link.entityId) || ''; } @@ -320,7 +329,7 @@ export class FormulaConfigurationComponent implements OnInit { return ''; } - public getFieldName(link: FormulaLink): string { + public getFieldName(link: IFormulaLink): string { if (link.type === 'schema') { return this.schemasFieldMap.get(`${link.entityId}.${link.item}`) || ''; } @@ -334,7 +343,7 @@ export class FormulaConfigurationComponent implements OnInit { return ''; } - public deleteLink(item: FormulaItem, $event: any) { + public deleteLink(item: IFormulaItem, $event: any) { $event.preventDefault(); $event.stopPropagation(); item.link = null; diff --git a/frontend/src/app/modules/formulas/models/formula-tree.ts b/frontend/src/app/modules/formulas/models/formula-tree.ts new file mode 100644 index 0000000000..aa396f8b68 --- /dev/null +++ b/frontend/src/app/modules/formulas/models/formula-tree.ts @@ -0,0 +1,247 @@ +import { IFormulaItem, IFormula, IFormulaLink, FormulaItemType, Schema, SchemaField } from "@guardian/interfaces"; + +export interface Link { + schema: string; + path: string; + item: FormulaItem; +} + +export class FormulaItem { + public readonly uuid: string; + public readonly name: string; + public readonly description: string; + public readonly type: FormulaItemType; + + private _value: any; + private _link: IFormulaLink | null; + private _relationships: string[] | null; + + private _schemaLink: { schema: string, path: string } | null; + private _formulaLink: { formula: string, variable: string } | null; + private _parent: FormulaTree; + + private _relationshipItems: FormulaItem[]; + private _formulaItems: FormulaItem[]; + private _linkEntity: FormulaTree | Schema | null; + private _linkItem: FormulaItem | SchemaField | null; + + constructor(config: IFormulaItem) { + this.uuid = config.uuid || ''; + this.name = config.name || ''; + this.description = config.description || ''; + this.type = config.type || ''; + + this._value = config.value || ''; + this._link = config.link || null; + this._relationships = config.relationships || null; + + this._schemaLink = null; + this._formulaLink = null; + if (this._link) { + if (this._link.type === 'schema') { + this._schemaLink = { + schema: this._link.entityId, + path: this._link.item + } + } + if (this._link.type === 'schema') { + this._formulaLink = { + formula: this._link.entityId, + variable: this._link.item + } + } + } + + this._relationshipItems = []; + this._formulaItems = []; + this._linkItem = null; + } + + public get schemaLink() { + return this._schemaLink; + } + + public get formulaLink() { + return this._formulaLink; + } + + public setParent(parent: FormulaTree) { + this._parent = parent; + } + + public setRelationship(items: FormulaItem[]) { + if (Array.isArray(this._relationships)) { + this._relationshipItems = items.filter((e) => this._relationships?.includes(e.uuid)); + } else { + this._relationshipItems = []; + } + this._formulaItems = []; + for (const item of items) { + if (Array.isArray(item._relationships) && item._relationships.includes(this.uuid)) { + this._formulaItems.push(item); + } + } + } + + public setFormulas(formulas: FormulaTree[]) { + if (this._formulaLink) { + this._linkEntity = formulas.find((e) => e.uuid === this._formulaLink?.formula) || null; + if (this._linkEntity) { + this._linkItem = this._linkEntity.get(this._formulaLink.variable); + } + } + } + + public setSchemas(schemas: Schema[]) { + if (this._schemaLink) { + this._linkEntity = schemas.find((e) => e.iri === this._schemaLink?.schema) || null; + if (this._linkEntity) { + this._linkItem = this._linkEntity.getField(this._schemaLink.path); + } + } + } + + public setDocuments(documents: any[]) { + + } +} + +export class FormulaTree { + public readonly uuid: string; + public readonly name: string; + public readonly description: string; + + private _links: Map>; + private _items: FormulaItem[]; + + constructor(formula: IFormula) { + this.uuid = formula.uuid || ''; + this.name = formula.name || ''; + this.description = formula.description || ''; + + this._links = new Map>(); + this.parse(formula?.config?.formulas) + } + + private parse(items?: IFormulaItem[]) { + if (!items) { + return; + } + + this._items = []; + for (const config of items) { + const item = new FormulaItem(config); + item.setParent(this); + const link = item.schemaLink; + if (link) { + const map = this._links.get(link.schema) || new Map(); + const array = map.get(link.path) || []; + array.push(item); + map.set(link.path, array); + this._links.set(link.schema, map); + } + this._items.push(item); + } + + for (const item of this._items) { + item.setRelationship(this._items); + } + } + + public setFormulas(formulas: FormulaTree[]) { + for (const item of this._items) { + item.setFormulas(formulas); + } + } + + public setSchemas(schemas: Schema[]) { + for (const item of this._items) { + item.setSchemas(schemas); + } + } + + public setDocuments(documents: any[]) { + for (const item of this._items) { + item.setDocuments(documents); + } + } + + public hasLink(schema: string, path: string): boolean { + return this._links.get(schema)?.has(path) || false; + } + + public getLink(schema: string, path: string): FormulaItem[] { + return this._links.get(schema)?.get(path) || []; + } + + public get(variable: string): FormulaItem | null { + for (const item of this._items) { + if (item.name === variable) { + return item; + } + } + return null; + } + + public merge(links: Map>) { + for (const [schema, map] of this._links.entries()) { + const fullMap = links.get(schema) || new Map(); + for (const [path, array] of map.entries()) { + const fullArray = fullMap.get(path) || []; + for (const item of array) { + fullArray.push(item); + } + fullMap.set(path, fullArray); + } + links.set(schema, fullMap); + } + } +} + +export class FormulasTree { + public items: FormulaTree[]; + + private _links: Map>; + + constructor() { + this._links = new Map>(); + } + + public setFormulas(formulas: IFormula[]) { + if (Array.isArray(formulas)) { + this.items = formulas.map((f) => new FormulaTree(f)); + } else { + this.items = []; + } + for (const item of this.items) { + item.setFormulas(this.items); + } + } + + public setSchemas(schemas: Schema[]) { + for (const item of this.items) { + item.setSchemas(schemas); + } + } + + public setDocuments(documents: any[]) { + for (const item of this.items) { + item.setDocuments(documents); + } + } + + public update() { + this._links.clear(); + for (const item of this.items) { + item.merge(this._links); + } + } + + public has(schema: string, path: string): boolean { + return this._links.get(schema)?.has(path) || false; + } + + public get(schema: string, path: string): FormulaItem[] { + return this._links.get(schema)?.get(path) || []; + } +} \ No newline at end of file diff --git a/frontend/src/app/modules/formulas/formula-configuration/formulas.ts b/frontend/src/app/modules/formulas/models/formulas.ts similarity index 70% rename from frontend/src/app/modules/formulas/formula-configuration/formulas.ts rename to frontend/src/app/modules/formulas/models/formulas.ts index cd5ee5278c..f689b2bf82 100644 --- a/frontend/src/app/modules/formulas/formula-configuration/formulas.ts +++ b/frontend/src/app/modules/formulas/models/formulas.ts @@ -1,33 +1,10 @@ -import { GenerateUUIDv4 } from '@guardian/interfaces'; - -export enum FormulaItemType { - Constant = 'constant', - Variable = 'variable', - Formula = 'formula', - Text = 'text' -} - -export interface FormulaLink { - type: 'formula' | 'schema'; - entityId: string; - item: string; -} - -export interface FormulaItem { - uuid: string; - name: string; - description: string; - type: FormulaItemType, - value?: any; - link?: FormulaLink | null; - relationships?: any; -} +import { IFormulaItem, FormulaItemType, GenerateUUIDv4, IFormulaConfig } from '@guardian/interfaces'; export class Formulas { - private items: FormulaItem[]; + private items: IFormulaItem[]; private filterMap: Map; - public data: FormulaItem[]; + public data: IFormulaItem[]; constructor() { this.items = []; @@ -38,8 +15,8 @@ export class Formulas { this.filterMap.set(FormulaItemType.Text, true); } - private create(type: FormulaItemType): FormulaItem { - const item: FormulaItem = { + private create(type: FormulaItemType): IFormulaItem { + const item: IFormulaItem = { uuid: GenerateUUIDv4(), name: '', description: '', @@ -73,7 +50,7 @@ export class Formulas { this.update(); } - public delete(item: FormulaItem): void { + public delete(item: IFormulaItem): void { this.items = this.items.filter((e) => e.uuid !== item?.uuid); this.update(); } @@ -90,27 +67,27 @@ export class Formulas { this.data = this.items.filter((e) => this.filterMap.get(e.type)); } - public fromData(config: any) { - const items: any[] = config?.formulas || []; + public fromData(config: IFormulaConfig) { + const items: IFormulaItem[] = config?.formulas || []; this.items = items.map((e) => this._fromJson(e)); this.update(); } - public getJson(): any { + public getJson(): IFormulaConfig { return { formulas: this.items.map((e) => this._toJson(e)) }; } - private _fromJson(item: any): FormulaItem { + private _fromJson(item: IFormulaItem): IFormulaItem { return item; } - private _toJson(item: FormulaItem): any { + private _toJson(item: IFormulaItem): IFormulaItem { return item; } - public getItem(uuid: string): FormulaItem | null { + public getItem(uuid: string): IFormulaItem | null { for (const item of this.items) { if (item.uuid === uuid) { return item; diff --git a/interfaces/src/interface/formulas.interface.ts b/interfaces/src/interface/formulas.interface.ts index 2d7541e707..e96db43998 100644 --- a/interfaces/src/interface/formulas.interface.ts +++ b/interfaces/src/interface/formulas.interface.ts @@ -1,9 +1,33 @@ -export interface IFormulaConfig { +export enum FormulaItemType { + Constant = 'constant', + Variable = 'variable', + Formula = 'formula', + Text = 'text' +} + +export interface IFormulaLink { + type: 'formula' | 'schema'; + entityId: string; + item: string; +} +export interface IFormulaItem { + uuid: string; + name: string; + description: string; + type: FormulaItemType; + value?: any; + link?: IFormulaLink | null; + relationships?: string[]; +} + +export interface IFormulaConfig { + formulas?: IFormulaItem[]; } export interface IFormula { id?: string; + uuid?: string; name?: string; description?: string; policyId?: string; diff --git a/interfaces/src/models/schema.ts b/interfaces/src/models/schema.ts index c64b5fe3d9..cc045c9e0e 100644 --- a/interfaces/src/models/schema.ts +++ b/interfaces/src/models/schema.ts @@ -449,4 +449,29 @@ export class Schema implements ISchema { } return result; } + + /** + * Get field + */ + public getField(path: string): SchemaField | null { + return this._getField(path, this.fields); + } + + /** + * Get field + */ + private _getField(path: string, fields?: SchemaField[]): SchemaField | null { + if (Array.isArray(fields)) { + for (const field of fields) { + if (field.path === path) { + return field; + } + const result = this._getField(path, field.fields); + if (result) { + return result; + } + } + } + return null; + } }