11import { Component , Input , Output , EventEmitter } from '@angular/core' ;
22import { ParseTerm } from './parse-term.component' ;
3- import { Dimensions } from '@/types' ;
4- import { sum } from "@/util " ;
3+ import { Dimensions , CCGTerm } from '@/types' ;
4+ import { TreeNode } from "@/tree " ;
55
66@Component ( {
77 selector : "[parse-tree]" ,
@@ -11,38 +11,10 @@ import { sum } from "@/util";
1111 styleUrl : "./parse-tree.component.scss" ,
1212} )
1313export class ParseTree {
14- expanded : boolean [ ] = [ ] ;
14+ expanded : boolean = true ;
1515
16- _tree : any ;
1716 @Input ( )
18- get tree ( ) : any {
19- return this . _tree ;
20- }
21-
22- set tree ( value : any ) {
23- if ( value . children ) {
24- // this is a temporary translation until the tree code can be refactored:
25- let translated =
26- ( function translate ( p : any ) {
27- if ( ! p ) return ;
28- let out : any = { } ;
29- if ( Array . isArray ( p . node ) ) {
30- out . nodes = [ { term : p . node } ] ;
31- }
32- else {
33- out . nodes = [ { term : [ p . node ] } ] ;
34- }
35- out . subtrees = p . children ? p . children . map ( translate ) : [ ] ;
36- return out ;
37- } ) ( value ) ;
38- this . _tree = translated ;
39- }
40- else {
41- this . _tree = value ;
42- }
43-
44- this . expanded = [ true ] ;
45- }
17+ treeNode : TreeNode < CCGTerm > = { value : [ ] , children : [ ] } ;
4618
4719 levelHeight = 40 ;
4820
@@ -73,62 +45,38 @@ export class ParseTree {
7345
7446 emitSize ( ) {
7547 this . onSize . emit ( {
76- width : Math . max ( this . subWidth * ( this . tree . subtrees ?. length ?? 0 ) , this . width ) ,
77- height : this . subHeight + this . totalNodeHeight ( ) + ( this . tree . subtrees ?. length ? this . levelHeight : 0 )
48+ width : Math . max ( this . subWidth * ( this . treeNode ?. children ?. length ?? 0 ) , this . width ) ,
49+ height : this . subHeight + this . nodeHeight ( ) + ( this . treeNode ?. children ?. length ? this . levelHeight : 0 )
7850 } ) ;
7951 }
8052
8153 /* Determines the X coordinate of where subtree of index `idx` should be drawn */
8254 subtreePosition ( idx : number ) {
8355 let widthWithPadding = 1.15 * this . subWidth ;
84- return widthWithPadding * idx - ( widthWithPadding / 2 ) * ( this . tree . subtrees . length - 1 ) ;
56+ return widthWithPadding * idx - ( widthWithPadding / 2 ) * ( this . treeNode . children . length - 1 ) ;
8557 }
8658
8759 /* Generates a path definition for linking the end of the current node to subtree index `idx` */
8860 subtreeLinkPath ( idx : number ) {
8961 return [
9062 // move to end of current node
91- `M 0 ${ this . totalNodeHeight ( ) - 15 } ` ,
63+ `M 0 ${ this . nodeHeight ( ) - 15 } ` ,
9264 // curve to half-way to subtree
9365 `q 0 ${ this . levelHeight / 2 } ${ this . subtreePosition ( idx ) / 2 } ${ this . levelHeight / 2 } ` ,
9466 // curve from half-way to subtree, to subtree position
9567 `q ${ this . subtreePosition ( idx ) / 2 } 0 ${ this . subtreePosition ( idx ) / 2 } ${ this . levelHeight / 2 } `
9668 ] . join ( ' ' ) ;
9769 }
9870
99- nodeHeight ( node : any ) {
100- // note that in this case height includes bottom padding for the node
101- return node . rule ? 60 : 40 ;
102- }
103-
104- totalNodeHeight ( ) {
105- return sum ( this . tree . nodes . map ( this . nodeHeight ) ) ;
106- }
107-
108- nodeY ( idx : number ) {
109- // y position of a given node is the sum of heights of all preceeding nodes
110- return sum ( this . tree . nodes . slice ( 0 , idx ) . map ( this . nodeHeight ) ) ;
111- }
112-
113- termClick ( idx : number ) {
114- // the last node can't be collapsed
115- // unless there are subtrees
116- if ( idx < this . expanded . length - 1 || this . tree . subtrees ) {
117- this . expanded [ idx ] = ! this . expanded [ idx ] ;
118- }
71+ nodeHeight ( ) {
72+ return 40 ;
11973 }
12074
121- visibleNodes ( ) {
122- const firstCollpased = this . expanded . indexOf ( false ) ;
123- if ( firstCollpased == - 1 ) {
124- return this . tree . nodes ;
125- }
126- return this . tree . nodes . slice ( 0 , firstCollpased + 1 ) ;
75+ termClick ( ) {
76+ this . expanded = ! this . expanded ;
12777 }
12878
12979 showSubtrees ( ) {
130- // if any of the current level nodes is collapsed, this also means
131- // we shouldn't render any subtrees
132- return this . expanded . indexOf ( false ) == - 1 ;
80+ return this . expanded ;
13381 }
13482}
0 commit comments