aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/ScriptManager.ts
blob: 87509f2ea8e5be4fc1a48d00cd01070b9e48fc5d (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
import { Doc, DocListCast } from '../../fields/Doc';
import { List } from '../../fields/List';
import { listSpec } from '../../fields/Schema';
import { Cast, StrCast } from '../../fields/Types';
import { Docs } from '../documents/Documents';
import { ScriptingGlobals } from './ScriptingGlobals';

export class ScriptManager {
    static _initialized = false;
    private static _instance: ScriptManager;
    public static get Instance(): ScriptManager {
        return this._instance || (this._instance = new this());
    }
    private constructor() {
        if (!ScriptManager._initialized) {
            ScriptManager._initialized = true;
            this.getAllScripts().forEach(scriptDoc => ScriptManager.addScriptToGlobals(scriptDoc));
        }
    }

    public get ScriptManagerDoc(): Doc | undefined {
        return Docs.Prototypes.MainScriptDocument();
    }
    public getAllScripts(): Doc[] {
        const sdoc = ScriptManager.Instance.ScriptManagerDoc;
        return sdoc ? DocListCast(sdoc.data) : [];
    }

    public addScript(scriptDoc: Doc): boolean {
        const scriptList = this.getAllScripts();
        scriptList.push(scriptDoc);
        if (ScriptManager.Instance.ScriptManagerDoc) {
            ScriptManager.Instance.ScriptManagerDoc.data = new List<Doc>(scriptList);
            ScriptManager.addScriptToGlobals(scriptDoc);
            return true;
        }
        return false;
    }

    public deleteScript(scriptDoc: Doc): boolean {
        if (scriptDoc.name) {
            ScriptingGlobals.removeGlobal(StrCast(scriptDoc.name));
        }
        const scriptList = this.getAllScripts();
        const index = scriptList.indexOf(scriptDoc);
        if (index > -1) {
            scriptList.splice(index, 1);
            if (ScriptManager.Instance.ScriptManagerDoc) {
                ScriptManager.Instance.ScriptManagerDoc.data = new List<Doc>(scriptList);
                return true;
            }
        }
        return false;
    }

    public static addScriptToGlobals(scriptDoc: Doc): void {
        ScriptingGlobals.removeGlobal(StrCast(scriptDoc.name));

        const params = Cast(scriptDoc['data-params'], listSpec('string'), []);
        const paramNames = params.reduce((o: string, p: string) => {
            if (params.indexOf(p) === params.length - 1) {
                o = o + p.split(':')[0].trim();
            } else {
                o = o + p.split(':')[0].trim() + ',';
            }
            return o;
        }, '' as string);

        const f = new Function(paramNames, StrCast(scriptDoc.script));

        Object.defineProperty(f, 'name', { value: StrCast(scriptDoc.name), writable: false });

        let parameters = '(';
        params.forEach((element: string, i: number) => {
            if (i === params.length - 1) {
                parameters = parameters + element + ')';
            } else {
                parameters = parameters + element + ', ';
            }
        });

        if (parameters === '(') {
            ScriptingGlobals.add(f, StrCast(scriptDoc.description));
        } else {
            ScriptingGlobals.add(f, StrCast(scriptDoc.description), parameters);
        }
    }
}