refactor classic widgets to composition API

This commit is contained in:
Johann150 2022-08-01 21:34:38 +02:00
parent bcb07d819f
commit 2e71083823
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -1,66 +1,58 @@
<template> <template>
<div class="ddiqwdnk"> <div class="ddiqwdnk">
<XWidgets class="widgets" :edit="editMode" :widgets="$store.reactiveState.widgets.value.filter(w => w.place === place)" @add-widget="addWidget" @remove-widget="removeWidget" @update-widget="updateWidget" @update-widgets="updateWidgets" @exit="editMode = false"/> <XWidgets class="widgets" :edit="editMode" :widgets="defaultStore.reactiveState.widgets.value.filter(w => w.place === place)" @add-widget="addWidget" @remove-widget="removeWidget" @update-widget="updateWidget" @update-widgets="updateWidgets" @exit="editMode = false"/>
<button v-if="editMode" class="_textButton edit" style="font-size: 0.9em;" @click="editMode = false"><i class="fas fa-check"></i> {{ $ts.editWidgetsExit }}</button> <button v-if="editMode" class="_textButton edit" style="font-size: 0.9em;" @click="editMode = false"><i class="fas fa-check"></i> {{ i81n.ts.editWidgetsExit }}</button>
<button v-else class="_textButton edit" style="font-size: 0.9em;" @click="editMode = true"><i class="fas fa-pencil-alt"></i> {{ $ts.editWidgets }}</button> <button v-else class="_textButton edit" style="font-size: 0.9em;" @click="editMode = true"><i class="fas fa-pencil-alt"></i> {{ i18n.ts.editWidgets }}</button>
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent, defineAsyncComponent } from 'vue'; import { defineAsyncComponent, onMounted } from 'vue';
import XWidgets from '@/components/widgets.vue'; import XWidgets from '@/components/widgets.vue';
import { defaultStore } from '@/store';
import { i18n } from '@/i18n';
export default defineComponent({ const emit = defineEmits<{
components: { (ev: 'mounted'): void;
XWidgets }>();
},
props: { const props = defineProps<{
place: { place: {
type: String, type: string;
} };
}, }>();
emits: ['mounted'], let editMode: boolean = $ref(false);
data() { onMounted(() => {
return { emit('mounted');
editMode: false, }
};
},
mounted() { function addWidget(widget) {
this.$emit('mounted', this.$el); defaultStore.set('widgets', [{
}, ...widget,
place: props.place,
}, ...defaultStore.state.widgets]);
}
methods: { function removeWidget(widget) {
addWidget(widget) { defaultStore.set('widgets', defaultStore.state.widgets.filter(w => w.id !== widget.id));
this.$store.set('widgets', [{ }
...widget,
place: this.place,
}, ...this.$store.state.widgets]);
},
removeWidget(widget) { function updateWidget({ id, data }) {
this.$store.set('widgets', this.$store.state.widgets.filter(w => w.id !== widget.id)); defaultStore.set('widgets', defaultStore.state.widgets.map(w => w.id === id ? {
}, ...w,
data,
} : w));
}
updateWidget({ id, data }) { function updateWidgets(widgets) {
this.$store.set('widgets', this.$store.state.widgets.map(w => w.id === id ? { defaultStore.set('widgets', [
...w, ...defaultStore.state.widgets.filter(w => w.place !== props.place),
data, ...widgets
} : w)); ]);
}, }
updateWidgets(widgets) {
this.$store.set('widgets', [
...this.$store.state.widgets.filter(w => w.place !== this.place),
...widgets
]);
}
}
});
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>