2929 } ,
3030 layout : { } // The layout of the directed graph
3131 } ;
32- let defaultGraphDirection = '' ; // The graph direction from the dgml file itself
32+ const defaultGraphDirection = '' ; // The graph direction from the dgml file itself
3333 const container = document . getElementById ( 'network' ) ;
3434 const hierarchicalOptionsDirection = document . getElementById ( 'hierarchicalOptions_direction' ) ;
3535 const hierarchicalOptionsSortMethod = document . getElementById ( 'hierarchicalOptions_sortmethod' ) ;
259259 }
260260 }
261261
262- function storeCoordinates ( node ) {
263- if ( node . x !== undefined && node . y !== undefined ) {
264- nodeCoordinates [ node . id ] = { x : node . x , y : node . y } ;
265- }
266- delete node . x ;
267- delete node . y ;
268- delete node . fixed ;
262+ function storeCoordinates ( ) {
263+ nodes . forEach ( node => {
264+ if ( node . x !== undefined && node . y !== undefined ) {
265+ nodeCoordinates [ node . id ] = { x : node . x , y : node . y } ;
266+ }
267+ delete node . x ;
268+ delete node . y ;
269+ delete node . fixed ;
270+ } ) ;
269271 }
270272
271- function restoreCoordinates ( node ) {
272- if ( node . id in nodeCoordinates ) {
273- var nodeCoords = nodeCoordinates [ node . id ] ;
274- if ( nodeCoords . x !== undefined && nodeCoords . y !== undefined ) {
275- node . x = nodeCoords . x ;
276- node . y = nodeCoords . y ;
277- node . fixed = { x : true , y : true } ;
273+ function restoreCoordinates ( ) {
274+ nodes . forEach ( function ( node ) {
275+ if ( node . id in nodeCoordinates ) {
276+ var nodeCoords = nodeCoordinates [ node . id ] ;
277+ nodes . update ( {
278+ id : node . id ,
279+ fixed : true ,
280+ x : nodeCoords . x ,
281+ y : nodeCoords . y
282+ } ) ;
278283 }
279- }
284+ } ) ;
285+ }
286+
287+ function setHierarchicalLayout ( direction , sortMethod ) {
288+ options . layout = {
289+ hierarchical : {
290+ enabled : true ,
291+ levelSeparation : 200 ,
292+ nodeSpacing : 200 ,
293+ direction : direction ,
294+ sortMethod : sortMethod
295+ }
296+ } ;
297+ options . physics = {
298+ enabled : true ,
299+ hierarchicalRepulsion : {
300+ springConstant : 0 ,
301+ avoidOverlap : 0.2
302+ }
303+ } ;
280304 }
281305
282306 function setNetworkLayout ( ) {
295319 avoidOverlap : 0.8
296320 }
297321 } ;
322+ var unfixNodes = false ;
298323 if ( showHierarchicalOptionsCheckbox . checked ) {
299324 if ( hierarchicalOptionsDirectionSelect . value && hierarchicalOptionsDirectionSelect . value === 'Random' ) {
300- nodes . getIds ( ) . forEach ( id => storeCoordinates ( nodes . get ( id ) ) ) ;
325+ storeCoordinates ( ) ;
301326 seed = Math . random ( ) ;
302327 options . layout . randomSeed = seed ;
303328 } else if ( hierarchicalOptionsDirectionSelect . value && hierarchicalOptionsDirectionSelect . value === 'Fixed' ) {
304- nodes . getIds ( ) . forEach ( id => restoreCoordinates ( nodes . get ( id ) ) ) ;
329+ restoreCoordinates ( ) ;
330+ options . physics . enabled = false ;
331+ unfixNodes = true ;
305332 } else {
306- nodes . getIds ( ) . forEach ( id => storeCoordinates ( nodes . get ( id ) ) ) ;
307- options . layout = {
308- hierarchical : {
309- enabled : true ,
310- levelSeparation : 200 ,
311- nodeSpacing : 200 ,
312- direction : hierarchicalOptionsDirectionSelect . value ? hierarchicalOptionsDirectionSelect . value : defaultGraphDirection ,
313- sortMethod : hierarchicalOptionsSortMethodSelect . value ? hierarchicalOptionsSortMethodSelect . value : 'hubsize'
314- }
315- } ;
316- options . physics = {
317- enabled : true ,
318- hierarchicalRepulsion : {
319- springConstant : 0 ,
320- avoidOverlap : 0.2
321- }
322- } ;
333+ storeCoordinates ( ) ;
334+ const direction = hierarchicalOptionsDirectionSelect . value ? hierarchicalOptionsDirectionSelect . value : defaultGraphDirection ;
335+ const sortMethod = hierarchicalOptionsSortMethodSelect . value ? hierarchicalOptionsSortMethodSelect . value : 'hubsize' ;
336+ setHierarchicalLayout ( direction , sortMethod ) ;
323337 }
324338 } else {
325339 if ( defaultGraphDirection !== '' ) {
326- options . layout = {
327- hierarchical : {
328- enabled : true ,
329- levelSeparation : 200 ,
330- nodeSpacing : 200 ,
331- direction : defaultGraphDirection ,
332- }
333- } ;
334- options . physics = {
335- enabled : true ,
336- hierarchicalRepulsion : {
337- springConstant : 0 ,
338- avoidOverlap : 0.2
339- }
340- } ;
341- nodes . getIds ( ) . forEach ( id => storeCoordinates ( nodes . get ( id ) ) ) ;
340+ storeCoordinates ( ) ;
341+ setHierarchicalLayout ( defaultGraphDirection , 'hubsize' ) ;
342342 } else {
343- nodes . getIds ( ) . forEach ( id => restoreCoordinates ( nodes . get ( id ) ) ) ;
343+ restoreCoordinates ( ) ;
344+ unfixNodes = false ;
344345 }
345346 }
347+ console . log ( options ) ;
346348 var network = new vis . Network ( container , data , options ) ;
349+ if ( unfixNodes ) {
350+ nodes . forEach ( function ( node ) {
351+ nodes . update ( { id : node . id , fixed : false } ) ;
352+ } ) ;
353+ }
347354 network . on ( "stabilizationIterationsDone" , function ( ) {
348355 network . setOptions ( {
349356 physics : { enabled : false }
350357 } ) ;
351- } ) ;
358+ nodes . forEach ( function ( node ) {
359+ nodes . update ( { id : node . id , fixed : false } ) ;
360+ } ) ;
361+ } ) ;
352362 }
353363} ( ) ) ;
0 commit comments