diff options
| author | yipstanley <stanley_yip@brown.edu> | 2019-02-10 22:12:04 -0500 |
|---|---|---|
| committer | yipstanley <stanley_yip@brown.edu> | 2019-02-10 22:12:04 -0500 |
| commit | 69f0b463a78c82aaf78ceb6a5162431424452311 (patch) | |
| tree | a54c7129f36d307420422b3babfa6da4d85db67e /src/fields | |
| parent | 2e930b98726a09e597106d43a6763dd36d038771 (diff) | |
| parent | 099c5823f05285fc5086c5a433658cf06dc4a04b (diff) | |
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web into server_implementation
Diffstat (limited to 'src/fields')
| -rw-r--r-- | src/fields/Document.ts | 20 | ||||
| -rw-r--r-- | src/fields/DocumentReference.ts | 11 | ||||
| -rw-r--r-- | src/fields/Field.ts | 16 | ||||
| -rw-r--r-- | src/fields/ImageField.ts | 6 | ||||
| -rw-r--r-- | src/fields/Key.ts | 3 | ||||
| -rw-r--r-- | src/fields/ListField.ts | 4 | ||||
| -rw-r--r-- | src/fields/NumberField.ts | 4 | ||||
| -rw-r--r-- | src/fields/RichTextField.ts | 4 | ||||
| -rw-r--r-- | src/fields/TextField.ts | 4 |
9 files changed, 52 insertions, 20 deletions
diff --git a/src/fields/Document.ts b/src/fields/Document.ts index 3d74c047c..6f9752a8e 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -1,11 +1,11 @@ -import { Field, Cast, Opt, FieldWaiting, FIELD_ID, DOC_ID } from "./Field" +import { Field, Cast, Opt, FieldWaiting, FIELD_ID, FieldValue } from "./Field" import { Key, KeyStore } from "./Key" import { NumberField } from "./NumberField"; import { ObservableMap, computed, action, observable } from "mobx"; import { TextField } from "./TextField"; import { ListField } from "./ListField"; import { findDOMNode } from "react-dom"; -import { Server } from "../Server"; +import { Server } from "../client/Server"; export class Document extends Field { public fields: ObservableMap<Key, Opt<Field>> = new ObservableMap(); @@ -16,8 +16,8 @@ export class Document extends Field { return this.GetText(KeyStore.Title, "<untitled>"); } - Get(key: Key, ignoreProto: boolean = false): Opt<Field> { - let field: Opt<Field>; + Get(key: Key, ignoreProto: boolean = false): FieldValue<Field> { + let field: FieldValue<Field>; 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<Document> = this; + let doc: FieldValue<Document> = 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<T extends Field = Field>(key: Key, ctor: { new(...args: any[]): T }, ignoreProto: boolean = false): Opt<T> { + GetT<T extends Field = Field>(key: Key, ctor: { new(...args: any[]): T }, ignoreProto: boolean = false): FieldValue<T> { 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<Document> { + GetPrototype(): FieldValue<Document> { return this.GetT(KeyStore.Prototype, Document, true); } GetAllPrototypes(): Document[] { let protos: Document[] = []; - let doc: Opt<Document> = this; + let doc: FieldValue<Document> = this; while (doc && doc != FieldWaiting) { protos.push(doc); doc = doc.GetPrototype(); @@ -141,6 +141,10 @@ export class Document extends Field { return delegate; } + ToScriptString(): string { + return ""; + } + TrySetValue(value: any): boolean { throw new Error("Method not implemented."); } 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<Field> { + Dereference(): FieldValue<Field> { return this.document.Get(this.key); } - DereferenceToRoot(): Opt<Field> { - let field: Opt<Field> = this; + DereferenceToRoot(): FieldValue<Field> { + let field: FieldValue<Field> = 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 9880116c0..6adee9b61 100644 --- a/src/fields/Field.ts +++ b/src/fields/Field.ts @@ -1,7 +1,7 @@ import { Utils } from "../Utils"; -export function Cast<T extends Field>(field: Opt<Field>, ctor: { new(): T }): Opt<T> { +export function Cast<T extends Field>(field: FieldValue<Field>, ctor: { new(): T }): Opt<T> { if (field) { if (ctor && field instanceof ctor) { return field; @@ -13,8 +13,8 @@ export function Cast<T extends Field>(field: Opt<Field>, ctor: { new(): T }): Op export let FieldWaiting: FIELD_WAITING = "<Waiting>"; export type FIELD_WAITING = "<Waiting>"; export type FIELD_ID = string | undefined; -export type DOC_ID = FIELD_ID; -export type Opt<T> = T | undefined | FIELD_WAITING; +export type Opt<T> = T | undefined; +export type FieldValue<T> = Opt<T> | FIELD_WAITING; export abstract class Field { //FieldUpdated: TypedEvent<Opt<FieldUpdatedArgs>> = new TypedEvent<Opt<FieldUpdatedArgs>>(); @@ -28,18 +28,18 @@ export abstract class Field { this.id = id || Utils.GenerateGuid(); } - Dereference(): Opt<Field> { + Dereference(): FieldValue<Field> { return this; } - DereferenceToRoot(): Opt<Field> { + DereferenceToRoot(): FieldValue<Field> { return this; } - DereferenceT<T extends Field = Field>(ctor: { new(): T }): Opt<T> { + DereferenceT<T extends Field = Field>(ctor: { new(): T }): FieldValue<T> { return Cast(this.Dereference(), ctor); } - DereferenceToRootT<T extends Field = Field>(ctor: { new(): T }): Opt<T> { + DereferenceToRootT<T extends Field = Field>(ctor: { new(): T }): FieldValue<T> { return Cast(this.DereferenceToRoot(), ctor); } @@ -47,6 +47,8 @@ export abstract class Field { return this.id === other.id; } + abstract ToScriptString(): string; + abstract TrySetValue(value: any): boolean; abstract GetValue(): any; diff --git a/src/fields/ImageField.ts b/src/fields/ImageField.ts index bc2e7cdf4..d82260f54 100644 --- a/src/fields/ImageField.ts +++ b/src/fields/ImageField.ts @@ -3,13 +3,17 @@ import { Field } from "./Field"; export class ImageField extends BasicField<URL> { constructor(data: URL | undefined = undefined) { - super(data == undefined ? new URL("http://cs.brown.edu/~bcz/face.gif") : data); + super(data == undefined ? new URL("http://cs.brown.edu/~bcz/bob_fettucine.jpg") : data); } toString(): string { 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<T extends Field> extends BasicField<T[]> { super(data.slice()); } + ToScriptString(): string { + return "new ListField([" + this.Data.map(field => field.ToScriptString()).join(", ") + "])"; + } + Copy(): Field { return new ListField<T>(this.Data); } diff --git a/src/fields/NumberField.ts b/src/fields/NumberField.ts index c3444f644..03926d696 100644 --- a/src/fields/NumberField.ts +++ b/src/fields/NumberField.ts @@ -5,6 +5,10 @@ export class NumberField extends BasicField<number> { super(data); } + ToScriptString(): string { + return "new NumberField(this.Data)"; + } + Copy() { return new NumberField(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<string> { 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<string> { super(data); } + ToScriptString(): string { + return `new TextField("${this.Data}")`; + } + Copy() { return new TextField(this.Data); } |
