/
InteractiveComap.vue
152 lines (130 loc) · 3.93 KB
/
InteractiveComap.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<template>
<div class="map-container">
<svg ref="canvas" class="map" preserveAspectRatio="xMinYMin">
<link-component
v-for="conn in activeConnections"
:key="conn.source + conn.target"
:source="getComponent(conn, 'source')"
:target="getComponent(conn, 'target')"
ref="links"
>
</link-component>
<map-node-component
v-for="node in activeNodes"
:key="node.title"
:node="node"
:connections="getNodeConnections(node)"
@interaction="emitInteraction"
ref="nodeComponents"
></map-node-component>
</svg>
</div>
</template>
<script setup lang="ts">
import { onBeforeMount, onBeforeUnmount, ref, computed, onMounted, nextTick, reactive, watch } from 'vue';
import MapNodeComponent from './MapNode.vue';
import LinkComponent from './LinkComponent.vue';
import { ZoomPanManager } from '../lib/ZoomPanManager';
import { defaultStyle } from '../lib/defaultStyles';
import mitt, { Emitter } from 'mitt';
const emit = defineEmits<{
(e: 'interaction', interaction: Interaction): void,
}>();
const props = defineProps<{
nodes: MapNode[],
connections: Connection[],
toggled: string[],
nodeStyle: 'custom' | 'default',
}>();
const emitter = mitt<ZoomPanEvents>();
const canvas = ref();
const nodeComponents = ref([]);
const links = ref([]);
let savedToggled : string[] = [];
let zoomPanManager : ZoomPanManager;
const activeNodes = computed(() => {
return props.nodes.filter((el) => {
return props.toggled.includes(el.toggler);
})
//return list.value;
});
const activeConnections = computed(() => {
return props.connections.filter((conn : Connection) => {
return activeNodes.value.find((node) => conn.source === node.title) &&
activeNodes.value.find((node) => conn.target === node.title) &&
existsComponent(conn);
})
});
const getNodeConnections = (node : MapNode) => {
return activeConnections.value.filter((conn) => {
return conn.target === node.title || conn.source === node.title;
});
}
const existsComponent = (conn : Connection) : Boolean => {
return nodeComponents.value.find((nodeComp : NodeComponent) => {
return nodeComp.node.title === conn.target;
}) !== undefined &&
nodeComponents.value.find((nodeComp : NodeComponent) => {
return nodeComp.node.title === conn.source;
}) !== undefined;
};
watch(activeNodes, async () => {
await nextTick();
const temp = nodeComponents.value;
nodeComponents.value = [];
nodeComponents.value = temp;
})
const getComponent = (conn : Connection, field : 'source' | 'target') => {
const res = nodeComponents.value.find((nodeComp : NodeComponent) => {
return nodeComp.node.title === conn[field];
});
if (res === undefined) {
throw `Could not find NodeComponent for connection ${conn.source}-${conn.target}`;
}
return res;
}
const emitInteraction = (data : Interaction) => {
emit('interaction', data);
};
watch(props.toggled, () => {
const added = props.toggled.filter(x => !savedToggled.includes(x));
const removed = savedToggled.filter(x => !props.toggled.includes(x));
savedToggled = [...props.toggled];
if (added.length) {
emit('interaction', {type:'addedToggle', data: JSON.stringify(added)});
}
if (removed.length) {
emit('interaction', {type:'removedToggle', data: JSON.stringify(removed)});
}
});
function addDefaultStyle() {
const style = document.createElement('style');
style.innerHTML = defaultStyle;
document.head.appendChild(style);
}
onBeforeMount(() => {
emitter.on('zoomPanInteraction', emitInteraction);
if (props.nodeStyle === 'default') {
addDefaultStyle();
}
});
onMounted(() => {
zoomPanManager = new ZoomPanManager(canvas.value, emitter);
});
onBeforeUnmount(() => {
emitter.off('zoomPanInteraction', emitInteraction);
});
</script>
<style scoped>
.map-container, .map {
width: 100%;
height: 100%;
position: relative;
}
.map-container {
z-index: 100;
}
.map {
z-index: 1000;
}
</style>