From 6189378f2856e1a829d99e4f4e51e93a8a1b14da Mon Sep 17 00:00:00 2001 From: Qiuxia Fan Date: Tue, 11 Jan 2022 20:16:33 +0800 Subject: [PATCH] fix: edit widget config --- src/components/Graph.vue | 4 +- src/components/index.ts | 2 - src/store/modules/dashboard.ts | 65 ++++++++----------- src/utils/is.ts | 6 ++ src/views/dashboard/New.vue | 1 - .../dashboard/configuration/ConfigEdit.vue | 51 +++++++++------ src/views/dashboard/controls/Tab.vue | 18 +++-- src/views/dashboard/controls/Widget.vue | 11 ++-- src/views/dashboard/data.ts | 3 +- src/views/dashboard/panel/Layout.vue | 7 +- src/views/dashboard/panel/Tool.vue | 9 +-- 11 files changed, 91 insertions(+), 86 deletions(-) diff --git a/src/components/Graph.vue b/src/components/Graph.vue index 92b8b2cb..cda7e30b 100644 --- a/src/components/Graph.vue +++ b/src/components/Graph.vue @@ -28,6 +28,7 @@ import { import type { PropType } from "vue"; import { useECharts } from "@/hooks/useEcharts"; import { addResizeListener, removeResizeListener } from "@/utils/event"; + /*global Nullable*/ const chartRef = ref>(null); const { setOptions, resize } = useECharts(chartRef as Ref); @@ -45,7 +46,7 @@ onMounted(() => { if (!chartRef.value) { return; } - window.addEventListener("resize", resize); + setOptions(props.option); addResizeListener(unref(chartRef), resize); }); @@ -60,7 +61,6 @@ onBeforeUnmount(() => { if (!chartRef.value) { return; } - window.removeEventListener("resize", resize); removeResizeListener(unref(chartRef), resize); }); diff --git a/src/components/index.ts b/src/components/index.ts index 57da373f..d807304a 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -20,7 +20,6 @@ import Selector from "./Selector.vue"; import Graph from "./Graph.vue"; import type { App } from "vue"; import VueGridLayout from "vue-grid-layout"; -import { ElLoading } from "element-plus"; const components: { [key: string]: any } = { Icon, @@ -28,7 +27,6 @@ const components: { [key: string]: any } = { VueGridLayout, Selector, Graph, - ElLoading, }; const componentsName: string[] = Object.keys(components); diff --git a/src/store/modules/dashboard.ts b/src/store/modules/dashboard.ts index e8d82433..35f3502c 100644 --- a/src/store/modules/dashboard.ts +++ b/src/store/modules/dashboard.ts @@ -29,13 +29,25 @@ interface DashboardState { layerId: string; activedGridItem: string; } - +const newControl: LayoutConfig = { + x: 0, + y: 0, + w: 24, + h: 12, + i: "0", + type: "Widget", + widget: { + title: "Title", + }, + graph: {}, + standard: {}, +}; export const dashboardStore = defineStore({ id: "dashboard", state: (): DashboardState => ({ layout: [ConfigData], showConfig: false, - selectedGrid: ConfigData, + selectedGrid: null, entity: "", layerId: "", activedGridItem: "", @@ -44,36 +56,15 @@ export const dashboardStore = defineStore({ setLayout(data: LayoutConfig[]) { this.layout = data; }, - addWidget() { + addControl(type: string) { const newWidget: LayoutConfig = { - x: 0, - y: 0, - w: 24, - h: 12, + ...newControl, i: String(this.layout.length), - type: "Widget", - widget: { - title: "Title", - }, - graph: {}, - standard: {}, + type, }; - this.layout = this.layout.map((d: LayoutConfig) => { - d.y = d.y + newWidget.h; - return d; - }); - this.layout.push(newWidget); - this.activedGridItem = newWidget.i; - }, - addTab() { - const newWidget: LayoutConfig = { - x: 0, - y: 0, - w: 24, - h: 20, - i: String(this.layout.length), - type: "Tab", - children: [ + if (type === "Tab") { + newWidget.h = 24; + newWidget.children = [ { name: "Tab1", children: [], @@ -82,11 +73,8 @@ export const dashboardStore = defineStore({ name: "Tab2", children: [], }, - ], - widget: {}, - graph: {}, - standard: {}, - }; + ]; + } this.layout = this.layout.map((d: LayoutConfig) => { d.y = d.y + newWidget.h; return d; @@ -153,7 +141,7 @@ export const dashboardStore = defineStore({ this.showConfig = show; }, selectWidget(widget: Nullable) { - this.selectedGrid = ConfigData || widget; //todo + this.selectedGrid = widget; }, setLayer(id: string) { this.layerId = id; @@ -176,9 +164,10 @@ export const dashboardStore = defineStore({ return res.data; }, async fetchMetricValue(config: LayoutConfig) { - if (!config.queryMetricType) { - return; - } + // if (!config.queryMetricType) { + // return; + // } + config.queryMetricType = "readMetricsValues"; const appStoreWithOut = useAppStoreWithOut(); const variable = { condition: { diff --git a/src/utils/is.ts b/src/utils/is.ts index edf4ef36..5f1be52f 100644 --- a/src/utils/is.ts +++ b/src/utils/is.ts @@ -92,3 +92,9 @@ export function isElement(val: unknown): val is Element { export function isMap(val: unknown): val is Map { return is(val, "Map"); } +export function isEmptyObject(val: T): val is T { + if (isObject(val)) { + return Object.keys(val).length === 0; + } + return false; +} diff --git a/src/views/dashboard/New.vue b/src/views/dashboard/New.vue index 4bb17a0d..7a7a5d0e 100644 --- a/src/views/dashboard/New.vue +++ b/src/views/dashboard/New.vue @@ -56,7 +56,6 @@ limitations under the License. --> import { reactive } from "vue"; import { useI18n } from "vue-i18n"; import router from "@/router"; -import { ElInput, ElButton } from "element-plus"; import { useSelectorStore } from "@/store/modules/selectors"; import { EntityType, Options } from "./data"; import uuid from "@/utils/uuid"; diff --git a/src/views/dashboard/configuration/ConfigEdit.vue b/src/views/dashboard/configuration/ConfigEdit.vue index cc4f12b0..1e4c7a84 100644 --- a/src/views/dashboard/configuration/ConfigEdit.vue +++ b/src/views/dashboard/configuration/ConfigEdit.vue @@ -18,22 +18,22 @@ limitations under the License. -->
{{ title }}
+
{{ t("noData") }}
- {{ t("noData") }}
class="selectors" /> v-for="(type, index) in ChartTypes" :key="index" @click="changeChartType(type)" - :class="{ active: type.value === chartType }" + :class="{ active: type.value === states.chartType }" > {{ type.label }} @@ -67,7 +67,7 @@ limitations under the License. --> @@ -94,13 +94,14 @@ import { reactive, defineComponent, toRefs, ref } from "vue"; import { useI18n } from "vue-i18n"; import { useDashboardStore } from "@/store/modules/dashboard"; import { useAppStoreWithOut } from "@/store/modules/app"; -import { ElMessage, ElButton, ElCollapse, ElCollapseItem } from "element-plus"; +import { ElMessage } from "element-plus"; import { ValuesTypes, MetricQueryTypes, ChartTypes } from "../data"; import { Option } from "@/types/app"; import graphs from "../graphs"; import configs from "./graph-styles"; import WidgetOptions from "./WidgetOptions.vue"; import StandardOptions from "./StandardOptions.vue"; +import { isEmptyObject } from "@/utils/is"; export default defineComponent({ name: "ConfigEdit", @@ -109,9 +110,6 @@ export default defineComponent({ ...configs, WidgetOptions, StandardOptions, - ElButton, - ElCollapse, - ElCollapseItem, }, setup() { const loading = ref(false); @@ -148,7 +146,9 @@ export default defineComponent({ tips: selectedGrid.widget.tips, title: selectedGrid.widget.title, }); - queryMetricType(states.metrics[0]); + if (states.metrics[0]) { + queryMetricType(states.metrics[0]); + } async function changeMetrics(arr: Option[]) { if (!arr.length) { @@ -157,7 +157,9 @@ export default defineComponent({ return; } states.metrics = arr.map((d: Option) => String(d.value)); - queryMetricType(String(arr[0].value)); + if (arr[0].value) { + queryMetricType(String(arr[0].value)); + } } async function queryMetricType(metric: string) { @@ -171,11 +173,13 @@ export default defineComponent({ const { typeOfMetrics } = resp.data; states.valueTypes = ValuesTypes[typeOfMetrics]; states.valueType = ValuesTypes[typeOfMetrics][0].value; + queryMetrics(); } function changeValueType(val: Option[]) { states.valueType = String(val[0].value); states.metricQueryType = (MetricQueryTypes as any)[states.valueType]; + queryMetrics(); } function changeChartType(item: Option) { @@ -207,6 +211,9 @@ export default defineComponent({ const json = await dashboardStore.fetchMetricValue( dashboardStore.selectedGrid ); + if (!json) { + return; + } if (json.error) { return; @@ -214,9 +221,7 @@ export default defineComponent({ const metricVal = json.data.readMetricsValues.values.values.map( (d: { value: number }) => d.value ); - const m = - dashboardStore.selectedGrid.metrics && - dashboardStore.selectedGrid.metrics[0]; + const m = states.metrics[0]; if (!m) { return; } @@ -251,7 +256,7 @@ export default defineComponent({ return { ...toRefs(widgetOpt), - ...toRefs(states), + states, changeChartType, changeValueType, changeMetrics, @@ -325,6 +330,12 @@ export default defineComponent({ } } +.no-data { + font-size: 14px; + text-align: center; + line-height: 350px; +} + span.active { background-color: #409eff; color: #fff; diff --git a/src/views/dashboard/controls/Tab.vue b/src/views/dashboard/controls/Tab.vue index de92e32e..8fe1c0ee 100644 --- a/src/views/dashboard/controls/Tab.vue +++ b/src/views/dashboard/controls/Tab.vue @@ -37,12 +37,16 @@ limitations under the License. --> /> - - - - - - + + + + + + + + + +
@@ -73,7 +77,7 @@ limitations under the License. --> -
No widgets, plase add widgets
+
Please add widgets.