@@ -77,6 +77,10 @@ const WordCollapser = (function() {
7777
7878 // replace backreferences of word in link with phrase
7979 function replaceLinkWordObject ( link ) {
80+ if ( ! link . _words ) {
81+ link . _words = link . words . slice ( ) ;
82+ }
83+
8084 [ 'leftWord' , 'rightWord' , 'nearestConnectedMaxWord' , 'nearestConnectedMinWord' , 'rootMaxWord' , 'rootMinWord' ] . forEach ( prop => {
8185 if ( link [ prop ] === word ) {
8286 link [ prop ] = phrase ;
@@ -90,9 +94,6 @@ const WordCollapser = (function() {
9094 }
9195 } ) ;
9296
93- if ( ! link . _words ) {
94- link . _words = link . words . slice ( ) ;
95- }
9697 link . parents . forEach ( replaceLinkWordObject ) ;
9798 }
9899
@@ -116,7 +117,13 @@ const WordCollapser = (function() {
116117
117118 } ) ;
118119
119- phrase . removedWords = Array . prototype . concat . apply ( [ ] , removedWords . map ( word => word . removedWords || word ) ) ;
120+ phrase . removedWords = Array . prototype . concat . apply ( [ ] , removedWords . map ( word => {
121+ if ( word . removedWords ) {
122+ word . svg . remove ( ) ;
123+ return word . removedWords ;
124+ }
125+ return word ;
126+ } ) ) ;
120127
121128 phrase . leftX = leftWord . leftX ;
122129 phrase . row = row ;
@@ -129,17 +136,39 @@ const WordCollapser = (function() {
129136 [ ] . splice . apply ( wordObjs , [ wordObjs . indexOf ( word ) , 1 ] . concat ( word . removedWords ) ) ;
130137 [ ] . splice . apply ( word . row . words , [ word . row . words . indexOf ( word ) , 1 ] . concat ( word . removedWords ) ) ;
131138
132- word . removedWords . forEach ( word => {
133- word . svg . show ( ) ;
139+ // set position of uncollapsed words
140+ const rowWidth = Config . svgWidth - Config . edgePadding * 2 ;
141+ let x = word . leftX ;
142+ const y = word . underneathRect . y ( ) ;
143+ word . removedWords . forEach ( rw => {
144+ rw . row = word . row ;
145+ moveWordToNewPosition ( rw , x , y ) ;
146+ x += rw . underneathRect . width ( ) + Config . wordPadding ;
147+ rw . svg . show ( ) ;
134148 } ) ;
149+ // rearrange remaining words on row
150+ let i = word . row . words . indexOf ( word . removedWords [ word . removedWords . length - 1 ] ) + 1 ;
151+ while ( word . row . words [ i ] ) {
152+ if ( x <= word . row . words [ i ] . leftX ) {
153+ break ;
154+ }
155+ moveWordToNewPosition ( word . row . words [ i ] , x , y ) ;
156+ x += word . row . words [ i ] . underneathRect . width ( ) + Config . wordPadding ;
157+ if ( x > rowWidth ) {
158+ if ( word . row . idx + 1 === rows . length ) { appendRow ( ) ; }
159+ moveWordDownARow ( word . row . words [ i ] ) ;
160+ }
161+ ++ i ;
162+ }
163+
135164
136165 // revert assigned references to word in link
137166 function revertLinkWordObject ( link ) {
138167 [ 'leftWord' , 'rightWord' , 'nearestConnectedMaxWord' , 'nearestConnectedMinWord' , 'rootMaxWord' , 'rootMinWord' ] . forEach ( prop => {
139168 let _prop = '_' + prop ;
140169 if ( link [ _prop ] ) {
141170 link [ prop ] = link [ _prop ] ;
142- delete link [ '_' ]
171+ delete link [ '_' ] ;
143172 }
144173 } ) ;
145174
@@ -152,9 +181,7 @@ const WordCollapser = (function() {
152181
153182 word . parents . forEach ( revertLinkWordObject ) ;
154183
155- word . svg . hide ( ) ;
156-
157- // todo: make it go on the correct row and leftX and make enough room
184+ word . svg . remove ( ) ;
158185
159186 redrawLinks ( true ) ;
160187 }
@@ -181,7 +208,6 @@ const WordCollapser = (function() {
181208 else if ( leftWord === null ) {
182209 leftWord = word ;
183210 listenForRightWord ( ) ;
184- console . log ( 'left' , word ) ;
185211 }
186212 // selected words in the wrong order
187213 else if ( leftWord . idx >= word . idx ) {
@@ -190,7 +216,6 @@ const WordCollapser = (function() {
190216 }
191217 // select second word
192218 else {
193- console . log ( 'right' , word ) ;
194219 joinWords ( word ) ;
195220 }
196221 }
0 commit comments