|
| 1 | +<script lang="ts" setup> |
| 2 | +import { onMounted, reactive, ref } from 'vue' |
| 3 | +import { |
| 4 | + findNextComponentIndex, |
| 5 | + saveDashboardResourceTarget, |
| 6 | +} from '@/views/dashboard/utils/canvasUtils.ts' |
| 7 | +import { useI18n } from 'vue-i18n' |
| 8 | +import { dashboardApi } from '@/api/dashboard.ts' |
| 9 | +import type { SQTreeNode } from '@/views/dashboard/utils/treeNode.ts' |
| 10 | +import cloneDeep from 'lodash/cloneDeep' |
| 11 | +import { findNewComponentFromList } from '@/views/dashboard/components/component-list.ts' |
| 12 | +import { guid } from '@/utils/canvas.ts' |
| 13 | +
|
| 14 | +const { t } = useI18n() |
| 15 | +const resource = ref(null) |
| 16 | +
|
| 17 | +const optInit = (viewInfo: any) => { |
| 18 | + resourceDialogShow.value = true |
| 19 | + state.viewInfo = viewInfo |
| 20 | +} |
| 21 | +
|
| 22 | +const state = reactive({ |
| 23 | + dashboardList: [] as SQTreeNode[], |
| 24 | + viewInfo: null, |
| 25 | +}) |
| 26 | +
|
| 27 | +const resourceDialogShow = ref(false) |
| 28 | +const loading = ref(false) |
| 29 | +const resourceForm = reactive({ |
| 30 | + addType: 'history', |
| 31 | + dashboardId: '', |
| 32 | + dashboardName: '', |
| 33 | +}) |
| 34 | +
|
| 35 | +const resourceFormRulesNew = ref({ |
| 36 | + dashboardName: [ |
| 37 | + { |
| 38 | + required: true, |
| 39 | + min: 1, |
| 40 | + max: 64, |
| 41 | + message: t('dashboard.length_limit64'), |
| 42 | + trigger: 'change', |
| 43 | + }, |
| 44 | + ], |
| 45 | +}) |
| 46 | +
|
| 47 | +const resourceFormRulesHistory = ref({ |
| 48 | + dashboardId: [ |
| 49 | + { |
| 50 | + required: true, |
| 51 | + min: 1, |
| 52 | + max: 64, |
| 53 | + message: '请选择仪表板', |
| 54 | + trigger: 'change', |
| 55 | + }, |
| 56 | + ], |
| 57 | +}) |
| 58 | +
|
| 59 | +const resetForm = () => { |
| 60 | + resourceForm.dashboardId = '' |
| 61 | + resourceForm.dashboardName = '' |
| 62 | + resourceDialogShow.value = false |
| 63 | +} |
| 64 | +
|
| 65 | +const saveResourcePrepare = () => { |
| 66 | + // @ts-expect-error eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 67 | + resource.value?.validate((result) => { |
| 68 | + if (result) { |
| 69 | + const component = cloneDeep(findNewComponentFromList('SQView')) |
| 70 | + const newComponentId = guid() |
| 71 | + if (resourceForm.addType === 'history' && component) { |
| 72 | + findNextComponentIndex({ id: resourceForm.dashboardId }, (result: any) => { |
| 73 | + const { |
| 74 | + bottomPosition, |
| 75 | + dashboardInfo, |
| 76 | + canvasDataResult, |
| 77 | + canvasStyleResult, |
| 78 | + canvasViewInfoPreview, |
| 79 | + } = result |
| 80 | + const params = { |
| 81 | + opt: 'updateLeaf', |
| 82 | + pid: 'root', |
| 83 | + id: resourceForm.dashboardId, |
| 84 | + name: dashboardInfo.name, |
| 85 | + } |
| 86 | + component['id'] = newComponentId |
| 87 | + component['y'] = bottomPosition + 1 |
| 88 | + canvasDataResult.push(component) |
| 89 | + canvasViewInfoPreview[newComponentId] = state.viewInfo |
| 90 | + const commonParams = { |
| 91 | + componentData: canvasDataResult, |
| 92 | + canvasStyleData: canvasStyleResult, |
| 93 | + canvasViewInfo: canvasViewInfoPreview, |
| 94 | + } |
| 95 | + saveResource(params, commonParams) |
| 96 | + }) |
| 97 | + } else if (resourceForm.addType === 'new' && component) { |
| 98 | + const params = { opt: 'newLeaf', pid: 'root', name: resourceForm.dashboardName } |
| 99 | + component['id'] = newComponentId |
| 100 | + const commonParams = { |
| 101 | + componentData: [component], |
| 102 | + canvasStyleData: {}, |
| 103 | + canvasViewInfo: { newComponentId: state.viewInfo }, |
| 104 | + } |
| 105 | + saveResource(params, commonParams) |
| 106 | + } |
| 107 | + } |
| 108 | + }) |
| 109 | +} |
| 110 | +
|
| 111 | +const saveResource = (params: any, commonParams: any) => { |
| 112 | + saveDashboardResourceTarget(params, commonParams, function () { |
| 113 | + const messageTips = t('common.save_success') |
| 114 | + ElMessage({ |
| 115 | + type: 'success', |
| 116 | + message: messageTips, |
| 117 | + }) |
| 118 | + resetForm() |
| 119 | + }) |
| 120 | +} |
| 121 | +
|
| 122 | +const initDashboardList = () => { |
| 123 | + state.dashboardList = [] |
| 124 | + const params = {} |
| 125 | + dashboardApi.list_resource(params).then((res: SQTreeNode[]) => { |
| 126 | + state.dashboardList = res || [] |
| 127 | + }) |
| 128 | +} |
| 129 | +
|
| 130 | +onMounted(() => { |
| 131 | + initDashboardList() |
| 132 | +}) |
| 133 | +
|
| 134 | +defineExpose({ |
| 135 | + optInit, |
| 136 | +}) |
| 137 | +</script> |
| 138 | + |
| 139 | +<template> |
| 140 | + <el-dialog |
| 141 | + v-model="resourceDialogShow" |
| 142 | + class="create-dialog" |
| 143 | + title="添加到仪表板" |
| 144 | + width="420px" |
| 145 | + :before-close="resetForm" |
| 146 | + append-to-body |
| 147 | + @submit.prevent |
| 148 | + > |
| 149 | + <el-form |
| 150 | + ref="resource" |
| 151 | + v-loading="loading" |
| 152 | + label-position="top" |
| 153 | + require-asterisk-position="right" |
| 154 | + :model="resourceForm" |
| 155 | + :rules="resourceForm.addType === 'new' ? resourceFormRulesNew : resourceFormRulesHistory" |
| 156 | + @submit.prevent |
| 157 | + > |
| 158 | + <el-form-item label="仪表板名称" required prop="addType"> |
| 159 | + <el-radio-group v-model="resourceForm.addType"> |
| 160 | + <el-radio value="history">存量仪表板</el-radio> |
| 161 | + <el-radio value="new">新建仪表板</el-radio> |
| 162 | + </el-radio-group> |
| 163 | + </el-form-item> |
| 164 | + <el-form-item |
| 165 | + v-if="resourceForm.addType === 'new'" |
| 166 | + label="仪表板" |
| 167 | + required |
| 168 | + prop="dashboardName" |
| 169 | + > |
| 170 | + <el-input |
| 171 | + v-model="resourceForm.dashboardName" |
| 172 | + clearable |
| 173 | + placeholder="请输入仪表板名称" |
| 174 | + @keydown.stop |
| 175 | + @keyup.stop |
| 176 | + /> |
| 177 | + </el-form-item> |
| 178 | + <el-form-item |
| 179 | + v-if="resourceForm.addType === 'history'" |
| 180 | + label="仪表板" |
| 181 | + required |
| 182 | + prop="dashboardId" |
| 183 | + > |
| 184 | + <el-select v-model="resourceForm.dashboardId" placeholder="请选择仪表板"> |
| 185 | + <el-option |
| 186 | + v-for="item in state.dashboardList" |
| 187 | + :key="item.id" |
| 188 | + :label="item.name" |
| 189 | + :value="item.id" |
| 190 | + /> |
| 191 | + </el-select> |
| 192 | + </el-form-item> |
| 193 | + </el-form> |
| 194 | + <template #footer> |
| 195 | + <el-button secondary @click="resetForm()">{{ t('common.cancel') }}</el-button> |
| 196 | + <el-button type="primary" @click="saveResourcePrepare()">{{ t('common.confirm') }}</el-button> |
| 197 | + </template> |
| 198 | + </el-dialog> |
| 199 | +</template> |
| 200 | + |
| 201 | +<style lang="less" scoped> |
| 202 | +.tree-content { |
| 203 | + width: 552px; |
| 204 | + height: 380px; |
| 205 | + border: 1px solid #dee0e3; |
| 206 | + border-radius: 4px; |
| 207 | + padding: 8px; |
| 208 | + overflow-y: auto; |
| 209 | +
|
| 210 | + .empty-search { |
| 211 | + width: 100%; |
| 212 | + margin-top: 57px; |
| 213 | + display: flex; |
| 214 | + flex-direction: column; |
| 215 | + align-items: center; |
| 216 | +
|
| 217 | + img { |
| 218 | + width: 100px; |
| 219 | + height: 100px; |
| 220 | + margin-bottom: 8px; |
| 221 | + } |
| 222 | +
|
| 223 | + span { |
| 224 | + font-family: var(--de-custom_font, 'PingFang'); |
| 225 | + font-size: 14px; |
| 226 | + font-weight: 400; |
| 227 | + line-height: 22px; |
| 228 | + color: #646a73; |
| 229 | + } |
| 230 | + } |
| 231 | +} |
| 232 | +
|
| 233 | +.custom-tree-node { |
| 234 | + display: flex; |
| 235 | + align-items: center; |
| 236 | +
|
| 237 | + span { |
| 238 | + margin-left: 8.75px; |
| 239 | + width: 120px; |
| 240 | + white-space: nowrap; |
| 241 | + text-overflow: ellipsis; |
| 242 | + overflow: hidden; |
| 243 | + } |
| 244 | +} |
| 245 | +
|
| 246 | +.custom-tree-folder { |
| 247 | + color: rgb(255, 198, 10); |
| 248 | +} |
| 249 | +</style> |
0 commit comments