summaryrefslogtreecommitdiff
path: root/packages/frontend/src/ui/universal.widgets.vue
blob: 35de23ebfa1a69c5ea8dd1ef981352e16ffe0a7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<template>
<div class="efzpzdvf" :class="{ universal: !classic, classic }">
	<XWidgets :edit="editMode" :widgets="widgets" @add-widget="addWidget" @remove-widget="removeWidget" @update-widget="updateWidget" @update-widgets="updateWidgets" @exit="editMode = false"/>

	<button v-if="editMode" class="_textButton" style="font-size: 0.9em;" @click="editMode = false"><i class="ti ti-check"></i> {{ i18n.ts.editWidgetsExit }}</button>
	<button v-else class="_textButton mk-widget-edit" style="font-size: 0.9em;" @click="editMode = true"><i class="ti ti-pencil"></i> {{ i18n.ts.editWidgets }}</button>
</div>
</template>

<script lang="ts">
let editMode = $ref(false);
</script>
<script lang="ts" setup>
import { onMounted } from 'vue';
import XWidgets from '@/components/MkWidgets.vue';
import { i18n } from '@/i18n';
import { defaultStore } from '@/store';

const props = withDefaults(defineProps<{
	// null = 全てのウィジェットを表示
	// left = place: leftだけを表示
	// right = rightとnullを表示
	place?: 'left' | null | 'right';
	classic?: boolean;
}>(), {
	place: null,
	classic: false,
});

const emit = defineEmits<{
	(ev: 'mounted', el?: Element): void;
}>();

let rootEl = $ref<HTMLDivElement>();

const widgets = $computed(() => {
	if (props.place === null) return defaultStore.reactiveState.widgets.value;
	if (props.place === 'left') return defaultStore.reactiveState.widgets.value.filter(w => w.place === 'left');
	return defaultStore.reactiveState.widgets.value.filter(w => w.place !== 'left');
});

onMounted(() => {
	emit('mounted', rootEl);
});

function addWidget(widget) {
	defaultStore.set('widgets', [{
		...widget,
		place: props.place,
	}, ...defaultStore.state.widgets]);
}

function removeWidget(widget) {
	defaultStore.set('widgets', defaultStore.state.widgets.filter(w => w.id !== widget.id));
}

function updateWidget({ id, data }) {
	defaultStore.set('widgets', defaultStore.state.widgets.map(w => w.id === id ? {
		...w,
		data,
		place: props.place,
	} : w));
}

function updateWidgets(thisWidgets) {
	if (props.place === null) {
		defaultStore.set('widgets', thisWidgets);
		return;
	}
	if (props.place === 'left') {
		defaultStore.set('widgets', [
			...thisWidgets.map(w => ({ ...w, place: 'left' })),
			...defaultStore.state.widgets.filter(w => w.place !== 'left' && !thisWidgets.some(t => w.id === t.id)),
		]);
		return;
	}
	defaultStore.set('widgets', [
		...defaultStore.state.widgets.filter(w => w.place === 'left' && !thisWidgets.some(t => w.id === t.id)),
		...thisWidgets.map(w => ({ ...w, place: 'right' })),
	]);
}
</script>

<style lang="scss" scoped>
.efzpzdvf {
	position: sticky;
	height: min-content;
	min-height: 100vh;
	box-sizing: border-box;

	&.universal {
		padding-top: var(--margin);

		> * {
			margin: var(--margin) 0;
		}
	}

	> * {
		width: 300px;

		&:first-child {
			margin-top: 0;
		}
	}

	> .add {
		margin: 0 auto;
	}
}
</style>