Files
mixtape/zero.Backoffice.UI/app/editor/editor-blueprint.ts
T

170 lines
4.6 KiB
TypeScript
Raw Normal View History

2021-11-16 13:48:18 +01:00
2021-12-09 10:51:10 +01:00
import { localize } from '../services/localization';
import { arrayRemove } from '../utils/arrays';
2021-12-20 16:13:09 +01:00
import { useUiStore } from '../ui/store';
2021-12-22 10:55:40 +01:00
import { ZeroCompiledEditor } from './compile';
2021-11-16 13:48:18 +01:00
function unlocked(config, model, field)
{
if (!config || !model || !model.blueprint)
{
return true;
}
return canUnlock(config, model, field)
&& (model.blueprint.desync.indexOf(field.path) > -1
|| model.blueprint.desync.find(x => field.path.indexOf(x + '.') === 0) != null);
}
function canUnlock(config, model, field)
{
if (!config || !model || !model.blueprint)
{
return false;
2021-11-16 13:48:18 +01:00
}
return config.unlocked.indexOf(field.path) > -1
|| config.unlocked.find(x => field.path.indexOf(x + '.') === 0) != null;
}
function isBlueprintChild(config, route, model)
{
return model && route.query.scope != 'shared' && !!model.blueprint;
}
function isBlueprintParent(config, route, model)
{
return model && route.query.scope == 'shared' && !model.blueprint;
}
2021-11-16 15:03:24 +01:00
async function lock(config, model, field)
{
2021-12-09 10:51:10 +01:00
arrayRemove(model.blueprint.desync, field.path);
//let blueprint = await BlueprintApi.getById(model.blueprint.id);
//return blueprint[field.path];
2021-11-16 15:03:24 +01:00
}
function unlock(config, model, field)
2021-11-16 15:03:24 +01:00
{
model.blueprint.desync.push(field.path);
}
2021-11-16 13:48:18 +01:00
/**
* [TODO] description
* @typedef {object} EditorBlueprint
* @param {string} alias - Alias which is used to match an editor
* @param {function} unlocked - Whether a field is unlocked and can be changed
* @param {function} canUnlock - Whether a field can be unlocked
* @param {EditorBlueprintField[]} fields - Fields which can be altered in synchronization
*/
/**
* [TODO] description
* @typedef {object} EditorBlueprintField
* @param {string} path - Property path
* @param {string} label - Label which describes the property
* @param {string} description - Additional description for this property
* @param {function} synced - Whether this field is synchronized with the parent
*/
/**
* [TODO] description
* @param {string} alias - Alias for the tab
* @returns {EditorBlueprint}
*/
2021-12-22 10:55:40 +01:00
export function createBlueprintConfig(alias: string, editor: ZeroCompiledEditor)
2021-11-16 13:48:18 +01:00
{
2021-12-20 16:13:09 +01:00
const store = useUiStore();
2021-12-22 10:55:40 +01:00
let blueprintAlias = editor.alias;
2021-12-20 16:13:09 +01:00
let canBeBlueprinted = store.blueprints.indexOf(blueprintAlias) > -1;
2021-11-16 13:48:18 +01:00
2021-12-20 16:13:09 +01:00
if (!blueprintAlias || !canBeBlueprinted)
2021-11-16 13:48:18 +01:00
{
return {
alias: blueprintAlias,
2021-12-22 10:55:40 +01:00
isEnabled: false,
2021-11-16 15:03:24 +01:00
unlocked: () => true,
canUnlock: () => false,
isBlueprintParent: () => false,
isBlueprintChild: () => false,
2021-12-20 16:13:09 +01:00
lock: () => { },
unlock: () => { },
2021-11-16 13:48:18 +01:00
fields: []
};
}
2021-12-20 16:13:09 +01:00
let config = {
unlocked: []
}
2021-11-16 13:48:18 +01:00
// check for blueprint availability
//config.isParent = this.value && this.$route.query.scope === 'shared';
//config.isNewParent = config.isParent && !this.value.id;
//config.isChild = !config.isParent && this.value && !!this.value.blueprint;
// get all editor fields
let fields = [];
let processed = ['blueprint'];
let defaults = ['name', /*'alias',*/ 'isActive', 'sort', /*'key'*/];
// add blueprint setting for default fields
defaults.forEach(alias =>
{
processed.push(alias);
2021-12-20 16:13:09 +01:00
//if (config.unlocked.indexOf(alias) > -1)
//{
2021-11-16 13:48:18 +01:00
fields.push({
path: alias,
2021-12-09 10:51:10 +01:00
label: localize("@ui.entityfields." + alias),
description: localize("@ui.entityfields." + alias + "_text", { hideEmpty: true }),
2021-11-16 13:48:18 +01:00
synced: model => !model.blueprint || model.blueprint.desync.indexOf(alias) < 0
});
2021-12-20 16:13:09 +01:00
//}
2021-11-16 13:48:18 +01:00
});
// add blueprint setting for custom fields
2021-12-22 10:55:40 +01:00
editor.tabs.forEach(tab =>
2021-11-16 13:48:18 +01:00
{
2021-12-22 10:55:40 +01:00
tab.fieldsets.forEach(set =>
2021-11-16 13:48:18 +01:00
{
2021-12-22 10:55:40 +01:00
set.fields.forEach(field =>
2021-11-16 13:48:18 +01:00
{
2021-12-22 10:55:40 +01:00
let alias = field.path;
if (processed.indexOf(alias) < 0)
{
processed.push(alias);
if (config.unlocked.indexOf(alias) > -1 || config.unlocked.find(x => alias.indexOf(x + '.') === 0))
{
fields.push({
path: alias,
label: field.label,
description: field.description,
synced: model => !model.blueprint || model.blueprint.desync.indexOf(alias) < 0
});
}
}
});
});
2021-11-16 13:48:18 +01:00
});
return {
alias: blueprintAlias,
2021-12-22 10:55:40 +01:00
isEnabled: true,
2021-11-16 13:48:18 +01:00
unlocked: (model, field) => unlocked(config, model, field),
canUnlock: (model, field) => canUnlock(config, model, field),
2021-11-16 15:03:24 +01:00
unlock: async (model, field) => await unlock(config, model, field),
lock: async (model, field) => await lock(config, model, field),
2021-11-16 13:48:18 +01:00
isBlueprintParent: (route, model) => isBlueprintParent(config, route, model),
isBlueprintChild: (route, model) => isBlueprintChild(config, route, model),
fields: fields
};
};