Skip to content

Commit e51c1a9

Browse files
committed
populate links for loaded json
1 parent 271b4c8 commit e51c1a9

File tree

4 files changed

+92
-455
lines changed

4 files changed

+92
-455
lines changed

index.html

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,32 +173,118 @@
173173
function changeDataset() {
174174
if (this.selectedIndex > 0) {
175175
parser.readJson(`./data/data${this.selectedIndex}.json`, function() {
176-
var words = parser.tokens.map((token, i) => new Word(token, i));
176+
177+
// construct word objects and tags from tokens, entities, and triggers
178+
const words = parser.tokens.map((token, i) => new Word(token, i));
179+
177180
[].concat(parser.data.entities, parser.data.triggers).forEach(el => {
178181
if (words[el.tokenIndex]) {
179182
words[el.tokenIndex].tag = el.type;
183+
words[el.tokenIndex].eventId = el.id;
180184
}
181185
});
182-
resetDrawing(words, []);
183-
console.log(parser);
186+
wordObjs = words;
187+
188+
// construct links from events and relations
189+
const links = [];
190+
parser.data.events.forEach(evt => {
191+
const trigger = words.find(word => word.eventId === evt.trigger);
192+
193+
// create a link between the trigger and each of its arguments
194+
evt.arguments.map(arg => {
195+
let anchor;
196+
switch (arg.id.charAt(0)) {
197+
case 'E':
198+
anchor = links.find(link => link.eventId === arg.id);
199+
break;
200+
case 'T':
201+
anchor = words.find(word => word.eventId === arg.id);
202+
break;
203+
default:
204+
console.log('unhandled argument type', arg);
205+
break;
206+
}
207+
208+
// style the arrow
209+
const ltr = arg.type === 'Controlled';
210+
const direction = ltr ? [1, -1] : [-1, 1];
211+
const arrowType = arg.type === 'Theme' ? styles.simpleLine : ltr ? styles.gradientLine1 : styles.gradientLine1r;
212+
213+
// create link
214+
const link = new Link(
215+
[trigger, anchor],
216+
direction,
217+
arrowType,
218+
arg.type,
219+
texts.linkText
220+
);
221+
link.eventId = evt.id;
222+
223+
// push link to link array
224+
links.push(link);
225+
createLink(link);
226+
227+
});//evt.argument.map
228+
});//parser.data.events.forEach
229+
230+
parser.data.relations.forEach(rel => {
231+
let ltr = true;
232+
const arguments = rel.arguments.map((arg, i) => {
233+
let anchor;
234+
switch (arg.id.charAt(0)) {
235+
case 'E':
236+
anchor = links.find(link => link.eventId === arg.id);
237+
break;
238+
case 'T':
239+
anchor = words.find(word => word.eventId === arg.id);
240+
break;
241+
default:
242+
console.log('unhandled argument type', arg);
243+
break;
244+
}
245+
if (arg.type === 'Controlled' && i > 0) {
246+
ltr = false;
247+
}
248+
return anchor;
249+
});
250+
251+
// create link
252+
const link = new Link(
253+
arguments,
254+
ltr ? [-1, 1] : [1, -1],
255+
ltr ? styles.gradientLine2 : styles.gradientLine2r,
256+
rel.type,
257+
texts.linkText
258+
);
259+
260+
// push link to link array
261+
links.push(link);
262+
createLink(link);
263+
});
264+
265+
resetDrawing(words, links);
184266
});
185267
}
186268
};
187269

188270
function resetDrawing(words, links) {
189-
draw.clear();
271+
draw.children().forEach(child => {
272+
if (child.type !== 'defs') {
273+
child.remove();
274+
}
275+
});
190276
rowGroup = draw.group();
191277
groupAllElements = linkG = textG = arrowG = undefined;
192278
isDragging = false;
193279
rowOffsetX = 0;
194280
rowOffsetWord = null;
195281
prevX = -1;
196-
styles = setupStyles(draw); //creates an associative array of whatever
197282
dragElem = null;
198283

199284
rows = [];
200285
wordObjs = words || createTestWords(5, 6, 10);
201286
linkObjs = links || createTestMultiLinks(1);
287+
// linkObjs.forEach(link => link.style = styles[link.style]);
202288
drawWords(wordObjs);
203289
drawLinks(linkObjs);
204290
if (Panel.isOpen) { Panel.close(); }

js/annotate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ function flipIfNecessary(link) {
582582

583583
function sortLinkWords(link) {
584584

585-
console.log("\n\n***\nUNSORTED!");
585+
console.log("\n\n***\nUNSORTED!", link, link.words);
586586

587587
for (var i = 0; i < link.words.length; i++) {
588588
var w = link.words[i];

js/utils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ function createTestMultiLinks(numWord2WordLinks) {
112112
return d1 - d2;
113113
});
114114
*/
115-
116115
Object.keys(ls).forEach(function(key) {
117116

118117
//console.log(ls[key].toString());

0 commit comments

Comments
 (0)