3131 } ,
3232 layout : { } // The layout of the directed graph
3333 } ;
34+ var defaultGraphDirection = '' ; // The graph direction from the dgml file itself
35+ setDefaultGraphDirection ( ) ;
3436 var container = document . getElementById ( 'network' ) ;
3537 var network = new vis . Network ( container , data , options ) ;
3638 var seed = network . getSeed ( ) ;
5759 copyToClipboardButton . addEventListener ( 'click' , copyToClipboard ) ;
5860 copyToClipboardButton . style [ 'display' ] = 'none' ; // TODO: Remove when copyToClipboard is implemented
5961 const showHierarchicalOptionsButton = document . getElementById ( 'showHierarchicalOptions' ) ;
60- showHierarchicalOptionsButton . addEventListener ( 'click' , setNetworkLayout ) ;
62+ showHierarchicalOptionsButton . addEventListener ( 'click' , showHierarchicalOptions ) ;
6163 const hierarchicalDirectionSelect = document . getElementById ( 'direction' ) ;
6264 hierarchicalDirectionSelect . addEventListener ( 'change' , setNetworkLayout ) ;
6365 const hierarchicalSortMethodSelect = document . getElementById ( 'sortMethod' ) ;
101103 function drawGuideLine ( ctx , mouseX , mouseY ) {
102104 ctx . beginPath ( ) ;
103105 ctx . setLineDash ( [ 3 , 7 ] ) ;
104- if ( mouseX > - 1 ) {
106+ if ( mouseX > - 1 ) {
105107 ctx . moveTo ( mouseX , 0 ) ;
106108 ctx . lineTo ( mouseX , selectionCanvas . height ) ;
107109 } else if ( mouseY > - 1 ) {
143145 }
144146
145147 function saveSelectionAsPng ( ) {
148+ graphDiv = document . getElementById ( 'network' ) ;
149+ visDiv = graphDiv . firstElementChild ;
150+ graphCanvas = visDiv . firstElementChild ;
151+
146152 // show the help text
147153 helpTextDiv . style [ 'display' ] = 'block' ;
148154
157163 selection = { } ;
158164
159165 selectionLayer . addEventListener ( "mouseup" , mouseUpEventListener , true ) ;
160- selectionLayer . addEventListener ( "mousedown" , mouseDownEventListener , true ) ;
166+ selectionLayer . addEventListener ( "mousedown" , mouseDownEventListener , true ) ;
161167 selectionLayer . addEventListener ( "mousemove" , mouseMoveEventListener , true ) ;
162168 }
163169
164170 function saveAsPng ( ) {
165171 graphDiv = document . getElementById ( 'network' ) ;
166172 visDiv = graphDiv . firstElementChild ;
167- graphCanvas = visDiv . firstElementChild ;
173+ graphCanvas = visDiv . firstElementChild ;
174+
168175 // Calculate the bounding box of all the elements on the canvas
169176 const boundingBox = getBoundingBox ( ) ;
170177
173180 finalSelectionCanvas . width = boundingBox . width ;
174181 finalSelectionCanvas . height = boundingBox . height ;
175182 const finalSelectionCanvasContext = finalSelectionCanvas . getContext ( '2d' ) ;
176- finalSelectionCanvasContext . drawImage ( graphCanvas , boundingBox . top , boundingBox . left , boundingBox . width , boundingBox . height , 0 , 0 , boundingBox . width , boundingBox . height ) ;
183+ finalSelectionCanvasContext . drawImage ( graphCanvas , boundingBox . top , boundingBox . left , boundingBox . width , boundingBox . height , 0 , 0 , boundingBox . width , boundingBox . height ) ;
177184
178185 // Call back to the extension context to save the image of the graph to the workspace folder.
179186 vscode . postMessage ( {
192199 var cWidth = graphCanvas . width * bytesPerPixels ;
193200 var cHeight = graphCanvas . height ;
194201 var minY = minX = maxY = maxX = - 1 ;
195- for ( var y = cHeight ; y > 0 && maxY === - 1 ; y -- ) {
202+ for ( var y = cHeight ; y > 0 && maxY === - 1 ; y -- ) {
196203 for ( var x = 0 ; x < cWidth ; x += bytesPerPixels ) {
197204 var arrayPos = x + y * cWidth ;
198205 if ( imgData . data [ arrayPos + 3 ] > 0 && maxY === - 1 ) {
231238 return {
232239 'top' : minX ,
233240 'left' : minY ,
234- 'width' : maxX - minX ,
235- 'height' : maxY - minY
241+ 'width' : maxX - minX ,
242+ 'height' : maxY - minY
236243 } ;
237244 }
238245
239246 function copyToClipboard ( ) {
240247 console . log ( 'Not implemented yet...' ) ;
241248 }
242-
249+
250+ function showHierarchicalOptions ( ) {
251+ setDefaultGraphDirection ( ) ;
252+ setNetworkLayout ( ) ;
253+ }
254+
255+ function setDefaultGraphDirection ( ) {
256+ const hierarchicalOptionsDirection = document . getElementById ( 'direction' ) ;
257+ for ( var i , j = 0 ; i = hierarchicalOptionsDirection . options [ j ] ; j ++ ) {
258+ if ( i . value === defaultGraphDirection ) {
259+ hierarchicalOptionsDirection . selectedIndex = j ;
260+ break ;
261+ }
262+ }
263+ }
264+
243265 function setNetworkLayout ( ) {
244266 const hierarchicalOptionsDirection = document . getElementById ( 'hierarchicalOptions_direction' ) ;
245267 const hierarchicalOptionsSortMethod = document . getElementById ( 'hierarchicalOptions_sortmethod' ) ;
248270 hierarchicalOptionsSortMethod . style [ 'display' ] = showHierarchicalOptionsCheckbox . checked ? 'block' : 'none' ;
249271 const hierarchicalOptionsDirectionSelect = document . getElementById ( 'direction' ) ;
250272 const hierarchicalOptionsSortMethodSelect = document . getElementById ( 'sortMethod' ) ;
251- if ( showHierarchicalOptionsCheckbox . checked ) {
252- options . layout = {
253- hierarchical : {
254- enabled : true ,
255- direction : hierarchicalOptionsDirectionSelect . value ? hierarchicalOptionsDirectionSelect . value : 'LR' ,
256- sortMethod : hierarchicalOptionsSortMethodSelect . value ? hierarchicalOptionsSortMethodSelect . value : 'hubsize'
257- }
258- } ;
273+ if ( showHierarchicalOptionsCheckbox . checked ) {
274+ if ( hierarchicalOptionsDirectionSelect . value && hierarchicalOptionsDirectionSelect . value === 'Random' ) {
275+ options . layout = { } ;
276+ seed = Math . random ( ) ;
277+ } else {
278+ options . layout = {
279+ hierarchical : {
280+ enabled : true ,
281+ direction : hierarchicalOptionsDirectionSelect . value ? hierarchicalOptionsDirectionSelect . value : defaultGraphDirection ,
282+ sortMethod : hierarchicalOptionsSortMethodSelect . value ? hierarchicalOptionsSortMethodSelect . value : 'hubsize'
283+ }
284+ } ;
285+ }
259286 } else {
260- options . layout = { } ;
287+ if ( defaultGraphDirection === '' ) {
288+ options . layout = { } ;
289+ } else {
290+ options . layout = {
291+ hierarchical : {
292+ enabled : true ,
293+ direction : defaultGraphDirection ,
294+ sortMethod : 'hubsize'
295+ }
296+ } ;
297+ }
261298 }
262299 options . layout . randomSeed = seed ;
263300 network = new vis . Network ( container , data , options ) ;
264301 }
265-
302+
266303} ( ) ) ;
0 commit comments