@@ -20,8 +20,8 @@ class D3Tree {
20
20
this . container = null ;
21
21
this . layerDepth = null ;
22
22
this . maxLayerDepth = 0 ;
23
- this . imageHeight = 100 ;
24
- this . imageWidth = 100 ;
23
+ this . resourceHeight = 100 ;
24
+ this . resourceWidth = 100 ;
25
25
this . transition = null ;
26
26
this . textLength = 0 ;
27
27
this . fontSize = 14 ;
@@ -34,8 +34,8 @@ class D3Tree {
34
34
selector : 'body' ,
35
35
prefixClass : 'd3-tree' ,
36
36
data : { } ,
37
- imageHeight : 200 ,
38
- imageWidth : 200 ,
37
+ resourceHeight : 200 ,
38
+ resourceWidth : 200 ,
39
39
imageMargin : 10
40
40
} , options ) ;
41
41
}
@@ -103,15 +103,15 @@ class D3Tree {
103
103
return maxLayerDepth ;
104
104
}
105
105
106
- checkImageHeight ( root ) {
106
+ checkResourceHeight ( root ) {
107
107
if ( root . children ) {
108
108
let imageMargin = root . children . length > 1 ? root . children [ 1 ] . x - root . children [ 0 ] . x : 0 ;
109
- if ( this . imageHeight + this . options . imageMargin > imageMargin && imageMargin ) {
110
- this . imageHeight = imageMargin - this . options . imageMargin ;
111
- this . imageWidth = this . imageHeight * this . options . imageWidth / this . options . imageHeight ;
109
+ if ( this . resourceHeight + this . options . imageMargin > imageMargin && imageMargin ) {
110
+ this . resourceHeight = imageMargin - this . options . imageMargin ;
111
+ this . resourceWidth = this . resourceHeight * this . options . resourceWidth / this . options . resourceHeight ;
112
112
}
113
113
root . children . forEach ( child => {
114
- this . checkImageHeight ( child ) ;
114
+ this . checkResourceHeight ( child ) ;
115
115
} ) ;
116
116
}
117
117
}
@@ -121,16 +121,16 @@ class D3Tree {
121
121
this . root = d3 . hierarchy ( this . options . data ) ;
122
122
this . layerDepth = Array ( this . root . height + 1 ) . fill ( 0 ) ;
123
123
this . maxLayerDepth = this . findMaxLayer ( this . root , 0 , this . layerDepth ) ;
124
- this . imageHeight = this . options . height / Math . max ( ...this . maxLayerDepth ) >= this . options . imageHeight ?
125
- this . options . imageHeight :
124
+ this . resourceHeight = this . options . height / Math . max ( ...this . maxLayerDepth ) >= this . options . resourceHeight ?
125
+ this . options . resourceHeight :
126
126
this . options . height / Math . max ( ...this . maxLayerDepth ) - 10 ;
127
- this . imageWidth = this . imageHeight * this . options . imageWidth / this . options . imageHeight ;
127
+ this . resourceWidth = this . resourceHeight * this . options . resourceWidth / this . options . resourceHeight ;
128
128
this . tree = d3 . tree ( ) . size ( [
129
129
this . options . height ,
130
- this . options . width - this . imageWidth - this . fontSize * this . textLength
130
+ this . options . width - this . resourceWidth - this . fontSize * this . textLength
131
131
] ) ;
132
132
this . tree ( this . root ) ;
133
- this . checkImageHeight ( this . root ) ;
133
+ this . checkResourceHeight ( this . root ) ;
134
134
135
135
this
136
136
. container
@@ -140,7 +140,7 @@ class D3Tree {
140
140
. append ( 'path' )
141
141
. attr ( 'class' , `${ this . options . prefixClass } -link` )
142
142
. attr ( 'd' , d => {
143
- return `M${ d . y } ,${ d . x } C${ d . parent . y + this . imageWidth } ,${ d . x } ${ d . parent . y + this . imageWidth + 110 } ,${ d . parent . x } ${ d . parent . y + this . imageWidth + 10 } ,${ d . parent . x } ` ;
143
+ return `M${ d . y } ,${ d . x } C${ d . parent . y + this . resourceWidth } ,${ d . x } ${ d . parent . y + this . resourceWidth + 110 } ,${ d . parent . x } ${ d . parent . y + this . resourceWidth + 10 } ,${ d . parent . x } ` ;
144
144
} ) ;
145
145
146
146
const node = this
@@ -154,32 +154,47 @@ class D3Tree {
154
154
return `translate(${ d . y } ,${ d . x } )` ;
155
155
} )
156
156
. each ( function ( d ) {
157
- let imageList = d . data . image ;
158
- if ( imageList && ! ! ~ imageList . indexOf ( '[' ) ) {
159
- imageList = imageList
157
+ let resourceList = d . data . image ;
158
+ if ( resourceList && ! ! ~ resourceList . indexOf ( '[' ) ) {
159
+ resourceList = resourceList
160
160
. replace ( / ( \[ \n | \n \] | ) / g, '' )
161
161
. split ( '\n' ) ;
162
162
} else {
163
- imageList = [ imageList ] ;
163
+ resourceList = [ resourceList ] ;
164
164
}
165
165
166
- const imageWidth = that . imageWidth / imageList . length ;
167
- const imageHeight = that . imageHeight / imageList . length ;
168
-
169
- imageList . forEach ( ( image , index ) => {
170
- d3 . select ( this )
171
- . append ( 'image' )
172
- . attr ( 'xlink:href' , image )
173
- . attr ( 'data-index' , index )
174
- . attr ( 'height' , d => {
175
- return imageHeight ;
176
- } )
177
- . attr ( 'transform' , d => {
178
- return `translate(${ imageWidth * index } , -${ imageHeight / 2 } )` ;
179
- } )
180
- . attr ( 'width' , d => {
181
- return imageWidth ;
182
- } ) ;
166
+ const resourceWidth = that . resourceWidth / resourceList . length ;
167
+ const resourceHeight = that . resourceHeight / resourceList . length ;
168
+
169
+ resourceList . forEach ( ( resource , index ) => {
170
+ if ( resource && resource . endsWith ( '.webm' ) ) {
171
+ d3 . select ( this )
172
+ . append ( 'video' )
173
+ . attr ( 'src' , resource )
174
+ . attr ( 'type' , 'video/webm' )
175
+ . attr ( 'controls' , 'controls' )
176
+ . attr ( 'data-index' , index )
177
+ . attr ( 'style' , d => {
178
+ return `width: ${ resourceWidth } px; height: ${ resourceHeight } px` ;
179
+ } )
180
+ . attr ( 'transform' , d => {
181
+ return `translate(${ resourceWidth * index } , -${ resourceHeight / 2 } )` ;
182
+ } ) ;
183
+ } else {
184
+ d3 . select ( this )
185
+ . append ( 'image' )
186
+ . attr ( 'xlink:href' , resource )
187
+ . attr ( 'data-index' , index )
188
+ . attr ( 'height' , d => {
189
+ return resourceHeight ;
190
+ } )
191
+ . attr ( 'transform' , d => {
192
+ return `translate(${ resourceWidth * index } , -${ resourceHeight / 2 } )` ;
193
+ } )
194
+ . attr ( 'width' , d => {
195
+ return resourceWidth ;
196
+ } ) ;
197
+ }
183
198
} ) ;
184
199
} ) ;
185
200
@@ -193,10 +208,10 @@ class D3Tree {
193
208
const isMulti = lineCount !== 1 ;
194
209
let heightHalf = isMulti ? this . fontSize * this . lineHeight * ( lineCount + 1 ) / 2 : 0 ;
195
210
if ( d . data . image ) {
196
- return `translate(${ that . imageWidth + 10 } , ${ - heightHalf } )` ;
211
+ return `translate(${ that . resourceWidth + 10 } , ${ - heightHalf } )` ;
197
212
} else {
198
213
const textLength = d . data && d . data . text && d . data . text . length || 0 ;
199
- return `translate(${ that . imageWidth / 2 - 2 * textLength } , ${ - heightHalf } )` ;
214
+ return `translate(${ that . resourceWidth / 2 - 2 * textLength } , ${ - heightHalf } )` ;
200
215
}
201
216
} ) ;
202
217
} ;
@@ -207,7 +222,7 @@ class D3Tree {
207
222
this . transition = this . container . transition ( ) . duration ( this . options . duration ) ;
208
223
this . transition . selectAll ( `.${ this . options . prefixClass } -link` )
209
224
. attr ( 'd' , d => {
210
- return `M${ d . y } ,${ d . x } C${ d . parent . y + this . imageWidth } ,${ d . x } ${ d . parent . y + this . imageWidth + 110 } ,${ d . parent . x } ${ d . parent . y + this . imageWidth + 10 } ,${ d . parent . x } ` ;
225
+ return `M${ d . y } ,${ d . x } C${ d . parent . y + this . resourceWidth } ,${ d . x } ${ d . parent . y + this . resourceWidth + 110 } ,${ d . parent . x } ${ d . parent . y + this . resourceWidth + 10 } ,${ d . parent . x } ` ;
211
226
} ) ;
212
227
this . transition . selectAll ( `.${ this . options . prefixClass } -node` )
213
228
. attr ( 'transform' , d => {
@@ -216,10 +231,10 @@ class D3Tree {
216
231
this . transition . selectAll ( 'text' )
217
232
. attr ( 'transform' , d => {
218
233
if ( d . data . image ) {
219
- return `translate(${ this . imageWidth + 10 } , 4)` ;
234
+ return `translate(${ this . resourceWidth + 10 } , 4)` ;
220
235
} else {
221
236
const textLength = d . data && d . data . text && d . data . text . length || 0 ;
222
- return `translate(${ this . imageWidth / 2 - 2 * textLength } , 4)` ;
237
+ return `translate(${ this . resourceWidth / 2 - 2 * textLength } , 4)` ;
223
238
}
224
239
} ) ;
225
240
}
0 commit comments