@@ -78,7 +78,7 @@ const wfColorAct = {
78
78
}
79
79
80
80
// need to fix center
81
- function graph2doms ( JSONgraph , containerId , width , height , activations ) {
81
+ function graph2doms ( JSONgraph , containerId , activations ) {
82
82
83
83
let zoom = d3 . behavior . zoom ( )
84
84
. on ( "zoom" , redraw ) ;
@@ -263,10 +263,7 @@ function graph2doms(JSONgraph, containerId, width, height, activations){
263
263
elkData = data ;
264
264
265
265
// by default, the graph resizes to fit the size of the container i.e. zoom-to-fit, showing the entire graph. This solves #582.
266
- resizeToFit ( width , height ) ;
267
-
268
- console . log ( `[wskflow] svg canvas width=${ width } , height=${ height } ` ) ;
269
-
266
+ resizeToFit ( ) ;
270
267
271
268
let getNodes = function ( graph ) {
272
269
var queue = [ graph ] ,
@@ -1148,27 +1145,25 @@ function graph2doms(JSONgraph, containerId, width, height, activations){
1148
1145
*/
1149
1146
1150
1147
let applyAutoScale = true , // if resizing the window will resize the graph. true by default.
1151
- customZoom = false
1148
+ customZoom = false ;
1152
1149
1153
- function resizeToFit ( w , h ) { // resizeToFit implements a zoom-to-fit behavior.
1154
- width = w ;
1155
- height = h ;
1156
- let initScale = Math . min ( width / elkData . width , height / elkData . height , 4 ) ,
1157
- initTransX = Math . max ( width / 2 - elkData . width * initScale / 2 , 0 ) ,
1158
- initTransY = 0 ;
1159
- zoom . translate ( [ initTransX , initTransY ] ) ;
1160
- zoom . scale ( initScale ) ;
1161
- container . attr ( 'transform' , `matrix(${ initScale } , 0, 0, ${ initScale } , ${ initTransX } , ${ initTransY } )` ) ;
1150
+ function resizeToFit ( ) {
1151
+ // resizeToFit implements a zoom-to-fit behavior using viewBox.
1152
+ // it no longer requires knowing the size of the container
1153
+ // disadventage is we cannot decide the max zoom level: it will always zoom to fit the entire container
1154
+ ssvg . attr ( 'viewBox' , `0 0 ${ elkData . width } ${ elkData . height } ` ) ;
1155
+ container . attr ( 'transform' , '' ) ;
1156
+ zoom . translate ( [ 0 , 0 ] ) ;
1157
+ zoom . scale ( 1 ) ;
1162
1158
}
1163
1159
1164
- function pan ( w , h ) { // pan implements a behavior that zooms in the graph by 2x.
1165
- width = w ;
1166
- height = h ;
1160
+ function pan ( ) { // pan implements a behavior that zooms in the graph by 2x.
1167
1161
let initScale = 2 ,
1168
- initTransX = width / 2 - elkData . width * initScale / 2 ,
1162
+ initTransX = $ ( '#wskflowSVG' ) . width ( ) / 2 - elkData . width * initScale / 2 ,
1169
1163
initTransY = 0 ;
1170
1164
zoom . translate ( [ initTransX , initTransY ] ) ;
1171
1165
zoom . scale ( initScale ) ;
1166
+ $ ( '#wskflowSVG' ) . removeAttr ( 'viewBox' ) ;
1172
1167
container . attr ( 'transform' , `matrix(${ initScale } , 0, 0, ${ initScale } , ${ initTransX } , ${ initTransY } )` ) ;
1173
1168
}
1174
1169
@@ -1185,40 +1180,50 @@ function graph2doms(JSONgraph, containerId, width, height, activations){
1185
1180
*/
1186
1181
const zoomToFit = useThisValue => {
1187
1182
applyAutoScale = useThisValue !== undefined ? useThisValue : ! applyAutoScale ; // toggle applyAutoScale
1188
- customZoom = false
1183
+ customZoom = false
1189
1184
notify ( )
1190
1185
//console.error('ZTF', applyAutoScale, customZoom)
1191
1186
if ( applyAutoScale ) {
1192
1187
// when clicking to switch from inactive to active, it resizes the graph to fit the window. #422
1193
- resizeToFit ( $ ( '#wskflowSVG' ) . width ( ) , $ ( '#wskflowSVG' ) . height ( ) ) ;
1188
+ resizeToFit ( ) ;
1194
1189
}
1195
1190
else {
1196
1191
// when clicking to switch from active to inactive, it resizes the graph to zoom in at graph entry by 2x.
1197
- pan ( $ ( '#wskflowSVG' ) . width ( ) , $ ( '#wskflowSVG' ) . height ( ) ) ;
1192
+ pan ( ) ;
1198
1193
}
1199
1194
}
1200
1195
1201
1196
/*
1202
1197
from https://github.com/OpenKieler/klayjs-d3/blob/master/examples/interactive/index.js
1203
1198
redraw is called by d3. d3.event.translate and d3.event.scale handle moving the graph and zooming. Note that when zooming, both event.translate and event.scale change to let the zoom center be the mouse cursor. Adding ohter values to event.translate and event.scale is likely to cause unwanted behaviors.
1204
1199
*/
1205
- function redraw ( ) {
1206
- if ( applyAutoScale || ! customZoom ) { // exit zoom-to-fit mode when the user uses the mouse to move or zoom the graph
1207
- applyAutoScale = false ;
1208
- customZoom = true
1209
- notify ( )
1200
+ function redraw ( ) { // redraw is called when there's mouse scrolling
1201
+ if ( applyAutoScale || ! customZoom ) { // exit zoom-to-fit mode when the user uses the mouse to move or zoom the graph
1202
+ applyAutoScale = false ;
1203
+ customZoom = true
1204
+ notify ( )
1210
1205
}
1211
- $ ( "#qtip" ) . removeClass ( "visible" )
1212
- container . attr ( 'transform' , `translate( ${ d3 . event . translate } ) scale( ${ d3 . event . scale } )` ) ;
1206
+ container . attr ( 'transform' , `translate( ${ d3 . event . translate } ) scale( ${ d3 . event . scale } )` ) ;
1207
+ $ ( "#qtip" ) . removeClass ( "visible" )
1213
1208
}
1214
-
1215
1209
// when zoom-to-fit is active, the graph resizes as the window resizes. #422
1216
- $ ( window ) . unbind ( 'resize' ) . resize ( ( ) => {
1217
- // auto-resize with window size works when the user haven't adjusted the zooming manually with scrolling.
1218
- if ( applyAutoScale ) {
1219
- resizeToFit ( $ ( '#wskflowSVG' ) . width ( ) , $ ( '#wskflowSVG' ) . height ( ) ) ;
1220
- }
1221
-
1210
+ $ ( window ) . unbind ( 'resize' ) . resize ( e => {
1211
+
1212
+ if ( customZoom && $ ( '#wskflowSVG' ) . attr ( 'viewBox' ) !== undefined ) {
1213
+ // this code is called when the user is in custom zoom mode but the viewbox still exists
1214
+ // remove viewbox here to stop auto-resizing,
1215
+ $ ( '#wskflowSVG' ) . removeAttr ( 'viewBox' ) ;
1216
+ // adjust transform to let the graph be in the same size and location
1217
+ let width = $ ( '#wskflowSVG' ) . width ( ) ,
1218
+ height = $ ( '#wskflowSVG' ) . height ( ) ,
1219
+ scale = Math . min ( width / elkData . width , height / elkData . height ) ,
1220
+ initScale = scale * zoom . scale ( ) ,
1221
+ initTransX = width / 2 - elkData . width * scale / 2 + zoom . translate ( ) [ 0 ] * scale ,
1222
+ initTransY = height / 2 - elkData . height * scale / 2 + zoom . translate ( ) [ 1 ] * scale ;
1223
+ zoom . translate ( [ initTransX , initTransY ] ) ;
1224
+ zoom . scale ( initScale ) ;
1225
+ container . attr ( 'transform' , `matrix(${ initScale } , 0, 0, ${ initScale } , ${ initTransX } , ${ initTransY } )` ) ;
1226
+ }
1222
1227
} ) ;
1223
1228
1224
1229
0 commit comments