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

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.PDF:
                return new PDFField(new URL(data), id, false)
            case Types.List:
                return ListField.FromJson(id, data)
            case Types.Ink:
                return InkField.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");
        }
    }
}