diff options
Diffstat (limited to 'src/fields/Document.ts')
-rw-r--r-- | src/fields/Document.ts | 71 |
1 files changed, 40 insertions, 31 deletions
diff --git a/src/fields/Document.ts b/src/fields/Document.ts index 742149a03..3d74c047c 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -1,17 +1,19 @@ -import { Field, Cast, Opt } from "./Field" +import { Field, Cast, Opt, FieldWaiting, FIELD_ID, DOC_ID } from "./Field" import { Key, KeyStore } from "./Key" import { NumberField } from "./NumberField"; -import { ObservableMap, computed } from "mobx"; +import { ObservableMap, computed, action, observable } from "mobx"; import { TextField } from "./TextField"; import { ListField } from "./ListField"; +import { findDOMNode } from "react-dom"; +import { Server } from "../Server"; export class Document extends Field { - private fields: ObservableMap<Key, Field> = new ObservableMap(); + public fields: ObservableMap<Key, Opt<Field>> = new ObservableMap(); + public _proxies: ObservableMap<Key, FIELD_ID> = new ObservableMap(); - static _untitledDocName = "<untitled>"; @computed public get Title() { - return this.GetData(KeyStore.Title, TextField, Document._untitledDocName); + return this.GetText(KeyStore.Title, "<untitled>"); } Get(key: Key, ignoreProto: boolean = false): Opt<Field> { @@ -19,15 +21,25 @@ export class Document extends Field { if (ignoreProto) { if (this.fields.has(key)) { field = this.fields.get(key); + } else if (this._proxies.has(key)) { + field = Server.GetDocumentField(this, key); } } else { let doc: Opt<Document> = this; - while (doc && !(doc.fields.has(key))) { - doc = doc.GetPrototype(); - } - - if (doc) { - field = doc.fields.get(key); + while (doc && doc != FieldWaiting && field != FieldWaiting) { + if (!doc.fields.has(key)) { + if (doc._proxies.has(key)) { + field = Server.GetDocumentField(doc, key); + break; + } + if ((doc.fields.has(KeyStore.Prototype) || doc._proxies.has(KeyStore.Prototype))) { + doc = doc.GetPrototype(); + } else + break; + } else { + field = doc.fields.get(key); + break; + } } } @@ -35,12 +47,16 @@ export class Document extends Field { } GetT<T extends Field = Field>(key: Key, ctor: { new(...args: any[]): T }, ignoreProto: boolean = false): Opt<T> { - return Cast(this.Get(key, ignoreProto), ctor); + var getfield = this.Get(key, ignoreProto); + if (getfield != FieldWaiting) { + return Cast(getfield, ctor); + } + return FieldWaiting; } GetOrCreate<T extends Field>(key: Key, ctor: { new(): T }, ignoreProto: boolean = false): T { const field = this.GetT(key, ctor, ignoreProto); - if (field) { + if (field && field != FieldWaiting) { return field; } const newField = new ctor(); @@ -66,46 +82,39 @@ export class Document extends Field { return this.GetData<T[], ListField<T>>(key, ListField, defaultVal) } + @action Set(key: Key, field: Field | undefined): void { if (field) { this.fields.set(key, field); + Server.AddDocumentField(this, key, field); } else { this.fields.delete(key); + Server.DeleteDocumentField(this, key); } } + @action SetData<T, U extends Field & { Data: T }>(key: Key, value: T, ctor: { new(): U }, replaceWrongType = true) { + let field = this.Get(key, true); + //if (field != WAITING) { // do we want to wait for the field to come back from the server to set it, or do we overwrite? if (field instanceof ctor) { field.Data = value; + Server.SetFieldValue(field, value); } else if (!field || replaceWrongType) { let newField = new ctor(); newField.Data = value; this.Set(key, newField); } + //} } - SetVal<T extends Field>(key: Key, value: any, ctor: { new(): T }, replaceWrongType = true): boolean { - let field = this.Get(key, true); - if (field != null) { - return field.TrySetValue(value); - } else if (field && replaceWrongType) { - field = new ctor(); - if (field.TrySetValue(value)) { - this.Set(key, field); - return true; - } else { - return false; - } - } else { - return false; - } - } - + @action SetText(key: Key, value: string, replaceWrongType = true) { this.SetData(key, value, TextField, replaceWrongType); } + @action SetNumber(key: Key, value: number, replaceWrongType = true) { this.SetData(key, value, NumberField, replaceWrongType); } @@ -117,7 +126,7 @@ export class Document extends Field { GetAllPrototypes(): Document[] { let protos: Document[] = []; let doc: Opt<Document> = this; - while (doc != null) { + while (doc && doc != FieldWaiting) { protos.push(doc); doc = doc.GetPrototype(); } |