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

144 lines
3.6 KiB
Vue
Raw Normal View History

2020-04-30 14:21:43 +02:00
<template>
2021-01-12 16:06:56 +01:00
<ui-property v-if="!isHidden" :field="config.path" :label="label" :hide-label="config.options.hideLabel" :description="description" :required="isRequired" :disabled="isDisabled" :class="{'is-disabled': isDisabled, 'has-block': !!blockComponent }">
2020-11-17 15:44:04 +01:00
<component :is="config.component" v-bind="config.componentOptions" :value="model" :entity="value" @input="onChange" :disabled="isDisabled" />
2020-10-18 23:47:59 +02:00
<p v-if="config.options.helpText" class="ui-property-help" v-localize="config.options.helpText"></p>
2021-01-12 16:06:56 +01:00
<template v-if="blockComponent && loaded" v-slot:after>
<component :is="blockComponent" v-bind="{ config, editor, value, model }" />
</template>
2020-04-30 14:21:43 +02:00
</ui-property>
</template>
<script>
import Strings from 'zero/helpers/strings.js';
import Objects from 'zero/helpers/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';
import Localization from 'zero/helpers/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-11-17 15:44:04 +01:00
inject: [ 'meta' ],
2020-10-18 23:47:59 +02:00
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
}
},
2020-04-30 14:21:43 +02:00
data: () => ({
2021-01-12 14:12:05 +01:00
model: null,
2021-01-12 16:06:56 +01:00
loaded: false,
blockComponent: null,
manualDisabled: false
2020-04-30 14:21:43 +02:00
}),
2020-05-19 13:13:40 +02:00
mounted()
{
this.rebuildModel();
2021-01-12 14:12:05 +01:00
this.loaded = true;
},
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
{
2021-01-12 14:12:05 +01:00
return !this.loaded || (typeof this.config.options.condition === 'function' && !this.config.options.condition(this.value));
2020-10-18 23:47:59 +02:00
},
isRequired()
{
return typeof this.config.isRequired === 'function' ? this.config.isRequired(this.value) : this.config.isRequired;
2020-09-11 17:40:56 +02:00
},
isDisabled()
{
2021-01-12 16:06:56 +01:00
return this.manualDisabled || (typeof this.config.options.disabled === 'boolean' && this.config.options.disabled) || (typeof this.config.options.disabled === 'function' && this.config.options.disabled(this.value, this.model));
},
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
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);
2021-01-12 16:06:56 +01:00
},
setDisabled(disabled)
{
this.manualDisabled = disabled;
},
setBlock(component)
{
this.blockComponent = component;
2020-04-30 15:35:09 +02:00
}
2020-04-30 14:21:43 +02:00
}
}
</script>