-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhgv.js
1131 lines (933 loc) · 40.4 KB
/
hgv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
var glycanviewer = {
// The "global variables" are declared here
// It is not necessary to do so
// It is just a reminder of I can use this "things" directly
firstInit: true,
// A list of div in HTML page. save the trouble of document.getElement blah blah
div_root: 0,
div_header: 0,
div_img: 0,
div_network: 0,
div_navi: 0,
// Parameters from HTML file
para: 0,
// A portion of json file that will be used to build the network
component: 0,
// Things related to the network
rootname: 0,
topoonly: 0,
rootlevel: 0,
displaynodes: 0,
verticalSpace: 0,
horizontalSpace: 0,
// Image cache
resourceStatus: {},
nodeImg: {},
imageWidth: [],
imageHeight: [],
// Constant
brokenImageSubstituentBase64: "",
init: function (parameters) {
if (this.IEQuestionMark())
{
this.compatibleDraw(parameters);
}
else {
this.para = this.paraCheck(parameters);
this.allocateDiv();
this.dataPreProcessing();
}
},
// Check the integrity and legitimacy of the parameters
paraCheck : function (para){
var checked = para;
if (!para.display.naviOption.size){
checked.display.naviOption.size = 0.15;
}
if (!para.display.naviOption.postion){
checked.display.naviOption.postion = 4;
}
if (![1,2,3,4].includes(para.display.naviOption.postion)){
checked.display.naviOption.postion = 4;
}
if (para.essentials.useGlyTouCanAsImageSource){
if (!["extended","normal","compact"].includes(para.essentials.GlyTouCanImagePara.style)){
checked.essentials.GlyTouCanImagePara.style = "extended";
}
if (!["png","jpg"].includes(para.essentials.GlyTouCanImagePara.format)){
checked.essentials.GlyTouCanImagePara.format = "png";
}
if (!["cfg","cfgbw","uoxf","uoxf-color","cfg-uoxf","iupac"].includes(para.essentials.GlyTouCanImagePara.notation)){
checked.essentials.GlyTouCanImagePara.notation = "cfg";
}
}
if (![1,2,3,4].includes(para.display.orientation)){
checked.display.orientation = 1;
}
return checked
},
// Allocate the DIV
allocateDiv: function () {
var thisLib = this;
// This variable stands for the prefix of the div id.
var id = this.para.essentials.div_ID + "_glycanviewer_";
//Locate the div && pre allocate space for each gadgets
this.div_root = document.getElementById(thisLib.para.essentials.div_ID);
while (thisLib.div_root.firstChild){
thisLib.div_root.removeChild(thisLib.div_root.firstChild);
}
this.div_root.style.overflow = "hidden";
this.div_header = document.createElement("div");
this.div_header.setAttribute("id",id+"header");
this.div_realStuff = document.createElement("div");
this.div_realStuff.setAttribute("id",id+"realStuff");
this.div_realStuff.style = "width: 100%; height: 100%; position: relative; left: 0; top: 0;";
this.div_network = document.createElement("div");
this.div_network.setAttribute("id",id+"container");
this.div_network.style = "width: 100%; height: 100%;position: relative; left: 0; top: 0;";
this.div_navi = document.createElement("div");
this.div_navi.setAttribute("id",id+"navi");
//this.div_navi.style = "position: absolute; left: 0; top: 0; float: right; width: 100%; height: 100%";
this.div_contextMenu = document.createElement("div");
this.div_contextMenu.setAttribute("id",id+"contextMenu");
this.div_realStuff.appendChild(this.div_network);
this.div_realStuff.appendChild(this.div_navi);
this.div_realStuff.appendChild(this.div_contextMenu);
this.div_root.appendChild(this.div_header);
this.div_root.appendChild(this.div_realStuff);
},
dataPreProcessing: function(){
var thisLib = this;
this.component = this.para.essentials.component;
var topoonly = this.para.essentials.topoOnly;
var component = this.component;
var rootname = component.root;
this.rootname = rootname;
if (this.para.essentials.viewRoot){
rootname = this.para.essentials.viewRoot;
}
this.levelCalculation(component, rootname);
this.network = new vis.Network(thisLib.div_network);
// After all images are cached (allow some of them not found), the rest pipeline are proceed by resourceSync
this.cacheImage();
},
levelCalculation: function(component, rootname){
var rootlevel = 0;
var thisLevel = 0;
var thisLevelNodes = [ rootname ];
var nextLevelNodes = [];
var displaynodes = [];
// Change all nodes level to undefined
for (var node in component.nodes){
var a = component.nodes[node].level;
component.nodes[node].level = 1;
var b = component.nodes[node].level;
}
while (thisLevelNodes.length > 0){
for(var i=0; i < thisLevelNodes.length; i++){
var currentNode = thisLevelNodes[i];
var edgesOfCurrentNode = component.edges[currentNode];
if (edgesOfCurrentNode != undefined){
for (var currentEdgeIndex in edgesOfCurrentNode){
var currentEdge = edgesOfCurrentNode[currentEdgeIndex];
nextLevelNodes.push(currentEdge.to);
component.nodes[ currentNode ].level = thisLevel;
displaynodes[ currentNode ] = 1;
}
}
else{
component.nodes[ currentNode ].level = thisLevel;
displaynodes[ currentNode ] = 1;
}
//var debug = "Node: " +currentNode + " is set to level-" + thisLevel;
//console.log(debug);
}
thisLevelNodes = nextLevelNodes;
nextLevelNodes = [];
thisLevel += 1;
}
this.rootlevel = rootlevel;
this.displaynodes = displaynodes;
this.component = component;
},
getImageURL: function(acc){
var imgURL = "";
if (this.para.essentials.useGlyTouCanAsImageSource){
imgURL = "https://glytoucan.org/glycans/" + acc + "/image?";
imgURL += "style=" + this.para.essentials.GlyTouCanImagePara.style;
imgURL += "&format=" + this.para.essentials.GlyTouCanImagePara.format;
imgURL += "¬ation=" + this.para.essentials.GlyTouCanImagePara.notation;
}else {
imgURL = this.para.essentials.imgURL1 + acc + this.para.essentials.imgURL2;
}
return imgURL
},
cacheImage: function(){
var thisLib = this;
var component = this.component;
var nodes = component.nodes;
for (var node in component.nodes) {
var nodeAcc = component.nodes[node].name;
this.resourceStatus[nodeAcc] = false;
}
for (var node in component.nodes) {
var nodeContent = component.nodes[node];
var nodeAcc = component.nodes[node].name;
var imageURL = "";
if (nodeContent.alternativeImageURL){
imageURL = nodeContent.alternativeImageURL;
}
else {
imageURL = this.getImageURL(nodeAcc);
}
nodeContent.imageURL = imageURL;
var imgEle = document.createElement("img");
imgEle.name = nodeAcc;
imgEle.src = imageURL;
imgEle.onload = function(){
thisLib.imageHeight.push(this.height);
thisLib.imageWidth.push(this.width);
thisLib.resourceStatus[this.name] = true;
thisLib.resourceSync();
};
imgEle.onerror = function(){
// Do something while image can not be found?
thisLib.imageHeight.push(undefined);
thisLib.imageWidth.push(undefined);
thisLib.resourceStatus[this.name] = true;
thisLib.resourceSync();
};
}
},
resourceSync: function(){
var thisLib = this;
var proceed = true;
for (var key in this.resourceStatus){
if (!this.resourceStatus[key]){
proceed = false;
}
}
if (proceed){
this.displayTitle();
this.layoutCalculation();
var options = this.visOptionGenerate();
var data = this.createNodeAndEdges();
this.data = data;
this.options = options;
this.network.setOptions(options);
this.forceRedraw(true);
this.doubleClickEvent();
if (this.para.contextMenu.enable){
if (this.para.contextMenu.defaultMenu){
this.defaultContextMenu();
}
else{
this.customizedContextMenu();
}
}
}
},
displayTitle: function(){
var rootname = this.rootname;
var component = this.component;
if (this.para.display.enableTitle){
var header = document.createElement("h2");
header.style = "margin: 1px;";
this.div_header.appendChild(header);
header.innerHTML = rootname + " (" + component.mw + ") - " + "Level " + component.nodes[rootname].level;
}
},
layoutCalculation: function(){
var allWidth = this.imageWidth;
var allHeight = this.imageHeight;
allWidth.sort(function(a, b){return b-a});
allHeight.sort(function(a, b){return b-a});
var greatestWidth = allWidth[0];
var greatestHeight = allHeight[0];
// vertical space is the distance between 2 level
// horizontal space is the distance between the center of 2 image
var horizontalSpace, verticalSpace;
if (greatestWidth != undefined && greatestHeight != undefined ){
if ([2,4].includes(this.para.display.orientation)){
verticalSpace = greatestWidth * 1.3;
horizontalSpace = greatestHeight * 1.3 + 30; // Extra 35 is for the label
}
else{
horizontalSpace = greatestWidth * 1.05;
verticalSpace = greatestHeight * 1.3 + 30; // Extra 35 is for the label
}
}
else{
verticalSpace = 220;
horizontalSpace = 400;
}
// console.log(greatestHeight, greatestWidth);
// console.log(verticalSpace, horizontalSpace);
this.verticalSpace = verticalSpace;
this.horizontalSpace = horizontalSpace;
},
visOptionGenerate: function(){
var thisLib = this;
var option_template = {
layout: {
hierarchical: {
direction: 'UD',
enabled: true,
levelSeparation: thisLib.verticalSpace, // the default value is 150
nodeSpacing: thisLib.horizontalSpace // might be affected by physics engine, default value 100
}
},
manipulation: {
enabled: false,
initiallyActive: true,
addNode: false,
addEdge: function(edgeData,callback) {
console.log(JSON.stringify(edgeData,null,4));
var lvl = nodes._data[edgeData.from].level;
d3.values(edges._data).forEach(function (e) {
if ((e.to == edgeData.to) && (e.id != edgeData.id)) {
if (nodes._data[e.from].level > lvl) {
lvl = nodes._data[e.from].level;
}
}
});
nodes._data[edgeData.to].level = (lvl + 1);
callback(edgeData);
forceredraw(false);
},
editEdge: function(edgeData,callback) {
console.log(JSON.stringify(edgeData,null,4));
var lvl = nodes._data[edgeData.from].level;
d3.values(edges._data).forEach(function (e) {
if ((e.to == edgeData.to) && (e.id != edgeData.id)) {
if (nodes._data[e.from].level > lvl) {
lvl = nodes._data[e.from].level;
}
}
});
nodes._data[edgeData.to].level = (lvl + 1);
callback(edgeData);
forceredraw(false);
},
deleteEdge: function(edgeData,callback){
callback(edgeData);
forceredraw(false);
}
},
physics: {
enabled: false,
hierarchicalRepulsion: {
springConstant: 0.3,
nodeDistance: thisLib.horizontalSpace
}
},
nodes: {
borderWidth: 0,
borderWidthSelected: 2,
chosen: true,
color: {
border: '#FFFFFF',
background: '#FFFFFF',
highlight: {
border: '#2B7CE9',
background: '#FFFFFF'
},
hover: {
border: '#2B7CE9',
background: '#FFFFFF'
}
}
}
};
var orientationDict = {1:"UD", 2:"LR", 3:"DU", 4:"RL"};
var orientationValue = orientationDict[this.para.display.orientation];
option_template.layout.hierarchical.direction = orientationValue;
return option_template
},
createNodeAndEdges: function(){
var thisLib = this;
var component = this.component;
var rootlevel = this.rootlevel;
var displaynodes = this.displaynodes;
var nodes = new vis.DataSet();
d3.keys(component.nodes).forEach(function (k) {
var d = component.nodes[k];
//d.id = d.id;
//d.label = d.label;
d.level -= rootlevel;
d.shape = 'image';
d.id = d.name;
if (d.label){}
else{
d.label = d.name;
}
d.borderColor = "#FFFFFF";
d.shapeProperties = {
useBorderWithImage: true,
useImageSize: true
};
d.image = d.imageURL;
//d.brokenImage = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+ip1sAAAAASUVORK5CYII";
if (displaynodes[k] != 1) {
//d.hidden = true;
} else {
//d.hidden = false;
nodes.update(d);
}
});
var edges = new vis.DataSet();
d3.keys(component.edges).forEach(function (k) {
component.edges[k].forEach(function(e) {
e.arrows = 'middle';
if (e.type == 'equals') {
e.color = {color:'red'};
} else if (e.type == 'contains') {
e.color = {color: 'blue'};
}else{
e.color = {};
}
if ((e.to in displaynodes) && (e.from in displaynodes)){
edges.update(e);
}
});
});
this.nodes = nodes;
this.edges = edges;
// create a data set for network
var data = {
nodes: nodes,
edges: edges
};
return data
},
forceRedraw: function(init){
var thisLib = this;
thisLib.network.setData(thisLib.data);
function drawNavi(){
if (init){
thisLib.network.fit(thisLib.naviInitial());
}
else {
thisLib.naviInitial();
}
}
function temp(){
setTimeout(drawNavi,50);
}
if (thisLib.para.display.enableNavi ){
thisLib.network.once('afterDrawing', temp, false);
}
},
// *********************************************************
// ******* Function for interacting with the canvas ********
// *********************************************************
doubleClickEvent: function(){
var thisLib = this;
thisLib.network.on("doubleClick",zoomWhenDoubleClicked);
function zoomWhenDoubleClicked(data){
var selectnode = data.nodes;
var connectednode = [];
if (selectnode.length > 0){
connectednode = thisLib.network.getConnectedNodes(selectnode);
connectednode.push(selectnode);
}
if (connectednode.length > 1) {
thisLib.network.fit({
nodes: connectednode,
animation: true
});
}
}
thisLib.network.on("doubleClick",highlightWhenDoubleClicked);
function highlightWhenDoubleClicked(data){
var selectnode = data.nodes;
if (selectnode == undefined){
selectnode = [];
}
if (selectnode.length > 0){
var highLightNodes = [];
selectnode.forEach(function(node){
var connectedNodes = thisLib.network.getConnectedNodes(node);
highLightNodes = highLightNodes.concat(connectedNodes);
highLightNodes.push(node);
});
highLightNodes.forEach(function(nodeID){
// Alternative way to highlight the node - Enlarge the node
//nodes._data[nodeID].size = nodes._data[nodeID].size * 1.5;
});
thisLib.network.selectNodes(highLightNodes);
//forceredraw(false);
}
}
},
defaultContextMenu: function(){
var thisLib = this;
// Context menu, pop-up when you right click
// The default context menu give you to chance to change to sub graph view
thisLib.div_network.addEventListener("contextmenu",rightClickMenuGenerator,false);
function rightClickMenuGenerator(clickData){
//console.log(clickData);
document.addEventListener("click",clearEverythingInContextMenu,{once: true});
var menuELE = thisLib.div_contextMenu;
var menuList = document.createElement("dl");
clearEverythingInContextMenu();
//var x = clickData.pointer.DOM.x;
//var y = clickData.pointer.DOM.y;
var x = clickData.layerX;
var y = clickData.layerY;
clickData.preventDefault();
var root = thisLib.rootname;
//var selectedNodes = thisLib.network.getSelectedNodes();
var selectedNode = thisLib.network.getNodeAt({x:x,y:y});
var selectedNodes = [ selectedNode ];
var connectedNodes = [];
//updateList("Close Menu","dt");
menuELE.style = "margin: 0; padding: 0; overflow: hidden; position: absolute; left: "+x+"px; top: "+y+"px; background-color: #333333; border: none; ";//width: 100px; height: 100px
updateList("Jump to Composition:", "dt");
updateList(root, "dd");
if (selectedNode !== undefined){
selectedNodes.forEach(function(nodeID){
var c0 = thisLib.network.getConnectedNodes(nodeID);
connectedNodes = connectedNodes.concat(c0);
});
updateList("Jump to Selected Nodes:","dt");
selectedNodes.forEach(function(nodeID){
updateList(nodeID,"dd");
});
if (connectedNodes.length > 0){
updateList("Jump to Connected Nodes:","dt");
connectedNodes.forEach(function(nodeID){
updateList(nodeID,"dd");
});
}
}
menuELE.appendChild(menuList);
function updateList(id,DOMType){
// dds are used to call functions
// dts are just descriptive words
var entry = document.createElement(DOMType);
entry.style = "display: block; color: white; text-align: left; padding: 5px; text-decoration: none;";
entry.onmouseover = function(d){
entry.style = "display: block; color: white; text-align: left; padding: 5px; text-decoration: none; background-color: #111111";
};
entry.onmouseout = function(d){
entry.style = "display: block; color: white; text-align: left; padding: 5px; text-decoration: none; background-color: #333333";
};
entry.innerHTML = id;
if(id == "Close Menu"){
entry.onclick = function(){
clearEverythingInContextMenu();
}
}
else if (DOMType == "dd"){
entry.onclick = function(){
var para = thisLib.para;
para.essentials.viewRoot = id;
thisLib.init(para)
}
}
menuList.appendChild(entry);
return 0;
}
}
function clearEverythingInContextMenu(){
//console.log("closing");
var menuELE = thisLib.div_contextMenu;
while (menuELE.firstChild){
menuELE.removeChild(menuELE.firstChild);
}
menuELE.style = "";
}
},
customizedContextMenu: function(){
var thisLib = this;
// Context menu, pop-up when you right click
// The default context menu give you to chance to change to sub graph view
thisLib.div_network.addEventListener("contextmenu", rightClickMenuGenerator, false);
function rightClickMenuGenerator(clickData){
//console.log(clickData);
document.addEventListener("click",clearEverythingInContextMenu,{once: true});
var menuELE = thisLib.div_contextMenu;
var menuList = document.createElement("dl");
clearEverythingInContextMenu();
var x = clickData.layerX;
var y = clickData.layerY;
clickData.preventDefault();
var root = thisLib.rootname;
//var selectedNodes = thisLib.network.getSelectedNodes();
var selectedNode = thisLib.network.getNodeAt({x:x,y:y});
var selectedNodes = [ selectedNode ];
var connectedNodes = [];
//updateList("Close Menu","dt");
menuELE.style = "margin: 0; padding: 0; overflow: hidden; position: absolute; left: "+x+"px; top: "+y+"px; background-color: #333333; border: none; ";//width: 100px; height: 100px
// updateList("Jump to:", "dt");
if (selectedNode !== undefined && selectedNode !== "Topology" && !selectedNode.startsWith("fake")){
var entry = document.createElement("dt");
entry.style = "display: block; color: white; text-align: left; padding: 5px; text-decoration: none;";
entry.onmouseover = function(d){
entry.style = "display: block; color: white; text-align: left; padding: 5px; text-decoration: none; background-color: #111111";
};
entry.onmouseout = function(d){
entry.style = "display: block; color: white; text-align: left; padding: 5px; text-decoration: none; background-color: #333333";
};
entry.innerHTML = "Jump to GlycoMotif"; //change the description
entry.name = selectedNode;
entry.onclick = function(){
var nodeID = this.name;
var pre, suf;
if (thisLib.para.contextMenu.externalURL1){pre = thisLib.para.contextMenu.externalURL1}else{pre = ""}
if (thisLib.para.contextMenu.externalURL2){suf = thisLib.para.contextMenu.externalURL2}else{suf = ""}
var externalURL = pre + nodeID + suf;
window.open(externalURL);
};
menuList.appendChild(entry);
var entry2 = document.createElement("dt");
entry2.style = "display: block; color: white; text-align: left; padding: 5px; text-decoration: none;";
entry2.onmouseover = function(d){
entry2.style = "display: block; color: white; text-align: left; padding: 5px; text-decoration: none; background-color: #111111";
};
entry2.onmouseout = function(d){
entry2.style = "display: block; color: white; text-align: left; padding: 5px; text-decoration: none; background-color: #333333";
};
entry2.innerHTML = "Jump to GlyTouCan"; //change the description
entry2.name = selectedNode;
entry2.onclick = function(){
var nodeID = this.name;
var pre, suf;
pre = "https://glytoucan.org/Structures/Glycans/";
suf = "";
//var acc = nodeID.split("_")[0];
var externalURL = pre + nodeID + suf;
window.open(externalURL);
};
menuList.appendChild(entry2);
}
menuELE.appendChild(menuList);
}
function clearEverythingInContextMenu(){
//console.log("closing");
var menuELE = thisLib.div_contextMenu;
while (menuELE.firstChild){
menuELE.removeChild(menuELE.firstChild);
}
menuELE.style = "";
}
},
// *********************************************************
// ******************* Mini Map Function *******************
// *********************************************************
// The "global variables" for Mini-map are declared here
fitScale : 0,
fitPos : 0,
rectPointer : 0,
naviWindowWidth : 0,
naviWindowHeight : 0,
naviInitial : function (){
var thisLib = this;
var currentScale = this.network.getScale();
var currentPos = this.network.getViewPosition();
while(thisLib.div_navi.firstChild){
thisLib.div_navi.remove(thisLib.div_navi.firstChild)
}
var css = thisLib.naviWindowPos() + " background-color: #f2ffff; height: "+ thisLib.naviWindowHeight + "px; width: " + thisLib.naviWindowWidth + "px;";
thisLib.div_navi.style = css;
var naviNetworkContainer = document.createElement("div");
naviNetworkContainer.style = "position: absolute; border: 1px solid lightgray; height: 100%; width: 100%";
thisLib.div_navi.appendChild(naviNetworkContainer);
var naviNetwork = new vis.Network(naviNetworkContainer);
var data, nodes, edges, options;
data = thisLib.data;
options = thisLib.options;
naviNetwork.setOptions(options);
naviNetwork.setData(data);
thisLib.rectPointer = document.createElement("canvas");
thisLib.rectPointer.setAttribute("style","position: absolute; border: 1px solid lightgray;"); // border: 1px solid lightgray
thisLib.rectPointer.setAttribute("width",thisLib.naviWindowWidth);
thisLib.rectPointer.setAttribute("height",thisLib.naviWindowHeight);
//thisLib.rectPointer.setAttribute("id","glycanviewer_rect");
thisLib.div_navi.appendChild(thisLib.rectPointer);
thisLib.fitScale = thisLib.network.getScale();
thisLib.fitPos = thisLib.network.getViewPosition();
thisLib.whereAmI();
//this.network.fit(capture());
function capture(){
var networkCanvas = thisLib.div_network.getElementsByTagName("canvas")[0];
var image,
dataURI = networkCanvas.toDataURL();
thisLib.naviWindowSizeCalc();
image = document.createElement('img');
image.src = dataURI;
image.width = thisLib.naviWindowWidth;
image.height = thisLib.naviWindowHeight;
image.setAttribute("style","position: absolute; background-color: #f2ffff; border: 1px solid lightgray;"); // border: 1px solid lightgray
var navi = thisLib.div_navi;
navi.setAttribute("style",thisLib.naviWindowPos());
while (navi.childElementCount > 0) {
navi.removeChild(navi.children[0]);
}
navi.appendChild(image);
thisLib.rectPointer = document.createElement("canvas");
thisLib.rectPointer.setAttribute("style","position: absolute; border: 1px solid lightgray;"); // border: 1px solid lightgray
thisLib.rectPointer.setAttribute("width",thisLib.naviWindowWidth);
thisLib.rectPointer.setAttribute("height",thisLib.naviWindowHeight);
thisLib.rectPointer.setAttribute("id","glycanviewer_rect");
navi.appendChild(thisLib.rectPointer);
thisLib.fitScale = thisLib.network.getScale();
thisLib.fitPos = thisLib.network.getViewPosition();
moveBack();
}
function moveBack(){
thisLib.network.moveTo(
{
position: {x:currentPos.x, y:currentPos.y},
scale: currentScale,
offset: {x:0, y:0}
},thisLib.whereAmI()
);
//console.log(currentPos);
return 0;
}
},
whereAmI : function (){
var thisLib = this;
working();
//thisLib.div_network.eventListeners = null;
thisLib.div_network.addEventListener('click', working);
thisLib.div_network.addEventListener('wheel', working);
thisLib.div_network.addEventListener('touchend', working);
function working(){
var ctx = thisLib.rectPointer.getContext("2d");
ctx.clearRect(0, 0, thisLib.rectPointer.width, thisLib.rectPointer.height);
ctx.beginPath();
var rectWidth, rectHeight, rectRatio, dx, dy;
var canvas2thumbnailScale;
var currentScale = thisLib.network.getScale();
var currentPos = thisLib.network.getViewPosition();
rectRatio = thisLib.fitScale / currentScale;
rectWidth = thisLib.naviWindowWidth * rectRatio;
rectHeight = thisLib.naviWindowHeight * rectRatio;
var networkCanvas = thisLib.div_network.getElementsByTagName("canvas")[0];
canvas2thumbnailScale = thisLib.naviWindowWidth / networkCanvas.width;
dx = (currentPos.x - thisLib.fitPos.x)*thisLib.fitScale*canvas2thumbnailScale - 0.5*(rectWidth - thisLib.naviWindowWidth);
dy = (currentPos.y - thisLib.fitPos.y)*thisLib.fitScale*canvas2thumbnailScale - 0.5*(rectHeight - thisLib.naviWindowHeight);
// Consider page zooming issue
// Not sure whether it will work with browser other than chrome
var wholePageZoomLevel = thisLib.getZoomLevel();
// This formula is use to compensate for window scale
dx = (currentPos.x - thisLib.fitPos.x)*thisLib.fitScale*canvas2thumbnailScale * wholePageZoomLevel - 0.5*(rectWidth - thisLib.naviWindowWidth);
dy = (currentPos.y - thisLib.fitPos.y)*thisLib.fitScale*canvas2thumbnailScale * wholePageZoomLevel - 0.5*(rectHeight - thisLib.naviWindowHeight);
// complement the whole rectangle
var x1, x2, y1, y2;
x1 = (dx > 0)&&(dx < thisLib.naviWindowWidth);
x2 = (dx+rectWidth > 0)&&(dx+rectWidth < thisLib.naviWindowWidth);
y1 = (dy > 0)&&(dy < thisLib.naviWindowHeight);
y2 = (dy+rectHeight > 0)&&(dy+rectHeight < thisLib.naviWindowHeight);
// console.log([dx,dy]);
ctx.lineWidth = 2;
ctx.strokeStyle = "orange";
ctx.rect(dx,dy,rectWidth,rectHeight);
if (!x1){ctx.rect(1,dy,0,rectHeight);}
if (!x2){ctx.rect(thisLib.naviWindowWidth - 1,dy,0,rectHeight);}
if (!y1){ctx.rect(dx,1,rectWidth,0);}
if (!y2){ctx.rect(dx,thisLib.naviWindowHeight - 1,rectWidth,0);}
ctx.stroke();
}
},
naviWindowSizeCalc : function (){
var networkCanvas = this.div_network.getElementsByTagName("canvas")[0];
var percentage = this.para.display.naviOption.size;
var devicePixelRatio = window.devicePixelRatio;
var w1 = Math.round(networkCanvas.width * percentage / devicePixelRatio);
var h1 = Math.round(networkCanvas.height * percentage / devicePixelRatio);
this.naviWindowWidth = w1;
this.naviWindowHeight = h1;
return 0;
},
getZoomLevel : function () {
var level = this.div_network.getElementsByTagName("canvas")[0].width / this.div_root.clientWidth;
return level
},
naviWindowPos : function (){
this.naviWindowSizeCalc();
var h = this.naviWindowHeight;
var w = this.naviWindowWidth;
var pos = this.para.display.naviOption.position;
var tl2t = 0,
tl2b = 0,
tl2r = 0,
tl2l = 0;
var css = "position: absolute; ";
if (pos == 1 || pos == 3){
tl2l = w / 10;
css += "left: "+tl2l+"px; "
}
if (pos == 2 || pos == 4){
//tl2r = this.div_root.clientWidth - w * 1.1;
tl2r = w / 10;
css += "right: "+tl2r+"px; "
}
if (pos == 1 || pos == 2){
tl2t = h / 10;
css += "top: "+ tl2t +"px; "
}
if (pos == 3 || pos == 4){
tl2b = h / 10;
if (this.para.display.enableTitle)
{
tl2b = tl2b + 27;
}
css += "bottom: "+tl2b+"px; "
}
return css;
},
// *********************************************************
// ******************* Helper Function ********************
// *********************************************************
// *********************************************************
// ********************** IE Related ***********************
// *********************************************************
IEQuestionMark : function(){
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
return (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))
},
compatibleDraw : function(para){
var component = para.essentials.component;
var rootname = component.root;
var topoonly = para.essentials.topoOnly;
if (para.essentials.viewRoot){
rootname = para.essentials.viewRoot;
}
recomputeLevels(component);
function recomputeLevels(component) {
var toexplore = [ component.root ];
var r;
while (true) {
if (toexplore.length == 0) {
break;
}
r = toexplore.pop();
var rlevel = component.nodes[r].level;
if (r in component.edges) {
component.edges[r].forEach(function(e) {
if (component.nodes[e.to].level < (rlevel+1)) {
component.nodes[e.to].level = (rlevel+1);
}
toexplore.push(e.to);
});
}
}
}
// Loading the title