aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields/Doc.ts
diff options
context:
space:
mode:
authormonikahedman <monika_hedman@brown.edu>2019-08-08 15:03:03 -0400
committermonikahedman <monika_hedman@brown.edu>2019-08-08 15:03:03 -0400
commit8f951d9110d096d665af6fbd295902ef8d3574e8 (patch)
tree37cc6881cbf93aeea5deae53a6415d6607377edc /src/new_fields/Doc.ts
parent030af1b9112cd12383abcd7f35142cc382ea4d6a (diff)
parent316c241d72fb83aad5f2bf9b143c317fdc906654 (diff)
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web into checkbox
Diffstat (limited to 'src/new_fields/Doc.ts')
-rw-r--r--src/new_fields/Doc.ts114
1 files changed, 83 insertions, 31 deletions
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts
index 84b8589dd..ba01cfd9c 100644
--- a/src/new_fields/Doc.ts
+++ b/src/new_fields/Doc.ts
@@ -8,11 +8,11 @@ import { listSpec } from "./Schema";
import { ObjectField } from "./ObjectField";
import { RefField, FieldId } from "./RefField";
import { ToScriptString, SelfProxy, Parent, OnUpdate, Self, HandleUpdate, Update, Id } from "./FieldSymbols";
-import { scriptingGlobal } from "../client/util/Scripting";
+import { scriptingGlobal, CompileScript, Scripting } from "../client/util/Scripting";
import { List } from "./List";
import { DocumentType } from "../client/documents/Documents";
-import { ComputedField } from "./ScriptField";
-import { PrefetchProxy } from "./Proxy";
+import { ComputedField, ScriptField } from "./ScriptField";
+import { PrefetchProxy, ProxyField } from "./Proxy";
export namespace Field {
export function toKeyValueString(doc: Doc, key: string): string {
@@ -68,6 +68,7 @@ export function DocListCast(field: FieldResult): Doc[] {
export const WidthSym = Symbol("Width");
export const HeightSym = Symbol("Height");
+const CachedUpdates = Symbol("Cached updates");
function fetchProto(doc: Doc) {
const proto = doc.proto;
@@ -147,6 +148,8 @@ export class Doc extends RefField {
return "invalid";
}
+ private [CachedUpdates]: { [key: string]: () => Promise<any> } = {};
+
public async [HandleUpdate](diff: any) {
const set = diff.$set;
if (set) {
@@ -154,11 +157,18 @@ export class Doc extends RefField {
if (!key.startsWith("fields.")) {
continue;
}
- const value = await SerializationHelper.Deserialize(set[key]);
const fKey = key.substring(7);
- updatingFromServer = true;
- this[fKey] = value;
- updatingFromServer = false;
+ const fn = async () => {
+ const value = await SerializationHelper.Deserialize(set[key]);
+ updatingFromServer = true;
+ this[fKey] = value;
+ updatingFromServer = false;
+ };
+ if (DocServer.getFieldWriteMode(fKey)) {
+ this[CachedUpdates][fKey] = fn;
+ } else {
+ await fn();
+ }
}
}
const unset = diff.$unset;
@@ -168,9 +178,16 @@ export class Doc extends RefField {
continue;
}
const fKey = key.substring(7);
- updatingFromServer = true;
- delete this[fKey];
- updatingFromServer = false;
+ const fn = async () => {
+ updatingFromServer = true;
+ delete this[fKey];
+ updatingFromServer = false;
+ };
+ if (DocServer.getFieldWriteMode(fKey)) {
+ this[CachedUpdates][fKey] = fn;
+ } else {
+ await fn();
+ }
}
}
}
@@ -187,6 +204,13 @@ export namespace Doc {
// return Cast(field, ctor);
// });
// }
+ export function RunCachedUpdate(doc: Doc, field: string) {
+ const update = doc[CachedUpdates][field];
+ if (update) {
+ update();
+ delete doc[CachedUpdates][field];
+ }
+ }
export function MakeReadOnly(): { end(): void } {
makeReadOnly();
return {
@@ -341,19 +365,24 @@ export namespace Doc {
return fieldExt && doc[fieldKey + "_ext"] instanceof Doc ? doc[fieldKey + "_ext"] as Doc : doc;
}
+ export function CreateDocumentExtensionForField(doc: Doc, fieldKey: string) {
+ let docExtensionForField = new Doc(doc[Id] + fieldKey, true);
+ docExtensionForField.title = fieldKey + ".ext";
+ docExtensionForField.extendsDoc = doc; // this is used by search to map field matches on the extension doc back to the document it extends.
+ docExtensionForField.type = DocumentType.EXTENSION;
+ let proto: Doc | undefined = doc;
+ while (proto && !Doc.IsPrototype(proto)) {
+ proto = proto.proto;
+ }
+ (proto ? proto : doc)[fieldKey + "_ext"] = new PrefetchProxy(docExtensionForField);
+ return docExtensionForField;
+ }
+
export function UpdateDocumentExtensionForField(doc: Doc, fieldKey: string) {
let docExtensionForField = doc[fieldKey + "_ext"] as Doc;
if (docExtensionForField === undefined) {
setTimeout(() => {
- docExtensionForField = new Doc(doc[Id] + fieldKey, true);
- docExtensionForField.title = fieldKey + ".ext";
- docExtensionForField.extendsDoc = doc; // this is used by search to map field matches on the extension doc back to the document it extends.
- docExtensionForField.type = DocumentType.EXTENSION;
- let proto: Doc | undefined = doc;
- while (proto && !Doc.IsPrototype(proto)) {
- proto = proto.proto;
- }
- (proto ? proto : doc)[fieldKey + "_ext"] = new PrefetchProxy(docExtensionForField);
+ CreateDocumentExtensionForField(doc, fieldKey);
}, 0);
} else if (doc instanceof Doc) { // backward compatibility -- add fields for docs that don't have them already
docExtensionForField.extendsDoc === undefined && setTimeout(() => docExtensionForField.extendsDoc = doc, 0);
@@ -361,10 +390,15 @@ export namespace Doc {
}
}
export function MakeAlias(doc: Doc) {
- if (!GetT(doc, "isPrototype", "boolean", true)) {
- return Doc.MakeCopy(doc);
+ let alias = !GetT(doc, "isPrototype", "boolean", true) ? Doc.MakeCopy(doc) : Doc.MakeDelegate(doc);
+ let aliasNumber = Doc.GetProto(doc).aliasNumber = NumCast(Doc.GetProto(doc).aliasNumber) + 1;
+ let script = `return renameAlias(self, ${aliasNumber})`;
+ //let script = "StrCast(self.title).replace(/\\([0-9]*\\)/, \"\") + `(${n})`";
+ let compiled = CompileScript(script, { params: { this: "Doc" }, capturedVariables: { self: doc }, typecheck: false });
+ if (compiled.compiled) {
+ alias.title = new ComputedField(compiled);
}
- return Doc.MakeDelegate(doc); // bcz?
+ return alias;
}
//
@@ -420,7 +454,7 @@ export namespace Doc {
export function MakeCopy(doc: Doc, copyProto: boolean = false): Doc {
const copy = new Doc;
Object.keys(doc).forEach(key => {
- const field = doc[key];
+ const field = ProxyField.WithoutProxy(() => doc[key]);
if (key === "proto" && copyProto) {
if (field instanceof Doc) {
copy[key] = Doc.MakeCopy(field);
@@ -431,7 +465,7 @@ export namespace Doc {
} else if (field instanceof ObjectField) {
copy[key] = ObjectField.MakeCopy(field);
} else if (field instanceof Promise) {
- field.then(f => (copy[key] === undefined) && (copy[key] = f)); //TODO what should we do here?
+ debugger; //This shouldn't happend...
} else {
copy[key] = field;
}
@@ -525,19 +559,37 @@ export namespace Doc {
}
export function UseDetailLayout(d: Doc) {
runInAction(async () => {
- let detailLayout1 = await PromiseValue(d.detailedLayout);
- let detailLayout = await PromiseValue(d.detailedLayout);
+ let detailLayout = await d.detailedLayout;
if (detailLayout) {
d.layout = detailLayout;
d.nativeWidth = d.nativeHeight = undefined;
if (detailLayout instanceof Doc) {
- let delegDetailLayout = Doc.MakeDelegate(detailLayout) as Doc;
+ let delegDetailLayout = Doc.MakeDelegate(detailLayout);
d.layout = delegDetailLayout;
- let subDetailLayout1 = await PromiseValue(delegDetailLayout.detailedLayout);
- let subDetailLayout = await PromiseValue(delegDetailLayout.detailedLayout);
- delegDetailLayout.layout = subDetailLayout;
+ delegDetailLayout.layout = await delegDetailLayout.detailedLayout;
}
}
});
}
-} \ No newline at end of file
+
+ export class DocBrush {
+ @observable BrushedDoc: Doc[] = [];
+ }
+ const manager = new DocBrush();
+ export function IsBrushed(doc: Doc) {
+ return manager.BrushedDoc.some(d => Doc.AreProtosEqual(d, doc));
+ }
+ export function IsBrushedDegree(doc: Doc) {
+ return manager.BrushedDoc.some(d => d === doc) ? 2 : Doc.IsBrushed(doc) ? 1 : 0;
+ }
+ export function BrushDoc(doc: Doc) {
+ if (manager.BrushedDoc.indexOf(doc) === -1) runInAction(() => manager.BrushedDoc.push(doc));
+ }
+ export function UnBrushDoc(doc: Doc) {
+ let index = manager.BrushedDoc.indexOf(doc);
+ if (index !== -1) runInAction(() => manager.BrushedDoc.splice(index, 1));
+ }
+}
+Scripting.addGlobal(function renameAlias(doc: any, n: any) {
+ return StrCast(doc.title).replace(/\([0-9]*\)/, "") + `(${n})`;
+}); \ No newline at end of file