Commit a3559c2fe925d9137e71c7c32268acfdd8a66404

Authored by fengwotao
1 parent d6614521

feat(packages/external): 新增自定义按钮,点击跳转

  1 +import { toRefs, ref } from 'vue'
  2 +import { CreateComponentType } from '@/packages/index.d'
  3 +import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  4 +
  5 +// 获取类型
  6 +type ChartEditStoreType = typeof useChartEditStore
  7 +
  8 +// Params 参数修改触发 api 更新图表请求
  9 +export const useChartInteract = (
  10 + chartConfig: CreateComponentType,
  11 + useChartEditStore: ChartEditStoreType,
  12 + param: { [T: string]: any },
  13 + interactEventOn: string,
  14 + target: string,
  15 + status: boolean
  16 +) => {
  17 + const chartEditStore = useChartEditStore()
  18 + const { interactEvents } = chartConfig.events
  19 + const fnOnEvent = interactEvents.filter(item => {
  20 + return item.interactOn === interactEventOn
  21 + })
  22 + if (fnOnEvent.length === 0) return
  23 + fnOnEvent.forEach(item => {
  24 + const index = chartEditStore.fetchTargetIndex(item.interactComponentId)
  25 + if (index === -1) return
  26 + const { Params, Header } = toRefs(chartEditStore.componentList[index].request.requestParams)
  27 + Object.keys(item.interactFn).forEach(key => {
  28 + if (Params.value[key]) {
  29 + Params.value[key] = param[item.interactFn[key]]
  30 + }
  31 + if (Header.value[key]) {
  32 + Header.value[key] = param[item.interactFn[key]]
  33 + }
  34 + })
  35 + updateIndex(chartConfig)
  36 + })
  37 +
  38 + function updateIndex(chart: any) {
  39 + try {
  40 + const targetId = chart.option.dataset[0].target
  41 + const currentId = chart.option.dataset[0].current
  42 + const currentButtonId = chart.option.dataset[0].currentButton
  43 + const targetButtonId = chart.option.dataset[0].targetButton
  44 + const targetIndex = chartEditStore.fetchTargetIndex(targetId)
  45 + const currentIndex = chartEditStore.fetchTargetIndex(currentId)
  46 + const currentButtonIndex = chartEditStore.fetchTargetIndex(currentButtonId)
  47 + const targetButtonIndex = chartEditStore.fetchTargetIndex(targetButtonId)
  48 + chartEditStore.componentList[targetIndex].status.hide = false
  49 + chartEditStore.componentList[currentIndex].status.hide = true
  50 + chartEditStore.componentList[currentButtonIndex].status.hide = true
  51 + chartEditStore.componentList[targetButtonIndex].status.hide = false
  52 + } catch (e) {
  53 + console.error(e)
  54 + }
  55 + }
  56 +}
  57 +
  58 +// 联动事件触发的 type 变更时,清除当前绑定内容
  59 +// eslint-disable-next-line @typescript-eslint/no-empty-function
  60 +export const clearInteractEvent = (chartConfig: CreateComponentType) => {}
... ...
... ... @@ -18,7 +18,8 @@ export const option = {
18 18 currentButton: '',
19 19 targetButton: ''
20 20 }
21   - ]
  21 + ],
  22 + buttonType:'primary'
22 23 }
23 24
24 25 export default class Config extends PublicConfigClass implements CreateComponentType {
... ...
1 1 <template>
2 2 <collapse-item name="按钮配置" :expanded="true">
3   - <setting-item-box name="配置项" :alone="true">
  3 + <setting-item-box name="默认值" :alone="true">
  4 + <n-select size="small" v-model:value="optionData.buttonType" :options="buttonTypeOptions" />
  5 + </setting-item-box>
  6 + <setting-item-box name="跳转配置项" :alone="true">
4 7 <setting-item v-for="(item, index) in optionData.dataset" :key="index">
5 8 <n-input v-model:value="item.label" size="small" placeholder="按钮文字"></n-input>
6 9 <div style="height: 5px"></div>
... ... @@ -28,4 +31,35 @@ defineProps({
28 31 required: true
29 32 }
30 33 })
  34 +
  35 +const buttonTypeOptions = [
  36 + {
  37 + label: 'default',
  38 + value: 'default'
  39 + },
  40 + {
  41 + label: 'primary',
  42 + value: 'primary'
  43 + },
  44 + {
  45 + label: 'tertiary',
  46 + value: 'tertiary'
  47 + },
  48 + {
  49 + label: 'info',
  50 + value: 'info'
  51 + },
  52 + {
  53 + label: 'success',
  54 + value: 'success'
  55 + },
  56 + {
  57 + label: 'warning',
  58 + value: 'warning'
  59 + },
  60 + {
  61 + label: 'error',
  62 + value: 'error'
  63 + }
  64 +]
31 65 </script>
... ...
1 1 <template>
2   - <n-button :style="{ width: `${w}px`, height: `${h}px` }" type="primary" @click="onClick(option.value.dataset[0])">{{
3   - option.value.dataset[0].label
4   - }}</n-button>
  2 + <n-button
  3 + :style="{ width: `${w}px`, height: `${h}px` }"
  4 + :type="buttonType"
  5 + @click="onClick(option.value.dataset[0],true)"
  6 + >{{ option.value.dataset[0].label }}</n-button
  7 + >
5 8 </template>
6 9
7 10 <script setup lang="ts">
... ... @@ -9,7 +12,7 @@ import { PropType, toRefs, shallowReactive, watch, onMounted } from 'vue'
9 12 import cloneDeep from 'lodash/cloneDeep'
10 13 import { CreateComponentType } from '@/packages/index.d'
11 14 import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
12   -import { useChartInteract } from '@/hooks'
  15 +import { useChartInteract } from '@/hooks/external/useButtonInteract.hook'
13 16 import { InteractEventOn } from '@/enums/eventEnum'
14 17 import { ComponentInteractParamsEnum } from './interact'
15 18
... ... @@ -21,21 +24,24 @@ const props = defineProps({
21 24 })
22 25
23 26 const { w, h } = toRefs(props.chartConfig.attr)
  27 +const { buttonType } = toRefs(props.chartConfig.option)
24 28 const option = shallowReactive({
25 29 value: cloneDeep(props.chartConfig.option)
26 30 })
27 31
28   -const onClick = (v: any) => {
  32 +const onClick = (v: any, s: boolean) => {
29 33 useChartInteract(
30 34 props.chartConfig,
31 35 useChartEditStore,
32 36 { [ComponentInteractParamsEnum.DATA]: v?.current },
33   - InteractEventOn.CHANGE
  37 + InteractEventOn.CHANGE,
  38 + v?.target,
  39 + s
34 40 )
35 41 }
36 42
37 43 onMounted(() => {
38   - // onClick(option.value.dataset[0], false)
  44 + onClick(option.value.dataset[0], false)
39 45 })
40 46
41 47 // 手动更新
... ...