Skip to content

Commit 4f16da0

Browse files
committed
syntax tags
1 parent 3cd8874 commit 4f16da0

File tree

7 files changed

+97
-42
lines changed

7 files changed

+97
-42
lines changed

css/style2.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ svg text {
2727

2828
#tooltip {
2929
width:175px;
30-
background:#f6f6f6;
30+
background:hsl(40,10%,98%);
3131
margin-bottom:10px;
3232
box-shadow: 0 0 10px #555;
3333
border-radius:3px;
@@ -102,6 +102,13 @@ svg text {
102102
.word .word-tag {
103103
fill:#333;
104104
}
105+
.word .syntax-tag {
106+
fill: rgb(203, 120, 111);
107+
}
108+
.toggle-syntax .syntax-tag {
109+
display:none;
110+
}
111+
105112
.editing .word-tag,
106113
.editing text {
107114
fill: #fff;

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<option>passage1</option>
1818
<option>passage2</option>
1919
</select>
20+
<button id="syntax-toggle">Toggle syntax</button>
2021
</div>
2122

2223
<div id="main">

js2/components/tag.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
class WordTag {
2-
constructor(val, word) {
2+
constructor(val, word, above = true) {
33
this.val = val;
44
this.entity = word;
5+
this.position = above;
56

67
if (!word.svg) {
78
console.log("error: word must have an svg element");
@@ -11,7 +12,8 @@ class WordTag {
1112
this.svg = word.svg.group()
1213
.y(this.tagOffset);
1314
this.svgText = this.svg.text(this.val)
14-
.addClass('word-tag');
15+
.addClass(above ? 'word-tag' : 'word-tag syntax-tag');
16+
this.ww = this.svgText.length();
1517

1618
// add click and right-click listeners
1719
let svg = word.mainSVG;
@@ -30,22 +32,25 @@ class WordTag {
3032
return this.svg.remove();
3133
}
3234
updateWordWidth() {
33-
let ww = this.entity.ww;
34-
if (this.entity.val.length < 9) {
35-
this.line.plot('M0,25,l0,8');
36-
}
37-
else {
38-
let diff = ww / 2;
39-
this.line.plot('M0,25,c0,8,'
40-
+ [diff,0,diff,8]
41-
+ ',M0,25,c0,8,'
42-
+ [-diff,0,-diff,8]
43-
);
35+
if (this.position) {
36+
let ww = this.entity.ww;
37+
if (this.entity.val.length < 9) {
38+
this.line.plot('M0,25,l0,8');
39+
}
40+
else {
41+
let diff = ww / 2;
42+
this.line.plot('M0,25,c0,8,'
43+
+ [diff,0,diff,8]
44+
+ ',M0,25,c0,8,'
45+
+ [-diff,0,-diff,8]
46+
);
47+
}
4448
}
4549
}
4650
text(val) {
4751
this.val = val;
4852
this.svgText.text(this.val);
53+
this.ww = this.svgText.length();
4954
if (this.editingRect) {
5055
let bbox = this.svgText.bbox();
5156
if (bbox.width > 0) {
@@ -95,11 +100,7 @@ class WordTag {
95100
this.entity.calculateBox();
96101
}
97102
}
98-
// word width
99-
get ww() {
100-
return this.svgText.length();
101-
}
102103
get tagOffset() {
103-
return -28;
104+
return this.position ? -28 : 20;
104105
}
105106
}

js2/components/word.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Word {
77
this.slot = 0;
88
this.boxWidth = 0;
99
this.boxHeight = 0;
10+
this.descendHeight = 0;
1011
this.isPunct = (val.length === 1 && val.charCodeAt(0) < 65); // FIXME: doesn't handle fancier unicode punct | should exclude left-punctuation e.g. left-paren or left-quote
1112
this.clusters = [];
1213
this.links = [];
@@ -23,6 +24,10 @@ class Word {
2324
this.eventIds.push(id);
2425
}
2526
}
27+
setSyntaxId(id) {
28+
this.syntaxId = id;
29+
}
30+
2631
setTag(tag) {
2732
if (this.svg) {
2833
if (tag instanceof WordTag) {
@@ -41,6 +46,25 @@ class Word {
4146
}
4247
return this.tag;
4348
}
49+
setSyntaxTag(tag) {
50+
if (this.svg) {
51+
if (tag instanceof WordTag) {
52+
this.syntaxTag = tag;
53+
}
54+
else if (this.syntaxTag instanceof WordTag) {
55+
this.syntaxTag.text(tag);
56+
}
57+
else {
58+
this.tag = new WordTag(tag, this, false);
59+
}
60+
this.calculateBox();
61+
}
62+
else {
63+
this.syntaxTag = tag;
64+
}
65+
return this.syntaxTag;
66+
}
67+
4468
init(svg) {
4569
this.mainSVG = svg;
4670
this.svg = svg.group()
@@ -53,6 +77,9 @@ class Word {
5377
if (this.tag && !(this.tag instanceof WordTag)) {
5478
this.tag = new WordTag(this.tag, this);
5579
}
80+
if (this.syntaxTag && !(this.syntaxTag instanceof WordTag)) {
81+
this.syntaxTag = new WordTag(this.syntaxTag, this, false);
82+
}
5683

5784
// draw cluster info
5885
this.clusters.forEach((cluster) => {
@@ -116,9 +143,12 @@ class Word {
116143

117144
calculateBox() {
118145
let minWidth = (this.tag instanceof WordTag) ? Math.max(this.tag.ww, this.ww) : this.ww;
146+
// if (this.syntaxTag instanceof WordTag && this.syntaxTag.ww > minWidth) { minWidth = this.syntaxTag.ww; }
119147
let diff = this.boxWidth - minWidth;
120148
this.boxWidth -= diff;
121-
this.boxHeight = this.svg.bbox().height;
149+
this.descendHeight = this.syntaxTag instanceof WordTag ? this.syntaxTag.svgText.bbox().height : 0;
150+
this.boxHeight = this.svg.bbox().height - this.descendHeight;
151+
122152
this.dx(diff / 2);
123153
this.mainSVG.fire('word-move', {object: this, x: 0});
124154
}
@@ -129,7 +159,8 @@ class Word {
129159
}
130160

131161
get absoluteY() {
132-
return this.row ? this.row.ry + this.row.rh - this.boxHeight - 5 : 0;
162+
// console.log(this.svgText.bbox().height);
163+
return this.row ? this.row.ry + this.row.rh - this.boxHeight : 0;
133164
}
134165
get cx() {
135166
return this.x + this.boxWidth / 2;

js2/components/wordcluster.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class WordCluster {
199199
return this.endpoints[0].row;
200200
}
201201
get absoluteY() {
202-
return this.endpoints[0].absoluteY + 10;
202+
return this.endpoints[0].absoluteY;
203203
}
204204
get idx() {
205205
return this.endpoints[0].idx;

js2/main.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ const Main = (function() {
5959
changeDataset(this.selectedIndex);
6060
}
6161
}
62+
document.getElementById('syntax-toggle').onclick = function() {
63+
svg.toggleClass('toggle-syntax');
64+
rm.resizeAll();
65+
}
6266
}
6367

6468
/**
@@ -97,6 +101,7 @@ const Main = (function() {
97101
links.forEach(link => {
98102
link.init(svg, words);
99103
});
104+
rm.resizeAll();
100105
}
101106

102107
//--------------------------------
@@ -106,8 +111,8 @@ const Main = (function() {
106111
// construct word objects and tags from tokens, entities, and triggers
107112
const words = parser.tokens.map((token, i) => {
108113
let w = new Word(token.text, i);
109-
w.syntaxTag = token.type;
110-
w.syntaxId = token.id;
114+
w.setSyntaxTag(token.type);
115+
w.setSyntaxId(token.id);
111116
return w;
112117
});
113118
const clusters = [];
@@ -169,21 +174,22 @@ const Main = (function() {
169174
links.push(link);
170175
});
171176

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-
// });
177+
// syntax data
178+
parser.data.syntax.forEach(syn => {
179+
// create a link between the trigger and each of its arguments
180+
const trigger = entities.find(word => word.eventIds.indexOf(syn.trigger) > -1);
181+
const arguments = syn.arguments.map(arg => {
182+
let anchor = words.find(w => w.syntaxId === arg.id);
183+
return { anchor, type: arg.type };
184+
});
185+
186+
// create link
187+
// console.log(syn.id, trigger, arguments, syn);
188+
// const link = new Link(syn.id, trigger, arguments);
189+
//
190+
// // push link to link array
191+
// links.push(link);
192+
});
187193

188194
return [ words, links, clusters ];
189195
}

js2/managers/rowmanager.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ const RowManager = (function() {
77
_svg = svg;
88
}
99

10+
resizeAll() {
11+
_rows.forEach(row => {
12+
row.calculateMaxSlot();
13+
let dy = row.minHeight - row.rh;
14+
if (dy > 0) { this.resizeRow(row.idx, dy); }
15+
});
16+
}
17+
1018
resizeRow(i, dy) {
1119
let row = _rows[i];
1220
dy = Math.max(-row.rh + row.minHeight, dy);
@@ -16,7 +24,7 @@ const RowManager = (function() {
1624
_rows[j].dy(dy);
1725
_rows[j].words.forEach(word => word.redrawLinks());
1826
}
19-
_svg.height(this.lastRow.ry2 + ROW_PADDING);
27+
_svg.height(this.lastRow.ry2 + ROW_PADDING + 20);
2028
}
2129

2230
width(rw) {
@@ -32,7 +40,7 @@ const RowManager = (function() {
3240
const lr = this.lastRow;
3341
const row = !lr ? new Row(_svg) : new Row(_svg, lr.idx + 1, lr.ry2 + ROW_PADDING);
3442
_rows.push(row);
35-
_svg.height(row.ry2 + ROW_PADDING);
43+
_svg.height(row.ry2 + ROW_PADDING + 20);
3644
return row;
3745
}
3846

@@ -42,12 +50,13 @@ const RowManager = (function() {
4250
removeRow() {
4351
_rows.pop().remove();
4452
if (this.lastRow) {
45-
_svg.height(this.lastRow.ry2 + ROW_PADDING);
53+
_svg.height(this.lastRow.ry2 + ROW_PADDING + 20);
4654
}
4755
}
4856

4957
addWordToRow(word, row, i, ignorePosition) {
5058
if (isNaN(i)) { i = row.words.length; }
59+
if (word.row && word.row !== row) { word.row.calculateMaxSlot(); }
5160

5261
let overflow = row.addWord(word, i, ignorePosition);
5362
while (overflow < row.words.length) {

0 commit comments

Comments
 (0)