Files
mixtape/zero.Web.UI/App/editor/fields/nested.vue
T

132 lines
3.0 KiB
Vue
Raw Normal View History

2020-06-25 16:15:23 +02:00
<template>
2020-06-30 12:12:31 +02:00
<div class="editor-nested" :depth="depth">
2020-09-10 15:16:45 +02:00
<div class="ui-pick-previews" v-if="items.length">
<div v-for="(item, index) in items" class="ui-pick-preview">
<ui-select-button :icon="config.icon" :label="getName(item)" :description="getDescription(item)" :disabled="disabled" @click="editItem(item)" />
2020-09-17 11:26:23 +02:00
<ui-icon-button v-if="!disabled" icon="fth-x" title="@ui.close" @click="removeItem(index)" :disabled="disabled" />
2020-06-25 16:15:23 +02:00
</div>
</div>
2020-09-10 16:12:06 +02:00
<ui-select-button v-if="limit > items.length" icon="fth-plus" :label="config.addLabel || '@ui.add'" @click="addItem" :disabled="disabled" />
2020-06-25 16:15:23 +02:00
</div>
</template>
<script>
2020-06-29 18:20:43 +02:00
import UiEditor from 'zero/editor/editor';
2020-09-10 15:16:45 +02:00
import UiEditorOverlay from 'zero/editor/editor-overlay';
import Overlay from 'zero/services/overlay';
2020-06-29 18:20:43 +02:00
2020-06-25 16:15:23 +02:00
export default {
2020-06-29 18:20:43 +02:00
components: { UiEditor },
2020-06-25 16:15:23 +02:00
props: {
value: {
type: [Array, Object]
},
2020-06-29 18:20:43 +02:00
meta: {
type: Object,
default: () => { }
},
2020-06-30 12:12:31 +02:00
depth: {
type: Number,
default: 0
},
2020-09-10 15:16:45 +02:00
disabled: {
type: Boolean,
default: false
},
2020-06-25 16:15:23 +02:00
config: Object
},
data: () => ({
2020-09-10 16:12:06 +02:00
items: [],
2020-09-29 14:15:22 +02:00
limit: 100,
multiple: false
2020-06-25 16:15:23 +02:00
}),
2020-06-30 12:12:31 +02:00
mounted()
{
this.items = JSON.parse(JSON.stringify(this.value));
2020-09-10 16:12:06 +02:00
this.limit = this.config.limit || this.limit;
2020-09-29 14:15:22 +02:00
this.multiple = this.limit > 1;
if (!this.multiple)
{
this.items = this.items ? [this.items] : [];
}
2020-06-30 12:12:31 +02:00
},
2020-06-25 16:15:23 +02:00
methods: {
2020-06-30 12:12:31 +02:00
getNewItem()
{
2020-09-10 15:16:45 +02:00
return JSON.parse(JSON.stringify(this.config.template || {}));
2020-06-30 12:12:31 +02:00
},
2020-06-25 16:15:23 +02:00
addItem()
{
2020-09-10 16:12:06 +02:00
if (this.limit <= this.items.length)
{
return;
}
2020-09-10 15:16:45 +02:00
this.editItem(this.getNewItem(), true);
this.onChange();
2020-06-29 18:20:43 +02:00
},
2020-09-10 15:16:45 +02:00
editItem(item, isAdd)
2020-06-29 18:20:43 +02:00
{
2020-09-10 15:16:45 +02:00
// open editing overlay
return Overlay.open({
component: UiEditorOverlay,
display: 'editor',
renderer: this.config.renderer,
title: this.config.title || '@ui.edit.title',
model: item,
width: 1100,
create: isAdd
}).then(value =>
{
if (isAdd)
{
this.items.push(value);
}
else
{
const index = this.items.indexOf(item);
this.removeItem(index);
this.items.splice(index, 0, value);
}
this.onChange();
});
2020-06-29 18:20:43 +02:00
},
removeItem(index)
{
this.items.splice(index, 1);
this.onChange();
2020-06-29 18:20:43 +02:00
},
onChange()
2020-06-29 18:20:43 +02:00
{
2020-09-29 14:15:22 +02:00
this.$emit('input', this.multiple ? this.items : (this.items.length > 0 ? this.items[0] : null));
},
getName(item)
{
return this.config.item && typeof this.config.item.label === 'function' ? this.config.item.label(item) : 'Item';
},
getDescription(item)
{
return this.config.item && typeof this.config.item.description === 'function' ? this.config.item.description(item) : '';
2020-06-25 16:15:23 +02:00
}
}
}
2020-09-10 15:16:45 +02:00
</script>