Skip to content

Commit 54ed9ab

Browse files
committed
link layout modifications
1 parent 1487279 commit 54ed9ab

File tree

3 files changed

+74
-49
lines changed

3 files changed

+74
-49
lines changed

js2/components/link.js

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ class Link {
22
constructor(eventId, trigger, args, reltype) {
33
this.eventId = eventId;
44
this.trigger = trigger;
5-
this.arguments = args;
5+
this.arguments = args.sort((a,b) => a.anchor.idx - b.anchor.idx);
66
this.links = [];
77
this.reltype = reltype;
8-
// console.log('trigger\t\t', this.val);
9-
// console.log.apply(null, args);
108

119
if (this.trigger) { this.trigger.links.push(this); }
1210
this.arguments.forEach(arg => arg.anchor.links.push(this));
@@ -76,41 +74,58 @@ class Link {
7674
}
7775
})
7876
if (this.line) {
79-
let d;
77+
let d = '';
8078
if (this.trigger) {
81-
d = this.arguments.map((arg, i) => {
82-
let tx = this.trigger.cx
83-
, ty = this.trigger.absoluteY
84-
, ax = arg.anchor.cx
85-
, ay = arg.anchor.absoluteY;
86-
87-
let dx = (tx < ax) ? 10 : -10;
88-
let dy = ty - ay; // FIXME: this only handles case for trigger.y on lower level than arg.y
89-
90-
// get text position
91-
let len = this.svgTexts[i]
92-
.x((ax + tx) / 2)
93-
.y(arg.anchor.absoluteY - 10 - 7)
94-
.length() / 2;
95-
96-
let cx = (ax - tx) / 2 - dx;
97-
if (cx < 0) { len = -len; }
98-
99-
// get path string
100-
return 'M' + [tx, ty]
101-
+ 'c' + [0, -10 - dy, 0, -10 - dy, dx, -10 - dy]
102-
+ 'l' + [cx - len, 0]
103-
+ 'm' + [len * 2, 0]
104-
+ 'l' + [cx - len, 0]
105-
+ 'c' + [dx, 0, dx, 10, dx, 10];
106-
}).join();
79+
// draw a polyline between the trigger and each of its arguments
80+
for (let i = 0, il = this.arguments.length; i < il; ++i) {
81+
let leftOfTrigger = this.arguments[i].anchor.idx < this.trigger.idx;
82+
let dx = leftOfTrigger ? 5 : -5;
83+
let textlen = leftOfTrigger ? this.svgTexts[i].length() : -this.svgTexts[i].length();
84+
85+
// draw a line from the prev arrow segment
86+
if (i > 0) {
87+
if (leftOfTrigger) {
88+
d += 'L' + [this.handles[i + 1].x + dx, this.handles[i + 1].y - 10]
89+
}
90+
else {
91+
d += 'L' + [this.handles[i + 1].x + dx + textlen, this.handles[i + 1].y - 10]
92+
}
93+
}
94+
else if (!leftOfTrigger) {
95+
// start drawing from the trigger
96+
d += 'M' + [this.handles[0].x, this.handles[0].y]
97+
+ 'c' + [0, -10, 0, -10, -dx, -10]
98+
+ 'L' + [this.handles[1].x + dx + textlen, this.handles[1].y - 10];
99+
}
100+
101+
// draw an arrow segment coming from each argument
102+
d += 'M' + [this.handles[i + 1].x, this.handles[i + 1].y]
103+
+ 'c' + [0, -10, 0, -10, dx, -10];
104+
if (leftOfTrigger) {
105+
d += 'm' + [textlen, 0];
106+
}
107+
108+
if (leftOfTrigger && (i + 2 > il || this.arguments[i + 1].anchor.idx >= this.trigger.idx)) {
109+
// draw trigger to the right of the arrow segment
110+
d += 'L' + [this.handles[0].x - dx, this.handles[0].y - 10]
111+
+ 'c' + [dx, 0, dx, 0, dx, 10];
112+
if (i + 1 < il) {
113+
d += 'c' + [0, -10, 0, -10, dx, -10];
114+
}
115+
}
116+
117+
// draw the text svg
118+
this.svgTexts[i]
119+
.x(this.handles[i + 1].x + dx + textlen / 2)
120+
.y(this.handles[i + 1].y - 10 - 7);
121+
}
107122
}
108-
else if (this.relType) {
123+
else if (this.reltype) {
109124
let avg = this.arguments.reduce((acc, a) => acc + a.anchor.cx, 0) / this.arguments.length;
110125
this.svgTexts[0].x(avg)
111126
.y(this.arguments[0].anchor.absoluteY - 10 - 7);
112-
d = 'M' + [this.arguments[0].anchor.cx, this.arguments[0].anchor.absoluteY]
113-
+ 'L' + [this.arguments[1].anchor.cx, this.arguments[1].anchor.absoluteY];
127+
// d = 'M' + [this.arguments[0].anchor.cx, this.arguments[0].anchor.absoluteY]
128+
// + 'L' + [this.arguments[1].anchor.cx, this.arguments[1].anchor.absoluteY];
114129
}
115130
this.line.plot(d);
116131
}
@@ -140,6 +155,13 @@ class Link {
140155
return this.arguments.find(arg => arg.anchor === a);
141156
}
142157

158+
get idx() {
159+
if (this.trigger) {
160+
return this.trigger.idx;
161+
}
162+
return this.arguments.reduce((acc, arg) => acc + arg.anchor.idx, 0) / this.arguments.length;
163+
}
164+
143165
get cx() {
144166
if (this.trigger) {
145167
// if (this.trigger.links.length > 1) {
@@ -149,7 +171,7 @@ class Link {
149171
return this.trigger.cx;
150172
}
151173
if (this.arguments.length > 0) {
152-
return this.arguments.reduce((acc, arg) => acc + arg.cx, 0) / this.arguments.length;
174+
return this.arguments.reduce((acc, arg) => acc + arg.anchor.cx, 0) / this.arguments.length;
153175
}
154176
return 0;
155177
}

js2/components/wordcluster.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ class WordCluster {
197197
get absoluteY() {
198198
return this.endpoints[0].absoluteY;
199199
}
200+
get idx() {
201+
return this.endpoints[0].idx;
202+
}
200203
get cx() {
201204
return this.x;
202205
}

js2/main.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,28 +162,28 @@ const Main = (function() {
162162

163163
parser.data.relations.forEach(rel => {
164164
const arguments = rel.arguments.map(searchForEntity);
165-
166165
// create link
167166
const link = new Link(rel.id, null, arguments, rel.type);
168167

169168
// push link to link array
170169
links.push(link);
171170
});
172171

173-
parser.data.syntax.forEach(syn => {
174-
// create a link between the trigger and each of its arguments
175-
const trigger = entities.find(word => word.eventIds.indexOf(syn.trigger) > -1);
176-
const arguments = syn.arguments.map(arg => {
177-
let anchor = words.find(w => w.syntaxId === arg.id);
178-
return { anchor, type: arg.type };
179-
});
180-
181-
// create link
182-
const link = new Link(syn.id, trigger, arguments);
183-
184-
// push link to link array
185-
links.push(link);
186-
});
172+
// // syntax data
173+
// parser.data.syntax.forEach(syn => {
174+
// // create a link between the trigger and each of its arguments
175+
// const trigger = entities.find(word => word.eventIds.indexOf(syn.trigger) > -1);
176+
// const arguments = syn.arguments.map(arg => {
177+
// let anchor = words.find(w => w.syntaxId === arg.id);
178+
// return { anchor, type: arg.type };
179+
// });
180+
//
181+
// // create link
182+
// const link = new Link(syn.id, trigger, arguments);
183+
//
184+
// // push link to link array
185+
// links.push(link);
186+
// });
187187

188188
return [ words, links, clusters ];
189189
}

0 commit comments

Comments
 (0)