aboutsummaryrefslogtreecommitdiff
path: root/src/fields/Document 2.ts
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/Document 2.ts
parent7a93f60c9529e5d175e617fc7c07145a9b33e572 (diff)
set up web box classes
Diffstat (limited to 'src/fields/Document 2.ts')
-rw-r--r--src/fields/Document 2.ts93
1 files changed, 93 insertions, 0 deletions
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