-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapNode.vue
170 lines (154 loc) · 5.46 KB
/
MapNode.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<template>
<foreignObject
ref="nodeContainer"
class="map-node-container">
<div
ref="nodeDiv"
:class="['map-node','sane-node', {'clicked' : clicked}, {'old' : old}]">
<span>
<div :class="['node-title', {'clicked' : clicked}]">{{node.title}}</div>
<div class="node-description" v-if="clicked">{{node.description}}
</div>
</span>
</div>
</foreignObject>
</template>
<script setup lang="ts">
import {ref, onMounted, nextTick} from 'vue';
import * as Utils from '../lib/utils';
import { NodeMovement }from '../lib/NodeMovement';
import mitt from 'mitt';
const emitter = mitt<Events>();
emitter.on('click', changeClicked);
const emit = defineEmits<{
(e: 'interaction', interaction: Interaction): void,
}>();
const props = defineProps<{
node: MapNode,
connections: Connection[],
}>();
let clicked = ref(false);
let old = ref(false);
const nodeContainer = ref<InstanceType<typeof SVGForeignObjectElement>>();
const nodeDiv = ref<InstanceType<typeof HTMLElement>>();
let canvas : HTMLElement;
async function changeClicked() {
clicked.value = !clicked.value;
await nextTick();
let viewBox = canvas.getAttribute("viewBox");
if (viewBox === undefined || viewBox === null) {
throw "[MapNode:changeClicked] viewBox is undefined or null";
}
if (nodeContainer.value === undefined) {
throw "[MapNode:changeClicked] nodeContainer.value is undefined";
}
const viewBoxSplit = viewBox.split(" ")
.map((el : string) => parseFloat(el)) as [number, number, number, number];
const bounding = canvas.getBoundingClientRect();
const prevSize : NodeSize = {
width: parseFloat(nodeContainer.value.getAttribute("width") as string),
height: parseFloat(nodeContainer.value.getAttribute("height") as string),
};
setSvgSize(viewBoxSplit, bounding);
const currentSize : NodeSize = {
width: parseFloat(nodeContainer.value.getAttribute("width") as string),
height: parseFloat(nodeContainer.value.getAttribute("height") as string),
};
centerSvg(prevSize, currentSize, viewBoxSplit, bounding);
emit('interaction', {
type: clicked.value === true ? 'openedNode' : 'closedNode',
node: props.node.title
});
old.value = true;
};
let movementHandler : NodeMovement;
const setSvgSize = (viewBoxSplit : [number, number, number, number], bounding : DOMRect) => {
if (nodeContainer.value === undefined) {
throw "[MapNode:onMounted] nodeContainer.value is or undefined";
}
if (nodeDiv.value === undefined) {
throw "nodeDiv is undefined";
}
nodeContainer.value.setAttribute("width", `600`);
nodeContainer.value.setAttribute("height", `500`);
const divBounding = nodeDiv.value.getBoundingClientRect();
const [ width, height ] = Utils.convertPixelDistanceToUnit(viewBoxSplit,
bounding,
[ divBounding.width + 4, divBounding.height + 4]);
console.log('divBounding', divBounding.width, divBounding.height, width, height);
nodeContainer.value.setAttribute("width", `${width}`);
nodeContainer.value.setAttribute("height", `${height}`);
nodeContainer.value.style.height = "";
nodeContainer.value.style.width = "";
};
const centerSvg = (prevSize : NodeSize, currentSize : NodeSize,
viewBoxSplit : [number, number, number, number], bounding : DOMRect) => {
const nodeX = parseFloat(nodeContainer.value?.getAttribute('x') as string);
const nodeY = parseFloat(nodeContainer.value?.getAttribute('y') as string);
const newX = nodeX - (currentSize.width - prevSize.width) / 2;
const newY = nodeY - (currentSize.height - prevSize.height) / 2;
nodeContainer.value?.setAttribute('x', newX.toString());
nodeContainer.value?.setAttribute('y', newY.toString());
};
const setSvgPosition = (viewBoxSplit : [number, number, number, number], bounding : DOMRect) => {
if (nodeContainer.value === undefined) {
throw "[MapNode:onMounted] nodeContainer.value is or undefined";
}
const [ x, y ] = Utils.convertPixelToUnit(
viewBoxSplit,
bounding,
props.node.nx,
props.node.ny
);
const position = {x, y};
nodeContainer.value.setAttribute("x", `${props.node.nx}`);
nodeContainer.value.setAttribute("y", `${props.node.ny}`);
}
const emitInteraction = (int : Interaction) => {
int.node = props.node.title;
emit('interaction', int);
};
defineExpose({
nodeContainer,
node: props.node,
});
onMounted(() => {
const tempCanvas = nodeContainer.value?.parentElement;
if (tempCanvas === null || tempCanvas === undefined) {
throw "[MapNode:onMounted] tempCanvas is null or undefined";
}
canvas = tempCanvas;
let viewBox = canvas.getAttribute("viewBox");
if (viewBox === undefined || viewBox === null) {
const size = canvas.getBoundingClientRect();
canvas.setAttribute('viewBox', `0 0 ${size?.width} ${size?.height}`);
viewBox = `0 0 ${size?.width} ${size?.height}`;
}
const viewBoxSplit = viewBox.split(" ")
.map((el : string) => parseFloat(el)) as [number, number, number, number];
const bounding = canvas.getBoundingClientRect();
setSvgSize(viewBoxSplit, bounding);
setSvgPosition(viewBoxSplit, bounding);
movementHandler = new NodeMovement(canvas, nodeContainer.value!, emitter, emitInteraction);
});
</script>
<style lang="scss" scoped>
.map-node-container {
position: absolute;
top: 0;
left: 0;
padding: 2px;
user-select: none;
-webkit-user-select: none;
overflow: visible;
cursor: pointer;
}
div.sane-node {
cursor: pointer;
z-index: 2000;
pointer-events: all;
position: static;
box-sizing: border-box;
display: inline-block;
}
</style>