aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields
diff options
context:
space:
mode:
Diffstat (limited to 'src/new_fields')
-rw-r--r--src/new_fields/Doc.ts44
-rw-r--r--src/new_fields/Proxy.ts10
-rw-r--r--src/new_fields/SchemaHeaderField.ts87
-rw-r--r--src/new_fields/ScriptField.ts1
-rw-r--r--src/new_fields/Types.ts2
5 files changed, 132 insertions, 12 deletions
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts
index 1a00db1c1..59314783b 100644
--- a/src/new_fields/Doc.ts
+++ b/src/new_fields/Doc.ts
@@ -12,6 +12,7 @@ import { scriptingGlobal } from "../client/util/Scripting";
import { List } from "./List";
import { DocumentType } from "../client/documents/Documents";
import { ComputedField } from "./ScriptField";
+import { PrefetchProxy } from "./Proxy";
export namespace Field {
export function toKeyValueString(doc: Doc, key: string): string {
@@ -283,21 +284,28 @@ export namespace Doc {
export function AddDocToList(target: Doc, key: string, doc: Doc, relativeTo?: Doc, before?: boolean, first?: boolean, allowDuplicates?: boolean) {
if (target[key] === undefined) {
+ console.log("target key undefined");
Doc.GetProto(target)[key] = new List<Doc>();
}
let list = Cast(target[key], listSpec(Doc));
if (list) {
+ console.log("has list");
if (allowDuplicates !== true) {
let pind = list.reduce((l, d, i) => d instanceof Doc && Doc.AreProtosEqual(d, doc) ? i : l, -1);
if (pind !== -1) {
list.splice(pind, 1);
}
}
- if (first) list.splice(0, 0, doc);
+ if (first) {
+ console.log("is first");
+ list.splice(0, 0, doc);
+ }
else {
+ console.log("not first");
let ind = relativeTo ? list.indexOf(relativeTo) : -1;
if (ind === -1) list.push(doc);
else list.splice(before ? ind : ind + 1, 0, doc);
+ console.log("index", ind);
}
}
return true;
@@ -341,7 +349,7 @@ export namespace Doc {
while (proto && !Doc.IsPrototype(proto)) {
proto = proto.proto;
}
- (proto ? proto : doc)[fieldKey + "_ext"] = docExtensionForField;
+ (proto ? proto : doc)[fieldKey + "_ext"] = new PrefetchProxy(docExtensionForField);
}, 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);
@@ -389,11 +397,22 @@ export namespace Doc {
}
if (expandedTemplateLayout === undefined) {
setTimeout(() =>
- dataDoc[expandedLayoutFieldKey] = Doc.MakeDelegate(templateLayoutDoc, undefined, "["+templateLayoutDoc.title + "]"), 0);
+ dataDoc[expandedLayoutFieldKey] = Doc.MakeDelegate(templateLayoutDoc, undefined, "[" + templateLayoutDoc.title + "]"), 0);
}
return templateLayoutDoc; // use the templateLayout when it's not a template or the expandedTemplate is pending.
}
+ export function GetLayoutDataDocPair(doc: Doc, dataDoc: Doc | undefined, fieldKey: string, childDocLayout: Doc) {
+ let layoutDoc = childDocLayout;
+ let resolvedDataDoc = !doc.isTemplate && dataDoc !== doc ? dataDoc : undefined;
+ if (resolvedDataDoc && Doc.WillExpandTemplateLayout(childDocLayout, resolvedDataDoc)) {
+ Doc.UpdateDocumentExtensionForField(resolvedDataDoc, fieldKey);
+ let fieldExtensionDoc = Doc.resolvedFieldDataDoc(resolvedDataDoc, StrCast(childDocLayout.templateField, StrCast(childDocLayout.title)), "dummy");
+ layoutDoc = Doc.expandTemplateLayout(childDocLayout, fieldExtensionDoc !== resolvedDataDoc ? fieldExtensionDoc : undefined);
+ } else layoutDoc = Doc.expandTemplateLayout(childDocLayout, resolvedDataDoc);
+ return { layout: layoutDoc, data: resolvedDataDoc };
+ }
+
export function MakeCopy(doc: Doc, copyProto: boolean = false): Doc {
const copy = new Doc;
Object.keys(doc).forEach(key => {
@@ -444,7 +463,7 @@ export namespace Doc {
return otherdoc;
}
- export function MakeTemplate(fieldTemplate: Doc, metaKey: string, proto: Doc) {
+ export function MakeTemplate(fieldTemplate: Doc, metaKey: string, templateDataDoc: Doc) {
// move data doc fields to layout doc as needed (nativeWidth/nativeHeight, data, ??)
let backgroundLayout = StrCast(fieldTemplate.backgroundLayout);
let fieldLayoutDoc = fieldTemplate;
@@ -455,21 +474,24 @@ export namespace Doc {
if (backgroundLayout) {
backgroundLayout = backgroundLayout.replace(/fieldKey={"[^"]*"}/, `fieldKey={"${metaKey}"}`);
}
- let nw = Cast(fieldTemplate.nativeWidth, "number");
- let nh = Cast(fieldTemplate.nativeHeight, "number");
let layoutDelegate = fieldTemplate.layout instanceof Doc ? fieldLayoutDoc : fieldTemplate;
layoutDelegate.layout = layout;
- fieldTemplate.title = metaKey;
fieldTemplate.templateField = metaKey;
+ fieldTemplate.title = metaKey;
+ fieldTemplate.isTemplate = true;
fieldTemplate.layout = layoutDelegate !== fieldTemplate ? layoutDelegate : layout;
fieldTemplate.backgroundLayout = backgroundLayout;
- fieldTemplate.nativeWidth = nw;
- fieldTemplate.nativeHeight = nh;
- fieldTemplate.isTemplate = true;
+ /* move certain layout properties from the original data doc to the template layout to avoid
+ inheriting them from the template's data doc which may also define these fields for its own use.
+ */
+ fieldTemplate.ignoreAspect = BoolCast(fieldTemplate.ignoreAspect);
+ fieldTemplate.singleColumn = BoolCast(fieldTemplate.singleColumn);
+ fieldTemplate.nativeWidth = Cast(fieldTemplate.nativeWidth, "number");
+ fieldTemplate.nativeHeight = Cast(fieldTemplate.nativeHeight, "number");
fieldTemplate.showTitle = "title";
- setTimeout(() => fieldTemplate.proto = proto);
+ setTimeout(() => fieldTemplate.proto = templateDataDoc);
}
export async function ToggleDetailLayout(d: Doc) {
diff --git a/src/new_fields/Proxy.ts b/src/new_fields/Proxy.ts
index 14f08814e..b3e8d6467 100644
--- a/src/new_fields/Proxy.ts
+++ b/src/new_fields/Proxy.ts
@@ -6,6 +6,7 @@ import { DocServer } from "../client/DocServer";
import { RefField } from "./RefField";
import { ObjectField } from "./ObjectField";
import { Id, Copy, ToScriptString } from "./FieldSymbols";
+import { scriptingGlobal } from "../client/util/Scripting";
@Deserializable("proxy")
export class ProxyField<T extends RefField> extends ObjectField {
@@ -66,3 +67,12 @@ export class ProxyField<T extends RefField> extends ObjectField {
return this.promise as any;
}
}
+
+function prefetchValue(proxy: PrefetchProxy<RefField>) {
+ return proxy.value() as any;
+}
+
+@scriptingGlobal
+@Deserializable("prefetch_proxy", prefetchValue)
+export class PrefetchProxy<T extends RefField> extends ProxyField<T> {
+}
diff --git a/src/new_fields/SchemaHeaderField.ts b/src/new_fields/SchemaHeaderField.ts
new file mode 100644
index 000000000..d5da56b10
--- /dev/null
+++ b/src/new_fields/SchemaHeaderField.ts
@@ -0,0 +1,87 @@
+import { Deserializable } from "../client/util/SerializationHelper";
+import { serializable, createSimpleSchema, primitive } from "serializr";
+import { ObjectField } from "./ObjectField";
+import { Copy, ToScriptString, OnUpdate } from "./FieldSymbols";
+import { scriptingGlobal, Scripting } from "../client/util/Scripting";
+import { ColumnType } from "../client/views/collections/CollectionSchemaView";
+
+export const PastelSchemaPalette = new Map<string, string>([
+ ["pink1", "#FFB4E8"],
+ ["pink2", "#ff9cee"],
+ ["pink3", "#ffccf9"],
+ ["pink4", "#fcc2ff"],
+ ["pink5", "#f6a6ff"],
+ ["purple1", "#b28dff"],
+ ["purple2", "#c5a3ff"],
+ ["purple3", "#d5aaff"],
+ ["purple4", "#ecd4ff"],
+ ["purple5", "#fb34ff"],
+ ["purple6", "#dcd3ff"],
+ ["purple7", "#a79aff"],
+ ["purple8", "#b5b9ff"],
+ ["purple9", "#97a2ff"],
+ ["bluegreen1", "#afcbff"],
+ ["bluegreen2", "#aff8db"],
+ ["bluegreen3", "#c4faf8"],
+ ["bluegreen4", "#85e3ff"],
+ ["bluegreen5", "#ace7ff"],
+ ["bluegreen6", "#6eb5ff"],
+ ["bluegreen7", "#bffcc6"],
+ ["bluegreen8", "#dbffd6"],
+ ["yellow1", "#f3ffe3"],
+ ["yellow2", "#e7ffac"],
+ ["yellow3", "#ffffd1"],
+ ["yellow4", "#fff5ba"],
+ ["red1", "#ffc9de"],
+ ["red2", "#ffabab"],
+ ["red3", "#ffbebc"],
+ ["red4", "#ffcbc1"],
+]);
+
+export const RandomPastel = () => Array.from(PastelSchemaPalette.values())[Math.floor(Math.random() * PastelSchemaPalette.size)];
+
+@scriptingGlobal
+@Deserializable("schemaheader")
+export class SchemaHeaderField extends ObjectField {
+ @serializable(primitive())
+ heading: string;
+ color: string;
+ type: number;
+
+ constructor(heading: string = "", color?: string, type?: ColumnType) {
+ super();
+
+ this.heading = heading;
+ this.color = color === "" || color === undefined ? RandomPastel() : color;
+ if (type) {
+ this.type = type;
+ }
+ else {
+ this.type = 0;
+ }
+ }
+
+ setHeading(heading: string) {
+ this.heading = heading;
+ this[OnUpdate]();
+ }
+
+ setColor(color: string) {
+ this.color = color;
+ this[OnUpdate]();
+ }
+
+ setType(type: ColumnType) {
+ this.type = type;
+ this[OnUpdate]();
+ }
+
+ [Copy]() {
+ return new SchemaHeaderField(this.heading, this.color, this.type);
+ }
+
+ [ToScriptString]() {
+ return `invalid`;
+ }
+}
+
diff --git a/src/new_fields/ScriptField.ts b/src/new_fields/ScriptField.ts
index 00b4dec2c..6d52525b8 100644
--- a/src/new_fields/ScriptField.ts
+++ b/src/new_fields/ScriptField.ts
@@ -26,6 +26,7 @@ const optionsSchema = createSimpleSchema({
requiredType: true,
addReturn: true,
typecheck: true,
+ editable: true,
readonly: true,
params: optional(map(primitive()))
});
diff --git a/src/new_fields/Types.ts b/src/new_fields/Types.ts
index f8a4a30b4..565ae2ee3 100644
--- a/src/new_fields/Types.ts
+++ b/src/new_fields/Types.ts
@@ -78,7 +78,7 @@ export function StrCast(field: FieldResult, defaultVal: string | null = "") {
return Cast(field, "string", defaultVal);
}
-export function BoolCast(field: FieldResult, defaultVal: boolean | null = null) {
+export function BoolCast(field: FieldResult, defaultVal: boolean | null = false) {
return Cast(field, "boolean", defaultVal);
}
export function DateCast(field: FieldResult) {