Skip to content

Commit 4d45cac

Browse files
committed
start dynamically adding new words to graph
1 parent 5a811a0 commit 4d45cac

File tree

3 files changed

+130
-60
lines changed

3 files changed

+130
-60
lines changed

index.html

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,82 @@
248248
changeDataset(2);
249249
};
250250

251+
let collapse;
252+
let c0;
253+
document.addEventListener('keydown', function(e) {
254+
let c = e.keyCode;
255+
if (c === 65) { // A
256+
collapse = true;
257+
c0 = null;
258+
}
259+
else {
260+
collapse = false;
261+
c0 = null;
262+
}
263+
});
264+
draw.on('wordClicked', function(e) {
265+
let word = e.detail.arbitrary;
266+
if (collapse) {
267+
if (c0 === null) {
268+
c0 = word;
269+
console.log('left', word);
270+
}
271+
else {
272+
console.log('right', c0, word);
273+
274+
let phrase = "";
275+
for (let i = c0.idx; i <= word.idx; ++i) {
276+
if (i > c0.idx) {
277+
phrase += ' ';
278+
}
279+
phrase += wordObjs[i].val;
280+
}
281+
// console.log(phrase);
282+
283+
let newWord = new Word(phrase, wordObjs.length);
284+
wordObjs.push(newWord);
285+
286+
let row = rows[rows.length - 1];
287+
row.words.push(newWord);
288+
289+
290+
var wh = getTextWidthAndHeight(newWord.val, texts.wordText.style);
291+
292+
//calculate the width of the Word
293+
newWord.tw = wh.w;
294+
newWord.maxtextw = wh.w;
295+
296+
if (newWord.tag != null) {
297+
var twh = getTextWidthAndHeight(newWord.tag, texts.tagText.style);
298+
299+
if (twh.w > word.tw) {
300+
newWord.tw = twh.w; //think tw is ONLY used for checking minWidth, so this should be ok
301+
newWord.maxtextw = twh.w;
302+
}
303+
}
304+
//calculate x position and width of the Word
305+
newWord.wx = 80;
306+
newWord.ww = newWord.tw + (Config.textPaddingX * 2);
307+
308+
309+
if (newWord.tag != null) {
310+
newWord.tww = newWord.ww; //maxtextw + (Config.textPaddingX * 2);
311+
newWord.twx = newWord.wx;
312+
}
313+
newWord.h = 0; //the number of link levels is 0 for the word itself
314+
newWord.wh = texts.wordText.maxHeight + Config.textPaddingY*2;
315+
newWord.wy = row.ry + rows[0].rh - newWord.wh;
316+
newWord.row = row;
317+
318+
319+
newWord.draw();
320+
collapse = false;
321+
c0 = null;
322+
}
323+
}
324+
});
325+
326+
251327
</script>
252328

253329

js/render.js

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -374,65 +374,6 @@ function setUpRowsAndWords(words) {
374374
}
375375

376376

377-
function drawWord(word) {
378-
379-
let g = word.svg = draw.group().addClass('word');
380-
381-
word.underneathRect = g.rect( word.ww, word.wh )
382-
.x( word.wx )
383-
.y( word.wy )
384-
.addClass('word--underneath');
385-
386-
var textwh = getTextWidthAndHeight(word.val, texts.wordText.style);
387-
388-
word.text = g.text(function(add) {
389-
390-
add.text(word.val)
391-
.y(word.wy + Config.textPaddingY*2 - texts.wordText.descent)
392-
.x(word.wx + (word.ww/2) - (textwh.w / 2))
393-
.font(texts.wordText.style);
394-
});
395-
396-
word.bbox = word.underneathRect.bbox();
397-
word.leftX = word.underneathRect.bbox().x;
398-
word.rightX = word.underneathRect.bbox().x + word.underneathRect.bbox().w;
399-
word.percPos = (word.leftX-Config.edgePadding) / (Config.svgWidth-Config.edgePadding*2);
400-
401-
if (word.tag != null) {
402-
var textwh = getTextWidthAndHeight(word.tag, texts.tagText.style);
403-
var tagXPos = word.twx + (word.ww/2) - (textwh.w / 2);
404-
405-
//add in tag text, if the word has an associated tag
406-
word.tagtext = g.text(function(add) {
407-
add.text(word.tag)
408-
.y(word.wy + Config.textPaddingY/2 - texts.wordText.descent)
409-
.x(tagXPos)
410-
.font(texts.tagText.style);
411-
});
412-
word.leftHandle = g.rect(Config.handleW, Config.handleH)
413-
.x(word.wx)
414-
.y( word.wy + (word.wh / 2 ) - (Config.handleH / 2) )
415-
.addClass('word--handle');
416-
417-
word.rightHandle = g.rect(Config.handleW,Config.handleH)
418-
.x(word.wx + word.ww - (Config.handleW))
419-
.y( word.wy + (word.wh / 2 ) - (Config.handleH / 2) )
420-
.addClass('word--handle');
421-
422-
//set up mouse interactions
423-
setUpLeftHandleDraggable(word);
424-
setUpRightHandleDraggable(word);
425-
}
426-
427-
setUpWordDraggable(word);
428-
429-
word.underneathRect.dblclick( () => {
430-
word.toggleHighlight();
431-
draw.fire('wordSelected', { arbitrary: word });
432-
});
433-
}
434-
435-
436377
var _linkLabels = [];
437378
var _links = [];
438379
var _arrows = [];

js/word.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,60 @@ class Word {
7373
}
7474

7575
draw() {
76-
drawWord(this);
76+
let g = this.svg = draw.group().addClass('word');
77+
78+
this.underneathRect = g.rect( this.ww, this.wh )
79+
.x( this.wx )
80+
.y( this.wy )
81+
.addClass('word--underneath');
82+
83+
var textwh = getTextWidthAndHeight(this.val, texts.wordText.style);
84+
85+
this.text = g.text(this.val)
86+
.y(this.wy + Config.textPaddingY*2 - texts.wordText.descent)
87+
.x(this.wx + (this.ww/2) - (textwh.w / 2))
88+
.font(texts.wordText.style);
89+
90+
this.bbox = this.underneathRect.bbox();
91+
this.leftX = this.underneathRect.bbox().x;
92+
this.rightX = this.underneathRect.bbox().x + this.underneathRect.bbox().w;
93+
this.percPos = (this.leftX-Config.edgePadding) / (Config.svgWidth-Config.edgePadding*2);
94+
95+
if (this.tag != null) {
96+
var textwh = getTextWidthAndHeight(this.tag, texts.tagText.style);
97+
var tagXPos = this.twx + (this.ww/2) - (textwh.w / 2);
98+
99+
//add in tag text, if the word has an associated tag
100+
this.tagtext = g.text(this.tag)
101+
.y(this.wy + Config.textPaddingY/2 - texts.wordText.descent)
102+
.x(tagXPos)
103+
.font(texts.tagText.style);
104+
105+
this.leftHandle = g.rect(Config.handleW, Config.handleH)
106+
.x(this.wx)
107+
.y( this.wy + (this.wh / 2 ) - (Config.handleH / 2) )
108+
.addClass('word--handle');
109+
110+
this.rightHandle = g.rect(Config.handleW,Config.handleH)
111+
.x(this.wx + this.ww - (Config.handleW))
112+
.y( this.wy + (this.wh / 2 ) - (Config.handleH / 2) )
113+
.addClass('word--handle');
114+
115+
//set up mouse interactions
116+
setUpLeftHandleDraggable(this);
117+
setUpRightHandleDraggable(this);
118+
}
119+
120+
setUpWordDraggable(this);
121+
122+
this.underneathRect.dblclick( () => {
123+
this.toggleHighlight();
124+
draw.fire('wordSelected', { arbitrary: this });
125+
});
126+
127+
this.underneathRect.click(() => {
128+
draw.fire('wordClicked', { arbitrary: this });
129+
})
77130
}
78131

79132
get minWidth() { //min width is the maximum of: the word text, the tag text, or the size of the two handles + a little bit

0 commit comments

Comments
 (0)