Files
mixtape/zero.Backoffice.UI/old_app/components/modules/modules.vue
T

231 lines
4.8 KiB
Vue
Raw Normal View History

2020-08-19 12:53:18 +02:00
<template>
<div v-if="loaded" 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 }">
<module-preview v-for="item in items" :key="item.id" :types="moduleTypes" :value="item" @edit="edit" @remove="remove" @isActive="onChange" :disabled="disabled" />
2020-08-27 12:29:33 +02:00
</div>
2021-01-06 19:29:11 +01:00
<button v-if="canAdd" type="button" class="ui-modules-start-button" @click="selectModule">
2021-01-21 15:14:33 +01:00
<span class="ui-modules-start-button-icon"><ui-icon symbol="fth-plus" :size="19" /></span>
2021-01-06 19:29:11 +01:00
<p class="ui-modules-start-button-text"><strong>Add content</strong> <!--<br>Compose the page by adding modules--></p>
</button>
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';
2021-01-06 19:29:11 +01:00
import ModuleSelectOverlay 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',
2021-01-06 19:29:11 +01:00
components: { ModulePreview },
2020-11-01 23:23:36 +01:00
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: () => ({
loaded: false,
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);
this.loaded = true;
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
2021-01-06 19:29:11 +01:00
selectModule()
{
Overlay.open({
2021-07-22 11:27:04 +02:00
alias: 'modules-select',
2021-01-06 19:29:11 +01:00
component: ModuleSelectOverlay,
types: this.moduleTypes,
2021-07-22 11:27:04 +02:00
width: null
2021-01-06 19:29:11 +01:00
}).then(module => this.onAdd(module), () => { });
},
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;
2020-10-20 16:00:55 +02:00
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.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,
2021-09-07 16:04:38 +02:00
width: 820
2020-08-20 01:35:08 +02:00
}).then(value =>
{
2020-08-20 14:35:08 +02:00
if (isAdd)
{
this.items.push(value);
}
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">
2021-09-23 15:04:36 +02:00
.ui-modules-inner
{
margin: var(--padding-m);
margin-top: -16px;
}
.ui-modules-inner-sortable
2020-08-19 12:53:18 +02:00
{
2021-09-23 15:04:36 +02:00
padding: var(--padding-s) 0;
2020-08-19 12:53:18 +02:00
}
2021-01-06 19:29:11 +01:00
.ui-modules-start-button
{
color: var(--color-primary);
font-size: var(--font-size);
display: inline-grid;
grid-template-columns: auto 1fr;
gap: 25px;
align-items: center;
2021-09-23 15:04:36 +02:00
margin-top: var(--padding);
margin-bottom: -10px;
2021-01-06 19:29:11 +01:00
}
.ui-modules-inner-sortable + .ui-modules-start-button
{
2021-09-23 15:04:36 +02:00
width: 100%;
padding-top: var(--padding);
margin-top: var(--padding-xs);
border-top: 1px dashed var(--color-line-dashed-onbg);
2021-01-06 19:29:11 +01:00
}
.ui-modules-start-button-icon
{
2021-01-21 15:14:33 +01:00
display: inline-flex;
justify-content: center;
align-items: center;
2021-01-06 19:29:11 +01:00
width: 52px;
height: 52px;
background: var(--color-button-light);
border-radius: var(--radius);
}
.ui-modules-start-button-text
{
line-height: 1.3;
color: var(--color-text-dim);
margin: 0;
font-size: var(--font-size-s);
strong
{
display: inline-block;
margin-bottom: 2px;
color: var(--color-text);
font-size: var(--font-size);
}
}
2020-08-19 12:53:18 +02:00
</style>