Files
mixtape/zero.Web.UI/App/components/modules/modules.vue
T

162 lines
3.2 KiB
Vue
Raw Normal View History

2020-08-19 12:53:18 +02:00
<template>
<div class="ui-modules-inner">
2020-08-27 12:29:33 +02:00
<div v-if="items.length" class="ui-modules-inner-sortable" v-sortable="{ onUpdate: onSortingUpdated }">
2020-11-01 23:23:36 +01:00
<module-preview v-for="item in items" :key="item.id" :types="moduleTypes" :value="item" @edit="edit" @remove="remove" :disabled="disabled" />
2020-08-27 12:29:33 +02:00
</div>
<module-select ref="moduleSelect" :types="moduleTypes" :value="value" v-if="canAdd" @selected="onAdd" />
2020-08-19 12:53:18 +02:00
</div>
</template>
<script>
2020-11-20 15:17:41 +01:00
import ModulesApi from 'zero/api/modules.js';
2020-11-01 22:20:29 +01:00
import EditModuleOverlay from './edit-module.vue';
2020-11-01 23:23:36 +01:00
import ModulePreview from './module-preview.vue';
import ModuleSelect from './module-select.vue';
import Overlay from 'zero/helpers/overlay.js';
import Arrays from 'zero/helpers/arrays.js';
2020-08-19 12:53:18 +02:00
export default {
name: 'uiModules',
2020-11-01 23:23:36 +01:00
components: { ModulePreview, ModuleSelect },
2020-08-19 12:53:18 +02:00
props: {
2020-08-20 01:35:08 +02:00
value: {
type: Array,
default: () => []
},
2020-09-17 11:26:23 +02:00
disabled: {
type: Boolean,
default: false
},
2020-10-20 16:00:55 +02:00
tags: {
type: Array,
default: () => []
},
2020-08-19 12:53:18 +02:00
config: Object
},
data: () => ({
2020-08-20 01:35:08 +02:00
items: [],
2020-08-19 12:53:18 +02:00
moduleTypes: []
}),
2020-08-20 01:35:08 +02:00
watch: {
value(val)
{
this.setup(val);
}
},
computed: {
canAdd()
{
2020-09-17 11:26:23 +02:00
return !this.disabled;
}
},
2020-08-20 01:35:08 +02:00
created()
{
2020-10-20 16:00:55 +02:00
ModulesApi.getModuleTypes(this.tags).then(res =>
2020-08-20 01:35:08 +02:00
{
this.moduleTypes = res;
this.setup(this.value);
2020-08-20 01:35:08 +02:00
});
},
2020-08-19 12:53:18 +02:00
methods: {
2020-08-20 01:35:08 +02:00
setup(value)
2020-08-19 12:53:18 +02:00
{
2020-08-20 01:35:08 +02:00
this.items = JSON.parse(JSON.stringify(value || []));
},
2020-08-19 12:53:18 +02:00
2020-08-20 01:35:08 +02:00
2020-08-20 14:35:08 +02:00
onAdd(module)
{
this.edit(module, null, true);
},
2020-08-27 12:29:33 +02:00
onSortingUpdated(ev)
{
this.items = Arrays.move(this.items, ev.oldIndex, ev.newIndex);
let sort = 0;
this.items.forEach(x => x.sort = sort++);
this.onChange();
},
2020-08-20 14:35:08 +02:00
edit(module, model, isAdd)
2020-08-20 01:35:08 +02:00
{
2020-10-20 16:00:55 +02:00
const alias = 'modules.' + module.alias;
const editor = this.zero.getEditor(alias);
if (!editor)
{
// TODO throw error
}
// some modules can have no fields for editing, so we add them directly
2020-10-20 16:00:55 +02:00
if (!editor.fields || editor.fields.length < 1)
{
return ModulesApi.getEmpty(module.alias).then(res =>
{
this.items.push(res.entity);
this.$refs.moduleSelect.reset();
this.onChange();
});
}
// open editing overlay
2020-08-20 01:35:08 +02:00
return Overlay.open({
component: EditModuleOverlay,
display: 'editor',
module: module,
2020-10-20 16:00:55 +02:00
editor: editor,
2020-08-20 14:35:08 +02:00
model: model,
2020-08-20 01:35:08 +02:00
width: 1100
}).then(value =>
{
2020-08-20 14:35:08 +02:00
if (isAdd)
{
this.items.push(value);
this.$refs.moduleSelect.reset();
}
else
{
Arrays.replace(this.items, model, value);
}
2020-08-20 01:35:08 +02:00
this.onChange();
});
},
2020-08-20 14:35:08 +02:00
remove(module, model)
{
Arrays.remove(this.items, model);
2020-08-20 16:30:08 +02:00
this.onChange();
2020-08-20 14:35:08 +02:00
},
2020-08-20 01:35:08 +02:00
onChange()
{
this.$emit('input', this.items);
}
2020-08-19 12:53:18 +02:00
}
}
</script>
<style lang="scss">
.ui-modules-inner-sortable
2020-08-19 12:53:18 +02:00
{
margin-top: -32px;
2020-08-19 12:53:18 +02:00
}
</style>