2020-10-17 13:12:00 +02:00
|
|
|
<template>
|
2022-07-17 14:06:33 +02:00
|
|
|
<div>
|
|
|
|
<div
|
|
|
|
ref="itemsEl" v-hotkey="keymap"
|
2023-01-09 22:02:46 +01:00
|
|
|
class="_popup _shadow"
|
|
|
|
:class="[$style.root, { [$style.center]: align === 'center', [$style.asDrawer]: asDrawer }]"
|
2022-07-17 14:06:33 +02:00
|
|
|
:style="{ width: (width && !asDrawer) ? width + 'px' : '', maxHeight: maxHeight ? maxHeight + 'px' : '' }"
|
|
|
|
@contextmenu.self="e => e.preventDefault()"
|
|
|
|
>
|
|
|
|
<template v-for="(item, i) in items2">
|
2023-01-09 22:02:46 +01:00
|
|
|
<div v-if="item === null" :class="$style.divider"></div>
|
|
|
|
<span v-else-if="item.type === 'label'" :class="[$style.label, $style.item]">
|
2022-07-17 14:06:33 +02:00
|
|
|
<span>{{ item.text }}</span>
|
|
|
|
</span>
|
2023-01-09 22:02:46 +01:00
|
|
|
<span v-else-if="item.type === 'pending'" :tabindex="i" :class="[$style.pending, $style.item]">
|
2022-07-17 14:06:33 +02:00
|
|
|
<span><MkEllipsis/></span>
|
|
|
|
</span>
|
2023-01-09 22:02:46 +01:00
|
|
|
<MkA v-else-if="item.type === 'link'" :to="item.to" :tabindex="i" class="_button" :class="$style.item" @click.passive="close(true)" @mouseenter.passive="onItemMouseEnter(item)" @mouseleave.passive="onItemMouseLeave(item)">
|
|
|
|
<i v-if="item.icon" class="ti-fw" :class="[$style.icon, item.icon]"></i>
|
2023-01-16 06:18:11 +01:00
|
|
|
<MkAvatar v-if="item.avatar" :user="item.avatar" :class="$style.avatar"/>
|
2022-07-17 14:06:33 +02:00
|
|
|
<span>{{ item.text }}</span>
|
2023-01-09 22:02:46 +01:00
|
|
|
<span v-if="item.indicate" :class="$style.indicator"><i class="_indicatorCircle"></i></span>
|
2022-07-17 14:06:33 +02:00
|
|
|
</MkA>
|
2023-01-09 22:02:46 +01:00
|
|
|
<a v-else-if="item.type === 'a'" :href="item.href" :target="item.target" :download="item.download" :tabindex="i" class="_button" :class="$style.item" @click="close(true)" @mouseenter.passive="onItemMouseEnter(item)" @mouseleave.passive="onItemMouseLeave(item)">
|
|
|
|
<i v-if="item.icon" class="ti-fw" :class="[$style.icon, item.icon]"></i>
|
2022-07-17 14:06:33 +02:00
|
|
|
<span>{{ item.text }}</span>
|
2023-01-09 22:02:46 +01:00
|
|
|
<span v-if="item.indicate" :class="$style.indicator"><i class="_indicatorCircle"></i></span>
|
2022-07-17 14:06:33 +02:00
|
|
|
</a>
|
2023-01-09 22:02:46 +01:00
|
|
|
<button v-else-if="item.type === 'user'" :tabindex="i" class="_button" :class="[$style.item, { [$style.active]: item.active }]" :disabled="item.active" @click="clicked(item.action, $event)" @mouseenter.passive="onItemMouseEnter(item)" @mouseleave.passive="onItemMouseLeave(item)">
|
2023-02-08 12:07:19 +01:00
|
|
|
<MkAvatar :user="item.user" :class="$style.avatar"/><MkUserName :user="item.user"/>
|
2023-01-09 22:02:46 +01:00
|
|
|
<span v-if="item.indicate" :class="$style.indicator"><i class="_indicatorCircle"></i></span>
|
2022-07-17 14:06:33 +02:00
|
|
|
</button>
|
2023-01-09 22:02:46 +01:00
|
|
|
<span v-else-if="item.type === 'switch'" :tabindex="i" :class="$style.item" @mouseenter.passive="onItemMouseEnter(item)" @mouseleave.passive="onItemMouseLeave(item)">
|
2023-01-07 06:59:54 +01:00
|
|
|
<MkSwitch v-model="item.ref" :disabled="item.disabled" class="form-switch">{{ item.text }}</MkSwitch>
|
2022-07-17 14:06:33 +02:00
|
|
|
</span>
|
2023-01-09 22:02:46 +01:00
|
|
|
<button v-else-if="item.type === 'parent'" :tabindex="i" class="_button" :class="[$style.item, $style.parent, { [$style.childShowing]: childShowingItem === item }]" @mouseenter="showChildren(item, $event)">
|
|
|
|
<i v-if="item.icon" class="ti-fw" :class="[$style.icon, item.icon]"></i>
|
2022-07-17 14:06:33 +02:00
|
|
|
<span>{{ item.text }}</span>
|
2023-01-09 22:02:46 +01:00
|
|
|
<span :class="$style.caret"><i class="ti ti-caret-right ti-fw"></i></span>
|
2022-07-17 14:06:33 +02:00
|
|
|
</button>
|
2023-01-09 22:02:46 +01:00
|
|
|
<button v-else :tabindex="i" class="_button" :class="[$style.item, { [$style.danger]: item.danger, [$style.active]: item.active }]" :disabled="item.active" @click="clicked(item.action, $event)" @mouseenter.passive="onItemMouseEnter(item)" @mouseleave.passive="onItemMouseLeave(item)">
|
|
|
|
<i v-if="item.icon" class="ti-fw" :class="[$style.icon, item.icon]"></i>
|
2023-01-16 06:18:11 +01:00
|
|
|
<MkAvatar v-if="item.avatar" :user="item.avatar" :class="$style.avatar"/>
|
2022-07-17 14:06:33 +02:00
|
|
|
<span>{{ item.text }}</span>
|
2023-01-09 22:02:46 +01:00
|
|
|
<span v-if="item.indicate" :class="$style.indicator"><i class="_indicatorCircle"></i></span>
|
2022-07-17 14:06:33 +02:00
|
|
|
</button>
|
|
|
|
</template>
|
2023-01-09 22:02:46 +01:00
|
|
|
<span v-if="items2.length === 0" :class="[$style.none, $style.item]">
|
2022-07-20 15:24:26 +02:00
|
|
|
<span>{{ i18n.ts.none }}</span>
|
2020-10-17 13:12:00 +02:00
|
|
|
</span>
|
2022-07-17 14:06:33 +02:00
|
|
|
</div>
|
2023-01-09 22:02:46 +01:00
|
|
|
<div v-if="childMenu" :class="$style.child">
|
2022-07-17 16:18:05 +02:00
|
|
|
<XChild ref="child" :items="childMenu" :target-element="childTarget" :root-element="itemsEl" showing @actioned="childActioned"/>
|
2022-07-17 14:06:33 +02:00
|
|
|
</div>
|
2020-10-17 13:12:00 +02:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-01-30 06:11:52 +01:00
|
|
|
<script lang="ts" setup>
|
2023-02-24 12:48:19 +01:00
|
|
|
import { defineAsyncComponent, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue';
|
2021-11-11 18:02:25 +01:00
|
|
|
import { focusPrev, focusNext } from '@/scripts/focus';
|
2023-01-07 06:59:54 +01:00
|
|
|
import MkSwitch from '@/components/MkSwitch.vue';
|
2022-01-30 06:11:52 +01:00
|
|
|
import { MenuItem, InnerMenuItem, MenuPending, MenuAction } from '@/types/menu';
|
2022-07-17 14:06:33 +02:00
|
|
|
import * as os from '@/os';
|
2022-07-20 15:24:26 +02:00
|
|
|
import { i18n } from '@/i18n';
|
|
|
|
|
2022-09-06 11:21:49 +02:00
|
|
|
const XChild = defineAsyncComponent(() => import('./MkMenu.child.vue'));
|
2022-01-30 06:11:52 +01:00
|
|
|
|
|
|
|
const props = defineProps<{
|
|
|
|
items: MenuItem[];
|
|
|
|
viaKeyboard?: boolean;
|
|
|
|
asDrawer?: boolean;
|
|
|
|
align?: 'center' | string;
|
|
|
|
width?: number;
|
|
|
|
maxHeight?: number;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
2022-07-17 14:06:33 +02:00
|
|
|
(ev: 'close', actioned?: boolean): void;
|
2022-01-30 06:11:52 +01:00
|
|
|
}>();
|
|
|
|
|
2023-01-03 02:12:37 +01:00
|
|
|
let itemsEl = $shallowRef<HTMLDivElement>();
|
2022-01-30 06:11:52 +01:00
|
|
|
|
|
|
|
let items2: InnerMenuItem[] = $ref([]);
|
|
|
|
|
2023-01-03 05:37:32 +01:00
|
|
|
let child = $shallowRef<InstanceType<typeof XChild>>();
|
2022-07-17 14:06:33 +02:00
|
|
|
|
2022-01-30 06:11:52 +01:00
|
|
|
let keymap = $computed(() => ({
|
|
|
|
'up|k|shift+tab': focusUp,
|
|
|
|
'down|j|tab': focusDown,
|
|
|
|
'esc': close,
|
|
|
|
}));
|
|
|
|
|
2022-07-17 14:06:33 +02:00
|
|
|
let childShowingItem = $ref<MenuItem | null>();
|
|
|
|
|
2022-01-30 06:11:52 +01:00
|
|
|
watch(() => props.items, () => {
|
|
|
|
const items: (MenuItem | MenuPending)[] = [...props.items].filter(item => item !== undefined);
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
const item = items[i];
|
|
|
|
|
|
|
|
if (item && 'then' in item) { // if item is Promise
|
|
|
|
items[i] = { type: 'pending' };
|
|
|
|
item.then(actualItem => {
|
|
|
|
items2[i] = actualItem;
|
2020-10-17 13:12:00 +02:00
|
|
|
});
|
|
|
|
}
|
2022-01-30 06:11:52 +01:00
|
|
|
}
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2022-01-30 06:11:52 +01:00
|
|
|
items2 = items as InnerMenuItem[];
|
|
|
|
}, {
|
|
|
|
immediate: true,
|
|
|
|
});
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2023-02-24 12:48:19 +01:00
|
|
|
let childMenu = ref<MenuItem[] | null>();
|
2023-01-03 02:12:37 +01:00
|
|
|
let childTarget = $shallowRef<HTMLElement | null>();
|
2022-07-17 14:06:33 +02:00
|
|
|
|
|
|
|
function closeChild() {
|
2023-02-24 12:48:19 +01:00
|
|
|
childMenu.value = null;
|
2022-07-17 14:06:33 +02:00
|
|
|
childShowingItem = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function childActioned() {
|
|
|
|
closeChild();
|
|
|
|
close(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onGlobalMousedown(event: MouseEvent) {
|
|
|
|
if (childTarget && (event.target === childTarget || childTarget.contains(event.target))) return;
|
|
|
|
if (child && child.checkHit(event)) return;
|
|
|
|
closeChild();
|
|
|
|
}
|
|
|
|
|
|
|
|
let childCloseTimer: null | number = null;
|
|
|
|
function onItemMouseEnter(item) {
|
|
|
|
childCloseTimer = window.setTimeout(() => {
|
|
|
|
closeChild();
|
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
function onItemMouseLeave(item) {
|
|
|
|
if (childCloseTimer) window.clearTimeout(childCloseTimer);
|
|
|
|
}
|
|
|
|
|
2023-02-24 12:48:19 +01:00
|
|
|
let childrenCache = new WeakMap();
|
2022-07-17 14:06:33 +02:00
|
|
|
async function showChildren(item: MenuItem, ev: MouseEvent) {
|
2023-02-24 12:48:19 +01:00
|
|
|
const children = ref([]);
|
|
|
|
if (childrenCache.has(item)) {
|
|
|
|
children.value = childrenCache.get(item);
|
|
|
|
} else {
|
|
|
|
if (typeof item.children === 'function') {
|
|
|
|
children.value = [{
|
|
|
|
type: 'pending',
|
|
|
|
}];
|
|
|
|
item.children().then(x => {
|
|
|
|
children.value = x;
|
|
|
|
childrenCache.set(item, x);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
children.value = item.children;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 14:06:33 +02:00
|
|
|
if (props.asDrawer) {
|
2023-02-24 12:48:19 +01:00
|
|
|
os.popupMenu(children, ev.currentTarget ?? ev.target);
|
2022-07-17 14:06:33 +02:00
|
|
|
close();
|
|
|
|
} else {
|
|
|
|
childTarget = ev.currentTarget ?? ev.target;
|
2023-02-24 12:48:19 +01:00
|
|
|
childMenu = children;
|
2022-07-17 14:06:33 +02:00
|
|
|
childShowingItem = item;
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
2022-07-17 14:06:33 +02:00
|
|
|
}
|
2022-01-30 06:11:52 +01:00
|
|
|
|
|
|
|
function clicked(fn: MenuAction, ev: MouseEvent) {
|
|
|
|
fn(ev);
|
2022-07-17 14:06:33 +02:00
|
|
|
close(true);
|
2022-01-30 06:11:52 +01:00
|
|
|
}
|
|
|
|
|
2022-07-17 14:06:33 +02:00
|
|
|
function close(actioned = false) {
|
|
|
|
emit('close', actioned);
|
2022-01-30 06:11:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function focusUp() {
|
|
|
|
focusPrev(document.activeElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
function focusDown() {
|
|
|
|
focusNext(document.activeElement);
|
|
|
|
}
|
2022-07-17 14:06:33 +02:00
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
if (props.viaKeyboard) {
|
|
|
|
nextTick(() => {
|
|
|
|
focusNext(itemsEl.children[0], true, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-08 05:44:38 +01:00
|
|
|
// TODO: アクティブな要素までスクロール
|
|
|
|
//itemsEl.scrollTo();
|
|
|
|
|
2022-07-17 14:06:33 +02:00
|
|
|
document.addEventListener('mousedown', onGlobalMousedown, { passive: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
document.removeEventListener('mousedown', onGlobalMousedown);
|
|
|
|
});
|
2020-10-17 13:12:00 +02:00
|
|
|
</script>
|
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
<style lang="scss" module>
|
|
|
|
.root {
|
2020-10-17 13:12:00 +02:00
|
|
|
padding: 8px 0;
|
2021-11-28 12:07:37 +01:00
|
|
|
box-sizing: border-box;
|
2021-10-03 08:39:39 +02:00
|
|
|
min-width: 200px;
|
2021-11-05 02:48:37 +01:00
|
|
|
overflow: auto;
|
2021-12-03 04:00:11 +01:00
|
|
|
overscroll-behavior: contain;
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2021-10-03 08:39:39 +02:00
|
|
|
&.center {
|
2020-10-17 13:12:00 +02:00
|
|
|
> .item {
|
2021-10-03 08:39:39 +02:00
|
|
|
text-align: center;
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
&.asDrawer {
|
2023-01-10 05:50:34 +01:00
|
|
|
padding: 12px 0 max(env(safe-area-inset-bottom, 0px), 12px) 0;
|
2020-10-17 13:12:00 +02:00
|
|
|
width: 100%;
|
2023-01-09 22:02:46 +01:00
|
|
|
border-radius: 24px;
|
|
|
|
border-bottom-right-radius: 0;
|
|
|
|
border-bottom-left-radius: 0;
|
2021-10-03 08:39:39 +02:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
> .item {
|
|
|
|
font-size: 1em;
|
|
|
|
padding: 12px 24px;
|
2022-06-05 05:23:57 +02:00
|
|
|
|
|
|
|
&:before {
|
2023-01-09 22:02:46 +01:00
|
|
|
width: calc(100% - 24px);
|
|
|
|
border-radius: 12px;
|
2022-06-05 05:23:57 +02:00
|
|
|
}
|
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
> .icon {
|
|
|
|
margin-right: 14px;
|
|
|
|
width: 24px;
|
|
|
|
}
|
|
|
|
}
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
> .divider {
|
|
|
|
margin: 12px 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-03 08:39:39 +02:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
.item {
|
|
|
|
display: block;
|
|
|
|
position: relative;
|
|
|
|
padding: 5px 16px;
|
|
|
|
width: 100%;
|
|
|
|
box-sizing: border-box;
|
|
|
|
white-space: nowrap;
|
|
|
|
font-size: 0.9em;
|
|
|
|
line-height: 20px;
|
|
|
|
text-align: left;
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
|
|
|
|
&:before {
|
|
|
|
content: "";
|
|
|
|
display: block;
|
|
|
|
position: absolute;
|
|
|
|
z-index: -1;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
right: 0;
|
|
|
|
margin: auto;
|
|
|
|
width: calc(100% - 16px);
|
|
|
|
height: 100%;
|
|
|
|
border-radius: 6px;
|
|
|
|
}
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
&:not(:disabled):hover {
|
|
|
|
color: var(--accent);
|
|
|
|
text-decoration: none;
|
2021-10-03 08:39:39 +02:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
&:before {
|
|
|
|
background: var(--accentedBg);
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
2023-01-09 22:02:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
&.danger {
|
|
|
|
color: #ff2a2a;
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
&:hover {
|
|
|
|
color: #fff;
|
2021-10-09 06:12:41 +02:00
|
|
|
|
|
|
|
&:before {
|
2023-01-09 22:02:46 +01:00
|
|
|
background: #ff4242;
|
2021-10-09 06:12:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
&:active {
|
|
|
|
color: #fff;
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
&:before {
|
|
|
|
background: #d42e2e !important;
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-09 22:02:46 +01:00
|
|
|
}
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
&:active,
|
|
|
|
&.active {
|
|
|
|
color: var(--fgOnAccent) !important;
|
|
|
|
opacity: 1;
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
&:before {
|
|
|
|
background: var(--accent) !important;
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
2023-01-09 22:02:46 +01:00
|
|
|
}
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
&:not(:active):focus-visible {
|
|
|
|
box-shadow: 0 0 0 2px var(--focus) inset;
|
|
|
|
}
|
2022-07-17 14:06:33 +02:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
&.label {
|
|
|
|
pointer-events: none;
|
|
|
|
font-size: 0.7em;
|
|
|
|
padding-bottom: 4px;
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
> span {
|
|
|
|
opacity: 0.7;
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
2023-01-09 22:02:46 +01:00
|
|
|
}
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
&.pending {
|
|
|
|
pointer-events: none;
|
|
|
|
opacity: 0.7;
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
&.none {
|
|
|
|
pointer-events: none;
|
|
|
|
opacity: 0.7;
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
2021-12-16 18:14:40 +01:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
&.parent {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
cursor: default;
|
2021-12-16 18:14:40 +01:00
|
|
|
|
2023-01-09 22:02:46 +01:00
|
|
|
&.childShowing {
|
|
|
|
color: var(--accent);
|
|
|
|
text-decoration: none;
|
2021-12-16 18:14:40 +01:00
|
|
|
|
|
|
|
&:before {
|
2023-01-09 22:02:46 +01:00
|
|
|
background: var(--accentedBg);
|
2021-12-16 18:14:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
2023-01-09 22:02:46 +01:00
|
|
|
|
|
|
|
.icon {
|
2023-01-25 11:49:17 +01:00
|
|
|
margin-right: 8px;
|
2023-01-09 22:02:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
.caret {
|
|
|
|
margin-left: auto;
|
|
|
|
}
|
|
|
|
|
|
|
|
.avatar {
|
|
|
|
margin-right: 5px;
|
|
|
|
width: 20px;
|
|
|
|
height: 20px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.indicator {
|
|
|
|
position: absolute;
|
|
|
|
top: 5px;
|
|
|
|
left: 13px;
|
|
|
|
color: var(--indicator);
|
|
|
|
font-size: 12px;
|
|
|
|
animation: blink 1s infinite;
|
|
|
|
}
|
|
|
|
|
|
|
|
.divider {
|
|
|
|
margin: 8px 0;
|
|
|
|
border-top: solid 0.5px var(--divider);
|
|
|
|
}
|
2020-10-17 13:12:00 +02:00
|
|
|
</style>
|