aboutsummaryrefslogtreecommitdiff
path: root/src/fields/ListField.ts
blob: e24099126d8b7628c6c1d15f9b0b331d0adf0af4 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { action, IArrayChange, IArraySplice, IObservableArray, observe, observable, Lambda } from "mobx";
import { Server } from "../client/Server";
import { UndoManager } from "../client/util/UndoManager";
import { Types } from "../server/Message";
import { BasicField } from "./BasicField";
import { Field, FieldId } from "./Field";
import { FieldMap } from "../client/SocketStub";
import { ScriptField } from "./ScriptField";

export class ListField<T extends Field> extends BasicField<T[]> {
    private _proxies: string[] = [];
    private _scriptIds: string[] = [];
    private scripts: ScriptField[] = [];

    constructor(data: T[] = [], scripts: ScriptField[] = [], id?: FieldId, save: boolean = true) {
        super(data, save, id);
        this.scripts = scripts;
        this.updateProxies();
        this._scriptIds = this.scripts.map(script => script.Id);
        if (save) {
            Server.UpdateField(this);
        }
        this.observeList();
    }

    private _processingServerUpdate: boolean = false;

    private observeDisposer: Lambda | undefined;
    private observeList(): void {
        if (this.observeDisposer) {
            this.observeDisposer();
        }
        this.observeDisposer = observe(this.Data as IObservableArray<T>, (change: IArrayChange<T> | IArraySplice<T>) => {
            const target = change.object;
            this.updateProxies();
            if (change.type === "splice") {
                this.runScripts(change.removed, false);
                UndoManager.AddEvent({
                    undo: () => target.splice(change.index, change.addedCount, ...change.removed),
                    redo: () => target.splice(change.index, change.removedCount, ...change.added)
                });
                this.runScripts(change.added, true);
            } else {
                this.runScripts([change.oldValue], false);
                UndoManager.AddEvent({
                    undo: () => target[change.index] = change.oldValue,
                    redo: () => target[change.index] = change.newValue
                });
                this.runScripts([change.newValue], true);
            }
            if (!this._processingServerUpdate) {
                Server.UpdateField(this);
            }
        });
    }

    private runScripts(fields: T[], added: boolean) {
        for (const script of this.scripts) {
            this.runScript(fields, script, added);
        }
    }

    private runScript(fields: T[], script: ScriptField, added: boolean) {
        if (!this._processingServerUpdate) {
            for (const field of fields) {
                script.script.run({ field, added });
            }
        }
    }

    addScript(script: ScriptField) {
        this.scripts.push(script);
        this._scriptIds.push(script.Id);

        this.runScript(this.Data, script, true);
        UndoManager.AddEvent({
            undo: () => this.removeScript(script),
            redo: () => this.addScript(script),
        });
        Server.UpdateField(this);
    }

    removeScript(script: ScriptField) {
        const index = this.scripts.indexOf(script);
        if (index === -1) {
            return;
        }
        this.scripts.splice(index, 1);
        this._scriptIds.splice(index, 1);
        UndoManager.AddEvent({
            undo: () => this.addScript(script),
            redo: () => this.removeScript(script),
        });
        this.runScript(this.Data, script, false);
        Server.UpdateField(this);
    }

    protected setData(value: T[]) {
        this.runScripts(this.data, false);

        this.data = observable(value);
        this.updateProxies();
        this.observeList();
        this.runScripts(this.data, true);
    }

    private updateProxies() {
        this._proxies = this.Data.map(field => field.Id);
    }

    private arraysEqual(a: any[], b: any[]) {
        if (a === b) return true;
        if (a === null || b === null) return false;
        if (a.length !== b.length) return false;

        // If you don't care about the order of the elements inside
        // the array, you should sort both arrays here.
        // Please note that calling sort on an array will modify that array.
        // you might want to clone your array first.

        for (var i = 0; i < a.length; ++i) {
            if (a[i] !== b[i]) return false;
        }
        return true;
    }

    init(callback: (field: Field) => any) {
        const fieldsPromise = Server.GetFields(this._proxies).then(action((fields: FieldMap) => {
            if (!this.arraysEqual(this._proxies, this.data.map(field => field.Id))) {
                var dataids = this.data.map(d => d.Id);
                var proxies = this._proxies.map(p => p);
                var added = this.data.length < this._proxies.length;
                var deleted = this.data.length > this._proxies.length;
                for (let i = 0; i < dataids.length && added; i++) {
                    added = proxies.indexOf(dataids[i]) !== -1;
                }
                for (let i = 0; i < this._proxies.length && deleted; i++) {
                    deleted = dataids.indexOf(proxies[i]) !== -1;
                }

                this._processingServerUpdate = true;
                for (let i = 0; i < proxies.length && added; i++) {
                    if (dataids.indexOf(proxies[i]) === -1) {
                        this.Data.splice(i, 0, fields[proxies[i]] as T);
                    }
                }
                for (let i = dataids.length - 1; i >= 0 && deleted; i--) {
                    if (proxies.indexOf(dataids[i]) === -1) {
                        this.Data.splice(i, 1);
                    }
                }
                if (!added && !deleted) {// otherwise, just rebuild the whole list
                    this.setData(proxies.map(id => fields[id] as T));
                }
                this._processingServerUpdate = false;
            }
        }));

        const scriptsPromise = Server.GetFields(this._scriptIds).then((fields: FieldMap) => {
            this.scripts = this._scriptIds.map(id => fields[id] as ScriptField);
        });

        Promise.all([fieldsPromise, scriptsPromise]).then(() => callback(this));
    }

    ToScriptString(): string {
        return "new ListField([" + this.Data.map(field => field.ToScriptString()).join(", ") + "])";
    }

    Copy(): Field {
        return new ListField<T>(this.Data);
    }


    UpdateFromServer(data: { fields: string[], scripts: string[] }) {
        this._proxies = data.fields;
        this._scriptIds = data.scripts;
    }
    ToJson() {
        return {
            type: Types.List,
            data: {
                fields: this._proxies,
                scripts: this._scriptIds,
            },
            id: this.Id
        };
    }

    static FromJson(id: string, data: { fields: string[], scripts: string[] }): ListField<Field> {
        let list = new ListField([], [], id, false);
        list._proxies = data.fields;
        list._scriptIds = data.scripts;
        return list;
    }
}