2023-09-13 18:12:36 +08:00
|
|
|
<template>
|
2023-11-24 18:08:28 +08:00
|
|
|
<div class="relative" v-if="listItems.length > 0 || alwaysShow" v-click-outside="closeMenu" >
|
2024-06-07 18:05:31 +08:00
|
|
|
<button ref="field" :class="btnClasses" :title="title" @click="isOpen = !isOpen" :data-e2e="dataE2e">
|
2023-09-13 18:12:36 +08:00
|
|
|
<i v-if="btnIcon" :class="btnIcon"></i>
|
|
|
|
{{ btnText }}
|
2023-12-05 03:59:16 +08:00
|
|
|
<i v-if="caret && isOpen" class="sn-icon sn-icon-up"></i>
|
2023-09-15 18:39:56 +08:00
|
|
|
<i v-else-if="caret" class="sn-icon sn-icon-down"></i>
|
2023-09-13 18:12:36 +08:00
|
|
|
</button>
|
2024-02-20 17:55:03 +08:00
|
|
|
<template v-if="isOpen">
|
|
|
|
<teleport to="body">
|
|
|
|
<div ref="flyout"
|
2024-07-12 20:21:40 +08:00
|
|
|
class="fixed z-[3000] sn-menu-dropdown bg-sn-white rounded p-2.5 sn-shadow-menu-sm flex flex-col gap-[1px]"
|
2024-02-20 17:55:03 +08:00
|
|
|
:class="{
|
|
|
|
'right-0': position === 'right',
|
|
|
|
'left-0': position === 'left',
|
|
|
|
}"
|
|
|
|
>
|
|
|
|
<span v-for="(item, i) in listItems" :key="i" class="contents">
|
|
|
|
<div v-if="item.dividerBefore" class="border-0 border-t border-solid border-sn-light-grey"></div>
|
|
|
|
<a :href="item.url" v-if="!item.submenu"
|
2024-04-02 04:43:29 +08:00
|
|
|
v-html="item.text"
|
2024-02-20 17:55:03 +08:00
|
|
|
:target="item.url_target || '_self'"
|
2024-03-28 21:51:28 +08:00
|
|
|
:class="{ 'bg-sn-super-light-blue': item.active, 'disabled': item.disabled }"
|
|
|
|
:style="item.disabled === 'style-only' && 'pointer-events: all'"
|
|
|
|
:title="item.title"
|
2024-02-20 17:55:03 +08:00
|
|
|
:data-toggle="item.modalTarget && 'modal'"
|
|
|
|
:data-target="item.modalTarget"
|
2024-03-06 09:17:45 +08:00
|
|
|
:data-e2e="item.data_e2e"
|
2024-04-02 04:43:29 +08:00
|
|
|
class="block whitespace-nowrap rounded px-3 py-2.5 hover:!text-sn-blue hover:no-underline cursor-pointer hover:bg-sn-super-light-grey leading-5 relative"
|
2024-02-20 17:55:03 +08:00
|
|
|
@click="handleClick($event, item)"
|
2023-12-05 03:59:16 +08:00
|
|
|
>
|
2024-02-20 17:55:03 +08:00
|
|
|
</a>
|
|
|
|
<div v-else class="-mx-2.5 px-2.5 group relative">
|
|
|
|
<span
|
2023-12-05 03:59:16 +08:00
|
|
|
:class="{ 'bg-sn-super-light-blue': item.active }"
|
2024-02-20 17:55:03 +08:00
|
|
|
class="flex group items-center rounded relative text-sn-blue whitespace-nowrap px-3 py-2.5 hover:no-underline cursor-pointer
|
|
|
|
group-hover:bg-sn-super-light-blue hover:!bg-sn-super-light-grey"
|
2024-06-07 18:05:31 +08:00
|
|
|
:data-e2e="item.data_e2e"
|
2024-02-20 17:55:03 +08:00
|
|
|
>
|
|
|
|
{{ item.text }}
|
|
|
|
<i class="sn-icon sn-icon-right ml-auto"></i>
|
|
|
|
</span>
|
|
|
|
<div
|
|
|
|
class="absolute bg-sn-white top-0 rounded p-2.5 sn-shadow-menu-sm flex flex-col gap-[1px] tw-hidden group-hover:block"
|
|
|
|
:class="{
|
|
|
|
'left-0 ml-[100%]': item.position === 'right',
|
|
|
|
'right-0 mr-[100%]': item.position === 'left'
|
|
|
|
}"
|
2023-12-05 03:59:16 +08:00
|
|
|
>
|
2024-02-20 17:55:03 +08:00
|
|
|
<a v-for="(sub_item, si) in item.submenu" :key="si"
|
|
|
|
:href="sub_item.url"
|
|
|
|
:traget="sub_item.url_target || '_self'"
|
|
|
|
:class="{ 'bg-sn-super-light-blue': item.active }"
|
2024-06-07 18:05:31 +08:00
|
|
|
:data-e2e="`${sub_item.data_e2e}`"
|
2024-02-20 17:55:03 +08:00
|
|
|
class="block whitespace-nowrap rounded px-3 py-2.5 hover:!text-sn-blue hover:no-underline cursor-pointer hover:bg-sn-super-light-grey leading-5"
|
|
|
|
@click="handleClick($event, sub_item)"
|
|
|
|
>
|
|
|
|
{{ sub_item.text }}
|
|
|
|
</a>
|
|
|
|
</div>
|
2023-12-05 03:59:16 +08:00
|
|
|
</div>
|
2024-02-20 17:55:03 +08:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</teleport>
|
|
|
|
</template>
|
2023-09-13 18:12:36 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
2024-01-11 22:40:03 +08:00
|
|
|
import { vOnClickOutside } from '@vueuse/components';
|
2023-12-05 03:59:16 +08:00
|
|
|
import FixedFlyoutMixin from './mixins/fixed_flyout.js';
|
2023-10-03 22:03:11 +08:00
|
|
|
|
2023-09-13 18:12:36 +08:00
|
|
|
export default {
|
|
|
|
name: 'DropdownMenu',
|
|
|
|
props: {
|
2023-09-15 18:39:56 +08:00
|
|
|
listItems: { type: Array, default: () => [] },
|
2023-09-13 18:12:36 +08:00
|
|
|
position: { type: String, default: 'left' },
|
|
|
|
btnClasses: { type: String, default: 'btn btn-light' },
|
|
|
|
btnText: { type: String, required: false },
|
|
|
|
btnIcon: { type: String, required: false },
|
|
|
|
caret: { type: Boolean, default: false },
|
2024-01-30 19:02:04 +08:00
|
|
|
alwaysShow: { type: Boolean, default: false },
|
2024-05-16 20:34:59 +08:00
|
|
|
title: { type: String, default: '' },
|
2024-06-07 18:05:31 +08:00
|
|
|
dataE2e: { type: String, default: '' }
|
2023-09-13 18:12:36 +08:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
2023-12-05 03:59:16 +08:00
|
|
|
isOpen: false
|
2024-01-04 23:34:36 +08:00
|
|
|
};
|
2023-09-13 18:12:36 +08:00
|
|
|
},
|
2023-11-09 19:40:06 +08:00
|
|
|
directives: {
|
|
|
|
'click-outside': vOnClickOutside
|
|
|
|
},
|
2023-12-05 03:59:16 +08:00
|
|
|
mixins: [FixedFlyoutMixin],
|
2024-03-04 16:24:04 +08:00
|
|
|
watch: {
|
|
|
|
isOpen(newValue) {
|
2024-03-20 23:06:46 +08:00
|
|
|
this.$emit('menu-toggle', newValue);
|
2024-03-04 16:24:04 +08:00
|
|
|
}
|
|
|
|
},
|
2023-09-13 18:12:36 +08:00
|
|
|
methods: {
|
|
|
|
closeMenu() {
|
2023-12-05 03:59:16 +08:00
|
|
|
this.isOpen = false;
|
2023-09-13 18:12:36 +08:00
|
|
|
},
|
|
|
|
handleClick(event, item) {
|
|
|
|
if (!item.url) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item.emit) {
|
2023-12-01 07:01:08 +08:00
|
|
|
this.$emit(item.emit, item.params);
|
|
|
|
this.$emit('dtEvent', item.emit, item);
|
2023-09-13 18:12:36 +08:00
|
|
|
}
|
2023-09-15 18:39:56 +08:00
|
|
|
this.closeMenu();
|
2023-09-13 18:12:36 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-04 23:34:36 +08:00
|
|
|
};
|
2023-09-13 18:12:36 +08:00
|
|
|
</script>
|