82 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-01-25 11:09:32 +08:00
import { ChartList } from '@/packages/components/Charts/index'
import { DecorateList } from '@/packages/components/Decorates/index'
import { InformationList } from '@/packages/components/Informations/index'
import { TableList } from '@/packages/components/Tables/index'
import { PackagesCategoryEnum, PackagesType, ConfigType, FetchComFlagType } from '@/packages/index.d'
2022-03-10 10:14:52 +08:00
const configModules = import.meta.globEager('./components/**/config.vue')
const indexModules = import.meta.globEager('./components/**/index.vue')
const imagesModules = import.meta.globEager('../assets/images/chart/**')
2022-01-14 16:17:14 +08:00
2022-01-25 18:19:44 +08:00
// * 所有图表
2022-03-07 12:33:05 +08:00
export let packagesList: PackagesType = {
2022-01-14 22:07:02 +08:00
[PackagesCategoryEnum.CHARTS]: ChartList,
2022-04-01 16:36:22 +08:00
[PackagesCategoryEnum.INFORMATIONS]: InformationList,
2022-01-15 12:50:00 +08:00
[PackagesCategoryEnum.TABLES]: TableList,
2022-01-19 19:59:11 +08:00
[PackagesCategoryEnum.DECORATES]: DecorateList
2022-01-14 16:17:14 +08:00
}
2022-01-25 18:19:44 +08:00
/**
2022-04-10 17:56:51 +08:00
* *
* @param targetData
2022-01-25 18:19:44 +08:00
*/
export const createComponent = async (targetData: ConfigType) => {
2022-04-10 17:56:51 +08:00
const { category, key } = targetData
const chart = await import(`./components/${targetData.package}/${category}/${key}/config.ts`)
2022-01-25 18:19:44 +08:00
return new chart.default()
}
2022-03-06 21:26:31 +08:00
/**
2022-03-10 10:14:52 +08:00
* *
* @param {string} chartName
* @param {FetchComFlagType} flag 0, 1
*/
const fetchComponent = (chartName: string, flag: FetchComFlagType) => {
const module = flag === FetchComFlagType.VIEW ? indexModules : configModules
2022-03-10 10:14:52 +08:00
for (const key in module) {
const urlSplit = key.split('/')
if (urlSplit[urlSplit.length - 2] === chartName) {
2022-03-10 10:14:52 +08:00
return module[key]
}
}
}
/**
* *
* @param {ConfigType} dropData
*/
export const fetchChartComponent = (dropData: ConfigType) => {
2022-03-10 10:14:52 +08:00
const { key } = dropData
return fetchComponent(key, FetchComFlagType.VIEW)?.default
}
/**
* *
* @param {ConfigType} dropData
2022-03-06 21:26:31 +08:00
*/
export const fetchConfigComponent = (dropData: ConfigType) => {
2022-03-10 10:14:52 +08:00
const { key } = dropData
return fetchComponent(key, FetchComFlagType.CONFIG)?.default
}
/**
* *
* @param {ConfigType} targetData
*/
export const fetchImages = async (targetData?: ConfigType) => {
if (!targetData) return ''
// 新数据动态处理
const { image, package: targetDataPackage } = targetData
// 兼容旧数据
if (image.includes('@') || image.includes('base64')) return image
const imageName = image.substring(image.lastIndexOf('/') + 1)
for (const key in imagesModules) {
const urlSplit = key.split('/')
if (urlSplit[urlSplit.length - 1] === imageName) {
return imagesModules[key]?.default
}
}
return ''
}