-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathruleMap.html
More file actions
439 lines (379 loc) · 11.9 KB
/
ruleMap.html
File metadata and controls
439 lines (379 loc) · 11.9 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.js" integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU=" crossorigin="anonymous"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<link rel="stylesheet" href="ruleMap.css">
</head>
<body>
<script>
var input_file = "bigger.json"
var i = 0,
mr = 4,
nodeSuggestions = [],
root,gWidth,minZoom,brushZoom;
var width = 1000,
height = 750,
nodeWidth = 400,
minimapWidth = width/mr,
minimapHeight,brush,zoom;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.attr('id','main-svg')
.append("g")
var miniSVG = d3.select("body").append("svg")
.attr('id', 'mini-svg')
var caption = d3.select("body").append("div")
.attr("class", "caption")
.style("display", "none")
.style("opacity", 1e-6);
d3.json(input_file, function(error, mttree) {
if (error) {
console.log(error);
} else {
var logic = mttree.data.logic
var subRules = mttree.data.subRules
function remove_GOTOs_and_ARFs(d){
var nodes = [d];
var parent = null;
while(node = nodes.pop()){ //set children of EMBEDDED RULES to children of subrule
if(node.children){ //add children to queue
var newChildren = []
for(var i=0; i<node.children.length;i++){
var child = node.children[i]
if(child.entity.entityType.name==='CONDITION_ALWAYS'){//if ARF, add grandchildren instead
for(var j=0;j<child.children.length;j++){
var grandChild = child.children[j]
if(grandChild.entity.entityType.name==='EMBEDDED_RULE'){
grandChild = subRules[grandChild.GOTO]
newChildren.push(grandChild)
nodes.push(grandChild)
}else{
newChildren.push(grandChild)
nodes.push(grandChild)
}
}
}
else if(child.entity.entityType.name==='EMBEDDED_RULE'){
child = subRules[child.GOTO]
newChildren.push(child)
nodes.push(child)
}
else{
newChildren.push(child)
nodes.push(child)
}
}
node.children = newChildren
}
parent = node
}
}
remove_GOTOs_and_ARFs(logic)
root = d3.hierarchy(logic, function(d){return d.children || null})
leafs = root.leaves().length
if(leafs > 15){
treemap = d3.tree().size([height * leafs/15, width])
}else{
treemap = d3.tree().size([height, width])
}
treemap(root)
// Compute the new tree layout.
var nodes = root.descendants()
var links = nodes.slice(1)
// Update the nodes...
var node_elems = svg.selectAll('g.node')
.data(nodes, function(d) {return d.id || (d.id = ++i) })
.enter().append('g')
.attr('class', 'node')
.attr("transform", function(d) { return `translate(${d.y},${d.x})`})
.on("click",clicked)
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseout", mouseout)
// Add labels for the nodes
node_elems.append("text")
.attr("x", -10)
.attr("dy", ".35em")
.attr("text-anchor", "start")
.style('font-size','20px')
.style('letter-spacing','0px')
.each(function(d) {
var isTruncated = false,
name;
if(d.data.entity.entityType.name === "OUTCOME"){
name = d.data.entity.content
}else{
name = d.data.entity.name
}
var text = d3.select(this)
text.html(name)
while(this.getBBox().width > nodeWidth){
isTruncated = true
name = name.substring(0,name.length-1)
text.html(name)
}
if(isTruncated) name = name + "..."
text.html(name)
})
.attr('textLength',function(d){return this.getBBox().width}) //fixes safari zoom bug
.each(function(d){
var bbox = this.getBBox()
bbox.width = nodeWidth + 10
d.bbox = bbox
})
root.y = 100
var n = 1
// var maxWidth = root.bbox.width
var maxWidth = nodeWidth
gWidth = 0
while(true){
var level = []
root.each(function(d){
if(d.depth===n){
d.y = d.parent.y + maxWidth + 100
level.push(d)
gWidth = Math.max(gWidth,d.y + d.bbox.width)
}
})
maxWidth = d3.max(level, d => d.bbox.width)
if(level.length === 0){
break
}
n+=1
}
yMin = d3.min(root.leaves(),d=>+d.x)
yMax = d3.max(root.leaves(),d=>+d.x)
gHeight = yMax - yMin + 200
gWidth += 200
height = width * gHeight/gWidth
if (height > 750){
height = 750
width = height * gWidth/gHeight
d3.select('svg').attr("width", width)
}else{
d3.select('svg').attr("height", height)
}
minZoom = width/gWidth
maxZoom = minZoom * 4
node_elems.attr("transform", function(d) { return `translate(${d.y},${d.x})`})
//Add rectangle surrounding node
node_elems.insert('rect','text')
.each(function(d){
var rect = d3.select(this)
// var bbox = this.nextSibling.getBBox()
var bbox = d.bbox
var type = d.data.entity.entityType.name
if(type === 'TOPIC'){
color = "#6D256C"
}else if(type === 'ADVICE' || type === 'OUTCOME'){
color = "#FAAB18"
}else if(type === 'INPUT_REQUEST'){
color = "#3DB1F2"
}else if(type === 'CONDITION'){
color = "#CE1B6D"
}else if(d.data.GOTO){
color = "yellow"
}else{
color = "white"
}
rect.attr('x', bbox.x-5)
.attr('y',bbox.y-2)
.attr('width', bbox.width+10)
.attr('height',bbox.height+4)
.attr('fill',color)
.attr('rx',2)
.attr('ry',2)
});
var link = svg.selectAll('path.link')
.data(links, function(d) { return d.id; })
.enter().insert('path','g')
.attr("class", "link")
.attr('d', function(d){return connection(d.parent, d)})
minimapHeight = minimapWidth * gHeight/gWidth
mr = gWidth / minimapWidth
minimap = d3.select('#mini-svg')
.attr('width',minimapWidth)
.attr('height',minimapHeight)
.attr('id','mini-svg')
.append('g')
// .attr("transform", "translate(" + (width - minimapWidth) + "," + (height - minimapHeight) + ")");
minimap.raise()
minimap.append("rect")
.attr("class", "minimap")
.attr("width", minimapWidth)
.attr("height", minimapHeight)
var mini_elems = minimap.selectAll('g.node')
.data(nodes)
.enter().append('g')
.attr('class', 'mininode')
.attr("transform", function(d) { return `translate(${d.y/mr},${d.x/mr})`})
mini_elems.append('rect')
.each(function(d){
var rect = d3.select(this)
var type = d.data.entity.entityType.name
if(type === 'TOPIC'){
color = "#6D256C"
}else if(type === 'ADVICE' || type === 'OUTCOME'){
color = "#FAAB18"
}else if(type === 'INPUT_REQUEST'){
color = "#3DB1F2"
}else if(type === 'CONDITION'){
color = "#CE1B6D"
}else if(d.data.GOTO){
color = "yellow"
}else{
color = "white"
}
rect.attr('x', (d.bbox.x-5)/mr)
.attr('y',(d.bbox.y-2)/mr)
.attr('width', (d.bbox.width+10)/mr)
.attr('height',(d.bbox.height+4)/mr)
.attr('fill',color)
.attr('rx',2)
.attr('ry',2)
})
var link = minimap.selectAll('path.link')
.data(links)
.enter().insert('path','g')
.attr("class", "mini-link")
.attr('d', function(d){return mini_connection(d.parent, d)})
brush = d3.brush()
.extent([[0, 0], [minimapWidth, minimapHeight]])
.on("brush", brushed)
zoom = d3.zoom().on("zoom",zoomed)
.translateExtent([[0,0],[gWidth,gHeight]])
.scaleExtent([minZoom,maxZoom])
d3.select('#main-svg').call(zoom)
.on("dblclick.zoom", null)
brushZoom = d3.zoom().on("zoom",brushZoomed)
.scaleExtent([minZoom,maxZoom])
minimap.append("g")
.attr("class", "brush")
.call(brush)
.call(brush.move, [[0, 0], [minimapWidth/2, minimapHeight/2]])
d3.select('#mini-svg').call(brushZoom)
.on("dblclick.zoom",null)
.on("mousedown.zoom", null)
.on("touchstart.zoom", null)
.on("touchmove.zoom", null)
.on("touchend.zoom", null);
}})
// Creates a right-angle path from parent to the child nodes
function connection(s, d) {
var sY = s.y + s.bbox.width
var path = `M ${sY - 5} ${s.x}
L ${d.y - 45},${s.x}
L ${d.y - 45},${d.x}
L ${d.y - 15},${d.x}`
return path
}
function mini_connection(s, d) {
var sY = (s.y + s.bbox.width)/mr
var sX = s.x/mr
var dY = d.y/mr
var dX = d.x/mr
var path = `M ${sY - 5/mr} ${sX}
L ${dY - 45/mr},${sX}
L ${dY - 45/mr},${dX}
L ${dY - 15/mr},${dX}`
return path
}
function clicked(d){
var ruleId = d.data.ruleSetId
var selectedNodeId = d.data.id
window.open(`https://staging.justgoodadvice.com/rules/${ruleId}?selectedNodeId=${selectedNodeId}`)
}
// Show the caption
function mouseover(d){
var name
if(d.data.entity.entityType.name === "OUTCOME"){ name = d.data.entity.content}
else{ name = d.data.entity.name}
caption
.html('<h3>' + d.data.entity.entityType.name + '</h3><p>' + name + '</p>')
.transition()
.duration(100)
.style("display", "block")
.style("opacity", 1);
}
// move caption pop-over as mouse moves
function mousemove(d){
caption
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY) + 20 + "px");
}
// hide caption
function mouseout(d){
caption.transition()
.duration(100)
.style("display", "none")
.style("opacity", 1e-6)
}
svgEl = document.getElementById('main-svg')
svgMiniEl = document.getElementById('mini-svg')
function zoomed(){
caption.style("display","none")
var t = d3.event.transform
brush.move(d3.select('.brush'), [ [-t.x/mr/t.k,
-t.y/mr/t.k],
[(-t.x/mr+minimapWidth*minZoom)/t.k,
(-t.y/mr+minimapHeight*minZoom)/t.k]
]);
}
var x0,x1,y0,y1,scale;
function brushZoomed(){
var k = d3.event.transform.k
if(k > maxZoom) k = maxZoom;
if(k < minZoom) k = minZoom;
var kr = scale/k
var midX = (x0 + x1)/2
var midY = (y0 + y1)/2
var radX = (x1 - x0)/2 * kr
var radY = (y1 - y0)/2 * kr
x0 = midX - radX
x1 = midX + radX
y0 = midY - radY
y1 = midY + radY
if(x0 < 0){
x1 += -x0
x0 = 0
}
if(x1 > minimapWidth){
x0 -= x1 - minimapWidth
x1 = minimapWidth
}
if(y0 < 0){
y1 += -y0
y0 = 0
}
if(y1 > minimapHeight){
y0 -= y1 - minimapHeight
y1 = minimapHeight
}
brush.move(d3.select('.brush'), [[x0,y0],[x1,y1]] );
}
function brushed() {
var s = d3.event.selection
var c0 = s[0]
var c1 = s[1]
//minimap coordinates
x0 = c0[0]
y0 = c0[1]
x1 = c1[0]
y1 = c1[1]
scale = minimapWidth/(x1-x0) * minZoom
svgEl.__zoom.x = -x0*scale*mr
svgEl.__zoom.y = -y0*scale*mr
svgEl.__zoom.k = scale
if(svgMiniEl.__zoom === undefined){
svgMiniEl.__zoom = jQuery.extend({}, svgEl.__zoom)
}
svgMiniEl.__zoom.x = -x0*scale
svgMiniEl.__zoom.y = -y0*scale
svgMiniEl.__zoom.k = scale
svg.attr("transform",`translate(${-x0*scale*mr},${-y0*scale*mr})scale(${scale})`)
}
</script>
</body>
</html>