summaryrefslogtreecommitdiff
path: root/packages/frontend/src/components/MkSortOrderEditor.vue
blob: 3ac809cdbf7266eadcf66cfc3ecbe0087f1b71a6 (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
112
113
114
115
116
117
118
<!--
SPDX-FileCopyrightText: syuilo and other misskey contributors
SPDX-License-Identifier: AGPL-3.0-only
-->

<template>
<div :class="$style.sortOrderArea">
	<div :class="$style.sortOrderAreaTags">
		<MkTagItem
			v-for="order in currentOrders"
			:key="order.key"
			:iconClass="order.direction === '+' ? 'ti ti-arrow-up' : 'ti ti-arrow-down'"
			:exButtonIconClass="'ti ti-x'"
			:content="order.key"
			:class="$style.sortOrderTag"
			@click="onToggleSortOrderButtonClicked(order)"
			@exButtonClick="onRemoveSortOrderButtonClicked(order)"
		/>
	</div>
	<MkButton :class="$style.sortOrderAddButton" @click="onAddSortOrderButtonClicked">
		<span class="ti ti-plus"></span>
	</MkButton>
</div>
</template>

<script setup lang="ts" generic="T extends string">
import { toRefs } from 'vue';
import type { MenuItem } from '@/types/menu.js';
import type { SortOrder } from '@/components/MkSortOrderEditor.define.js';
import MkTagItem from '@/components/MkTagItem.vue';
import MkButton from '@/components/MkButton.vue';
import * as os from '@/os.js';

const emit = defineEmits<{
	(ev: 'update', sortOrders: SortOrder<T>[]): void;
}>();

const props = defineProps<{
	baseOrderKeyNames: T[];
	currentOrders: SortOrder<T>[];
}>();

const { currentOrders } = toRefs(props);

function onToggleSortOrderButtonClicked(order: SortOrder<T>) {
	switch (order.direction) {
		case '+':
			order.direction = '-';
			break;
		case '-':
			order.direction = '+';
			break;
	}

	emitOrder(currentOrders.value);
}

function onAddSortOrderButtonClicked(ev: PointerEvent) {
	const menuItems: MenuItem[] = props.baseOrderKeyNames
		.filter(baseKey => !currentOrders.value.map(it => it.key).includes(baseKey))
		.map(it => {
			return {
				text: it,
				action: () => {
					emitOrder([...currentOrders.value, { key: it, direction: '+' }]);
				},
			};
		});
	os.contextMenu(menuItems, ev);
}

function onRemoveSortOrderButtonClicked(order: SortOrder<T>) {
	emitOrder(currentOrders.value.filter(it => it.key !== order.key));
}

function emitOrder(sortOrders: SortOrder<T>[]) {
	emit('update', sortOrders);
}

</script>

<style module lang="scss">
.sortOrderArea {
	display: flex;
	flex-direction: row;
	align-items: flex-start;
	justify-content: flex-start;
}

.sortOrderAreaTags {
	display: flex;
	flex-direction: row;
	align-items: flex-start;
	justify-content: flex-start;
	flex-wrap: wrap;
	gap: 8px;
}

.sortOrderAddButton {
	display: flex;
	justify-content: center;
	align-items: center;
	box-sizing: border-box;
	min-width: 2.0em;
	min-height: 2.0em;
	max-width: 2.0em;
	max-height: 2.0em;
	padding: 8px;
	margin-left: auto;
	border-radius: 9999px;
	background-color: var(--MI_THEME-buttonBg);
}

.sortOrderTag {
	user-select: none;
	cursor: pointer;
}
</style>