-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgestures.html
263 lines (218 loc) · 6.85 KB
/
gestures.html
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<!DOCTYPE html>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v2.min.js?2.9.3"></script>
<style>
.link {
stroke: rgba(0, 0, 0, 0.2);
}
.node text {
cursor: pointer;
}
.node circle {
stroke: #FFF;
stroke-width: 3px;
fill: #555;
}
body, html {
width: 100%;
height: 100%;
overflow: hidden;
font-family: system-ui;
}
.legend {
position: fixed;
bottom: 1em;
left: 1em;
width: 150px;
z-index: 1000;
}
.legend > div {
margin: 0.5em 0;
}
.state-color {
width: 1em;
height: 1em;
margin-right: 4px;
display: inline-block;
border-radius: 4px;
}
</style>
<body>
<div class="legend">
<div><span class="state-color possible"> </span>Possible</div>
<div><span class="state-color began"> </span>Began</div>
<div><span class="state-color changed"> </span>Changed</div>
<div><span class="state-color ended"> </span>Ended</div>
<div><span class="state-color cancelled"> </span>Cancelled</div>
<div><span class="state-color failed"> </span>Failed</div>
</div>
<script>
const stateColorMap = {
possible : "darkgreen",
began : "blue",
changed : "cyan",
ended : "orange",
cancelled : "purple",
failed : "red"
};
const legendContainer = document.querySelector(".legend");
legendContainer.style.display = "none";
function initializeGraph(graphData) {
let translationOffsetX = 0;
let translationOffsetY = 0;
let isMovingBackground = false;
let gravity = 0.1;
const width = innerWidth, height = innerHeight;
const svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
const force = d3.layout.force()
.gravity(gravity)
.distance(100)
.charge(-1000)
.size([width, height]);
force.nodes(graphData.nodes)
.links(graphData.links)
.start();
const link = svg.selectAll(".link")
.data(graphData.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", d => Math.sqrt(d.weight));
const node = svg.selectAll(".node")
.data(graphData.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
const getFillColor = d => {
return stateColorMap[d.state?.toLowerCase()] || "black";
};
const getFontWeight = d => {
if (d.name.includes("WKDeferringGestureRecognizer") || d.name.includes("WK"))
return 700;
return 400;
};
const getFontSize = d => {
if (d.name.includes("WKDeferringGestureRecognizer"))
return 18;
return 14;
};
const getDotRadius = d => {
if (d.name.includes("WKDeferringGestureRecognizer"))
return 8;
return 5;
};
node.append("circle")
.attr("r", getDotRadius)
.style("fill", getFillColor);
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(d => d.name)
.attr("font-weight", getFontWeight)
.attr("font-size", getFontSize)
.style("fill", getFillColor);
force.on("tick", () => {
link.attr("x1", d => translationOffsetX + d.source.x)
.attr("y1", d => translationOffsetY + d.source.y)
.attr("x2", d => translationOffsetX + d.target.x)
.attr("y2", d => translationOffsetY + d.target.y);
node.attr("transform", d => "translate(" + (translationOffsetX + d.x) + "," + (translationOffsetY + d.y) + ")");
});
addEventListener("resize", () => {
let [width, height] = [innerWidth, innerHeight];
svgContainer.setAttribute("width", width);
svgContainer.setAttribute("height", height);
force.size([width, height]).resume();
});
let lastScaleFactor = 1;
addEventListener("gesturestart", e => {
lastScaleFactor = e.scale;
e.preventDefault();
});
addEventListener("gesturechange", e => {
const scale = e.scale / lastScaleFactor;
lastScaleFactor = e.scale;
e.preventDefault();
gravity /= scale;
force.gravity(gravity).resume();
});
addEventListener("gestureend", e => {
lastScaleFactor = e.scale;
e.preventDefault();
});
let svgContainer = document.querySelector("svg");
let lastMouseX = 0;
let lastMouseY = 0;
let updateMouseLocation = e => {
[lastMouseX, lastMouseY] = [e.pageX, e.pageY];
};
svgContainer.addEventListener("mousedown", e => {
isMovingBackground = e.target === svgContainer;
updateMouseLocation(e);
});
svgContainer.addEventListener("mousemove", e => {
if (!isMovingBackground)
return;
translationOffsetX += (e.pageX - lastMouseX);
translationOffsetY += (e.pageY - lastMouseY);
updateMouseLocation(e);
force.resume();
});
svgContainer.addEventListener("mouseup", e => {
isMovingBackground = false;
updateMouseLocation(e);
});
for (let state in stateColorMap)
document.querySelector(`.state-color.${state}`).style.backgroundColor = stateColorMap[state];
legendContainer.style.display = "";
}
function parseText(text) {
return JSON.parse(text.split("\n").map(line => {
const i = line.indexOf("<WK>: ");
return i === -1 ? line : line.substring(i + 6);
}).join(""));
}
function filterGraphData(graphData) {
let originalNodeIDToNodeMap = {};
let originalNodeIDToNewNodeIDMap = {};
let connectedOriginalNodeIDs = new Set();
let filteredNodes = [];
let filteredLinks = [];
for (let index in graphData.nodes)
originalNodeIDToNodeMap[index] = graphData.nodes[index];
for (let link of graphData.links) {
connectedOriginalNodeIDs.add(link.source);
connectedOriginalNodeIDs.add(link.target);
}
for (let index in originalNodeIDToNodeMap) {
if (connectedOriginalNodeIDs.has(parseInt(index))) {
originalNodeIDToNewNodeIDMap[index] = filteredNodes.length;
filteredNodes.push(originalNodeIDToNodeMap[index]);
}
}
for (let link of graphData.links) {
filteredLinks.push({
source: originalNodeIDToNewNodeIDMap[link.source],
target: originalNodeIDToNewNodeIDMap[link.target],
weight: link.weight
});
}
return {
nodes: filteredNodes,
links: filteredLinks
};
}
function handleDropOrPaste(event) {
event.preventDefault();
const dataTransfer = event.dataTransfer ? event.dataTransfer : event.clipboardData;
let graphData = parseText(dataTransfer.getData("text/plain"));
graphData = filterGraphData(graphData);
initializeGraph(graphData);
document.designMode = "off";
}
document.body.addEventListener("drop", handleDropOrPaste);
document.body.addEventListener("paste", handleDropOrPaste);
document.designMode = "on";
</script>