aboutsummaryrefslogtreecommitdiff
path: root/src/fields
diff options
context:
space:
mode:
authormadelinegr <mgriswold99@gmail.com>2019-02-12 18:55:11 -0500
committermadelinegr <mgriswold99@gmail.com>2019-02-12 18:55:11 -0500
commit1d667d19f5402dc9f9069e0a57008dac96f6de2a (patch)
tree59572ebc84ae0dea9780c96a6d43a811a21fed10 /src/fields
parent7a93f60c9529e5d175e617fc7c07145a9b33e572 (diff)
set up web box classes
Diffstat (limited to 'src/fields')
-rw-r--r--src/fields/BasicField 2.ts38
-rw-r--r--src/fields/Document 2.ts93
-rw-r--r--src/fields/DocumentReference 2.ts46
-rw-r--r--src/fields/Field 2.ts54
-rw-r--r--src/fields/FieldUpdatedArgs 2.ts27
-rw-r--r--src/fields/Key 2.ts45
-rw-r--r--src/fields/ListField 2.ts21
-rw-r--r--src/fields/NumberField 2.ts13
-rw-r--r--src/fields/TextField 2.ts13
-rw-r--r--src/fields/WebField.ts21
10 files changed, 371 insertions, 0 deletions
diff --git a/src/fields/BasicField 2.ts b/src/fields/BasicField 2.ts
new file mode 100644
index 000000000..437024d07
--- /dev/null
+++ b/src/fields/BasicField 2.ts
@@ -0,0 +1,38 @@
+import { Field } from "./Field"
+import { observable, computed, action } from "mobx";
+
+export abstract class BasicField<T> extends Field {
+ constructor(data: T) {
+ super();
+
+ this.data = data;
+ }
+
+ @observable
+ private data:T;
+
+ @computed
+ get Data(): T {
+ return this.data;
+ }
+
+ set Data(value: T) {
+ if(this.data === value) {
+ return;
+ }
+ this.data = value;
+ }
+
+ @action
+ TrySetValue(value: any): boolean {
+ if (typeof value == typeof this.data) {
+ this.Data = value;
+ return true;
+ }
+ return false;
+ }
+
+ GetValue(): any {
+ return this.Data;
+ }
+}
diff --git a/src/fields/Document 2.ts b/src/fields/Document 2.ts
new file mode 100644
index 000000000..0bba9c21e
--- /dev/null
+++ b/src/fields/Document 2.ts
@@ -0,0 +1,93 @@
+import { Field, Cast, Opt } from "./Field"
+import { Key, KeyStore } from "./Key"
+import { ObservableMap } from "mobx";
+
+export class Document extends Field {
+ private fields: ObservableMap<Key, Field> = new ObservableMap();
+
+ GetField(key: Key, ignoreProto: boolean = false): Opt<Field> {
+ let field: Opt<Field>;
+ if (ignoreProto) {
+ if (this.fields.has(key)) {
+ field = this.fields.get(key);
+ }
+ } else {
+ let doc: Opt<Document> = this;
+ while (doc && !(doc.fields.has(key))) {
+ doc = doc.GetPrototype();
+ }
+
+ if (doc) {
+ field = doc.fields.get(key);
+ }
+ }
+
+ return field;
+ }
+
+ GetFieldT<T extends Field = Field>(key: Key, ctor: { new(): T }, ignoreProto?: boolean): Opt<T> {
+ return Cast(this.GetField(key, ignoreProto), ctor);
+ }
+
+ GetFieldValue<T, U extends { Data: T }>(key: Key, ctor: { new(): U }, defaultVal: T): T {
+ let val = this.GetField(key);
+ return (val && val instanceof ctor) ? val.Data : defaultVal;
+ }
+
+ SetField(key: Key, field: Opt<Field>): void {
+ if (field) {
+ this.fields.set(key, field);
+ } else {
+ this.fields.delete(key);
+ }
+ }
+
+ SetFieldValue<T extends Field>(key: Key, value: any, ctor: { new(): T }): boolean {
+ let field = this.GetField(key, true);
+ if (field != null) {
+ return field.TrySetValue(value);
+ } else {
+ field = new ctor();
+ if (field.TrySetValue(value)) {
+ this.SetField(key, field);
+ return true;
+ } else {
+ return false;
+ }
+ }
+ }
+
+ GetPrototype(): Opt<Document> {
+ return this.GetFieldT(KeyStore.Prototype, Document, true);
+ }
+
+ GetAllPrototypes(): Document[] {
+ let protos: Document[] = [];
+ let doc: Opt<Document> = this;
+ while (doc != null) {
+ protos.push(doc);
+ doc = doc.GetPrototype();
+ }
+ return protos;
+ }
+
+ MakeDelegate(): Document {
+ let delegate = new Document();
+
+ delegate.SetField(KeyStore.Prototype, this);
+
+ return delegate;
+ }
+
+ TrySetValue(value: any): boolean {
+ throw new Error("Method not implemented.");
+ }
+ GetValue() {
+ throw new Error("Method not implemented.");
+ }
+ Copy(): Field {
+ throw new Error("Method not implemented.");
+ }
+
+
+} \ No newline at end of file
diff --git a/src/fields/DocumentReference 2.ts b/src/fields/DocumentReference 2.ts
new file mode 100644
index 000000000..936067bd2
--- /dev/null
+++ b/src/fields/DocumentReference 2.ts
@@ -0,0 +1,46 @@
+import { Field, Opt } from "./Field";
+import { Document } from "./Document";
+import { Key } from "./Key";
+import { DocumentUpdatedArgs } from "./FieldUpdatedArgs";
+
+export class DocumentReference extends Field {
+ get Key(): Key{
+ return this.key;
+ }
+
+ get Document(): Document {
+ return this.document;
+ }
+
+ constructor(private document: Document, private key: Key) {
+ super();
+ }
+
+ private DocFieldUpdated(args: DocumentUpdatedArgs):void{
+ // this.FieldUpdated.emit(args.fieldArgs);
+ }
+
+ Dereference() : Opt<Field> {
+ return this.document.GetField(this.key);
+ }
+
+ DereferenceToRoot(): Opt<Field> {
+ let field: Opt<Field> = this;
+ while (field instanceof DocumentReference) {
+ field = field.Dereference();
+ }
+ return field;
+ }
+
+ TrySetValue(value: any): boolean {
+ throw new Error("Method not implemented.");
+ }
+ GetValue() {
+ throw new Error("Method not implemented.");
+ }
+ Copy(): Field {
+ throw new Error("Method not implemented.");
+ }
+
+
+} \ No newline at end of file
diff --git a/src/fields/Field 2.ts b/src/fields/Field 2.ts
new file mode 100644
index 000000000..46f92f203
--- /dev/null
+++ b/src/fields/Field 2.ts
@@ -0,0 +1,54 @@
+import { TypedEvent } from "../util/TypedEvent";
+import { FieldUpdatedArgs } from "./FieldUpdatedArgs";
+import { DocumentReference } from "./DocumentReference";
+import { Utils } from "../Utils";
+
+export function Cast<T extends Field>(field: Opt<Field>, ctor: { new(): T }): Opt<T> {
+ if (field) {
+ if (ctor && field instanceof ctor) {
+ return field;
+ }
+ }
+ return undefined;
+}
+
+export type Opt<T> = T | undefined;
+
+export abstract class Field {
+ //FieldUpdated: TypedEvent<Opt<FieldUpdatedArgs>> = new TypedEvent<Opt<FieldUpdatedArgs>>();
+
+ private id: string;
+ get Id(): string {
+ return this.id;
+ }
+
+ constructor(id: Opt<string> = undefined) {
+ this.id = id || Utils.GenerateGuid();
+ }
+
+ Dereference(): Opt<Field> {
+ return this;
+ }
+ DereferenceToRoot(): Opt<Field> {
+ return this;
+ }
+
+ DereferenceT<T extends Field = Field>(ctor: { new(): T }): Opt<T> {
+ return Cast(this.Dereference(), ctor);
+ }
+
+ DereferenceToRootT<T extends Field = Field>(ctor: { new(): T }): Opt<T> {
+ return Cast(this.DereferenceToRoot(), ctor);
+ }
+
+ Equals(other: Field): boolean {
+ return this.id === other.id;
+ }
+
+ abstract TrySetValue(value: any): boolean;
+
+ abstract GetValue(): any;
+
+ abstract Copy(): Field;
+
+} \ No newline at end of file
diff --git a/src/fields/FieldUpdatedArgs 2.ts b/src/fields/FieldUpdatedArgs 2.ts
new file mode 100644
index 000000000..23ccf2a5a
--- /dev/null
+++ b/src/fields/FieldUpdatedArgs 2.ts
@@ -0,0 +1,27 @@
+import { Field, Opt } from "./Field";
+import { Document } from "./Document";
+import { Key } from "./Key";
+
+export enum FieldUpdatedAction {
+ Add,
+ Remove,
+ Replace,
+ Update
+}
+
+export interface FieldUpdatedArgs {
+ field: Field;
+ action: FieldUpdatedAction;
+}
+
+export interface DocumentUpdatedArgs {
+ field: Document;
+ key: Key;
+
+ oldValue: Opt<Field>;
+ newValue: Opt<Field>;
+
+ fieldArgs?: FieldUpdatedArgs;
+
+ action: FieldUpdatedAction;
+}
diff --git a/src/fields/Key 2.ts b/src/fields/Key 2.ts
new file mode 100644
index 000000000..db30f545d
--- /dev/null
+++ b/src/fields/Key 2.ts
@@ -0,0 +1,45 @@
+import { Field } from "./Field"
+import { Utils } from "../Utils";
+import { observable } from "mobx";
+
+export class Key extends Field {
+ private name:string;
+
+ get Name():string {
+ return this.name;
+ }
+
+ constructor(name:string){
+ super(Utils.GenerateDeterministicGuid(name));
+
+ this.name = name;
+ }
+
+ TrySetValue(value: any): boolean {
+ throw new Error("Method not implemented.");
+ }
+
+ GetValue() {
+ return this.Name;
+ }
+
+ Copy(): Field {
+ return this;
+ }
+
+
+}
+
+export namespace KeyStore {
+ export let Prototype = new Key("Prototype");
+ export let X = new Key("X");
+ export let Y = new Key("Y");
+ export let PanX = new Key("PanX");
+ export let PanY = new Key("PanY");
+ export let Width = new Key("Width");
+ export let Height = new Key("Height");
+ export let Data = new Key("Data");
+ export let Layout = new Key("Layout");
+ export let LayoutKeys = new Key("LayoutKeys");
+ export let LayoutFields = new Key("LayoutFields");
+} \ No newline at end of file
diff --git a/src/fields/ListField 2.ts b/src/fields/ListField 2.ts
new file mode 100644
index 000000000..dd96ea8c8
--- /dev/null
+++ b/src/fields/ListField 2.ts
@@ -0,0 +1,21 @@
+import { Field } from "./Field";
+import { BasicField } from "./BasicField";
+
+export class ListField<T extends Field> extends BasicField<T[]> {
+ constructor(data: T[] = []) {
+ super(data.slice());
+ }
+
+ Get(index:number) : T{
+ return this.Data[index];
+ }
+
+ Set(index:number, value:T):void {
+ this.Data[index] = value;
+ }
+
+ Copy(): Field {
+ return new ListField<T>(this.Data);
+ }
+
+} \ No newline at end of file
diff --git a/src/fields/NumberField 2.ts b/src/fields/NumberField 2.ts
new file mode 100644
index 000000000..cbe15f987
--- /dev/null
+++ b/src/fields/NumberField 2.ts
@@ -0,0 +1,13 @@
+import { BasicField } from "./BasicField"
+
+export class NumberField extends BasicField<number> {
+ constructor(data: number = 0) {
+ super(data);
+ }
+
+ Copy() {
+ return new NumberField(this.Data);
+ }
+
+
+} \ No newline at end of file
diff --git a/src/fields/TextField 2.ts b/src/fields/TextField 2.ts
new file mode 100644
index 000000000..a7c221788
--- /dev/null
+++ b/src/fields/TextField 2.ts
@@ -0,0 +1,13 @@
+import { BasicField } from "./BasicField"
+
+export class TextField extends BasicField<string> {
+ constructor(data: string = "") {
+ super(data);
+ }
+
+ Copy() {
+ return new TextField(this.Data);
+ }
+
+
+}
diff --git a/src/fields/WebField.ts b/src/fields/WebField.ts
new file mode 100644
index 000000000..88e6130e0
--- /dev/null
+++ b/src/fields/WebField.ts
@@ -0,0 +1,21 @@
+import { BasicField } from "./BasicField";
+import { Field } from "./Field";
+
+export class WebField extends BasicField<URL> {
+ constructor(data: URL | undefined = undefined) {
+ super(data == undefined ? new URL("https://cs.brown.edu/") : data);
+ }
+
+ toString(): string {
+ return this.Data.href;
+ }
+
+ ToScriptString(): string {
+ return `new WebField("${this.Data}")`;
+ }
+
+ Copy(): Field {
+ return new WebField(this.Data);
+ }
+
+} \ No newline at end of file