Browse Source

fix(cockpit): 预警列表刷新时保留滚动位置

刷新期间保留列表 DOM,仅做弱提示;loading 结束时按同一浏览上下文还原面板滚动位置。打开事件后主动刷新列表,避免详情与列表状态脱节。
ChenJZ 1 ngày trước cách đây
mục cha
commit
6b56866f8b

+ 47 - 3
src/modules/cockpit/components/EventListPane.vue

@@ -19,7 +19,7 @@
   `data-event-status` 是状态文本,`data-event-mode` 是当前切换。
 -->
 <template>
-    <div class="event-list-pane" :data-event-mode="view.switchView.key">
+    <div ref="rootRef" class="event-list-pane" :data-event-mode="view.switchView.key">
         <!-- 预警/报警分段切换(共享状态:eventStore.mode,四块面板共用一份) -->
         <div class="event-list-pane__switch" role="group" aria-label="预警/报警切换">
             <button
@@ -136,7 +136,7 @@
 </template>
 
 <script setup>
-import { computed, onBeforeUnmount, watch } from 'vue'
+import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue'
 import { eventListSwitchOptions } from '../domain/eventList.js'
 import { sectionPending } from '../domain/panelBlocks.js'
 import { useEventStore } from '../stores/eventStore.js'
@@ -162,6 +162,46 @@ const switchOptions = eventListSwitchOptions()
 /** 还没取过数(界面说"正在取数",不说"没有数据")。 */
 const pending = computed(() => sectionPending(view.value.section))
 
+const rootRef = ref(null)
+let savedScrollTop = 0
+let savedScrollKey = ''
+let stableScrollKey = ''
+
+function scrollHost() {
+    return rootRef.value && rootRef.value.closest ? rootRef.value.closest('.cockpit-panel__body') : null
+}
+
+function scrollContextKey() {
+    const current = view.value
+    return [eventStore.mode, current.section && current.section.state, current.pagination && current.pagination.page, current.filterView && current.filterView.active].join('|')
+}
+
+watch(view, () => {
+    if (!eventStore.loading) stableScrollKey = scrollContextKey()
+}, { immediate: true })
+
+/**
+ * 定时刷新/手动刷新会重取整张当前列表。刷新期间保留列表 DOM,同时在 loading
+ * 结束的同一拍把面板滚动位置放回去;否则长列表一刷新就跳回第一条,用户找不到刚点的卡片。
+ */
+watch(() => eventStore.loading, (loading, previous) => {
+    if (loading && !previous) {
+        savedScrollTop = scrollHost() ? scrollHost().scrollTop : 0
+        savedScrollKey = stableScrollKey || scrollContextKey()
+        return
+    }
+    if (!loading && previous) {
+        const top = savedScrollTop
+        const key = savedScrollKey
+        nextTick(() => {
+            // 换族/翻页/换筛选是另一种浏览上下文,不能被上一次列表的滚动位置污染。
+            if (key !== scrollContextKey()) return
+            const host = scrollHost()
+            if (host) host.scrollTop = top
+        })
+    }
+})
+
 function canOpen(row) {
     return typeof row.code === 'string' && row.code.trim() !== ''
 }
@@ -169,7 +209,11 @@ function canOpen(row) {
 function openEvent(row) {
     eventStore.closeEvent()
     // 取数用内部键;展示与详情里的「事件编码」用业务编号(definition 在 domain 层)。
-    return entityDetailStore.show({ kind: 'event', id: row.code, family: row.family || eventStore.mode }, row, { side })
+    const opened = entityDetailStore.show({ kind: 'event', id: row.code, family: row.family || eventStore.mode }, row, { side })
+    Promise.resolve(opened)
+        .then((ok) => { if (ok) return eventStore.loadCurrentEvents() })
+        .catch(() => {})
+    return opened
 }
 
 function setMode(next) {

+ 7 - 1
src/modules/cockpit/components/WarningResponsePane.vue

@@ -31,7 +31,7 @@
             {{ rangeLabel }} · {{ eventStore.loading ? '正在更新…' : '当前统计窗口' }}
         </p>
         <p v-if="!eventStore.loading && section && section.reason && /历史接口回显/.test(section.reason)" class="warning-response-pane__range-status" role="alert">{{ section.reason }}</p>
-        <PanelBlocks v-if="view && !eventStore.loading" :key="`${panelId}-${eventStore.mode}-${eventStore.range}`" :view="view" @list-page="onListPage" @card-action="onCardAction" />
+        <PanelBlocks v-if="view" :key="`${panelId}-${eventStore.mode}-${eventStore.range}`" :class="{ 'is-updating': eventStore.loading }" :view="view" @list-page="onListPage" @card-action="onCardAction" />
     </div>
 </template>
 
@@ -179,6 +179,12 @@ function refreshAfterOpen(opened) {
     color: var(--text-2);
     font-size: var(--fs-caption);
 }
+/* 刷新时保留列表 DOM 与滚动位置,只做弱提示;不能因 v-if 卸载把用户正在看的卡片滚回顶部。 */
+.warning-response-pane [data-panel-blocks].is-updating {
+    transition: opacity .15s;
+    opacity: .58;
+    pointer-events: none;
+}
 .is-updating { opacity: .45; transition: opacity .15s; }
 
 /* ---- 票 65:工单处置轨迹浮层 ---- */