Skip to content

Commit 44dd85a

Browse files
committed
another class for the word collapser lol
1 parent 6e5e677 commit 44dd85a

File tree

7 files changed

+121
-83
lines changed

7 files changed

+121
-83
lines changed

css/style.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ svg text {
2828
margin-top:30px;
2929
margin-bottom: 210px;
3030
}
31+
#drawing.bracket-left{
32+
cursor:e-resize !important;
33+
}
34+
#drawing.bracket-right {
35+
cursor:w-resize !important;
36+
}
3137

3238
#graph {
3339
cursor:-webkit-grab;
@@ -42,7 +48,7 @@ svg text {
4248
}
4349

4450
#graph:before {
45-
cursor:ns-resize;
51+
cursor:row-resize;
4652
content:'';
4753
width:100%;
4854
height:7px;

index.html

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<script src="js/interact.js"></script>
5656
<script src="js/utils.js"></script>
5757
<script src="js/minimap.js"></script>
58+
<script src="js/wordcollapser.js"></script>
5859

5960
<script type="text/javascript">
6061
/* a lot of these global vars should be wrapped eventually */
@@ -69,7 +70,6 @@
6970

7071
var texts = setupTexts(draw); //creates an associate array of whatever text you want to use. Currently using 'texts.wordText' and 'texts.linkText', and using style, maxHeight, and descent to layout text
7172

72-
Config.handleH = texts.wordText.maxHeight + (Config.textPaddingY * 2);
7373
Config.wordHeight = texts.wordText.maxHeight + (Config.textPaddingY * 2);
7474
Config.minWordWidth = getTextWidth("i", texts.wordText.style) + (Config.handleW * 2);
7575

@@ -96,6 +96,7 @@
9696
const graph = new GraphLayout(panel.el);
9797
panel.onresize = graph.resize.bind(graph);
9898
const parser = new Parser();
99+
const wc = new WordCollapser();
99100

100101
var prevWidth = Config.svgWidth;
101102
function svgResize() {
@@ -114,6 +115,9 @@
114115
draw.on('wordSelected', function() {
115116
graph.graph(getSelectedObjects());
116117
});
118+
draw.on('wordClicked', function(e) {
119+
wc.setClick(e.detail.arbitrary);
120+
});
117121

118122
function changeDataset(index = 1) {
119123
parser.readJson(`./data/data${index}.json`, function() {
@@ -248,59 +252,6 @@
248252
changeDataset(2);
249253
};
250254

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-
//calculate x position and width of the Word
290-
newWord.wx = 80;
291-
newWord.ww = newWord.tw + (Config.textPaddingX * 2);
292-
newWord.wy = row.wordY;
293-
newWord.row = row;
294-
295-
296-
newWord.draw();
297-
collapse = false;
298-
c0 = null;
299-
}
300-
}
301-
});
302-
303-
304255
</script>
305256

306257

js/interact.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,12 +502,11 @@ function moveWordToNewPosition(w, nx, ny) {
502502
w.tagtext.x(nx + w.bbox.w/2);
503503
w.tagtext.y(ny + Config.textPaddingY/2);// - texts.tagText.descent);
504504

505-
var handley = ny + ( w.wh / 2 ) - ( Config.handleH / 2 );
506505
w.leftHandle.x(nx);
507-
w.leftHandle.y(handley);
506+
w.leftHandle.y(ny);
508507

509508
w.rightHandle.x( w.rightX - Config.handleW );
510-
w.rightHandle.y(handley);
509+
w.rightHandle.y(ny);
511510
}
512511

513512
w.needsUpdate = true;

js/render.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ function calculateMaxSlotForRow(row) {
275275
}
276276
}
277277

278-
279278
function setUpRowsAndWords(words) {
280279

281280
var rowNum = 0;
@@ -314,10 +313,9 @@ function setUpRowsAndWords(words) {
314313
word.th = texts.wordText.maxHeight; //guaranteed to be the same for all words
315314

316315
//calculate x position and width of the Word
317-
word.wx = x;
318-
word.ww = word.tw + (Config.textPaddingX * 2);
316+
word.leftX = x;
319317

320-
x += word.ww + Config.wordPadding;
318+
x += word.defaultWidth + Config.wordPadding;
321319
}
322320

323321

@@ -336,9 +334,7 @@ function setUpRowsAndWords(words) {
336334

337335
for (var ii = 0; ii < row.words.length; ii++) {
338336
var word = row.words[ii];
339-
340-
word.wy = row.wordY;
341-
337+
342338
if (word.tag != null) {
343339
var tag_textwh = getTextWidthAndHeight(word.tag, texts.tagText.style);
344340
word.twh = texts.tagText.maxHeight + Config.textPaddingY*2;

js/row.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,6 @@ class Row {
118118
return returnVal;
119119
}
120120

121-
get wordY() {
122-
return this.ry + this.rh - (texts.wordText.maxHeight + Config.textPaddingY*2);
123-
}
124121
get minWidth() {
125122
return this.words.reduce((acc, val) => acc + val.minWidth, 0);
126123
}

js/word.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ class Word {
66

77
this.h = 0; //num slots
88

9-
this.ww = 0;
109
this.wh = texts.wordText.maxHeight + Config.textPaddingY*2;
11-
this.wx = 0;
12-
this.wy = 0;
10+
this.leftX = 0;
1311

1412
this.slotsL = []; //all the slots that links attached the left side of this word occupy
1513
this.slotsR = []; //all the slots that links attached the left side of this word occupy
@@ -81,39 +79,38 @@ class Word {
8179

8280

8381
let g = this.svg = draw.group().addClass('word');
82+
let x = this.leftX + this.defaultWidth/2;
8483

85-
this.underneathRect = g.rect( this.ww, this.wh )
86-
.x( this.wx )
84+
this.underneathRect = g.rect( this.defaultWidth, this.wh )
85+
.x( this.leftX )
8786
.y( this.wy )
8887
.addClass('word--underneath');
8988

9089
this.text = g.text(this.val)
9190
.y(this.wy + Config.textPaddingY*2 - texts.wordText.descent)
92-
.x(this.wx + this.ww/2)
91+
.x(x)
9392
.font(texts.wordText.style);
9493

9594
this.bbox = this.underneathRect.bbox();
96-
this.leftX = this.underneathRect.bbox().x;
9795
this.rightX = this.underneathRect.bbox().x + this.underneathRect.bbox().w;
9896
this.percPos = (this.leftX-Config.edgePadding) / (Config.svgWidth-Config.edgePadding*2);
9997

10098
if (this.tag != null) {
101-
var tagXPos = this.wx + this.ww/2;
10299

103100
//add in tag text, if the word has an associated tag
104101
this.tagtext = g.text(this.tag)
105102
.y(this.wy + Config.textPaddingY/2 - texts.wordText.descent)
106-
.x(tagXPos)
103+
.x(x)
107104
.font(texts.tagText.style);
108105

109-
this.leftHandle = g.rect(Config.handleW, Config.handleH)
110-
.x(this.wx)
111-
.y( this.wy + (this.wh / 2 ) - (Config.handleH / 2) )
106+
this.leftHandle = g.rect(Config.handleW, Config.wordHeight)
107+
.x(this.leftX)
108+
.y( this.wy)
112109
.addClass('word--handle');
113110

114-
this.rightHandle = g.rect(Config.handleW,Config.handleH)
115-
.x(this.wx + this.ww - (Config.handleW))
116-
.y( this.wy + (this.wh / 2 ) - (Config.handleH / 2) )
111+
this.rightHandle = g.rect(Config.handleW,Config.wordHeight)
112+
.x(this.rightX - (Config.handleW))
113+
.y( this.wy)
117114
.addClass('word--handle');
118115

119116
//set up mouse interactions
@@ -133,6 +130,17 @@ class Word {
133130
})
134131
}
135132

133+
get wy() {
134+
if (this.row) {
135+
return this.row.ry + this.row.rh - (texts.wordText.maxHeight + Config.textPaddingY*2);
136+
}
137+
return 0;
138+
}
139+
140+
get defaultWidth() {
141+
return this.tw + (Config.textPaddingX * 2)
142+
}
143+
136144
get minWidth() { //min width is the maximum of: the word text, the tag text, or the size of the two handles + a little bit
137145
return Math.max(Config.minWordWidth, this.tw);
138146
}

js/wordcollapser.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const WordCollapser = (function() {
2+
3+
let div;
4+
let selecting;
5+
let leftWord;
6+
7+
function listenForLeftWord() {
8+
selecting = true;
9+
leftWord = null;
10+
div.className = 'bracket-left';
11+
}
12+
13+
function listenForRightWord() {
14+
div.className = 'bracket-right';
15+
}
16+
17+
function cancel() {
18+
selecting = false;
19+
leftWord = null;
20+
div.className = null;
21+
}
22+
23+
class WordCollapser {
24+
25+
constructor() {
26+
div = document.getElementById('drawing');
27+
28+
document.addEventListener('keydown', function(e) {
29+
let c = e.keyCode;
30+
if (c === 65) { // A
31+
listenForLeftWord();
32+
}
33+
else {
34+
cancel();
35+
}
36+
});
37+
}
38+
39+
setClick(word) {
40+
if (selecting) {
41+
// selecting left word
42+
if (leftWord === null) {
43+
leftWord = word;
44+
console.log('left', word);
45+
}
46+
// selected words in the wrong order
47+
else if (leftWord.idx >= word.idx) {
48+
cancel();
49+
console.log('out of order... canceling');
50+
}
51+
// select second word
52+
else {
53+
console.log('right', leftWord, word);
54+
55+
let text = "";
56+
for (let i = leftWord.idx; i <= word.idx; ++i) {
57+
if (i > leftWord.idx) {
58+
text += ' ';
59+
}
60+
text += wordObjs[i].val;
61+
}
62+
63+
let phrase = new Word(text, leftWord.idx);
64+
65+
let row = leftWord.row;
66+
67+
wordObjs.splice(wordObjs.indexOf(leftWord), 1, phrase);
68+
row.words.splice(row.words.indexOf(leftWord), 1, phrase);
69+
70+
phrase.leftX = leftWord.leftX;
71+
phrase.row = row;
72+
phrase.draw();
73+
leftWord.svg.hide();
74+
cancel();
75+
}
76+
}
77+
}
78+
}
79+
80+
return WordCollapser;
81+
})();

0 commit comments

Comments
 (0)