From 9b942166ca6b09ce4f310928c3daf8503a63ee5c Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Thu, 7 Feb 2019 23:33:44 -0500 Subject: Restored Opt and made a new FieldValue type to replace old Opt --- src/fields/Document.ts | 14 +++++++------- src/fields/DocumentReference.ts | 11 +++++++---- src/fields/Field.ts | 13 +++++++------ src/fields/ImageField.ts | 4 ++++ src/fields/Key.ts | 3 +++ src/fields/ListField.ts | 4 ++++ src/fields/RichTextField.ts | 4 ++++ src/fields/TextField.ts | 4 ++++ 8 files changed, 40 insertions(+), 17 deletions(-) (limited to 'src/fields') diff --git a/src/fields/Document.ts b/src/fields/Document.ts index 53138eda9..e482df622 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -1,4 +1,4 @@ -import { Field, Cast, Opt, FieldWaiting, FIELD_ID, DOC_ID } from "./Field" +import { Field, Cast, Opt, FieldWaiting, FIELD_ID, DOC_ID, FieldValue } from "./Field" import { Key, KeyStore } from "./Key" import { NumberField } from "./NumberField"; import { ObservableMap, computed, action, observable } from "mobx"; @@ -16,8 +16,8 @@ export class Document extends Field { return this.GetText(KeyStore.Title, ""); } - Get(key: Key, ignoreProto: boolean = false): Opt { - let field: Opt; + Get(key: Key, ignoreProto: boolean = false): FieldValue { + let field: FieldValue; if (ignoreProto) { if (this.fields.has(key)) { field = this.fields.get(key); @@ -25,7 +25,7 @@ export class Document extends Field { field = Server.GetDocumentField(this, key); } } else { - let doc: Opt = this; + let doc: FieldValue = this; while (doc && doc != FieldWaiting && field != FieldWaiting) { if (!doc.fields.has(key)) { if (doc._proxies.has(key)) { @@ -46,7 +46,7 @@ export class Document extends Field { return field; } - GetT(key: Key, ctor: { new(...args: any[]): T }, ignoreProto: boolean = false): Opt { + GetT(key: Key, ctor: { new(...args: any[]): T }, ignoreProto: boolean = false): FieldValue { var getfield = this.Get(key, ignoreProto); if (getfield != FieldWaiting) { return Cast(getfield, ctor); @@ -119,13 +119,13 @@ export class Document extends Field { this.SetData(key, value, NumberField, replaceWrongType); } - GetPrototype(): Opt { + GetPrototype(): FieldValue { return this.GetT(KeyStore.Prototype, Document, true); } GetAllPrototypes(): Document[] { let protos: Document[] = []; - let doc: Opt = this; + let doc: FieldValue = this; while (doc && doc != FieldWaiting) { protos.push(doc); doc = doc.GetPrototype(); diff --git a/src/fields/DocumentReference.ts b/src/fields/DocumentReference.ts index 10dac9f92..983b162a3 100644 --- a/src/fields/DocumentReference.ts +++ b/src/fields/DocumentReference.ts @@ -1,4 +1,4 @@ -import { Field, Opt } from "./Field"; +import { Field, Opt, FieldValue } from "./Field"; import { Document } from "./Document"; import { Key } from "./Key"; @@ -15,12 +15,12 @@ export class DocumentReference extends Field { super(); } - Dereference(): Opt { + Dereference(): FieldValue { return this.document.Get(this.key); } - DereferenceToRoot(): Opt { - let field: Opt = this; + DereferenceToRoot(): FieldValue { + let field: FieldValue = this; while (field instanceof DocumentReference) { field = field.Dereference(); } @@ -37,5 +37,8 @@ export class DocumentReference extends Field { throw new Error("Method not implemented."); } + ToScriptString(): string { + return ""; + } } \ No newline at end of file diff --git a/src/fields/Field.ts b/src/fields/Field.ts index 20d8bf5ed..2bfc9d1da 100644 --- a/src/fields/Field.ts +++ b/src/fields/Field.ts @@ -1,7 +1,7 @@ import { Utils } from "../Utils"; -export function Cast(field: Opt, ctor: { new(): T }): Opt { +export function Cast(field: FieldValue, ctor: { new(): T }): Opt { if (field) { if (ctor && field instanceof ctor) { return field; @@ -14,7 +14,8 @@ export let FieldWaiting: FIELD_WAITING = ""; export type FIELD_WAITING = ""; export type FIELD_ID = string | undefined; export type DOC_ID = FIELD_ID; -export type Opt = T | undefined | FIELD_WAITING; +export type Opt = T | undefined; +export type FieldValue = Opt | FIELD_WAITING; export abstract class Field { //FieldUpdated: TypedEvent> = new TypedEvent>(); @@ -28,18 +29,18 @@ export abstract class Field { this.id = id || Utils.GenerateGuid(); } - Dereference(): Opt { + Dereference(): FieldValue { return this; } - DereferenceToRoot(): Opt { + DereferenceToRoot(): FieldValue { return this; } - DereferenceT(ctor: { new(): T }): Opt { + DereferenceT(ctor: { new(): T }): FieldValue { return Cast(this.Dereference(), ctor); } - DereferenceToRootT(ctor: { new(): T }): Opt { + DereferenceToRootT(ctor: { new(): T }): FieldValue { return Cast(this.DereferenceToRoot(), ctor); } diff --git a/src/fields/ImageField.ts b/src/fields/ImageField.ts index bc2e7cdf4..63baf815b 100644 --- a/src/fields/ImageField.ts +++ b/src/fields/ImageField.ts @@ -10,6 +10,10 @@ export class ImageField extends BasicField { return this.Data.href; } + ToScriptString(): string { + return `new ImageField(${this.Data})`; + } + Copy(): Field { return new ImageField(this.Data); } diff --git a/src/fields/Key.ts b/src/fields/Key.ts index 5cd43f55e..993102613 100644 --- a/src/fields/Key.ts +++ b/src/fields/Key.ts @@ -27,6 +27,9 @@ export class Key extends Field { return this; } + ToScriptString(): string { + return name; + } } diff --git a/src/fields/ListField.ts b/src/fields/ListField.ts index 8607ebe43..8843338c1 100644 --- a/src/fields/ListField.ts +++ b/src/fields/ListField.ts @@ -6,6 +6,10 @@ export class ListField extends BasicField { super(data.slice()); } + ToScriptString(): string { + return "new ListField([" + this.Data.map(field => field.ToScriptString()).join(", ") + "])"; + } + Copy(): Field { return new ListField(this.Data); } diff --git a/src/fields/RichTextField.ts b/src/fields/RichTextField.ts index 24c7472d8..4a77c669c 100644 --- a/src/fields/RichTextField.ts +++ b/src/fields/RichTextField.ts @@ -5,6 +5,10 @@ export class RichTextField extends BasicField { super(data); } + ToScriptString(): string { + return `new RichTextField(${this.Data})`; + } + Copy() { return new RichTextField(this.Data); } diff --git a/src/fields/TextField.ts b/src/fields/TextField.ts index 95825d2ae..11d2ed7cd 100644 --- a/src/fields/TextField.ts +++ b/src/fields/TextField.ts @@ -5,6 +5,10 @@ export class TextField extends BasicField { super(data); } + ToScriptString(): string { + return `new TextField("${this.Data}")`; + } + Copy() { return new TextField(this.Data); } -- cgit v1.2.3-70-g09d2