Files
mixtape/zero.Backoffice.UI/app/editor/compile.ts
T

289 lines
6.8 KiB
TypeScript
Raw Normal View History

2021-12-21 13:58:48 +01:00
import { Component } from "vue";
2021-12-30 13:39:31 +01:00
import { ZeroEditorField, ZeroEditorDisplay } from "zero/schemas";
2021-12-21 13:58:48 +01:00
import { Zero } from "../core";
import { ZeroEditor } from "./editor";
2021-12-22 10:55:40 +01:00
import { createBlueprintConfig } from "./editor-blueprint";
2021-12-21 13:58:48 +01:00
import { ZeroEditorCanvasBase, ZeroEditorTab } from "./editor-canvas";
2022-01-03 15:28:12 +01:00
import { ZeroEditorFieldConfiguration, ZeroEditorFieldFilterPreview } from "./editor-field";
2021-12-22 10:55:40 +01:00
import { localize } from '../services/localization';
2021-12-21 13:58:48 +01:00
2022-01-14 11:51:38 +01:00
let appliedExtensionsTo: string[] = [];
2021-12-21 13:58:48 +01:00
export interface ZeroCompiledEditor
{
2021-12-22 10:55:40 +01:00
alias: string;
blueprint: any;
2021-12-30 13:39:31 +01:00
display: ZeroEditorDisplay;
2021-12-21 13:58:48 +01:00
tabs: ZeroCompiledEditorTab[];
origin: ZeroEditor;
2021-12-21 13:58:48 +01:00
}
export interface ZeroCompiledEditorTab
{
alias: string;
name: string;
sort: number;
class: string | null;
count: (model: any) => number | null;
disabled: (model: any) => boolean;
hidden: (model: any) => boolean;
fieldsets: ZeroCompiledEditorFieldset[];
component: Component | null;
}
export interface ZeroCompiledEditorFieldset
{
fields: ZeroCompiledEditorField[];
2022-01-03 01:08:55 +01:00
hasColumns: boolean;
2021-12-21 13:58:48 +01:00
}
2022-01-10 16:13:59 +01:00
export interface ZeroCompiledEditorChangeEvent
{
value: any;
oldValue: any;
model: any;
component: Component;
}
2021-12-21 13:58:48 +01:00
export interface ZeroCompiledEditorField
{
path: string;
configuration: ZeroEditorFieldConfiguration;
fieldType: string;
options: any | null;
component: Component;
optional: (model: any) => boolean;
readonly: (model: any) => boolean;
hidden: (model: any) => boolean;
label: string;
hideLabel: boolean;
description: string | null;
helpText: string | null;
classes: string | null;
horizontal: boolean;
sort: number;
2022-01-03 01:08:55 +01:00
columns: number;
2022-01-03 15:28:12 +01:00
preview?: ZeroEditorFieldFilterPreview;
2022-01-10 16:13:59 +01:00
onChange: (event: ZeroCompiledEditorChangeEvent) => void;
2021-12-21 13:58:48 +01:00
}
export function compileField(zero: Zero, editor: ZeroEditor, field: ZeroEditorField): ZeroCompiledEditorField | undefined
{
const component = field.customComponent || zero.getFieldTypeComponent(field.fieldType);
2021-12-21 13:58:48 +01:00
if (!component)
{
return undefined;
}
let model = {
configuration: field.configuration,
fieldType: field.fieldType,
options: field.options,
path: field.path,
component,
optional(model: any)
{
if (typeof field.configuration.optional === 'boolean')
{
return field.configuration.optional;
}
if (typeof field.configuration.optional === 'function')
{
return field.configuration.optional(model);
}
return false;
},
readonly(model: any)
{
if (typeof field.configuration.readonly === 'boolean')
{
return field.configuration.readonly;
}
if (typeof field.configuration.readonly === 'function')
{
return field.configuration.readonly(model);
}
return false;
},
hidden(model: any)
{
if (typeof field.configuration.hidden === 'boolean')
{
return field.configuration.hidden;
}
if (typeof field.configuration.hidden === 'function')
{
return field.configuration.hidden(model);
}
return false;
},
2021-12-22 10:55:40 +01:00
label: editor.onLabelCreate(field),
description: localize(editor.onDescriptionCreate(field), { hideEmpty: true }),
2021-12-21 13:58:48 +01:00
hideLabel: field.configuration.hideLabel,
helpText: field.configuration.helpText,
classes: field.configuration.classes,
horizontal: field.configuration.horizontal,
2022-01-03 01:08:55 +01:00
sort: field.configuration.sort,
2022-01-03 15:28:12 +01:00
columns: 12,
preview: field.configuration.preview ? {
icon: field.configuration.preview.icon || 'fth-square',
selected: field.configuration.preview.selected || (x => !!x),
value: field.configuration.preview.value || (x => x ? x : null)
2022-01-10 16:13:59 +01:00
} : null,
onChange(event: ZeroCompiledEditorChangeEvent)
{
(field.configuration.changeHandlers || []).forEach(handler =>
{
handler(event);
});
}
2021-12-21 13:58:48 +01:00
} as ZeroCompiledEditorField;
return model;
}
2021-12-22 15:41:11 +01:00
export function compileEditor(zero: Zero, editor: ZeroEditor): ZeroCompiledEditor | null
2021-12-21 13:58:48 +01:00
{
2021-12-22 15:41:11 +01:00
if (!editor)
{
return null;
}
2022-01-14 11:51:38 +01:00
if (appliedExtensionsTo.indexOf(editor.alias) < 0)
{
appliedExtensionsTo.push(editor.alias);
let extensions = zero.getSchemaExtensions(editor.alias);
extensions.forEach(extension =>
{
extension.extension(editor);
});
}
2021-12-21 13:58:48 +01:00
let model = {
2021-12-22 10:55:40 +01:00
alias: editor.alias,
blueprint: null,
2021-12-30 13:39:31 +01:00
display: editor.display,
tabs: [],
origin: editor,
2021-12-21 13:58:48 +01:00
} as ZeroCompiledEditor;
let tabs = [...editor.tabs.sort((a, b) => a.sort - b.sort)];
// add default tab if necessary
if (!tabs.length && (editor.fields.length || editor.fieldsets.length))
{
let tab = new ZeroEditorTab('content', '@ui.tab_content');
tab.fieldsets.push(new ZeroEditorCanvasBase());
tabs.push(tab);
}
tabs.forEach((editorTab, tabIndex) =>
{
let tab = {
name: editorTab.name,
sort: editorTab.sort,
alias: editorTab.alias,
class: null,
count: (model: any) => null,
disabled: (model: any) => false,
hidden: (model: any) => false,
fieldsets: [],
component: null
} as ZeroCompiledEditorTab;
let fieldsets = [];
// add fieldsets (which are not part of a tab) to the first tab
if (tabIndex == 0 && editor.fieldsets.length)
{
fieldsets.push(...editor.fieldsets);
}
// push registered fieldsets
fieldsets.push(...editorTab.fieldsets.sort((a, b) => a.sort - b.sort));
// add default fieldset if none are defined
if (!fieldsets.length)
{
2022-01-03 01:08:55 +01:00
const fakeFieldset = new ZeroEditorCanvasBase();
fakeFieldset.isFake = false;
fieldsets.push(fakeFieldset);
2021-12-21 13:58:48 +01:00
}
fieldsets.forEach((editorFieldset, fieldsetIndex) =>
{
let fieldset = {
sort: editorFieldset.sort,
2022-01-03 01:08:55 +01:00
fields: [],
hasColumns: !editorFieldset.isFake
2021-12-21 13:58:48 +01:00
} as ZeroCompiledEditorFieldset;
let fields = [];
// add fields (which are not part of a fieldset) to the first fieldset
2022-01-03 01:08:55 +01:00
if (fieldsetIndex == fieldsets.length - 1)
2021-12-21 13:58:48 +01:00
{
2022-01-03 01:08:55 +01:00
if (editor.fields.length)
{
fields.push(...editor.fields);
}
if (editorTab.fields.length)
{
fields.push(...editorTab.fields);
}
2021-12-21 13:58:48 +01:00
}
// push registered fields
// @ts-ignore
fields.push(...editorFieldset.fields.sort((a, b) => a.configuration.sort - b.configuration.sort));
fields.forEach(editorField =>
{
const field = compileField(zero, editor, editorField);
if (field == null)
{
console.error('Could not find registered field type [' + editorField.fieldType + ']');
}
else
{
fieldset.fields.push(field);
}
});
if (fieldset.fields.length)
{
tab.fieldsets.push(fieldset);
}
});
if (tab.fieldsets.length)
{
model.tabs.push(tab);
}
});
2022-01-03 01:08:55 +01:00
2021-12-22 10:55:40 +01:00
model.blueprint = createBlueprintConfig(model.alias, model);
2021-12-21 13:58:48 +01:00
return model;
}