aboutsummaryrefslogtreecommitdiff
path: root/src/server/ServerUtil.ts
blob: a53fb5d2baa123f211521e3d235fc3de80616e47 (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
import { Field } from './../fields/Field';
import { TextField } from './../fields/TextField';
import { NumberField } from './../fields/NumberField';
import { RichTextField } from './../fields/RichTextField';
import { Key } from './../fields/Key';
import { ImageField } from './../fields/ImageField';
import { ListField } from './../fields/ListField';
import { Document } from './../fields/Document';
import { Server } from './../client/Server';
import { Types } from './Message';
import { Utils } from '../Utils';
import { HtmlField } from '../fields/HtmlField';
import { WebField } from '../fields/WebField';

export class ServerUtils {
    public static FromJson(json: any): Field {
        let obj = json
        let data: any = obj.data
        let id: string = obj._id
        let type: Types = obj.type

        if (!(data !== undefined && id && type !== undefined)) {
            console.log("how did you manage to get an object that doesn't have a data or an id?")
            return new TextField("Something to fill the space", Utils.GenerateGuid());
        }

        switch (type) {
            case Types.Number:
                return new NumberField(data, id, false)
            case Types.Text:
                return new TextField(data, id, false)
            case Types.Html:
                return new HtmlField(data, id, false)
            case Types.Web:
                return new WebField(new URL(data), id, false)
            case Types.RichText:
                return new RichTextField(data, id, false)
            case Types.Key:
                return new Key(data, id, false)
            case Types.Image:
                return new ImageField(new URL(data), id, false)
            case Types.List:
                return ListField.FromJson(id, data)
            case Types.Document:
                let doc: Document = new Document(id, false)
                let fields: [string, string][] = data as [string, string][]
                fields.forEach(element => {
                    doc._proxies.set(element[0], element[1]);
                });
                return doc
            default:
                throw Error("Error, unrecognized field type received from server. If you just created a new field type, be sure to add it here");
        }
    }
}