diff options
author | tschicke-brown <tyler_schicke@brown.edu> | 2019-02-19 21:58:18 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-19 21:58:18 -0500 |
commit | 4610f1cc4653aa3b8389a677b9a1bd94590752a1 (patch) | |
tree | 6fd1cfe02eaeaf1dc074a26cc4cde39c3313d1f3 /src/fields/ListField.ts | |
parent | 190d0225d7bf605aa53847eb7beda5de72e753a8 (diff) | |
parent | ef10a6bdf753f75cbc5b5d8b7dcc53c3d4c219ad (diff) |
Merge pull request #8 from browngraphicslab/server_database_merge
Server database merge
Diffstat (limited to 'src/fields/ListField.ts')
-rw-r--r-- | src/fields/ListField.ts | 69 |
1 files changed, 66 insertions, 3 deletions
diff --git a/src/fields/ListField.ts b/src/fields/ListField.ts index 8843338c1..2e192bf90 100644 --- a/src/fields/ListField.ts +++ b/src/fields/ListField.ts @@ -1,9 +1,58 @@ -import { Field } from "./Field"; +import { Field, FieldId, FieldValue, Opt } from "./Field"; import { BasicField } from "./BasicField"; +import { Types } from "../server/Message"; +import { observe, action } from "mobx"; +import { Server } from "../client/Server"; +import { ServerUtils } from "../server/ServerUtil"; export class ListField<T extends Field> extends BasicField<T[]> { - constructor(data: T[] = []) { - super(data.slice()); + private _proxies: string[] = [] + constructor(data: T[] = [], id?: FieldId, save: boolean = true) { + super(data, save, id); + this.updateProxies(); + if (save) { + Server.UpdateField(this); + } + observe(this.Data, () => { + this.updateProxies() + Server.UpdateField(this); + }) + } + + private updateProxies() { + this._proxies = this.Data.map(field => field.Id); + } + + UpdateFromServer(fields: string[]) { + this._proxies = fields; + } + 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) { + Server.GetFields(this._proxies, action((fields: { [index: string]: Field }) => { + if (!this.arraysEqual(this._proxies, this.Data.map(field => field.Id))) { + this.data = this._proxies.map(id => fields[id] as T) + observe(this.Data, () => { + this.updateProxies() + Server.UpdateField(this); + }) + } + callback(this); + })) } ToScriptString(): string { @@ -13,4 +62,18 @@ export class ListField<T extends Field> extends BasicField<T[]> { Copy(): Field { return new ListField<T>(this.Data); } + + ToJson(): { type: Types, data: string[], _id: string } { + return { + type: Types.List, + data: this._proxies, + _id: this.Id + } + } + + static FromJson(id: string, ids: string[]): ListField<Field> { + let list = new ListField([], id, false); + list._proxies = ids; + return list + } }
\ No newline at end of file |