This commit is contained in:
Qiuxia Fan 2022-06-21 17:23:59 +08:00
parent 6b9170cb4a
commit 23f2f11446

View File

@ -18,6 +18,7 @@ limitations under the License. -->
<script lang="ts" setup> <script lang="ts" setup>
import { ref, watch, onMounted } from "vue"; import { ref, watch, onMounted } from "vue";
import dayjs from "dayjs"; import dayjs from "dayjs";
import { useThrottleFn } from "@vueuse/core";
import { useEventStore } from "@/store/modules/event"; import { useEventStore } from "@/store/modules/event";
import { DataSet, Timeline } from "vis-timeline/standalone"; import { DataSet, Timeline } from "vis-timeline/standalone";
import "vis-timeline/styles/vis-timeline-graph2d.css"; import "vis-timeline/styles/vis-timeline-graph2d.css";
@ -26,13 +27,20 @@ const eventStore = useEventStore();
/*global Nullable */ /*global Nullable */
const timeline = ref<Nullable<HTMLDivElement>>(null); const timeline = ref<Nullable<HTMLDivElement>>(null);
const visGraph = ref<Nullable<any>>(null); const visGraph = ref<Nullable<any>>(null);
const oldVal = ref<{ width: number; height: number }>({ width: 0, height: 0 });
const dateFormat = (date: number, pattern = "YYYY-MM-DD HH:mm:ss") => const dateFormat = (date: number, pattern = "YYYY-MM-DD HH:mm:ss") =>
new Date(dayjs(date).format(pattern)); new Date(dayjs(date).format(pattern));
const visDate = (date: number, pattern = "YYYY-MM-DD HH:mm:ss") => const visDate = (date: number, pattern = "YYYY-MM-DD HH:mm:ss") =>
dayjs(date).format(pattern); dayjs(date).format(pattern);
onMounted(() => { onMounted(() => {
resize(); oldVal.value = (timeline.value && timeline.value.getBoundingClientRect()) || {
width: 0,
height: 0,
};
useThrottleFn(resize, 500)();
}); });
function visTimeline() { function visTimeline() {
if (!timeline.value) { if (!timeline.value) {
return; return;
@ -74,14 +82,24 @@ function visTimeline() {
visGraph.value = new Timeline(timeline.value, items, options); visGraph.value = new Timeline(timeline.value, items, options);
} }
function resize() { function resize() {
const observer = new ResizeObserver(() => { const observer = new ResizeObserver((entries) => {
const entry = entries[0];
const cr = entry.contentRect;
if (
Math.abs(cr.width - oldVal.value.width) < 3 &&
Math.abs(cr.height - oldVal.value.height) < 3
) {
return;
}
visTimeline(); visTimeline();
oldVal.value = { width: cr.width, height: cr.height };
}); });
if (timeline.value) {
observer.observe(timeline.value); observer.observe(timeline.value);
}
} }
watch( watch(
() => [eventStore.events], () => eventStore.events,
() => { () => {
visTimeline(); visTimeline();
} }