Skip to content

Commit c8bcb65

Browse files
committed
dashboard/graphs: fix preview not updating when toggling graph legend
The showLegend computed used a non-reactive local variable, so toggling the "Graph legend" checkbox did not update the preview. Read show_legend from props.content directly and reconnect the ResizeObserver via nextTick after the scroll container appears in the DOM. JIRA-Ref: CMK-29278 Change-Id: Ica78b739ff933351262ffde117f679ee043a4070 (cherry picked from commit 8892447)
1 parent 157699a commit c8bcb65

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

packages/cmk-frontend-vue/src/dashboard/components/DashboardContent/DashboardContentGraph.vue

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This file is part of Checkmk (https://checkmk.com). It is subject to the terms a
44
conditions defined in the file COPYING, which is part of this source code package.
55
-->
66
<script setup lang="ts">
7-
import { type Ref, computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
7+
import { type Ref, computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
88
99
import useTimer from '@/lib/useTimer.ts'
1010
@@ -18,7 +18,6 @@ import DashboardContentContainer from './DashboardContentContainer.vue'
1818
import type { ContentProps } from './types.ts'
1919
2020
const props = defineProps<ContentProps>()
21-
const content = props.content as GraphWidgetContent
2221
const cmkToken = useInjectCmkToken()
2322
const dataEndpointUrl: Ref<string> = computed(() => {
2423
return cmkToken ? 'widget_graph_token_auth.py' : 'widget_graph.py'
@@ -31,16 +30,16 @@ const contentDiv = ref<HTMLDivElement | null>(null)
3130
const parentDiv = computed(() => contentDiv.value?.parentElement || null)
3231
const isLoading = ref<boolean>(true)
3332
34-
const observedElement = computed(() => {
35-
if (props.isPreview && contentDiv.value) {
33+
const resolveObservedElement = (): HTMLElement | null => {
34+
if (props.isPreview && showLegend.value && contentDiv.value) {
3635
return (
3736
(contentDiv.value.closest(
3837
'.db-content-container__preview-scroll-container'
3938
) as HTMLElement) ?? parentDiv.value
4039
)
4140
}
4241
return parentDiv.value
43-
})
42+
}
4443
4544
const httpVars: Ref<FilterHTTPVars> = computed(() => {
4645
if (cmkToken !== undefined) {
@@ -93,14 +92,35 @@ const resizeObserver = new ResizeObserver((entries) => {
9392
}
9493
})
9594
96-
const showLegend = computed(() => content.graph_render_options?.show_legend ?? false)
95+
const showLegend = computed(
96+
() => (props.content as GraphWidgetContent).graph_render_options?.show_legend ?? false
97+
)
9798
const timer = useTimer(updateGraph, 60_000)
9899
100+
let currentObservedElement: HTMLElement | null = null
101+
const reconnectResizeObserver = () => {
102+
const newElement = resolveObservedElement()
103+
if (newElement !== currentObservedElement) {
104+
if (currentObservedElement) {
105+
resizeObserver.unobserve(currentObservedElement)
106+
}
107+
if (newElement) {
108+
resizeObserver.observe(newElement)
109+
}
110+
currentObservedElement = newElement
111+
}
112+
}
113+
114+
// Reconnect the ResizeObserver after DOM updates when the scroll container appears/disappears
115+
watch([showLegend, () => props.isPreview], () => {
116+
void nextTick(() => {
117+
reconnectResizeObserver()
118+
})
119+
})
120+
99121
onMounted(() => {
100122
timer.start()
101-
if (observedElement.value) {
102-
resizeObserver.observe(observedElement.value)
103-
}
123+
reconnectResizeObserver()
104124
})
105125
106126
onBeforeUnmount(() => {

0 commit comments

Comments
 (0)