index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <div class="go-content-charts-item-box">
  3. <!-- 每一项组件的渲染 -->
  4. <div
  5. class="item-box"
  6. v-for="(item, index) in menuOptions"
  7. :key="index"
  8. draggable
  9. @dragstart="dragStartHandle($event, item)"
  10. @dragend="dragendHandle"
  11. @dblclick="dblclickHandle(item)"
  12. >
  13. <div class="list-header">
  14. <mac-os-control-btn :mini="true" :disabled="true"></mac-os-control-btn>
  15. <n-text class="list-header-text" depth="3">{{ item.title }}</n-text>
  16. </div>
  17. <div class="list-center go-flex-center">
  18. <img class="list-img" v-lazy="item.image" alt="图表图片" />
  19. </div>
  20. </div>
  21. </div>
  22. </template>
  23. <script setup lang="ts">
  24. import { PropType } from 'vue'
  25. import { MacOsControlBtn } from '@/components/Tips/MacOsControlBtn/index'
  26. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  27. import { EditCanvasTypeEnum } from '@/store/modules/chartEditStore/chartEditStore.d'
  28. import { componentInstall, loadingStart, loadingFinish, loadingError } from '@/utils'
  29. import { DragKeyEnum } from '@/enums/editPageEnum'
  30. import { createComponent } from '@/packages'
  31. import { ConfigType, CreateComponentType } from '@/packages/index.d'
  32. import { fetchConfigComponent, fetchChartComponent } from '@/packages/index'
  33. import omit from 'lodash/omit'
  34. const chartEditStore = useChartEditStore()
  35. defineProps({
  36. menuOptions: {
  37. type: Array as PropType<ConfigType[]>,
  38. default: () => []
  39. }
  40. })
  41. // 拖拽处理
  42. const dragStartHandle = (e: DragEvent, item: ConfigType) => {
  43. // 动态注册图表组件
  44. componentInstall(item.chartKey, fetchChartComponent(item))
  45. componentInstall(item.conKey, fetchConfigComponent(item))
  46. // 将配置项绑定到拖拽属性上
  47. e!.dataTransfer!.setData(DragKeyEnum.DRAG_KEY, JSON.stringify(omit(item, ['image'])))
  48. // 修改状态
  49. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_CREATE, true)
  50. }
  51. // 拖拽结束
  52. const dragendHandle = () => {
  53. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_CREATE, false)
  54. }
  55. // 双击添加
  56. const dblclickHandle = async (item: ConfigType) => {
  57. try {
  58. loadingStart()
  59. // 动态注册图表组件
  60. componentInstall(item.chartKey, fetchChartComponent(item))
  61. componentInstall(item.conKey, fetchConfigComponent(item))
  62. // 创建新图表组件
  63. let newComponent: CreateComponentType = await createComponent(item)
  64. // 添加
  65. chartEditStore.addComponentList(newComponent, false, true)
  66. // 选中
  67. chartEditStore.setTargetSelectChart(newComponent.id)
  68. loadingFinish()
  69. } catch (error) {
  70. loadingError()
  71. window['$message'].warning(`图表正在研发中, 敬请期待...`)
  72. }
  73. }
  74. </script>
  75. <style lang="scss" scoped>
  76. /* 列表项宽度 */
  77. $itemWidth: 86%;
  78. /* 内容高度 */
  79. $centerHeight: 100px;
  80. @include go('content-charts-item-box') {
  81. .item-box {
  82. margin: 0 7%;
  83. margin-bottom: 15px;
  84. width: $itemWidth;
  85. overflow: hidden;
  86. border-radius: 6px;
  87. cursor: pointer;
  88. border: 1px solid rgba(0, 0, 0, 0);
  89. @include fetch-bg-color('background-color2');
  90. @extend .go-transition;
  91. &:hover {
  92. @include hover-border-color('background-color4');
  93. .list-img {
  94. transform: scale(1.1);
  95. }
  96. }
  97. .list-header {
  98. display: flex;
  99. align-items: center;
  100. justify-content: space-between;
  101. padding: 2px 15px;
  102. @include fetch-bg-color('background-color3');
  103. &-text {
  104. font-size: 12px;
  105. margin-left: 8px;
  106. }
  107. }
  108. .list-center {
  109. padding: 6px 0;
  110. height: $centerHeight;
  111. overflow: hidden;
  112. .list-img {
  113. height: 100%;
  114. border-radius: 6px;
  115. @extend .go-transition;
  116. }
  117. }
  118. }
  119. }
  120. </style>