aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/DataVizBox/DocCreatorMenu/Template.tsx
blob: 39215eb0673d79a0f4212b0ff77b0c95e630e774 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { Doc, FieldType } from "../../../../../fields/Doc";
import { Docs } from "../../../../documents/Documents";
import { Col } from "./DocCreatorMenu";
import { DynamicField } from "./FieldTypes/DynamicField";
import { Field, FieldSettings, ViewType } from "./FieldTypes/Field";
import {  } from "./FieldTypes/FieldUtils";
import { } from "./FieldTypes/StaticField";

export class Template {

    mainField: DynamicField;
    settings: FieldSettings;

    constructor(templateInfo: FieldSettings) {
        this.mainField = this.setupMainField(templateInfo);
        this.settings = templateInfo;
    }

    get childFields(): Field[] { return this.mainField.getSubfields };
    get allFields(): Field[] { return this.mainField.getAllSubfields };
    get contentFields(): Field[] { return this.allFields.filter(field => field.getViewType === ViewType.STATIC) };
    get doc(){ return this.mainField.renderedDoc(); };

    cloneBase = () => {
        const clone: Template = new Template(this.settings);
        clone.allFields.forEach(field => {
            const matchingField: Field = this.allFields.filter(f => f.getID === field.getID)[0];
            matchingField.applyAttributes(field);
        })
        return clone;
    }

    getRenderedDoc = () => {
        const doc: Doc = this.mainField.renderedDoc();
        this.contentFields.forEach(field => {
            const title: string = field.getTitle();
            const val: FieldType = field.getContent() as FieldType;
            if (!title || !val) return;
            doc[title] = val;
        });
        return doc;
    }

    getFieldByID = (id: number): Field => {
        return this.allFields.filter(field => field.getID === id)[0];
    }

    getFieldByTitle = (title: string) => {
        return this.allFields.filter(field => field.getTitle() === title)[0];
    }

    setupMainField = (templateInfo: FieldSettings) => {
        return new DynamicField(templateInfo, 1);
    }

    get descriptionSummary(): string {
        let summary: string = '';
        this.contentFields.forEach(field => {
            summary += `--- Field #${field.getID} (title: ${field.getTitle()}): ${field.getDescription ?? ''} ---`;
        });
        return summary;
    }

    get compiledContent(): string {
        let summary: string = '';
        this.contentFields.forEach(field => {
            summary += `--- Field #${field.getID} (title: ${field.getTitle()}): ${field.getContent() ?? ''} ---`;
        });
        return summary;
    }

    renderUpdates = () => {
        this.allFields.forEach(field => {
            field.updateRenderedDoc(field.renderedDoc());
        });
    };

    resetToBase = () => {
        this.allFields.forEach(field => {
            field.updateRenderedDoc();
        })
    }

    isValidTemplate = (cols: Col[]) => {
        const matches: number[][] = this.getMatches(cols);
        const maxMatches: number = this.maxMatches(matches);
        return maxMatches === this.contentFields.length;
    }

    getMatches = (cols: Col[]): number[][] => {
        const numFields = this.contentFields.length;

        if (cols.length !== numFields) return [];

        const matches: number[][] = Array(numFields)
            .fill([])
            .map(() => []);

        this.contentFields.forEach((field, i) => {
            matches[i] = (field.matches(cols));
        });

        return matches;
    }

    maxMatches = (matches: number[][]) => {
        if (matches.length === 0) return 0;

        const fieldsCt = this.contentFields.length;
        const used: boolean[] = Array(fieldsCt).fill(false);
        const mt: number[] = Array(fieldsCt).fill(-1);

        const augmentingPath = (v: number): boolean => {
            if (used[v]) return false;
            used[v] = true;

            for (const to of matches[v]) {
                if (mt[to] === -1 || augmentingPath(mt[to])) {
                    mt[to] = v;
                    return true;
                }
            }
            return false;
        };

        for (let v = 0; v < fieldsCt; ++v) {
            used.fill(false);
            augmentingPath(v);
        }

        let count: number = 0;

        for (let i = 0; i < fieldsCt; ++i) {
            if (mt[i] !== -1) ++count;
        }

        return count;
    };

}