-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhierachy_plugin.js
2848 lines (2378 loc) · 83.4 KB
/
hierachy_plugin.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
$.fn.hierarchyDesigner = function (initData) {
//Default Load/Save Functions, Useful for reading to write your own
let defaults = {
load: function() {
//Default Load Data Function (Uses Browser LocalStorage)
exampleLoad();
},
store: function() {
//Default Save Data Function (Uses Browser LocalStorage)
exampleSave();
},
initExtra: function() {
//Extra Functionality
},
height:3000,
width: 3000
}
let onStartup = false;
//Extending the Defaults
initData = $.extend({}, defaults, initData);
const ThisSelector = this.selector;
//Declaratives
let CompleteList = []; //Complete list of User Groups and Org Types
let GroupList = []; //User Groups
let LabelList = []; //Organization Types
let dragging; //Dragging Function
let dragok = false; //Are we dragging
let GlobalIDList = []; //List of all AlphaNumeric Identifiers
let GlobalParentUser = "off"; //Capture Values for a User Check
let GlobalSourceUser = "off"; //Capture Values for a User Check
let taggingElement = undefined; //Capture Value for Tagging an Element
let offset = {x: 0, y: 0}; //Capture Values for Mouse Offset
let linking = false; //Are we trying to link an element?
let linkingBaseID = undefined; //Capture Value for Linking Paths
const FontAwesomeLicense = "https://fontawesome.com/license/free" //Credit for Vector Graphics Icon
const GlobalVersion = "1.0.0";
let svg; //Global SVG Element
let uiSVG; //Global UI SVG Element
let bootstrapModal; //Global Bootstrap Modal Element
let EventListenerList = [];
/*
Saving and Loading Structure will be as follows
type: (Organization/OrgType/Group/User/Line)
name: (Title of Whatever, Blank if a Line)
identifier: (Alphanumeric ID)
labels: (Types/Groups of User/Org can be blank if not one of those)
anchor1: (Alphanumeric of Element, If we are a line)
anchor2:(Alphanumeric of Element, If we are a line)
*/
//Attempt at Creating Custom Modals, Not Finished
const ModalCreator = {
createModal: function(entry, modalId, role) {
const parentDiv = d3.select("#BootstrapModal");
const topDiv = parentDiv.append("div")
.attr("class", "modal " + entry)
.attr("id", modalId)
.attr("role", role)
const secondDiv = topDiv.append("div")
.attr("class", "modal-"+role)
.attr("role", "document")
//Modal content
const ModalContent = secondDiv.append("div")
.attr("class", "modal-content")
const ModalHeader = ModalContent.append("div")
.attr("class", "modal-header")
const ModalBody = ModalContent.append("div")
.attr("class", "modal-body")
const ModalFooter = ModalContent.append("div")
.attr("class", "modal-footer")
/*$('#'+modalId).on('hidden.bs.modal', function () {
$(this).remove();
})*/
//$("#"+modalId).modal();
return modalId;
},
addCloseButton: function(selectorId) {
let modify = undefined;
if (selectorId.length > 0) {
modify = d3.select(selectorId[0])
for (let i = 1; i < selectorId.length; i++) {
modify = modify.select(selectorId[i])
}
}
let ret = modify.append("button")
.attr("class", "close")
.attr("data-dismiss", "modal")
.text("✕")
return ret;
},
addHeader: function(selectorId, headerClass, headerSize, input) {
let modify = undefined;
if (selectorId.length > 0) {
modify = d3.select(selectorId[0])
for (let i = 1; i < selectorId.length; i++) {
modify = modify.select(selectorId[i])
}
}
let ret = modify.append("h"+String(headerSize))
.attr("class", headerClass)
.text(input)
return ret;
},
addForm: function(selectorId, formId) {
let modify = undefined;
if (selectorId.length > 0) {
modify = d3.select(selectorId[0])
for (let i = 1; i < selectorId.length; i++) {
modify = modify.select(selectorId[i])
}
}
let ret = modify.append("form")
.attr("id", formId)
return ret;
},
addDiv: function(selectorId, divClass, divId, divInput) {
let modify = undefined;
if (selectorId.length > 0) {
modify = d3.select(selectorId[0])
for (let i = 1; i < selectorId.length; i++) {
modify = modify.select(selectorId[i])
}
}
let ret = modify.append("div")
.attr("class", divClass)
.attr("id", divId)
.text(divInput)
return ret;
},
addLabel: function(selectorId, labelId, labelClass, labelInput) {
let modify = undefined;
if (selectorId.length > 0) {
modify = d3.select(selectorId[0])
for (let i = 1; i < selectorId.length; i++) {
modify = modify.select(selectorId[i])
}
}
let ret = modify.append("label")
.attr("id", labelId)
.attr("class", labelClass)
.text(labelInput)
return ret;
},
addInput: function(selectorId, inputType, inputClass, inputId, inputName, inputPlaceholder, inputInnerText) {
let modify = undefined;
if (selectorId.length > 0) {
modify = d3.select(selectorId[0])
for (let i = 1; i < selectorId.length; i++) {
modify = modify.select(selectorId[i])
}
}
let ret = modify.append("input")
.attr("type", inputType)
.attr("class", inputClass)
.attr("id", inputId)
.attr("name", inputName)
.attr("placeholder", inputPlaceholder)
.text(inputInnerText)
return ret;
},
addSubmit: function(selectorId, buttonClass, buttonText) {
let modify = undefined;
if (selectorId.length > 0) {
modify = d3.select(selectorId[0])
for (let i = 1; i < selectorId.length; i++) {
modify = modify.select(selectorId[i])
}
}
let ret = modify.append("button")
.attr("type", "submit")
.attr("class", buttonClass)
.text(buttonText)
return ret;
},
addLine: function(selectorId, lineStyle, lineSize, lineColor) {
let modify = undefined;
if (selectorId.length > 0) {
modify = d3.select(selectorId[0])
for (let i = 1; i < selectorId.length; i++) {
modify = modify.select(selectorId[i])
}
}
let ret = modify.append("hr")
.attr("style", lineStyle)
.attr("size", lineSize)
.attr("color", lineColor)
return ret;
},
addBreak: function(selectorId) {
let modify = undefined;
if (selectorId.length > 0) {
modify = d3.select(selectorId[0])
for (let i = 1; i < selectorId.length; i++) {
modify = modify.select(selectorId[i])
}
}
let ret = modify.append("br")
return ret;
},
addParagraph: function(selectorId, paragraphStyle, paragraphInput, paragraphId) {
let modify = undefined;
if (selectorId.length > 0) {
modify = d3.select(selectorId[0])
for (let i = 1; i < selectorId.length; i++) {
modify = modify.select(selectorId[i])
}
}
let ret = modify.append("p")
.attr("style", paragraphStyle)
.attr("id", paragraphId)
.text(paragraphInput)
return ret;
},
addLink: function(selectorId, linkUrl, linkText, linkTarget) {
//<a href='https://fontawesome.com/license/free'>FontAwesome Free License</a>
let modify = undefined;
if (selectorId.length > 0) {
modify = d3.select(selectorId[0])
for (let i = 1; i < selectorId.length; i++) {
modify = modify.select(selectorId[i])
}
}
let ret = modify.append("a")
.attr("href", linkUrl)
.attr("target", linkTarget)
.text(linkText)
return ret;
}
}
function initModals() {
/*
Deals with creating all the Bootstrap Modals using d3.js to append divs and form elements
*/
bootstrapModal = ModalCreator.addDiv([".hierarchy_designer"], "BootstrapModal", "BootstrapModal", "")
console.log(bootstrapModal)
console.log("Hello")
const ListTags = ModalCreator.createModal("fade", "modalNodeListTags", "dialog");
ModalCreator.addCloseButton(["#"+ListTags, ".modal-header"])
ModalCreator.addHeader(["#"+ListTags, ".modal-header"], "modal-title", 4, "List of all Tags for this Node")
ModalCreator.addForm(["#"+ListTags, ".modal-body"], "nodeListTagsForm")
ModalCreator.addDiv(["#nodeListTagsForm"], "form-check", "addMoreTags")
console.log(ListTags);
const CreateOrg = ModalCreator.createModal("fade", "modalNodeCreateOrg", "dialog");
ModalCreator.addCloseButton(["#"+CreateOrg, ".modal-header"])
ModalCreator.addHeader(["#"+CreateOrg, ".modal-header"], "modal-title", 4, "Create a new organization node");
ModalCreator.addForm(["#"+CreateOrg, ".modal-body"], "nodeCreateForm")
ModalCreator.addDiv(["#nodeCreateForm"], "form-group", "form-group")
ModalCreator.addDiv(["#nodeCreateForm"], "form-check", "form-check")
ModalCreator.addLabel(["#nodeCreateForm", ".form-group"], "OrgName", "", "Organization Name")
ModalCreator.addInput(["#nodeCreateForm", ".form-group"], "text", "form-control", "nodeCreateName", "Org", "GitHub")
ModalCreator.addInput(["#nodeCreateForm", ".form-group"], "checkbox", "form-check-input", "exampleCheck1", "UserCheck")
ModalCreator.addLabel(["#nodeCreateForm", ".form-group"], "", "form-check-label", "Check this box to create a User")
ModalCreator.addSubmit(["#nodeCreateForm"], "btn btn-primary", "Submit")
console.log(CreateOrg)
const CreateTags = ModalCreator.createModal("fade", "modalNodeCreateTags", "dialog")
ModalCreator.addCloseButton(["#"+CreateTags, ".modal-header"]);
ModalCreator.addHeader(["#"+CreateTags, ".modal-header"], "modal-title", 4, "Create a new User Group or Organization Type")
ModalCreator.addForm(["#"+CreateTags, ".modal-body"], "nodeCreateTagsForm")
ModalCreator.addDiv(["#nodeCreateTagsForm"], "form-group", "form-group", "User Group/Org Type Name")
ModalCreator.addDiv(["#nodeCreateTagsForm"], "form-check", "form-check")
ModalCreator.addInput(["#nodeCreateTagsForm", ".form-group"], "text", "form-control", "nodeCreateName", "TagName", "Dealership/Dealer")
ModalCreator.addInput(["#nodeCreateTagsForm", ".form-check"], "checkbox", "form-check-input", "exampleCheck1", "UserCheck")
ModalCreator.addLabel(["#nodeCreateTagsForm", ".form-check"], "", "form-check-label", "Check this box to create a User Group")
ModalCreator.addLine(["#nodeCreateTagsForm"], "width:100%", "2", "black")
ModalCreator.addHeader(["#nodeCreateTagsForm"], "modal-title", 4, "Can only link to these tags")
ModalCreator.addHeader(["#nodeCreateTagsForm"], "modal-title", 6, "(Leave Blank for Unrestricted Linking)")
ModalCreator.addHeader(["#nodeCreateTagsForm"], "modal-title", 6, "(Organization would be child of these tags)")
ModalCreator.addDiv(["#nodeCreateTagsForm"], "form-check", "RestrictiveTags")
ModalCreator.addSubmit(["#nodeCreateTagsForm"], "btn btn-primary", "Submit")
const ConnectUser = ModalCreator.createModal("fade", "modalNodeAddUserGroup", "dialog")
ModalCreator.addCloseButton(["#"+ConnectUser, ".modal-header"]);
ModalCreator.addHeader(["#"+ConnectUser, ".modal-header"], "modal-title", 4, "Connect User to Org Using These Tags")
ModalCreator.addForm(["#"+ConnectUser, ".modal-body"], "nodeAddUserGroup")
ModalCreator.addDiv(["#nodeAddUserGroup"], "form-check", "addUserGroup")
ModalCreator.addSubmit(["#nodeAddUserGroup"], "btn btn-primary", "Submit")
const EditOrg = ModalCreator.createModal("fade", "modalNodeEditOrg", "dialog")
ModalCreator.addCloseButton(["#"+EditOrg, ".modal-header"]);
ModalCreator.addHeader(["#"+EditOrg, ".modal-header"], "modal-title", 4, "Edit Organization Node");
ModalCreator.addForm(["#"+EditOrg, ".modal-body"], "nodeEditOrgForm")
ModalCreator.addDiv(["#nodeEditOrgForm"], "form-group")
ModalCreator.addLabel(["#nodeEditOrgForm", ".form-group"], "", "", "Organization Name")
ModalCreator.addInput(["#nodeEditOrgForm", ".form-group"], "text", "form-control", "nodeEditName", "Org", "GitHub");
ModalCreator.addDiv(["#nodeEditOrgForm"], "form-check")
ModalCreator.addSubmit(["#nodeEditOrgForm"], "btn btn-primary", "Submit")
const NewSvg = ModalCreator.createModal("fade", "modalNewSVG", "dialog")
ModalCreator.addCloseButton(["#"+NewSvg, ".modal-header"]);
ModalCreator.addHeader(["#"+NewSvg, ".modal-header"], "modal-title", 4, "Start a new blank SVG")
ModalCreator.addForm(["#"+NewSvg, ".modal-body"], "nodeNewSVG");
ModalCreator.addDiv(["#nodeNewSVG"], "form-group")
ModalCreator.addInput(["#nodeNewSVG", ".form-group"], "text", "", "", "height", "Height", " Height")
ModalCreator.addBreak(["#nodeNewSVG", ".form-group"])
ModalCreator.addInput(["#nodeNewSVG", ".form-group"], "text", "", "", "width", "Width", " Width")
ModalCreator.addSubmit(["#nodeNewSVG"], "btn btn-primary", "Submit")
const aboutTool = ModalCreator.createModal("fade", "modalAboutTool", "dialog")
ModalCreator.addCloseButton(["#"+aboutTool, ".modal-header"])
ModalCreator.addHeader(["#"+aboutTool, ".modal-header"], "modal-title", 4, "About Hierarchy Designer")
ModalCreator.addParagraph(["#"+aboutTool, ".modal-body"], "text-align:center;", "Developed by Shubshub", "p1")
ModalCreator.addParagraph(["#"+aboutTool, ".modal-body"], "text-align:center;", "Vector Icon License: ", "p2")
ModalCreator.addLink(["#"+aboutTool, ".modal-body", "#p2"], "https://fontawesome.com/license/free", "FontAwesome Free License", "_blank")
ModalCreator.addParagraph(["#"+aboutTool, ".modal-body"], "text-align:center;", "Tested on Chrome 91.0.4472.114, Firefox 89.0.2, Edge 91.0.864.59", "p3")
ModalCreator.addParagraph(["#"+aboutTool, ".modal-body"], "text-align:center;", "hopefully it works lol", "p4")
ModalCreator.addParagraph(["#"+aboutTool, ".modal-body"], "text-align:center;", `Version ${GlobalVersion}`, "p5")
}
function svgToImage() {
var doctype = '<?xml version="1.0" standalone="no"?>'
+ '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
var source = (new XMLSerializer()).serializeToString(d3.select(svg).node());
var blob = new Blob([ doctype + source], { type: 'image/svg+xml' });
var url = window.URL.createObjectURL(blob);
var img = new Image();
img.src = url;
img.onload = function(){
var canvas = d3.select('body').append('canvas').node();
canvas.width = 680;
canvas.height = 530;
var ctx = canvas.getContext('2d');
// draw image on canvas
ctx.drawImage(img, 0, 0);
var canvasUrl = canvas.toDataURL("image/png");
canvas.remove();
// ajax call to send canvas(base64) url to server. Or create anchor tag to give download option
}
}
//Default Loading Function, Grabs from Local Storage
function exampleLoad() {
/*
Elements to Load
data[0] = {Node List, "nodes"}
data[1] = {GroupList, "userLabel"}
data[2] = {LabelList, "orgLabels"}
data[3] = {User Line Parents, "userParents"}
data[4] = {Org Line Parents, "orgParents"}
Loop through Node List
{
CreationData[0] = Organization Name
CreationData[1] = Is User?
createNode(x, y, width, height, CreationData, Identifier, false);
All Lines that are attached to this node
Concatenate this to a seperate variable that is outside of the loop
User Data: {Tag Name, Is User?} Loop this and store all Tag Data attached to Node in its own array
makeLabel(User Data, is User?, d3.select("#"+_id));
}
Create a seperate array with duplicate entries of User Line Parents removed
Loop through unique data[3] (User Parents)
{
const lineID = linkPaths(userData[z].par1, userData[z].par2);
addLinkButton(User Data Current.par1, User Data Current.par2, lineID[0], false);
const label = addLabelLine(lineID[0], User Data Current.tex, lineID[1],User Data Current.par1, User Data Current.par2);
}
Do the same with Organization Line Parents
*/
d3.selectAll("svg#designerID > .draggable").remove()
d3.selectAll("svg#designerID > #lineGroup > *").remove()
d3.selectAll("svg#designerID > #userSpaceGroup > *").remove()
const data = JSON.parse(localStorage.getItem("GameData"));
console.log(data)
AllLineData = [];
GroupList = data[1].data;
LabelList = data[2].data;
if (data[0].type == "nodes") {
const workingData = data[0].data;
for (var j = 0; j < workingData.length; j++) {
//createNode(x, y, width, height, CreationData, id)
//createNode(45, 55, 250, 100, CreationData);
const CreationData = [];
const TagData = workingData[j].TagData;
CreationData[0] = workingData[j].OrgName;
CreationData[1] = workingData[j].isUser;
const _x = workingData[j].x;
const _y = workingData[j].y;
const _id = workingData[j].id;
createNode(_x, _y, 250, 100, CreationData, _id, false);
AllLineData = AllLineData.concat(workingData[j].LineData);
console.log(TagData);
const UserData = [];
for (var k = 0; k < TagData.length; k++) {
workingTag = TagData[k];
UserData.push({name: workingTag, value: CreationData[1]});
}
makeLabel(UserData, CreationData[1], d3.select("#"+_id));
}
}
if (data[3].type == "userParents") {
let userData = data[3].data;
console.log("User Data Parents");
userData = Array.from(new Set(userData.map(JSON.stringify)), JSON.parse)
console.log(userData);
for (var z = 0; z < userData.length; z++) {
const lineID = linkPaths(userData[z].par1, userData[z].par2);
addLinkButton(userData[z].par1, userData[z].par2, lineID[0], false, lineID[1]);
if (lineID[0] != false) {
const label = addLabelLine(lineID[0], userData[z].tex, lineID[1], userData[z].par1, userData[z].par2);
} else {
console.log("Failed Link for Some Reason");
}
}
}
if (data[4].type == "orgParents") {
let userData = data[4].data;
userData = Array.from(new Set(userData.map(JSON.stringify)), JSON.parse)
console.log("Org Data Parents");
console.log(userData);
for (var z = 0; z < userData.length; z++) {
const lineID = linkPaths(userData[z].par1, userData[z].par2);
addLinkButton(userData[z].par1, userData[z].par2, lineID[0], false, lineID[1]);
if (lineID[0] != false) {
const label = addLabelLine(lineID[0], userData[z].tex, lineID[1], userData[z].par1, userData[z].par2);
} else {
console.log("Failed Link for Some Reason");
}
}
}
console.log("All Line Data")
console.log(AllLineData);
console.log("User Groups");
console.log(GroupList);
console.log("Org Types");
console.log(LabelList);
}
//Default Saving Function, Just Saves to Local Storage
function exampleSave() {
/*
Elements to Save
GroupList - User Groups
LabelList - Organization Types
All Nodes - Visual Display
All Lines - Connected Pathways
All Created User Group Labels - Theyre attached to the Lines
All Created Organization Type Labels - Theyre attached to the Nodes
User Linked Pathways
{
par1: Parent1 ID
par2: Parent2 ID
tex: Label Text
}
Organization Linked Pathways
{
par1: Parent1 ID
par2: Parent2 ID
tex: Label Text
}
CompleteNode(name, _x, _y, LineData, TagData, OrgIdentifier, myself.attr("isUser"))
Nodes
{
name: Organization/User Name
x: X Position
y: Y Position
LineData: [Line Anchor1, Line Anchor2] Lines that attached to this node
TagData: [Label Number] //All of them in an array
id: Alpha Numeric Identifier of this Node
isUser: Is this a user or an organization "on" for User
}
data[0] = {Node List, "nodes"}
data[1] = {GroupList, "userLabel"}
data[2] = {LabelList, "orgLabels"}
data[3] = {User Line Parents, "userParents"}
data[4] = {Org Line Parents, "orgParents"}
EntireStructure.push(StructureData(NodeList, "nodes"))
EntireStructure.push(StructureData(UserLabels, "userLabel"));
EntireStructure.push(StructureData(OrgLabels, "orgLabels"));
EntireStructure.push(StructureData(UserParents, "userParents"));
EntireStructure.push(StructureData(OrgParents, "orgParents"));
*/
const CompleteNode = function(OrgName, X, Y, LineData, TagData, ID, isUser) {
let FullNode = {};
FullNode.OrgName = OrgName
FullNode.id = ID
FullNode.x = X
FullNode.y = Y
FullNode.LineData = LineData
FullNode.TagData = TagData
FullNode.isUser = isUser
return FullNode;
}
UserLabels = GroupList;
OrgLabels = LabelList;
UserParents = [];
OrgParents = [];
NodeList = [];
console.log("All Nodes");
const AllNodes = d3.selectAll("g.draggable");
const AllLines = d3.selectAll("line.linkedLine");
const AllUserSpace = d3.selectAll(".userLineSpaceText")
const AllOrgSpace = d3.selectAll(".orgLineSpaceText");
AllUserSpace
.each(function(d, i) {
const myself = d3.select(this);
const parent1 = myself.attr("parent1");
const parent2 = myself.attr("parent2");
const txt = myself.text();
UserParents.push({par1: parent1, par2: parent2, tex: txt});
});
AllOrgSpace
.each(function(d, i) {
const myself = d3.select(this);
const parent1 = myself.attr("parent1");
const parent2 = myself.attr("parent2");
const txt = myself.text();
OrgParents.push({par1: parent1, par2: parent2, tex: txt});
});
AllNodes
.each(function(d, i) {
const myself = d3.select(this);
const OrgIdentifier = myself.attr("id");
const nameRect = myself.select(".displayText");
const name = nameRect.text();
const _x = myself.attr("offX");
const _y = myself.attr("offY");
const LineData = [];
const TagData = [];
const AllTags = myself.selectAll(".LabelSpace");
//console.log(AllTags);
AllTags
.each(function(d, i) {
thisTag = d3.select(this);
TagData.push(thisTag.attr("typeNum"));
});
//console.log("Tag Data");
//console.log(TagData);
AllLines
.each(function(d, i) {
const meHere = d3.select(this);
let anchor1 = meHere.attr("anchor1");
let anchor2 = meHere.attr("anchor2");
anchor1 = anchor1.replace("#", "");
anchor2 = anchor2.replace("#", "");
if (anchor1 == OrgIdentifier || anchor2 == OrgIdentifier) {
LineData.push([anchor1, anchor2]);
}
});
NodeList.push(CompleteNode(name, _x, _y, LineData, TagData, OrgIdentifier, myself.attr("isUser")));
});
//console.log(NodeList);
EntireStructure = []
const StructureData = function(data, type) {
let dt = {}
dt.type = type;
dt.data = data;
return dt;
}
EntireStructure.push(StructureData(NodeList, "nodes"))
EntireStructure.push(StructureData(UserLabels, "userLabel"));
EntireStructure.push(StructureData(OrgLabels, "orgLabels"));
EntireStructure.push(StructureData(UserParents, "userParents"));
EntireStructure.push(StructureData(OrgParents, "orgParents"));
console.log("Entire Structure");
console.log(EntireStructure);
localStorage.setItem('GameData', JSON.stringify(EntireStructure));
}
//Start Dragging
function dragStart() {
/*
Grab the Mouse X and Y and subtract the Elements X and Y for Smooth Dragging
*/
const current = d3.select(this);
const baseElement = current;
offset.x = d3.event.x - baseElement.attr('offX');
offset.y = d3.event.y - baseElement.attr('offY');
}
//Currently Dragging
function dragged() {
/*
Updates the Position of All Nodes, All Lines, All Label Text on Lines
*/
if (linking == false) {
const current = d3.select(this);
const currentX = current.attr("offX");
const currentY = current.attr("offY");
const realPositionX = d3.event.x - offset.x;
const realPositionY = d3.event.y - offset.y;
const allElements = current;
//We act upon all the elements and use a built in offset value thats created when the element is create which anchors it within the baseElement
allElements
.each(function(d, i) {
const myself = d3.select(this);
myself
.attr("transform", `translate(${realPositionX}, ${realPositionY})`)
.attr("offX", realPositionX)
.attr("offY", realPositionY)
if (myself.attr("offX") != currentX || myself.attr("offY") != currentY) {
myself.attr("dirty", true)
}
});
const allLines = d3.selectAll("line.linkedLine");
allLines
.each(function(d, i) {
const myself = d3.select(this);
const currentX1 = myself.attr("x1");
const currentY1 = myself.attr("y1");
const currentX2 = myself.attr("x2");
const currentY2 = myself.attr("y2");
const firstBaseElementID = myself.attr("anchor1");
const secondBaseElementID = myself.attr("anchor2");
const firstGroup = d3.select(firstBaseElementID);
const secondGroup = d3.select(secondBaseElementID);
const firstBaseElem = d3.select(firstBaseElementID).select(".baseElement");
const secondBaseElem = d3.select(secondBaseElementID).select(".baseElement");
const firstWidth = parseInt(firstBaseElem.attr("width"));
const firstHeight = parseInt(firstBaseElem.attr("height"));
const secondWidth = parseInt(secondBaseElem.attr("width"));
const secondHeight = parseInt(secondBaseElem.attr("height"));
const firstX = parseInt(firstGroup.attr("offX"));
const firstY = parseInt(firstGroup.attr("offY"));
const secondX = parseInt(secondGroup.attr("offX"));
const secondY = parseInt(secondGroup.attr("offY"));
myself
.attr("x1", firstX + (firstWidth/2))
.attr("y1", firstY + (firstHeight/2))
.attr("x2", secondX + (secondWidth/2))
.attr("y2", secondY + (secondHeight/2));
if (myself.attr("x1") != currentX1 || myself.attr("x2") != currentX2 || myself.attr("y1") == currentY1 || myself.attr("y2") != currentY2) {
myself.attr("dirty", true);
}
});
const allUserSpaceText = d3.selectAll("text.userLineSpaceText, text.userLineSpaceTextBG");
allUserSpaceText
.each(function(d, i) {
const myself = d3.select(this);
const myLine = d3.select("#"+myself.attr("anchor"));
const xLine1 = parseInt(myLine.attr("x1"));
const xLine2 = parseInt(myLine.attr("x2"));
const yLine1 = parseInt(myLine.attr("y1"));
const yLine2 = parseInt(myLine.attr("y2"));
const midpointx = (xLine1 + xLine2) / 2;
const midpointy = (yLine1 + yLine2) / 2;
myself.attr("x", midpointx);
myself.attr("y", midpointy + parseInt(myself.attr("height")));
myself.attr("offX", midpointx);
myself.attr("offY", midpointy + parseInt(myself.attr("height")));
});
const allOrgSpaceText = d3.selectAll("text.orgLineSpaceText, text.orgLineSpaceTextBG");
allOrgSpaceText
.each(function(d, i) {
const myself = d3.select(this);
const myLine = d3.select("#"+myself.attr("anchor"));
const parentElement = myself.attr("parentElement");
const xLine1 = parseInt(myLine.attr("x1"));
const xLine2 = parseInt(myLine.attr("x2"));
const yLine1 = parseInt(myLine.attr("y1"));
const yLine2 = parseInt(myLine.attr("y2"));
const midpointx = (xLine1 + xLine2) / 2;
const midpointy = (yLine1 + yLine2) / 2;
let modmidpointx = 0;
let modmidpointy = 0;
if (parentElement == "first") {
modmidpointx = (xLine1 + midpointx) / 2;
modmidpointy = (yLine1 + midpointy) / 2;
} else {
modmidpointx = (xLine2 + midpointx) / 2;
modmidpointy = (yLine2 + midpointy) / 2;
}
myself.attr("x", modmidpointx);
myself.attr("y", modmidpointy + parseInt(myself.attr("height")));
myself.attr("offX", modmidpointx);
myself.attr("offY", modmidpointy + parseInt(myself.attr("height")));
});
}
}
//Edit the Organization/User Name
function editText(e) {
const parentElement = d3.select(this.parentNode)
const myself = parentElement.select(".displayText");
$("#modalNodeEditOrg").modal();
$("#nodeEditOrgForm").submit(function(event) {
const data = $("#nodeEditOrgForm").serializeArray();
const OrgName = data[0].value;
//console.log(data);
if (OrgName != "") {
myself.text(OrgName);
const textSpace = parentElement.select(".spaceText")
const TextLength = myself.node().getComputedTextLength();
textSpace.attr("width", TextLength + 4);
parentElement.attr("dirty", true)
}
$("#modalNodeEditOrg").modal("hide");
event.preventDefault();
$("#nodeEditOrgForm").unbind();
});
}
function AlphaNumericRandom()
{
/*
Generates an AlphaNumeric String and stores it in a Global List so only unique values can be created
*/
const anr = "a" + Math.random().toString(36).slice(2);
if (GlobalIDList.indexOf(anr) == -1) {
GlobalIDList.push(anr);
} else {
console.log("Regenerating");
anr = AlphaNumericRandom();
}
return anr;
}
function MouseMoveCapture() {
/*
If we are linking an object to another then have a line that follows the position of the mouse
Removed upon us no longer being in a link state wether the link succeeded or not
*/
//Execute code while the mouse is moving
if (linking == true) {
MouseLines = d3.select("line.followMouse")
//var offsetX = d3.event.x - MouseLines.attr("x2");
//var offsetY = d3.event.y - MouseLines.attr("y2");
MouseLines
.attr("x2", d3.event.x + document.documentElement.scrollLeft)
.attr("y2", d3.event.y + document.documentElement.scrollTop)
/*
offset.x = d3.event.x - baseElement.attr('offX');
offset.y = d3.event.y - baseElement.attr('offY');
}
*/
} else if (linking == false) {
MouseLines = d3.select("line.followMouse")
MouseLines.remove();
}
}
function populateUserTags() {
/*
Populates a Modal with Checkboxes allowing the user to add tags to a node
*/
const tagsModal = d3.select("#addUserGroup");
const createCheckbox = function(obj) {
const inputBox = tagsModal.append("input")
.attr("type", "checkbox")
.attr("class", "form-check-input")
.attr("name", obj)
.attr("id", AlphaNumericRandom())
//console.log(ListOfAlready);
//console.log(obj);
}
const createLabel = obj => tagsModal.append("label")
.attr("class", "form-check-label")
.attr("name", AlphaNumericRandom())
.style("padding-right", "10px")
.style("padding-left", "2px")
.text(obj)
for (let i = 0; i < GroupList.length; i++) {
createCheckbox(i);
createLabel(GroupList[i][0].value + " ");
}
if (GroupList.length == 0) {
createLabel("There are no groups to display");
}
}
function addLabelLine(lineID, userText, parentElement, firstElementID, secondElementID) {
if (parentElement == undefined) {
return;
}
/*
Creates a User Label on a connected line between two nodes
*/
const thisLine = d3.select(`line#${lineID}`);
thisLine.append("rect")
const userSpace = {
x: 0,
y: 0,
h: 24,
w: 128,
color: "white",
class: "userLineSpace",
toolTip: "User Role",
zindex: -1
}
const orgSpace = {
x: 0,
y: 0,
h: 24,
w: 128,
color: "white",
class: "orgLineSpace",
toolTip: "User Role",
zindex: -1
}
const spaceGroup = d3.select("#userSpaceGroup")
const createElement = function(obj) {
const identifier = AlphaNumericRandom();
const xLine1 = parseInt(thisLine.attr("x1"));
const xLine2 = parseInt(thisLine.attr("x2"));
const yLine1 = parseInt(thisLine.attr("y1"));
const yLine2 = parseInt(thisLine.attr("y2"));
const midpointx = (xLine1 + xLine2) / 2;
const midpointy = (yLine1 + yLine2) / 2;
let mx = midpointx;
let my = midpointy;
if (parentElement != "none") {
let modmidpointx = 0;
let modmidpointy = 0;
if (parentElement == "first") {
d3.select("#"+secondElementID).attr("CanLink", false)
modmidpointx = (xLine1 + midpointx) / 2;
modmidpointy = (yLine1 + midpointy) / 2;
} else {
d3.select("#"+firstElementID).attr("CanLink", false)
modmidpointx = (xLine2 + midpointx) / 2;
modmidpointy = (yLine2 + midpointy) / 2;
}
mx = modmidpointx;
my = modmidpointy;
}
spaceGroup.append("text")
.text(userText)
.attr("x", mx)
.attr("y", my + (obj.h))
.attr("offX", mx)
.attr("offY", my + (obj.h))
.attr("height", obj.h)
.attr("fill", "black")
.attr("class", obj.class + "TextBG")
.attr("id", identifier)
.attr("anchor", lineID)
.attr("parent1", firstElementID)
.attr("parent2", secondElementID)
.attr("parentElement", parentElement)
.attr("text-anchor", "middle")
.attr("filter", "url(#solid)")
.append("title")
.text(obj.toolTip)
spaceGroup.append("text")
.text(userText)
.attr("x", mx)
.attr("y", my + (obj.h))
.attr("offX", mx)
.attr("offY", my + (obj.h))
.attr("height", obj.h)
.attr("fill", "black")
.attr("parent1", firstElementID)
.attr("parent2", secondElementID)
.attr("parentElement", parentElement)
.attr("class", obj.class + "Text")
.attr("id", identifier)
.attr("anchor", lineID)
.attr("text-anchor", "middle")
return d3.select("#"+identifier);
}
if (parentElement == "none") {
createElement(userSpace);
} else {
createElement(orgSpace);
}
}
function clickHandler() {
/*
Handles the Click Event for each Node
Deals with Linking and User Group Label Adding
*/
const thisElement = d3.select(this)
const parentElement = d3.select(this.parentNode)
//console.log(thisElement)
const testValue = thisElement.attr("linking");
//console.log((testValue == "true"))
if (testValue == "true" && linking == true) {