aboutsummaryrefslogtreecommitdiff
path: root/src/client/documents/Documents.ts
blob: 460bf9b23073863b37aa75f989e2b6c5628d49b2 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { Document } from "../../fields/Document";
import { Server } from "../Server";
import { KeyStore } from "../../fields/Key";
import { TextField } from "../../fields/TextField";
import { NumberField } from "../../fields/NumberField";
import { ListField } from "../../fields/ListField";
import { FormattedTextBox } from "../views/nodes/FormattedTextBox";
import { CollectionDockingView } from "../views/collections/CollectionDockingView";
import { CollectionSchemaView } from "../views/collections/CollectionSchemaView";
import { ImageField } from "../../fields/ImageField";
import { ImageBox } from "../views/nodes/ImageBox";
import { CollectionFreeFormView } from "../views/collections/CollectionFreeFormView";
import { FIELD_ID } from "../../fields/Field";

interface DocumentOptions {
    x?: number;
    y?: number;
    width?: number;
    height?: number;
    title?: string;
}

export namespace Documents {
    function setupOptions(doc: Document, options: DocumentOptions): void {
        if (options.x) {
            doc.SetData(KeyStore.X, options.x, NumberField);
        }
        if (options.y) {
            doc.SetData(KeyStore.Y, options.y, NumberField);
        }
        if (options.width) {
            doc.SetData(KeyStore.Width, options.width, NumberField);
        }
        if (options.height) {
            doc.SetData(KeyStore.Height, options.height, NumberField);
        }
        if (options.title) {
            doc.SetData(KeyStore.Title, options.title, TextField);
        }
        doc.SetData(KeyStore.Scale, 1, NumberField);
        doc.SetData(KeyStore.PanX, 0, NumberField);
        doc.SetData(KeyStore.PanY, 0, NumberField);
    }

    let textProto: Document;
    function GetTextPrototype(): Document {
        if (!textProto) {
            textProto = new Document();
            textProto.Set(KeyStore.X, new NumberField(0));
            textProto.Set(KeyStore.Y, new NumberField(0));
            textProto.Set(KeyStore.Width, new NumberField(300));
            textProto.Set(KeyStore.Height, new NumberField(150));
            textProto.Set(KeyStore.Layout, new TextField(FormattedTextBox.LayoutString()));
            textProto.Set(KeyStore.LayoutKeys, new ListField([KeyStore.Data]));
        }
        return textProto;
    }

    export function TextDocument(options: DocumentOptions = {}): Document {
        let doc = GetTextPrototype().MakeDelegate();
        setupOptions(doc, options);
        // doc.SetField(KeyStore.Data, new RichTextField());
        return doc;
    }

    let schemaProto: Document;
    function GetSchemaPrototype(): Document {
        if (!schemaProto) {
            schemaProto = new Document();
            schemaProto.Set(KeyStore.X, new NumberField(0));
            schemaProto.Set(KeyStore.Y, new NumberField(0));
            schemaProto.Set(KeyStore.Width, new NumberField(300));
            schemaProto.Set(KeyStore.Height, new NumberField(150));
            schemaProto.Set(KeyStore.Layout, new TextField(CollectionSchemaView.LayoutString()));
            schemaProto.Set(KeyStore.LayoutKeys, new ListField([KeyStore.Data]));
        }
        return schemaProto;
    }

    export function SchemaDocument(documents: Array<Document>, options: DocumentOptions = {}): Document {
        let doc = GetSchemaPrototype().MakeDelegate();
        setupOptions(doc, options);
        doc.Set(KeyStore.Data, new ListField(documents));
        return doc;
    }


    let dockProto: Document;
    function GetDockPrototype(): Document {
        if (!dockProto) {
            dockProto = new Document();
            dockProto.Set(KeyStore.X, new NumberField(0));
            dockProto.Set(KeyStore.Y, new NumberField(0));
            dockProto.Set(KeyStore.Width, new NumberField(300));
            dockProto.Set(KeyStore.Height, new NumberField(150));
            dockProto.Set(KeyStore.Layout, new TextField(CollectionDockingView.LayoutString()));
            dockProto.Set(KeyStore.LayoutKeys, new ListField([KeyStore.Data]));
        }
        return dockProto;
    }

    export function DockDocument(documents: Array<Document>, options: DocumentOptions = {}): Document {
        let doc = GetDockPrototype().MakeDelegate();
        setupOptions(doc, options);
        doc.Set(KeyStore.Data, new ListField(documents));
        return doc;
    }


    let imageProtoId: FIELD_ID;
    function GetImagePrototype(): Document {
        if (imageProtoId === undefined) {
            let imageProto = new Document();
            imageProtoId = imageProto.Id;
            imageProto.Set(KeyStore.Title, new TextField("IMAGE PROTO"));
            imageProto.Set(KeyStore.X, new NumberField(0));
            imageProto.Set(KeyStore.Y, new NumberField(0));
            imageProto.Set(KeyStore.Width, new NumberField(300));
            imageProto.Set(KeyStore.Height, new NumberField(300));
            imageProto.Set(KeyStore.Layout, new TextField(ImageBox.LayoutString()));
            // imageProto.SetField(KeyStore.Layout, new TextField('<div style={"background-image: " + {Data}} />'));
            imageProto.Set(KeyStore.LayoutKeys, new ListField([KeyStore.Data]));
            // Server.AddDocument(imageProto);
            return imageProto;
        }
        return new Document();
        // return Server.GetField(imageProtoId) as Document;
    }

    export function ImageDocument(url: string, options: DocumentOptions = {}): Document {
        let doc = GetImagePrototype().MakeDelegate();
        setupOptions(doc, options);
        doc.Set(KeyStore.Data, new ImageField(new URL(url)));
        // Server.AddDocument(doc);
        // var sdoc = Server.GetField(doc.Id) as Document;
        return doc;
    }

    let collectionProto: Document;
    function GetCollectionPrototype(): Document {
        if (!collectionProto) {
            collectionProto = new Document();
            collectionProto.Set(KeyStore.X, new NumberField(0));
            collectionProto.Set(KeyStore.Y, new NumberField(0));
            collectionProto.Set(KeyStore.Scale, new NumberField(1));
            collectionProto.Set(KeyStore.PanX, new NumberField(0));
            collectionProto.Set(KeyStore.PanY, new NumberField(0));
            collectionProto.Set(KeyStore.Width, new NumberField(300));
            collectionProto.Set(KeyStore.Height, new NumberField(300));
            collectionProto.Set(KeyStore.Layout, new TextField(CollectionFreeFormView.LayoutString()));
            collectionProto.Set(KeyStore.LayoutKeys, new ListField([KeyStore.Data]));
        }
        return collectionProto;
    }

    export function CollectionDocument(documents: Array<Document>, options: DocumentOptions = {}): Document {
        let doc = GetCollectionPrototype().MakeDelegate();
        setupOptions(doc, options);
        doc.Set(KeyStore.Data, new ListField(documents));
        return doc;
    }
}