Newer
Older
import {Component, ElementRef, EventEmitter, HostListener, OnInit, Output, ViewChild} from '@angular/core';
declare let JTopo:any;
@Component({
selector: 'smart-jtopo',
templateUrl: './jtopo.component.html',
styleUrls: []
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
})
/**
* 提供拓扑图面板相关操作的函数集,编辑器继承其全部功能
*/
export class JtopoComponent implements OnInit{
@Output() clickNode = new EventEmitter<any>();
@Output() clickLink = new EventEmitter<any>();
@ViewChild('topologyCanvas') topologyCanvas: ElementRef;
@ViewChild('topologyBody') topologyBody: ElementRef;
json = {
"version": "0.4.8",
"wheelZoom": "0.95",
"id": "ST172.19.105.52015100809430700001",
"childs": [
{
"id": "S172.19.105.52015100809430700002",
"elementType": "scene",
"translateX": "106.5",
"translateY": "20",
"scaleX": "1",
"scaleY": "1",
"childs": [
{
"id": "L172.19.105.52015100811153300001",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "1136300297928.587",
"nodeDst": "1406176935353.6848",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100811153300002",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "1394105924967.951",
"nodeDst": "1136300297928.587",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100811153300004",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "564241624829.4331",
"nodeDst": "318016674603.1266",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100811153300005",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "1062340763210.1544",
"nodeDst": "564241624829.4331",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100811153300006",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "564241624829.4331",
"nodeDst": "488323736331.2087",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100811153300007",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "564241624829.4331",
"nodeDst": "827722371280.0199",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100811153300008",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "564241624829.4331",
"nodeDst": "1045863334277.662",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100811153300009",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "1262070648024.5728",
"nodeDst": "564241624829.4331",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100811153300010",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "1262070648024.5728",
"nodeDst": "1136300297928.587",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100811214600001",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "253209434749.73596",
"nodeDst": "564241624829.4331",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100811214600002",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "253209434749.73596",
"nodeDst": "1136300297928.587",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100811214600003",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "520008948580.8119",
"nodeDst": "564241624829.4331",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100811214600004",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "520008948580.8119",
"nodeDst": "1136300297928.587",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100811512000002",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "809054608657.865",
"nodeDst": "1394105924967.951",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100811512000004",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "1250687739831.9912",
"nodeDst": "1062340763210.1544",
"lineType": "line",
"zindex": "2"
},
{
"id": "L172.19.105.52015100911051100001",
"elementType": "link",
"x": "0",
"y": "0",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "undefined",
"nodeSrc": "564241624829.4331",
"nodeDst": "597645745716.1871",
"lineType": "line",
"zindex": "2"
},
{
"elementType": "node",
"x": "198",
"y": "315",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "attack-network",
"textPosition": "Bottom_Center",
"nodeId": "1136300297928.587",
"nodeType": "EC",
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
},
{
"id": "N172.19.105.52015100809465000001",
"elementType": "node",
"x": "196",
"y": "242",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "attackr-router",
"textPosition": "Bottom_Center",
"nodeId": "1394105924967.951",
"nodeType": "VR",
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
},
{
"id": "N172.19.105.52015100809500700002",
"elementType": "node",
"x": "104",
"y": "383.5",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "attack-client",
"textPosition": "Bottom_Center",
"nodeId": "1406176935353.6848",
"nodeType": "VM",
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
},
{
"id": "N172.19.105.52015100810295700002",
"elementType": "node",
"x": "539",
"y": "321.5",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "intert-networker",
"textPosition": "Bottom_Center",
"nodeId": "564241624829.4331",
"nodeType": "EC",
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
},
{
"id": "N172.19.105.52015100810320100002",
"elementType": "node",
"x": "719",
"y": "160.5",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "ws-manage",
"textPosition": "Bottom_Center",
"nodeId": "318016674603.1266",
"nodeType": "VM",
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
},
{
"id": "N172.19.105.52015100810402700002",
"elementType": "node",
"x": "725",
"y": "225.5",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "ws-browser",
"textPosition": "Bottom_Center",
"nodeId": "488323736331.2087",
"nodeType": "VM",
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
},
{
"id": "N172.19.105.52015100810461200002",
"elementType": "node",
"x": "726",
"y": "293.5",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "internal-mssql",
"textPosition": "Bottom_Center",
"nodeId": "827722371280.0199",
"nodeType": "VM",
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
},
{
"id": "N172.19.105.52015100810485100002",
"elementType": "node",
"x": "726",
"y": "361.5",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "internal-ossec",
"textPosition": "Bottom_Center",
"nodeId": "1045863334277.662",
"nodeType": "VM",
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
},
{
"id": "N172.19.105.52015100810523500002",
"elementType": "node",
"x": "369",
"y": "245.5",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "internet-www",
"textPosition": "Bottom_Center",
"nodeId": "1262070648024.5728",
"nodeType": "ECVR",
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
},
{
"id": "N172.19.105.52015100811153300003",
"elementType": "node",
"x": "536",
"y": "237.5",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "intert-router",
"textPosition": "Bottom_Center",
"nodeId": "1062340763210.1544",
"nodeType": "VR",
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
},
{
"id": "N172.19.105.52015100811163200002",
"elementType": "node",
"x": "367.5",
"y": "320",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "internet-blog",
"textPosition": "Bottom_Center",
"nodeId": "253209434749.73596",
"nodeType": "ECVR",
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
},
{
"id": "N172.19.105.52015100811204100002",
"elementType": "node",
"x": "370.5",
"y": "403",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "internet-vpn",
"textPosition": "Bottom_Center",
"nodeId": "520008948580.8119",
"nodeType": "ECVR",
"nodeParams": {},
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
},
{
"id": "N172.19.105.52015100811512000001",
"elementType": "node",
"x": "194.5",
"y": "166",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "attack-firewall",
"textPosition": "Bottom_Center",
"nodeId": "809054608657.865",
"nodeType": "FW",
"nodeParams": {},
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
},
{
"id": "N172.19.105.52015100811512000003",
"elementType": "node",
"x": "534.5",
"y": "163",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "intert-firewall",
"textPosition": "Bottom_Center",
"nodeId": "1250687739831.9912",
"nodeType": "FW",
"nodeParams": {},
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
},
{
"id": "N172.19.105.52015100911045400002",
"elementType": "node",
"x": "727.5",
"y": "430",
"width": "32",
"height": "32",
"rotate": "0",
"scaleX": "1",
"scaleY": "1",
"text": "defend-client",
"textPosition": "Bottom_Center",
"nodeId": "597645745716.1871",
"nodeType": "VM",
"nodeImage": "http://10.10.38.99:8888/file/icon/15474547275540202.png",
"zindex": "3"
}
]
}
]
};
utils;
config = {
// Stage属性
stageFrames: 500, // 舞台播放的帧数/秒
defaultScal: 0.95, // 鼠标滚轮缩放比例
eagleEyeVsibleDefault: false, // 是否显示鹰眼对象
// Node属性
nodeAlpha: 1, // 节点透明度,取值范围[0-1]
nodeStrokeColor: '22,124,255', // 节点描边的颜色
nodeFillColor: '255,222,173', // 节点填充颜色
nodeShadow: false, // 节点是否显示阴影
nodeShadowColor: 'rgba(0,0,0,0.5)', // 节点阴影的颜色
nodeFont: '12px Consolas', // 节点字体
nodeFontColor: 'black', // 节点文字颜色,如"255,255,0"
nodeDefaultWidth: 32, // 新建节点默认宽
nodeDefaultHeight: 32, // 新建节点默认高
nodeBorderColor: 'black', // 节点容器边框颜色,如"255,255,0"
nodeBorderRadius: 30, // 节点半径,非圆节点有此属性会变形
nodeRotateValue: 0.5, // 节点旋转的角度(弧度)
nodeScale: 0.2, // 节点缩放幅度(此处保证X和Y均等缩放)
// Link属性
linkAlpha: 1, // 连线透明度,取值范围[0-1]
linkStrokeColor: '0,0,0', // 连线的颜色
linkFillColor: '0,0,0',
linkShadow: false, // 是否显示连线阴影
linkShadowColor: 'rgba(0,0,0,0.5)',
linkFont: '12px Consolas', // 节点字体
linkFontColor: 'black', // 连线文字颜色,如"255,255,0"
linkArrowsRadius: 0, // 线条箭头半径
linkDefaultWidth: 3, // 连线宽度
linkOffsetGap: 80, // 折线拐角处的长度
linkDirection: 'vertical', // 折线的方向
// Container属性
containerAlpha: 1,
containerStrokeColor: '22,124,255',
containerFillColor: '22,124,255',
containerShadow: false,
containerShadowColor: 'rgba(0,0,0,0.5)',
containerFont: '12px Consolas',
containerFontColor: 'black',
containerBorderColor: 'black',
containerBorderRadius: 30,
arrowsRadius:null
};
// 布局参数
layout = {};
// 绘图区属性
stage = null;
scene = null;
// 当前模式
stageMode = 'edit';
// 默认连线类型
lineType = 'line';
// 当前选择的节点对象
currentNode = null;
// 当前选择的连线对象
currentLink = null;
beginNode = null;
link = null;
isSelectedMode;
tempNodeA = new JTopo.Node('tempA');
tempNodeZ = new JTopo.Node('tempZ');
constructor(){}
ngOnInit(){
this.init('assets/img/backimg.png',this.json);
}
/**
* 编辑器初始化方法,根据请求返回结果加载空白的或者指定结构的拓扑编辑器
* @param backImg 背景图片
* @param topologyJson 拓扑JSON结构
*/
init(backImg, topologyJson){
if(!topologyJson){
return;
}
let canvas = this.topologyCanvas.nativeElement;
canvas.width = this.topologyBody.nativeElement.clientWidth;
canvas.height = this.topologyBody.nativeElement.clientHeight;
this.tempNodeA.setSize(1,1);
this.tempNodeZ.setSize(1,1);
let self = this;
if(topologyJson === '-1'){
this.stage = new JTopo.Stage(canvas); //定义舞台对象
this.scene = new JTopo.Scene(this.stage); //定义场景对象
}else{
this.stage = JTopo.createStageFromJson(topologyJson,canvas);
this.scene = this.stage.childs[0];
}
this.getAllNodes()[0].alarm = "告警";
this.scene.mouseover(event=>{
//进入某个节点
// if(event.target != null && event.target instanceof JTopo.Node && event.target.nodeTooltip &&)
});
this.scene.click(event=>{
if(!event.target){
//单击空白处
return;
}
if(event.target instanceof JTopo.Node && this.stageMode === 'normal'){
}else if(event.target instanceof JTopo.Node && this.stageMode === 'edit'){
}else if(event.target instanceof JTopo.Link && this.stageMode === 'normal'){
}else if(event.target instanceof JTopo.Link && this.stageMode === 'edit'){
}
});
//双击编辑事件
this.scene.dbclick(event=>{
if(!event.target){
//双击空白处
return;
}
this.currentNode = event.target;
if(event.target instanceof JTopo.Node && this.stageMode === 'normal'){
}else if(event.target instanceof JTopo.Node && this.stageMode === 'edit'){
}else if(event.target instanceof JTopo.Link && this.stageMode === 'normal'){
}else if(event.target instanceof JTopo.Link && this.stageMode === 'edit'){
console.log(event.target);
}
})
//清楚起始节点不完整的拖放线
this.scene.mousedown(event=>{
if(!event.target){
return;
}
if(this.link && !this.isSelectedMode && (event.target == null || event.target === this.beginNode || event.target === this.link)){
this.scene.remove(this.link);
}
});
//监听鼠标松开事件;
//处理右键菜单、左键连线
//event.button: 0-左键 1-中键 2-右键
this.scene.mouseup(event=>{
if (event.target && event.target instanceof JTopo.Node) {
this.currentNode = event.target
} else if (event.target && event.target instanceof JTopo.Link) {
this.currentLink = event.target
}
if (event.target && event.target instanceof JTopo.Node && event.target.layout && event.target.layout.on && event.target.layout.type && event.target.layout.type !== 'auto') {
JTopo.layout.layoutNode(this, event.target, true, event)
}
if(event.button === 0){
if (event.target != null && event.target instanceof JTopo.Node && !this.isSelectedMode && this.stageMode === 'edit') {
if (this.beginNode == null) {
// 在起始节点处松开鼠标,创建动态的线条(临时节点A-Z)
this.beginNode = event.target;
if (this.lineType === 'line') {
// 直线
self.link = new JTopo.Link(self.tempNodeA, self.tempNodeZ);
self.link.lineType = 'line'
} else if (self.lineType === 'foldLine') {
// 折线
self.link = new JTopo.FoldLink(self.tempNodeA, self.tempNodeZ);
self.link.lineType = 'foldLine';
self.link.direction = self.config.linkDirection
} else if (self.lineType === 'flexLine') {
// 二次折线
self.link = new JTopo.FlexionalLink(self.tempNodeA, self.tempNodeZ);
self.link.direction = self.config.linkDirection;
self.link.lineType = 'flexLine'
} else if (self.lineType === 'curveLine') {
// 曲线
self.link = new JTopo.CurveLink(self.tempNodeA, self.tempNodeZ);
self.link.lineType = 'curveLine'
}
self.link.dashedPattern = 2;
self.link.lineWidth = self.config.linkDefaultWidth;
self.link.shadow = self.config.linkShadow;
self.link.strokeColor = JTopo.util.randomColor();
this.scene.add(self.link);
self.tempNodeA.setLocation(event.x, event.y);
self.tempNodeZ.setLocation(event.x, event.y);
} else if (event.target && event.target instanceof JTopo.Node && self.beginNode !== event.target) {
// 在终点节点处松开鼠标,则建立连线
let endNode = event.target;
let link;
if (self.lineType === 'line') {
link = new JTopo.Link(self.beginNode, endNode);
link.lineType = 'line'
} else if (self.lineType === 'foldLine') {
link = new JTopo.FoldLink(self.beginNode, endNode);
link.direction = self.config.linkDirection;
link.bundleOffset = self.config.linkOffsetGap; // 折线拐角处的长度
link.lineType = 'foldLine'
} else if (self.lineType === 'flexLine') {
link = new JTopo.FlexionalLink(self.beginNode, endNode);
link.direction = self.config.linkDirection;
link.lineType = 'flexLine';
link.offsetGap = self.config.linkOffsetGap;
} else if (self.lineType === 'curveLine') {
link = new JTopo.CurveLink(self.beginNode, endNode);
link.lineType = 'curveLine';
}
// 保存线条所连接的两个节点ID
link.nodeSrc = self.beginNode.nodeId;
link.nodeDst = endNode.nodeId;
if (self.lineType !== 'curveLine') {
link.arrowsRadius = self.config.arrowsRadius;
}
link.strokeColor = self.config.linkStrokeColor;
link.lineWidth = self.config.linkDefaultWidth;
this.scene.add(link);
self.beginNode = null;
this.scene.remove(self.link);
self.link = null;
} else {
self.beginNode = null;
}
} else {
if (this.link) {
console.log(this);
this.scene.remove(this.link);
}
this.beginNode = null;
}
}
});
//动态更新连线坐标
this.scene.mousemove(event=>{
if(!self.isSelectedMode && self.beginNode){
self.tempNodeA.setLocation(event.x,event.y);
}
});
this.scene.mousedrag(event=>{
if (!self.isSelectedMode && self.beginNode) {
self.tempNodeZ.setLocation(event.x, event.y)
}
});
//鼠标移出舞台
this.stage.mouseout(event=>{
// 删掉节点带出来的连线
if (self.link && !self.isSelectedMode && (event.target == null || event.target === self.beginNode || event.target === self.link)) {
self.scene.remove(self.link)
}
})
this.scene.background = backImg;
}
//键盘事件
@HostListener('window:keyup',['$event'])
keyEvent(e:KeyboardEvent){
if (e.shiftKey) { // 组合键模式
switch (e.which) {
// 放大 ctrl+=
case 187:
case 61:
// 单个节点可以撤销操作
if (this.currentNode instanceof JTopo.Node) {
// 保存初始状态
this.utils.saveNodeInitState();
this.utils.scalingBig();
this.utils.saveNodeNewState();
} else {
this.utils.scalingBig();
}
break;
// 缩小 ctrl+-
case 189:
case 173:
if (this.currentNode instanceof JTopo.Node) {
// 保存初始状态
this.utils.saveNodeInitState();
this.utils.scalingSmall();
this.utils.saveNodeNewState();
} else {
this.utils.scalingSmall();
}
break
case 70:
// ctrl+f 全屏显示
this.utils.showInFullScreen(this.stage.canvas, 'RequestFullScreen')
break;
case 72:
// h 帮助
// alert('帮助文档')
break;
case 71:
// ctrl+g 居中显示
this.utils.showInCenter();
break;
case 73:
// shif+I 逆时针旋转
if (this.currentNode instanceof JTopo.Node) {
this.utils.saveNodeInitState();
this.utils.rotateSub();
this.utils.saveNodeNewState();
}
break;
case 67:
this.utils.cloneSelectedNodes();
break;
case 80:
// ctrl + p
this.utils.showPic();
break;
case 82:
// 单个节点重做
if (this.currentNode instanceof JTopo.Node) {
this.utils.reMakeNodeAction();
}
break;
case 83:
// ctrl+s 保存
this.saveTopology(true);
break;
case 85:
// shif+U 顺时针旋转
if (this.currentNode instanceof JTopo.Node) {
this.utils.saveNodeInitState();
this.utils.rotateAdd();
this.utils.saveNodeNewState()
}
break;
case 87:
// alert('ctrl + w 另存为')
break
case 89:
// ctrl+y
this.utils.clearTopology();
break
case 90:
// 单个节点撤销
if (this.currentNode instanceof JTopo.Node) {
this.utils.cancleNodeAction()
}
break
}
} else if (e.which === 46) { // 单独按下delete
this.utils.deleteSelectedNodes()
} else if (e.which === 17) { // 单独按下ctrl
this.isSelectedMode = true
}
}
/**
* 图元拖放功能实现
* @param modeDiv 备选列表中的元素(各种样式的节点)
* @param drawArea 舞台区域
*/
drag(modeDiv, drawArea, text){
if(!text){
text = '';
}
let self = this;
// 拖拽开始,携带必要的参数
modeDiv.ondragstart = (event)=> {
event = event || window.event;
let dragSrc = this;
// let backImg = $(dragSrc).find('img').eq(0).attr('src')
// backImg = backImg.substring(backImg.lastIndexOf('/') + 1)
// let nodeType = $(this).attr('topo-nodetype')
try {
// IE只允许KEY为text和URL
// event.dataTransfer.setData('text', backImg + ';' + text + ';' + nodeType)
} catch (ex) {
console.log(ex)
}
};
// 阻止默认事件
drawArea.ondragover = (event)=> {
event.preventDefault();
return false
};
// 创建节点
drawArea.ondrop = (event)=> {
// 只读模式
if (this.stageMode === "normal") {
return;
}
event = event || window.event;
let data = event.dataTransfer.getData('text');
let img, text, nodeType;
if (data) {
let datas = data.split(';');
if (datas && datas.length === 3) {
img = datas[0];
text = datas[1];
nodeType = datas[2];
let node = new JTopo.Node();
node.fontColor = self.config.nodeFontColor;
// 节点坐标
node.setBound((event.layerX ? event.layerX : event.offsetX) - self.scene.translateX - self.config.nodeDefaultWidth / 2,
(event.layerY ? event.layerY : event.offsetY) - self.scene.translateY - self.config.nodeDefaultHeight / 2, self.config.nodeDefaultWidth, self.config.nodeDefaultHeight);
// 节点图片
node.setImage( img);
// 节点数据
node.nodeId = generateUUID;
node.nodeType = nodeType;
node.text = text;
node.nodeImage = img;
self.scene.add(node);
self.currentNode = node;
}
}
}
}
/**
* 保存序列化的拓扑图成json
*/
saveTopology(url){
let self = this;
// 保存container状态
let containers = this.getContainers();
for (let c = 0; c < containers.length; c++) {
let temp = [];
let nodes = containers[c].childs;
for (let n = 0; n < nodes.length; n++) {
if (nodes[n] instanceof JTopo.Node) {
temp.push(nodes[n].nodeId)
}
}
containers[c].childNodes = temp.join(',')
}
// 获取json
let topologyJSON = this.stage.toJson();
}
/**
* 重置拓扑图
*/
resetTopology(){
}