jsketcher/web/app/cad/craft/schema/initializeBySchema.ts
2022-06-25 15:19:47 -07:00

44 lines
1.7 KiB
TypeScript

import {TypeRegistry, Types} from "cad/craft/schema/types";
import {OperationSchema, SchemaField} from "cad/craft/schema/schema";
import {ApplicationContext} from "context";
export default function initializeBySchema(schema: OperationSchema, context: ApplicationContext) {
let fields = Object.keys(schema);
let obj = {};
for (let field of fields) {
let val = undefined;
let md = schema[field] as SchemaField;
if (md.type === Types.array) {
if (md.items.type === Types.entity && md.items.defaultValue !== undefined) {
const defaultValue = md.items.defaultValue;
if (defaultValue.usePreselection === true) {
const entitySchema = md.items;
const currentSelection =
context.entityContextService.selectedEntities.value.filter(e => entitySchema.allowedKinds.includes(e.TYPE));
val = currentSelection.map(e => e.id);
}
} else {
val = md.defaultValue || [];
}
} else if (md.type === Types.entity && md.defaultValue !== undefined) {
const defaultValue = md.defaultValue;
if (defaultValue.usePreselection === true && defaultValue.preselectionIndex !== undefined) {
const allowedKinds = md.allowedKinds;
const currentSelection =
context.entityContextService.selectedEntities.value.filter(e => allowedKinds.includes(e.TYPE));
let mObject = currentSelection[defaultValue.preselectionIndex as number];
if (mObject) {
val = mObject.id;
}
}
} else if (md.type === Types.object) {
val = md.defaultValue || initializeBySchema(md.schema, context);
} else {
val = md.defaultValue;
}
obj[field] = val;
}
return obj;
}