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

192 lines
4.9 KiB
Vue
Raw Normal View History

2020-04-30 14:21:43 +02:00
<template>
2021-11-16 13:48:18 +01:00
<ui-property v-if="!isHidden"
:field="config.path"
:label="label"
:hide-label="config.options.hideLabel"
:description="description"
:required="isRequired"
:disabled="isDisabled"
2021-08-24 16:06:13 +02:00
:vertical="config.options.vertical"
2021-11-16 13:48:18 +01:00
:class="{'is-disabled': isDisabled }"
:locked="isLocked"
:can-unlock="canUnlock || false"
2021-11-16 15:03:24 +01:00
@unlock="unlock"
@lock="lock">
2021-09-14 16:00:45 +02:00
<component :is="config.component" v-bind="config.componentOptions" :value="model" :entity="value" :meta="meta" @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>
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';
2021-11-16 13:48:18 +01:00
import Overlay from 'zero/helpers/overlay.js';
2021-11-16 15:03:24 +01:00
import Arrays from 'zero/helpers/arrays.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
2021-11-15 13:15:00 +01:00
},
disabled: {
type: Boolean,
default: false
},
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,
2021-03-16 11:07:13 +01:00
manualDisabled: false,
selector: null
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-08-23 12:56:40 +02:00
return this.loaded && typeof this.config.options.condition === 'function' && !this.config.options.condition(this.value, this);
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-11-15 13:15:00 +01:00
return this.manualDisabled || this.disabled || (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 });
2021-11-16 13:48:18 +01:00
},
isLocked()
{
return !this.editor.blueprint.unlocked(this.value, this.config);
},
canUnlock()
{
2021-11-16 15:03:24 +01:00
return this.editor.blueprint.canUnlock(this.value, this.config);
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)
{
2021-09-14 10:14:54 +02:00
let oldValue = JSON.parse(JSON.stringify(this.model));
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-09-14 10:14:54 +02:00
if (typeof this.config.options.onChange === 'function')
{
this.config.options.onChange(value, {
oldValue,
model: this.value,
component: this
});
}
2021-01-12 16:06:56 +01:00
},
setDisabled(disabled)
{
this.manualDisabled = disabled;
2021-11-16 13:48:18 +01:00
},
2021-11-16 15:03:24 +01:00
async unlock()
2021-11-16 13:48:18 +01:00
{
Overlay.confirm({
title: 'Unlock property',
text: 'Unlock this property to override the value passed by the blueprint',
confirmLabel: 'Confirm',
closeLabel: 'Cancel'
}).then(
2021-11-16 15:03:24 +01:00
async () => await this.editor.blueprint.unlock(this.value, this.config),
2021-11-16 13:48:18 +01:00
() => {}
);
2021-11-16 15:03:24 +01:00
},
async lock()
{
let originalValue = await this.editor.blueprint.lock(this.value, this.config);
this.onChange(originalValue);
2020-04-30 15:35:09 +02:00
}
2020-04-30 14:21:43 +02:00
}
}
</script>