diff options
author | madelinegr <laura_wilson@brown.edu> | 2019-02-18 19:38:08 -0500 |
---|---|---|
committer | madelinegr <laura_wilson@brown.edu> | 2019-02-18 19:38:08 -0500 |
commit | 6fd2cec91efd6672a70e15a786954f92c1d23416 (patch) | |
tree | 0cbf4c1dd399bd041e05eb4c911a642547f673f9 /src/fields/ListField.ts | |
parent | 41ba832136aef2b7e6a5034486757aa4b3047cf9 (diff) | |
parent | 70a8b4ab8075af4d06efb04c083b3bf11636373e (diff) |
Merge remote-tracking branch 'origin/server_database_merge' into authentication
Diffstat (limited to 'src/fields/ListField.ts')
-rw-r--r-- | src/fields/ListField.ts | 65 |
1 files changed, 59 insertions, 6 deletions
diff --git a/src/fields/ListField.ts b/src/fields/ListField.ts index 9a26cb142..1357dc445 100644 --- a/src/fields/ListField.ts +++ b/src/fields/ListField.ts @@ -1,11 +1,58 @@ -import { Field, FIELD_ID } from "./Field"; +import { Field, FIELD_ID, FieldValue, Opt } from "./Field"; import { BasicField } from "./BasicField"; import { Types } from "../server/Message"; -import { ObjectId } from "bson"; +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[] = [], id: FIELD_ID = undefined) { - super(data.slice(), id); + private _proxies: string[] = [] + constructor(data: T[] = [], id: FIELD_ID = undefined, 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 { @@ -16,11 +63,17 @@ export class ListField<T extends Field> extends BasicField<T[]> { return new ListField<T>(this.Data); } - ToJson(): { type: Types, data: T[], _id: String } { + ToJson(): { type: Types, data: string[], _id: string } { return { type: Types.List, - data: this.Data, + 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 |