Files
mixtape/zero.Web.UI/App/editor/editor-component.vue
T

203 lines
5.1 KiB
Vue
Raw Normal View History

2020-04-30 14:21:43 +02:00
<template>
2020-10-21 12:10:21 +02:00
<ui-property v-if="!isHidden" :field="config.path" :label="label" :hide-label="config.options.hideLabel" :description="description" :required="isRequired">
2020-10-20 13:27:17 +02:00
<component :is="config.component" v-bind="config.componentOptions" :value="model" :entity="value" @input="onChange" />
2020-10-18 23:47:59 +02:00
<p v-if="config.options.helpText" class="ui-property-help" v-localize="config.options.helpText"></p>
2020-04-30 14:21:43 +02:00
</ui-property>
</template>
<script>
2020-11-01 22:20:29 +01:00
import Strings from 'zero/services/strings.js';
import Objects from 'zero/services/objects.js';
2020-11-01 23:23:36 +01:00
import Editor from 'zero/core/editor.ts';
import EditorField from 'zero/core/editor-field.ts';
2020-11-01 22:20:29 +01:00
import Localization from 'zero/services/localization.js';
2020-04-30 14:21:43 +02:00
export default {
2020-04-30 15:35:09 +02:00
name: 'uiEditorComponent',
2020-04-30 14:21:43 +02:00
2020-10-18 23:47:59 +02:00
inject: [ 'meta', 'disabled' ],
2020-04-30 14:21:43 +02:00
props: {
2020-06-23 12:23:23 +02:00
config: {
2020-11-01 23:23:36 +01:00
type: Object,
2020-10-18 23:47:59 +02:00
required: true
2020-05-01 12:40:08 +02:00
},
2020-10-18 23:47:59 +02:00
editor: {
2020-11-01 23:23:36 +01:00
type: Object,
2020-10-18 23:47:59 +02:00
required: true
2020-06-29 18:20:43 +02:00
},
2020-10-18 23:47:59 +02:00
value: {
type: Object,
required: true
2020-06-30 12:12:31 +02:00
}
2020-04-30 14:21:43 +02:00
},
watch: {
value: {
deep: true,
handler: function ()
{
this.rebuildModel();
}
},
2020-06-23 12:23:23 +02:00
config: {
deep: true,
handler: function ()
{
this.rebuildConfig();
}
},
2020-10-18 23:47:59 +02:00
editor: {
2020-06-23 12:23:23 +02:00
deep: true,
handler: function ()
{
this.rebuildConfig();
}
}
},
2020-04-30 14:21:43 +02:00
data: () => ({
2020-10-18 23:47:59 +02:00
model: null
2020-04-30 14:21:43 +02:00
}),
2020-10-18 23:47:59 +02:00
created()
{
this.rebuildModel();
this.rebuildConfig();
},
2020-05-19 13:13:40 +02:00
mounted()
{
this.rebuildModel();
},
2020-04-30 14:21:43 +02:00
computed: {
2020-09-10 16:12:06 +02:00
isHidden()
2020-06-24 11:53:20 +02:00
{
2020-10-18 23:47:59 +02:00
return typeof this.config.options.condition === 'function' && !this.config.options.condition(this.value);
},
isRequired()
{
return typeof this.config.isRequired === 'function' ? this.config.isRequired(this.value) : this.config.isRequired;
2020-09-11 17:40:56 +02:00
},
isDisabled()
{
2020-10-18 23:47:59 +02:00
return this.disabled || (typeof this.config.options.disabled === 'boolean' && this.config.options.disabled) || (typeof this.config.options.disabled === 'function' && this.config.options.disabled(this.value));
},
2020-10-18 23:47:59 +02:00
label()
{
2020-10-18 23:47:59 +02:00
return this.config.options.label || this.editor.templateLabel(this.config.path);
},
description()
2020-05-01 11:36:26 +02:00
{
2020-10-19 14:48:21 +02:00
return Localization.localize(this.config.options.description || this.editor.templateDescription(this.config.path), { hideEmpty: true });
2020-05-01 11:36:26 +02:00
}
2020-04-30 15:35:09 +02:00
},
methods: {
rebuildModel()
{
2020-10-18 23:47:59 +02:00
this.selector = Strings.selectorToArray(this.config.path);
let currentValue = this.value;
let found = false;
if (!this.selector || !this.selector.length || !currentValue)
{
2020-10-18 23:47:59 +02:00
found = true;
this.model = null;
}
else
{
for (var key of this.selector)
{
if (key in currentValue)
{
2020-10-18 23:47:59 +02:00
found = true;
currentValue = currentValue[key];
}
else
{
break;
}
}
2020-10-18 23:47:59 +02:00
this.model = found ? currentValue : null;
}
},
2020-06-23 12:23:23 +02:00
rebuildConfig()
{
// build class list
2020-10-18 23:47:59 +02:00
//let classes = typeof this.config.class === 'string' ? this.config.class.split(' ') : (this.config.class || []);
//if (this.view === 'renderer' || this.view === 'nested')
//{
// classes.push('full-width');
//}
//if (this.depth > 0)
//{
// classes.push('is-nested');
//}
//if (this.isStatic)
//{
// classes.push('is-static');
//}
//this.classList = classes;
2020-06-23 12:23:23 +02:00
2020-10-18 23:47:59 +02:00
//// build label
//let label = null;
//if (this.config.label && this.config.label.indexOf('@') === 0)
//{
// label = this.config.label;
//}
//else if (this.renderer.labelTemplate)
//{
// label = this.renderer.labelTemplate(this.config.label || this.config.field);
//}
//else
//{
// label = this.config.label || this.config.field;
//}
//this.label = Localization.localize(label);
2020-06-23 12:23:23 +02:00
2020-10-18 23:47:59 +02:00
//// build description
//let description = null;
//if (this.config.description && this.config.description.indexOf('@') === 0)
//{
// description = this.config.description;
//}
//else if (this.config.description === null)
//{
// description = null;
//}
//else if (this.renderer.descriptionTemplate)
//{
// description = this.renderer.descriptionTemplate(this.config.description || this.config.field);
//}
//else
//{
// description = this.config.description;
//}
//this.description = Localization.localize(description, { hideEmpty: true });
2020-06-23 12:23:23 +02:00
},
2020-04-30 15:35:09 +02:00
onChange(value)
{
2020-08-24 15:17:43 +02:00
if (typeof value === 'function')
{
value(this.value);
}
else
{
Objects.setValue(this.value, this.selector, value);
}
this.$emit('input', this.value);
2020-04-30 15:35:09 +02:00
}
2020-04-30 14:21:43 +02:00
}
}
</script>