208 lines
5.3 KiB
Vue
Raw Normal View History

2022-04-21 22:35:28 +08:00
<template>
<div class="go-chart-search">
<n-popover
class="chart-search-popover"
:show-arrow="false"
:show="showPopover"
:to="false"
trigger="hover"
placement="bottom-start"
>
<template #trigger>
<n-input-group>
<n-input
v-model:value.trim="search"
size="small"
:loading="loading"
placeholder="请输入组件名称"
@update:value="searchHandle"
>
<template #suffix>
2022-04-22 10:04:55 +08:00
<n-icon v-show="!loading" :component="SearchIcon" />
2022-04-21 22:35:28 +08:00
</template>
</n-input>
</n-input-group>
</template>
<div class="search-list-box">
<n-scrollbar style="max-height: 500px">
2022-04-22 10:04:55 +08:00
<n-empty
v-show="!searchRes.length"
size="small"
description="没有找到组件~"
></n-empty>
2022-04-21 22:35:28 +08:00
<div
2022-04-22 10:04:55 +08:00
class="list-item go-flex-items-center go-ellipsis-1"
2022-04-21 22:35:28 +08:00
v-for="item in searchRes"
:key="item.key"
:title="item.title"
2022-04-22 10:04:55 +08:00
@click="selectChartHandle(item)"
2022-04-21 22:35:28 +08:00
>
<img class="list-item-img" v-lazy="item.image" alt="展示图" />
2022-06-05 11:40:32 +08:00
<n-text class="list-item-fs" depth="2">{{ item.title }}</n-text>
2022-04-21 22:35:28 +08:00
</div>
</n-scrollbar>
<div class="popover-modal"></div>
</div>
</n-popover>
</div>
</template>
<script setup lang="ts">
2022-04-22 10:04:55 +08:00
import { ref, onUnmounted } from 'vue'
2022-04-21 22:35:28 +08:00
import { icon } from '@/plugins'
import { themeColor, MenuOptionsType } from '../../hooks/useAside.hook'
2022-04-22 10:04:55 +08:00
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { ConfigType, CreateComponentType } from '@/packages/index.d'
import { createComponent } from '@/packages'
2022-04-21 22:35:28 +08:00
import { isString, addEventListener, removeEventListener } from '@/utils'
2022-04-22 10:04:55 +08:00
import { fetchConfigComponent, fetchChartComponent } from '@/packages/index'
import {
componentInstall,
loadingStart,
loadingFinish,
loadingError,
} from '@/utils'
2022-04-21 22:35:28 +08:00
const props = defineProps({
menuOptions: {
type: Array,
default: () => [],
},
})
const { SearchIcon } = icon.ionicons5
2022-04-22 10:04:55 +08:00
const chartEditStore = useChartEditStore()
2022-04-21 22:35:28 +08:00
const showPopover = ref<boolean>(false)
const loading = ref<boolean | undefined>(undefined)
const search = ref<string | null>(null)
const searchRes = ref<ConfigType[]>([])
// 组件数组提取
const listFormatHandle = (options: any[]) => {
const arr = []
for (const item of options) {
arr.push(...item.list)
}
return arr
}
// 组件列表
const List = listFormatHandle(props.menuOptions as unknown as ConfigType[])
2022-04-22 10:04:55 +08:00
// 关闭处理
const closeHandle = () => {
loading.value = undefined
showPopover.value = false
search.value = null
searchRes.value = []
}
2022-04-21 22:35:28 +08:00
// 搜索处理
const searchHandle = (key: string | null) => {
if (!isString(key) || !key.length) {
2022-04-22 10:04:55 +08:00
closeHandle()
2022-04-21 22:35:28 +08:00
return
}
2022-04-22 10:04:55 +08:00
loading.value = true
2022-04-21 22:35:28 +08:00
showPopover.value = true
searchRes.value = List.filter(
(e: ConfigType) => !key || e.title.toLowerCase().includes(key.toLowerCase())
)
2022-04-22 10:04:55 +08:00
setTimeout(() => {
loading.value = undefined
}, 500)
2022-04-21 22:35:28 +08:00
}
// 关闭处理
2022-04-22 10:04:55 +08:00
const listenerCloseHandle = (e: Event) => {
2022-04-21 22:35:28 +08:00
if (!showPopover.value) return
if (!e.target) return
if (!(e.target as any).closest('.go-chart-search')) {
2022-04-22 10:04:55 +08:00
closeHandle()
}
}
// 选择处理
const selectChartHandle = async (item: ConfigType) => {
try {
loadingStart()
// 动态注册图表组件
componentInstall(item.chartKey, fetchChartComponent(item))
componentInstall(item.conKey, fetchConfigComponent(item))
// 创建新图表组件
let newComponent: CreateComponentType = await createComponent(item)
// 添加
chartEditStore.addComponentList(newComponent, false, true)
// 选中
chartEditStore.setTargetSelectChart(newComponent.id)
// 清空搜索
closeHandle()
loadingFinish()
} catch (error) {
loadingError()
window['$message'].warning(`图表正在研发中, 敬请期待...`)
2022-04-21 22:35:28 +08:00
}
}
addEventListener(document, 'click', (e: Event) => {
2022-04-22 10:04:55 +08:00
listenerCloseHandle(e)
2022-04-21 22:35:28 +08:00
})
onUnmounted(() => {
2022-04-22 10:04:55 +08:00
removeEventListener(document, 'click', listenerCloseHandle)
2022-04-21 22:35:28 +08:00
})
</script>
<style lang="scss" scoped>
2022-04-22 10:04:55 +08:00
$width: 178px;
2022-04-21 22:35:28 +08:00
@include go('chart-search') {
width: $width;
2022-04-22 10:04:55 +08:00
margin-right: -12px;
2022-04-21 22:35:28 +08:00
.chart-search-popover {
.search-list-box {
2022-04-22 10:04:55 +08:00
width: calc(#{$width} - 30px);
2022-04-21 22:35:28 +08:00
.list-item {
z-index: 2;
position: relative;
cursor: pointer;
2022-04-22 10:04:55 +08:00
padding: 2px;
2022-06-05 11:40:32 +08:00
padding-left: 6px;
2022-04-21 22:35:28 +08:00
margin-bottom: 5px;
2022-06-05 11:40:32 +08:00
&-fs {
font-size: 12px;
}
2022-04-22 10:04:55 +08:00
&-img {
2022-06-05 11:40:32 +08:00
height: 28px;
2022-04-22 10:04:55 +08:00
margin-right: 5px;
border-radius: 5px;
}
2022-04-21 22:35:28 +08:00
&:hover {
2022-06-05 11:40:32 +08:00
&::before {
content: '';
position: absolute;
width: 3px;
height: 100%;
left: 0;
top: 0;
border-radius: 2px;
background-color: v-bind('themeColor');
}
2022-04-21 22:35:28 +08:00
&::after {
2022-04-22 10:04:55 +08:00
z-index: -1;
2022-04-21 22:35:28 +08:00
content: '';
position: absolute;
width: 100%;
height: 100%;
opacity: 0.1;
left: 0;
top: 0;
border-radius: 5px;
background-color: v-bind('themeColor');
}
}
}
2022-04-22 10:04:55 +08:00
}
2022-04-21 22:35:28 +08:00
}
}
</style>