aboutsummaryrefslogtreecommitdiff
path: root/src/fields/ListField.ts
diff options
context:
space:
mode:
authorab <abdullah_ahmed@brown.edu>2019-03-09 15:59:36 -0500
committerab <abdullah_ahmed@brown.edu>2019-03-09 15:59:36 -0500
commit30b082a9d94465ae24d9c392843518e8df2c9d66 (patch)
tree01c464b96defe06b2c99e8e298b9287901d4d9b0 /src/fields/ListField.ts
parent12d20ab1b9b843d816bde445d9b67b3aa6abc6ae (diff)
parent96eede5f7d1706a3f7ac6ee02a85bb3da217f467 (diff)
merging
Diffstat (limited to 'src/fields/ListField.ts')
-rw-r--r--src/fields/ListField.ts112
1 files changed, 109 insertions, 3 deletions
diff --git a/src/fields/ListField.ts b/src/fields/ListField.ts
index 8843338c1..ce32da0a6 100644
--- a/src/fields/ListField.ts
+++ b/src/fields/ListField.ts
@@ -1,9 +1,101 @@
-import { Field } from "./Field";
+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";
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);
+ }
+ this.observeList();
+ }
+
+ private observeDisposer: Lambda | undefined;
+ private observeList(): void {
+ this.observeDisposer = observe(this.Data as IObservableArray<T>, (change: IArrayChange<T> | IArraySplice<T>) => {
+ this.updateProxies()
+ if (change.type == "splice") {
+ UndoManager.AddEvent({
+ undo: () => this.Data.splice(change.index, change.addedCount, ...change.removed),
+ redo: () => this.Data.splice(change.index, change.removedCount, ...change.added)
+ })
+ } else {
+ UndoManager.AddEvent({
+ undo: () => this.Data[change.index] = change.oldValue,
+ redo: () => this.Data[change.index] = change.newValue
+ })
+ }
+ Server.UpdateField(this);
+ });
+ }
+
+ protected setData(value: T[]) {
+ if (this.observeDisposer) {
+ this.observeDisposer()
+ }
+ this.data = observable(value);
+ this.updateProxies();
+ this.observeList();
+ }
+
+ 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))) {
+ var dataids = this.data.map(d => d.Id);
+ var added = this.data.length == this._proxies.length - 1;
+ var deleted = this.data.length > this._proxies.length;
+ for (let i = 0; i < dataids.length && added; i++)
+ added = this._proxies.indexOf(dataids[i]) != -1;
+ for (let i = 0; i < this._proxies.length && deleted; i++)
+ deleted = dataids.indexOf(this._proxies[i]) != -1;
+ if (added) { // if only 1 items was added
+ for (let i = 0; i < this._proxies.length; i++)
+ if (dataids.indexOf(this._proxies[i]) === -1)
+ this.Data.splice(i, 0, fields[this._proxies[i]] as T);
+ } else if (deleted) { // if only items were deleted
+ for (let i = this.data.length - 1; i >= 0; i--) {
+ if (this._proxies.indexOf(this.data[i].Id) === -1) {
+ this.Data.splice(i, 1);
+ }
+ }
+ } else // otherwise, just rebuild the whole list
+ this.data = this._proxies.map(id => fields[id] as T)
+ observe(this.Data, () => {
+ this.updateProxies()
+ Server.UpdateField(this);
+ })
+ }
+ callback(this);
+ }))
}
ToScriptString(): string {
@@ -13,4 +105,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