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

149 lines
3.0 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-09-17 11:26:23 +02:00
<ui-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>
2020-08-20 14:35:08 +02:00
<ui-modules-select ref="moduleSelect" :types="moduleTypes" :value="value" v-if="canAdd" @selected="onAdd" />
2020-08-19 12:53:18 +02:00
</div>
</template>
<script>
import ModulesApi from 'zero/resources/modules.js';
2020-08-20 01:35:08 +02:00
import EditModuleOverlay from './edit-module';
import Overlay from 'zero/services/overlay.js';
2020-08-20 14:35:08 +02:00
import Arrays from 'zero/services/arrays.js';
2020-08-19 12:53:18 +02:00
export default {
name: 'uiModules',
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-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()
{
ModulesApi.getModuleTypes().then(res =>
{
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
{
const alias = 'module.' + module.alias;
let renderer = zero.renderers[alias];
// some modules can have no fields for editing, so we add them directly
if (!renderer.fields || renderer.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,
renderer: alias,
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>