OrgChart.as
32 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
/**
* Neal Mi
* email: imzw.net@gmail.com
*
* Any problem, please contact me via email. Please use following format as Subject.
* format: OrgChart - your problem - your name
* eg: OrgChart - node in a bad position - neal
*
* 有任何问题,请通过电子邮件联系我。使用以下的格式做为邮件的主题。
* 格式: OrgChart - 问题的一句话描述 - 你的名字
* 示例: OrgChart - 节点错位 - Peter Wang
**/
package control {
/* import flash.display.DisplayObject;
import flash.events.MouseEvent;
*/
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.net.URLVariables;
import flexlib.containers.DragScrollingCanvas;
import mx.controls.Alert;
import mx.collections.ArrayCollection;
import mx.collections.ICollectionView;
import mx.collections.IViewCursor;
import mx.controls.treeClasses.DefaultDataDescriptor;
import mx.controls.treeClasses.ITreeDataDescriptor;
import mx.core.Application;
import mx.core.UIComponent;
import mx.effects.Zoom;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
import mx.events.CloseEvent;
import util.XmlUtil;
import common.StaticObj;
[Event(name="nodeClick", type="control.OrgChartEvent")]
[Event(name="nodeSelected", type="control.OrgChartEvent")]
[Event(name="nodeMouseOver", type="control.OrgChartEvent")]
[Event(name="nodeMouseOut", type="control.OrgChartEvent")]
public class OrgChart extends UIComponent {
private var _data:ICollectionView;
private var dataProviderChanged:Boolean=false;
private var _treeDataDesciptor:ITreeDataDescriptor=new DefaultDataDescriptor;
private var _children:ArrayCollection=new ArrayCollection();
private var _selectable:Boolean=true;
private var _selected:IOrgChartNode=null;
private var _depth:int=0;
private var _currentDepth:int=0;
private var _maxX:Number=0;
private var _maxY:Number=0;
/*
They're should be better.
*/
private var _hItemWidth:Number=180; //横向的节点
private var _hItemHeight:Number=50;
private var _vItemWidth:Number=25; //纵向的节点
private var _vItemHeight:Number=80;
private var _verticalSpacing:Number=40; //间隔
private var _horizonalSpacing:Number=40; //
private var _container:DragScrollingCanvas;
private var _nodes:ArrayCollection=new ArrayCollection();
private var _polyLines:ArrayCollection = new ArrayCollection();
private var _maxHeight:Number=0;
private var _maxWidth:Number=0;
private var _canEdit:Boolean=false;
private var _canView:Boolean =false;
public function OrgChart() {
super();
}
///////////////////////////////////////////////////////////
// Private funtions
///////////////////////////////////////////////////////////
/**
* 计算树的深度,即为XML文件有多少层级
**/
private function calculateDepth(data:ICollectionView):void {
for (var cursor:IViewCursor=data.createCursor(); !cursor.afterLast; cursor.moveNext()) {
if (_treeDataDesciptor.isBranch(cursor.current, data) && _treeDataDesciptor.getChildren(cursor.current, data).length != 0) {
_currentDepth++;
if (_currentDepth > _depth) {
_depth=_currentDepth;
}
var __tmp:ICollectionView=_treeDataDesciptor.getChildren(cursor.current, data);
calculateDepth(__tmp);
_currentDepth--;
}
}
//Alert.show("zzl--xml总共有多少层"+_currentDepth);
}
/**
* 创建子节点
* */
private function _createSubNodes(data:ICollectionView, parentNode:IOrgChartNode):void {
for (var cursor:IViewCursor=data.createCursor(); !cursor.afterLast; cursor.moveNext()) {
//Alert.show("zzl-创建子节点"+parentNode);
var node:IOrgChartNode=_createNode(cursor.current, parentNode);
if (_treeDataDesciptor.isBranch(cursor.current, data) && _treeDataDesciptor.getChildren(cursor.current, data).length != 0) {
//I know this is not good, but...
if (cursor.current.@type != "more") {
var __tmp:ICollectionView=_treeDataDesciptor.getChildren(cursor.current, data);
_createSubNodes(__tmp, node);
}
}
}
}
private function updatePosition(node:IOrgChartNode):void{
var my:Number = getMaxY(node);
}
private function getMaxY(node:IOrgChartNode):Number{
var ny:Number = node.y;
var subs:ArrayCollection = node.subNodes;
for(var i:int=0; i<subs.length; i++){
var sn:IOrgChartNode = subs.getItemAt(i) as IOrgChartNode;
ny = Math.max(ny, getMaxY(sn));
}
return ny;
}
/**
* 创建节点
* */
private function _createNode2(data:Object, parentNode:IOrgChartNode):IOrgChartNode {
//Alert.show("zzl-创建节点");
var node:IOrgChartNode=new DefaultOrgChartNode(_canView);
//node.addEventListener(MouseEvent.CLICK, nodeClick);
/*node.addEventListener(MouseEvent.MOUSE_OVER, nodeMouseOver);
node.addEventListener(MouseEvent.ROLL_OVER, nodeRollOver);
node.addEventListener(MouseEvent.ROLL_OUT, nodeRollOut);
node.addEventListener(MouseEvent.MOUSE_OUT, nodeMouseOut);*/
node.parentNode=parentNode;
node.data=data;
node.width=hItemWidth;
node.height=hItemHeight;
node.oriented="v";
//起始时,根节点在最左边
if (parentNode == null) {
node.x=horizontalSpacing;
node.y=verticalSpacing;
} else {
normalLayout2(node);
//移动父节点
updateParentNodePosition2(node.parentNode);
}
_nodes.addItem(node);
return node;
}
private function normalLayout2(node:IOrgChartNode):IOrgChartNode {
var parentNode:IOrgChartNode=node.parentNode;
if (node.previousSibling == null) {
//与父节点在同一中轴线上
node.y=parentNode.y + (parentNode.height - node.height) / 2;
//我们只保存最大值
_maxY=Math.max(node.y + node.height, parentNode.y + parentNode.height);
} else {
node.y=_maxY + verticalSpacing;
_maxY=Math.max(node.y + node.height, _maxY);
}
node.x=parentNode.x + parentNode.width + horizontalSpacing;
return node;
}
/**
* 递归移动所有父节点的位置。
* Recursive update the positon of parent node.
* */
private function updateParentNodePosition2(node:IOrgChartNode):void {
if (node != null) {
var subs:ArrayCollection=node.subNodes;
var lastChild:IOrgChartNode=node.firstChild;
var firstChild:IOrgChartNode=node.lastChild;
node.y=firstChild.y + (lastChild.y - firstChild.y + lastChild.height - node.height) / 2;
//递归更新直到根节点
updateParentNodePosition2(node.parentNode);
}
}
/**
* 创建节点之间的线
* */
private function createLines2(node:IOrgChartNode):void {
//Alert.show("zzl创建节点之间的线");
createLineForNormalLayout2(node);
}
/**
* 创建节点之间的线(正常布局)
* */
private function createLineForNormalLayout2(node:IOrgChartNode):void {
//画当前节点下面的短竖线
//Alert.show("test");
if (node.hasChildren) {
//Alert.show("1");
var headVLine:LineDescriptor=new LineDescriptor();
headVLine.startY=node.y + node.height / 2;
headVLine.startX=node.x + node.width;
headVLine.endY=headVLine.startY;
if (node.subNodes.length == 1) {
headVLine.endX=node.x + node.width + horizontalSpacing;
} else {
headVLine.endX=node.x + node.width + horizontalSpacing / 2;
}
headVLine.hasArrowHead=true;
node.addLine(headVLine);
addToContainer(headVLine);
}
var subs:ArrayCollection=node.subNodes;
//画子节点上面的横线,跨度从第一个子节点的中心到最后一个子节点的中心
if (subs.length > 1) {
var firstChild:IOrgChartNode=subs.getItemAt(0) as IOrgChartNode;
var lastChild:IOrgChartNode=subs.getItemAt(subs.length - 1) as IOrgChartNode;
//Alert.show("2");
var hLine:LineDescriptor=new LineDescriptor();
hLine.startY=firstChild.y + firstChild.height / 2;
hLine.startX=node.x + node.width + horizontalSpacing / 2;
hLine.endY=hLine.startY + lastChild.y + lastChild.height / 2 - (firstChild.y + firstChild.height / 2);
hLine.endX=hLine.startX;
addToContainer(hLine);
node.addLine(hLine);
//画每个子节点头上的短竖线
for (var j:int=0; j < subs.length; j++) {
var child:IOrgChartNode=subs.getItemAt(j) as IOrgChartNode;
//Alert.show("3");
var vline:LineDescriptor=new LineDescriptor();
vline.startY=child.y + child.height / 2;
vline.startX=child.x - horizontalSpacing / 2;
vline.endY=vline.startY;
vline.endX=vline.startX + horizontalSpacing / 2;
node.addLine(vline);
addToContainer(vline);
}
}
}
/**
* 创建节点
* */
private function _createNode(data:Object, parentNode:IOrgChartNode):IOrgChartNode {
var node:IOrgChartNode=new DefaultOrgChartNode(_canView);
//node.addEventListener(MouseEvent.CLICK, nodeClick);
node.parentNode=parentNode;
node.data=data;
node.width=hItemWidth;
node.height=hItemHeight;
node.oriented="h";
//起始时,根节点在最左边
if (parentNode == null) {
//parentNode == null说明是跟节点
node.x=horizontalSpacing;
node.y=verticalSpacing;
} else {
if (node.parentNode.data.@layout == "leftHanging") {
leftHangingLayout(node);
} else if (node.parentNode.data.@layout == "rightHanging") {
rightHangingLayout(node);
} else if (node.parentNode.data.@layout == "bothHanging") {
bothHangingLayout(node);
} else {
normalLayout(node);
}
//移动父节点
updateParentNodePosition(node.parentNode);
}
_nodes.addItem(node);
return node;
}
/**
* 左侧悬挂布局(leftHanging)
* */
private function leftHangingLayout(node:IOrgChartNode):IOrgChartNode {
var parentNode:IOrgChartNode=node.parentNode;
if (node.previousSibling == null) {
node.x=parentNode.x;
node.y=parentNode.y + parentNode.height + verticalSpacing / 2;
} else {
node.x=node.previousSibling.x;
_maxX=Math.max(node.x + node.width, _maxX);
node.y=node.previousSibling.y + node.previousSibling.height + verticalSpacing / 2;
}
return node;
}
/**
* 右侧悬挂布局(rightHanging)
* */
private function rightHangingLayout(node:IOrgChartNode):IOrgChartNode {
var parentNode:IOrgChartNode=node.parentNode;
if (node.previousSibling == null) {
node.x=parentNode.x + parentNode.width / 2 + horizontalSpacing / 2;
node.y=parentNode.y + parentNode.height + verticalSpacing / 2;
} else {
node.x=node.previousSibling.x;
_maxX=Math.max(node.x + node.width, _maxX);
node.y=node.previousSibling.y + node.previousSibling.height + verticalSpacing / 2;
}
return node;
}
/**
* 两侧侧悬挂布局(bothHanging)
* */
private function bothHangingLayout(node:IOrgChartNode):IOrgChartNode {
var parentNode:IOrgChartNode=node.parentNode;
if (node.previousSibling == null) {
node.x=parentNode.x;
node.y=parentNode.y + parentNode.height + verticalSpacing / 2;
} else {
if (parentNode.subNodes.length % 2 == 0) {
node.x=node.previousSibling.x + node.previousSibling.width + horizontalSpacing;
node.y=node.previousSibling.y;
_maxX=Math.max(node.x + node.width, _maxX);
} else {
node.x=node.previousSibling.previousSibling.x;
node.y=node.previousSibling.previousSibling.y + node.previousSibling.previousSibling.height + verticalSpacing / 2;
}
}
return node;
}
private function normalLayout(node:IOrgChartNode):IOrgChartNode {
var parentNode:IOrgChartNode=node.parentNode;
if (node.previousSibling == null) {
//与父节点在同一中轴线上
node.x=parentNode.x + (parentNode.width - node.width) / 2;
//我们只保存最大值
_maxX=Math.max(node.x + node.width, parentNode.x + parentNode.width);
} else {
node.x=_maxX + horizontalSpacing;
_maxX=Math.max(node.x + node.width, _maxX);
}
node.y=parentNode.y + parentNode.height + verticalSpacing;
return node;
}
/**
* 递归移动所有父节点的位置。
* Recursive update the positon of parent node.
* */
private function updateParentNodePosition(node:IOrgChartNode):void {
if (node != null) {
if (node.data.@layout == "leftHanging" || node.data.@layout == "bothHanging") {
if (node.previousSibling != null) {
node.x=node.previousSibling.x + node.previousSibling.width / 2 + horizontalSpacing / 2;
}
node.x=node.firstChild.x + node.firstChild.width / 2 + horizontalSpacing / 2;
_maxX=Math.max(node.x + node.width, _maxX);
} else if (node.data.@layout == "rightHanging") {
updateParentNodePosition(node.parentNode);
} else {
var subs:ArrayCollection=node.subNodes;
var lastChild:IOrgChartNode=node.firstChild;
var firstChild:IOrgChartNode=node.lastChild;
node.x=firstChild.x + (lastChild.x - firstChild.x + lastChild.width - node.width) / 2;
}
//递归更新直到根节点
updateParentNodePosition(node.parentNode);
}
}
public function addToContainer(child:DisplayObject):void {
_children.addItem(child);
addChild(child);
}
/**
* 创建节点之间的线
* */
private function createLines(node:IOrgChartNode):void {
createLineForNormalLayout(node);
return;
if (node.data.@layout == "leftHanging") {
createLineFormLeftHangingLayout(node);
} else if (node.data.@layout == "rightHanging") {
createLineForRightHangingLayout(node);
} else if (node.data.@layout == "bothHanging") {
createLineForBothHangingLayout(node);
} else {
createLineForNormalLayout(node);
}
}
/**
* 创建节点之间的线(Both Hanging)
* */
private function createLineForBothHangingLayout(node:IOrgChartNode):void {
if (node.hasChildren) {
//Alert.show("4");
var parentLine:LineDescriptor=new LineDescriptor();
parentLine.startX=node.x + node.width / 2;
parentLine.startY=node.y + node.height;
parentLine.endX=node.x + node.width / 2;
parentLine.endY=node.lastChild.y + node.lastChild.height / 2;
node.addLine(parentLine);
addToContainer(parentLine);
}
var subNodes:ArrayCollection=node.subNodes;
for (var i:int=0; i < subNodes.length; i++) {
var childNode:IOrgChartNode=subNodes.getItemAt(i) as IOrgChartNode;
//Alert.show("5");
var bothHangingChildLine:LineDescriptor=new LineDescriptor();
if (i % 2 == 0) {
bothHangingChildLine.startX=node.x + node.width / 2;
bothHangingChildLine.startY=childNode.y + childNode.height / 2;
bothHangingChildLine.endX=childNode.x + childNode.width;
bothHangingChildLine.endY=bothHangingChildLine.startY;
} else {
bothHangingChildLine.startX=node.x + node.width / 2;
bothHangingChildLine.startY=childNode.y + childNode.height / 2;
bothHangingChildLine.endX=childNode.x;
bothHangingChildLine.endY=bothHangingChildLine.startY;
}
node.addLine(bothHangingChildLine);
addToContainer(bothHangingChildLine);
}
}
/**
* 创建节点之间的线(Right Hanging)
* */
private function createLineForRightHangingLayout(node:IOrgChartNode):void {
//Alert.show("6");
if (node.hasChildren) {
var parentLine:LineDescriptor=new LineDescriptor();
parentLine.startX=node.x + node.width / 2;
parentLine.startY=node.y + node.height;
parentLine.endX=node.x + node.width / 2;
parentLine.endY=node.lastChild.y + node.lastChild.height / 2;
node.addLine(parentLine);
addToContainer(parentLine);
}
var subNodes:ArrayCollection=node.subNodes;
for (var i:int=0; i < subNodes.length; i++) {
var childNode:IOrgChartNode=subNodes.getItemAt(i) as IOrgChartNode;
var rightHangingChildLine:LineDescriptor=new LineDescriptor();
rightHangingChildLine.startX=node.x + node.width / 2;
rightHangingChildLine.startY=childNode.y + childNode.height / 2;
rightHangingChildLine.endX=childNode.x;
rightHangingChildLine.endY=rightHangingChildLine.startY;
node.addLine(rightHangingChildLine);
addToContainer(rightHangingChildLine);
}
}
/**
* 创建节点之间的线(Left Hanging)
* */
private function createLineFormLeftHangingLayout(node:IOrgChartNode):void {
//Alert.show("7");
if (node.hasChildren) {
var leftHangingParentLine:LineDescriptor=new LineDescriptor();
leftHangingParentLine.startX=node.x + node.width / 2;
leftHangingParentLine.startY=node.y + node.height;
leftHangingParentLine.endX=node.x + node.width / 2;
leftHangingParentLine.endY=node.lastChild.y + node.lastChild.height / 2;
node.addLine(leftHangingParentLine);
addToContainer(leftHangingParentLine);
}
var subNodes:ArrayCollection=node.subNodes;
for (var i:int=0; i < subNodes.length; i++) {
var childNode:IOrgChartNode=subNodes.getItemAt(i) as IOrgChartNode;
var leftHangingVline:LineDescriptor=new LineDescriptor();
leftHangingVline.startX=childNode.x + childNode.width;
leftHangingVline.startY=childNode.y + childNode.height / 2;
leftHangingVline.endX=node.x + node.width / 2;
leftHangingVline.endY=leftHangingVline.startY;
node.addLine(leftHangingVline);
addToContainer(leftHangingVline);
}
}
/**
* 创建节点之间的线(正常布局)
* */
private function createLineForNormalLayout(node:IOrgChartNode):void {
//画当前节点下面的短竖线
// Alert.show(node.hasShow+"%"+node.name);
if(!node.hasShow){
node.hasShow = true;
}else{
return;
}
if (node.hasChildren) {
var pointList :Array = new Array();
var startPoint:Point = new Point(node.x + node.width / 2,node.y + node.height);
var endPoint:Point;
//headVLine.endX=headVLine.startX;
var hasArrowHead:Boolean=false;
if (node.subNodes.length == 1) {
endPoint = new Point(node.x + node.width / 2,node.y + node.height + verticalSpacing);
//headVLine.endY=node.y + node.height + verticalSpacing;
hasArrowHead = true;
} else {
endPoint = new Point(node.x + node.width / 2,node.y + node.height + verticalSpacing/2);
hasArrowHead = false;
//headVLine.endY=node.y + node.height + verticalSpacing / 2;
}
var child:IOrgChartNode=node.subNodes.getItemAt(0) as IOrgChartNode;
pointList.push(startPoint);
pointList.push(endPoint);
var isShow = node.getChildIsShow(child.name);
if(isShow=='1'||true){
var label = node.getChildLabel(child.name);
var headPoints:ArrayCollection = new ArrayCollection(pointList)
var headVLine:PolylineDescriptor=new PolylineDescriptor(this,headPoints,hasArrowHead,false,hasArrowHead,label);
//Alert.show("test")
headVLine.name="";
node.addLine(headVLine);
addToContainer(headVLine);
}
//_polyLines.addItem(headVLine);
}
//Alert.show(node.name)
//Alert.show(node.otherChildren)
if(node.otherChildren!=""){
//Alert.show(node.otherChildren);
var others :Array = node.otherChildren.split(",")
for(var k:int=0;k<others.length;k++){
//var vline:PolylineDescriptor
var pointList :Array = new Array();
var startPoint:Point = new Point(node.x + node.width / 2,node.y + node.height);
var endPoint:Point;
var otherNode :IOrgChartNode = Application.application.oc.getChildByName(others[k]) as IOrgChartNode
endPoint = new Point(otherNode.x+otherNode.width,otherNode.y+otherNode.height/2);
var otherPoints:Array = node.getChildLine(others[k]);
//Alert.show(""+startPoint.x+"%%%"+startPoint.y);
pointList.push(startPoint);
for(var x:int=0;x<otherPoints.length;x++){
pointList.push(otherPoints[x]);
var p:Point = otherPoints[x] as Point;
if(p.y>_maxHeight){
_maxHeight = p.y;
}
if(p.x>_maxWidth){
_maxWidth = p.x;
}
}
//pointList.concat(otherPoints);
pointList.push(endPoint);
//Alert.show(pointList.length+"");
var headPoints:ArrayCollection = new ArrayCollection(pointList)
isShow = node.getChildIsShow(otherNode.name);
if(isShow=='1'){
var label = node.getChildLabel(otherNode.name);
var pline:PolylineDescriptor=new PolylineDescriptor(this,headPoints,true,_canEdit,true,label);
pline.name=node.name+"_"+otherNode.name;
node.addLine(pline);
addToContainer(pline);
if (!_polyLines.contains(pline)){
_polyLines.addItem(pline);
}
}
}
}
node.otherChildren="";
var subs:ArrayCollection=node.subNodes;
//画子节点上面的横线,跨度从第一个子节点的中心到最后一个子节点的中心
if (subs.length > 1) {
//Alert.show("8");
var firstChild:IOrgChartNode;
var lastChild:IOrgChartNode;
for(var i:int=0;i<subs.length;i++){
var nodeTmp:IOrgChartNode = subs.getItemAt(i) as IOrgChartNode;
if(node.getChildIsShow(nodeTmp.name)=='1'){
firstChild = nodeTmp;
break;
}
}
for(var i:int=subs.length-1;i>-1;i--){
var nodeTmp:IOrgChartNode = subs.getItemAt(i) as IOrgChartNode;
if(node.getChildIsShow(nodeTmp.name)=='1'){
lastChild = nodeTmp;
break;
}
}
var hLine:LineDescriptor=new LineDescriptor();
hLine.hasArrowHead=false;
hLine.startX=firstChild.x + firstChild.width / 2;
hLine.startY=node.y + node.height + verticalSpacing / 2;
if(firstChild.name==lastChild.name){
hLine.endX=hLine.startX + node.x + node.width / 2 - (firstChild.x + firstChild.width / 2);
hLine.endY=hLine.startY;
}else{
hLine.endX=hLine.startX + lastChild.x + lastChild.width / 2 - (firstChild.x + firstChild.width / 2);
hLine.endY=hLine.startY;
}
addToContainer(hLine);
node.addLine(hLine);
//画每个子节点头上的短竖线
for (var j:int=0; j < subs.length; j++) {
var child:IOrgChartNode=subs.getItemAt(j) as IOrgChartNode;
var pointList :Array = new Array();
var startPoint:Point = new Point(child.x + child.width / 2,child.y - verticalSpacing / 2);
var endPoint:Point = new Point(child.x + child.width / 2,child.y);
pointList.push(startPoint);
pointList.push(endPoint);
var headPoints:ArrayCollection = new ArrayCollection(pointList)
var isShow:String = node.getChildIsShow(child.name);
if(isShow=='1'){
var label:String = node.getChildLabel(child.name);
var vline:PolylineDescriptor=new PolylineDescriptor(this,headPoints,true,false,true,label);
node.addLine(vline);
addToContainer(vline);
}
}
}
}
/**
* 删除所有节点和线
* */
private function _removeAllChildren():void {
for (var cursor:IViewCursor=_children.createCursor(); !cursor.afterLast; cursor.moveNext()) {
removeChild(cursor.current as DisplayObject);
}
_children=new ArrayCollection();
//removeAllChildren();
}
////////////////////////////////////////////////////
// Override funtions
///////////////////////////////////////////////////
override protected function createChildren():void {
super.createChildren();
}
override protected function measure():void {
super.measure();
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
super.updateDisplayList(unscaledWidth, unscaledHeight);
//这里添加所有节点到OrgChart上,并画线
// Render the all node, create lines.
//Alert.show("test"+dataProviderChanged)
if (dataProviderChanged) {
for (var cursor:IViewCursor=_nodes.createCursor(); !cursor.afterLast; cursor.moveNext()) {
var node:IOrgChartNode=cursor.current as IOrgChartNode;
addToContainer(node as UIComponent);
//根据内容自动设置OrgChart的宽和高
/*height=_maxY + verticalSpacing;
width=(depth + 1) * (vItemWidth + horizontalSpacing);
*/
width=_maxX + horizontalSpacing;
if(width<_container.width){
width = _container.width;
}
height=(depth + 1) * (hItemHeight + verticalSpacing)+20;
if(_maxHeight>height){
height = _maxHeight+10;
}
if(_maxWidth>width){
width = _maxWidth+10;
}
//trace("[OrgChart updateDisplayList] width,height = " + width + "," + height);
}
//dataProviderChanged=false;
}
if (dataProviderChanged) {
for (var cursor:IViewCursor=_nodes.createCursor(); !cursor.afterLast; cursor.moveNext()) {
var node:IOrgChartNode=cursor.current as IOrgChartNode;
createLines(node);
//根据内容自动设置OrgChart的宽和高
/*height=_maxY + verticalSpacing;
width=(depth + 1) * (vItemWidth + horizontalSpacing);
*/
width=_maxX + horizontalSpacing;
if(width<_container.width){
width = _container.width;
}
height=(depth + 1) * (hItemHeight + verticalSpacing)+20;
if(_maxHeight>height){
height = _maxHeight+10;
}
if(_maxWidth>width){
width = _maxWidth+10;
}
//trace("[OrgChart updateDisplayList] width,height = " + width + "," + height);
}
dataProviderChanged=false;
}
dataProviderChanged=false;
}
override protected function commitProperties():void {
super.commitProperties();
if (dataProviderChanged) {
//remove all children ,
_removeAllChildren();
_nodes=new ArrayCollection();
//reset the depth, should recalculate
//重新计算树深度值
_depth=0;
calculateDepth(_data);
_maxY=0;
_createSubNodes(_data, null);
invalidateDisplayList();
}
}
////////////////////////////////////////////////////////////
// Getters and Setters
////////////////////////////////////////////////////////////
public function get depth():int {
return _depth;
}
public function set dataProvider1(data:ICollectionView):void {
_data=data;
//Alert.show("dataProviderChanged");
dataProviderChanged=true;
invalidateProperties();
}
[Bindable]
public function get selected():Object {
return _selected ? _selected.data : null;
}
public function set selected(value:Object):void {
if (!value)
return;
for (var cursor:IViewCursor=_nodes.createCursor(); !cursor.afterLast; cursor.moveNext()) {
var n:IOrgChartNode=cursor.current as IOrgChartNode;
if (n.data == value) {
selectedNode=n;
}
}
}
[Bindable]
public function get selectedNode():IOrgChartNode {
return _selected;
}
public function set selectedNode(value:IOrgChartNode):void {
if (value) {
_selected=value;
if (willTrigger(OrgChartEvent.NODE_SELECTED) && selectable) {
var e:OrgChartEvent=new OrgChartEvent(OrgChartEvent.NODE_SELECTED, selectedNode);
dispatchEvent(e);
}
}
}
[Bindable]
public function get selectable():Object {
return _selectable;
}
public function set selectable(value:Boolean):void {
_selectable=value;
}
public function get hItemWidth():Number {
return _hItemWidth;
}
public function set hItemWidth(w:Number):void {
_hItemWidth=w;
}
public function get hItemHeight():Number {
return _hItemHeight;
}
public function set hItemHeight(h:Number):void {
hItemHeight=h;
}
public function get vItemWidth():Number {
return _vItemWidth;
}
public function set vItemWidth(w:Number):void {
_vItemWidth=w;
}
public function get vItemHeight():Number {
return _vItemHeight;
}
public function set vItemHeight(h:Number):void {
_vItemHeight=h;
}
public function get horizontalSpacing():Number {
return _horizonalSpacing;
}
public function set horizontalSpacing(value:Number):void {
_horizonalSpacing=value;
}
public function get verticalSpacing():Number {
return _verticalSpacing;
}
public function set verticalSpacing(value:Number):void {
_verticalSpacing=value;
}
public function set container(container:DragScrollingCanvas):void{
this._container = container;
}
public function set canEdit(canEdit:Boolean):void{
this._canEdit = canEdit;
}
public function set canView(canView:Boolean):void{
this._canView = canView;
}
/////////////////////////////////////////////////////
// Event handlers.
/////////////////////////////////////////////////////
private var zoom:Zoom=new Zoom();
private function nodeRollOver(e:MouseEvent):void {
//doZoom(e);
}
private function nodeRollOut(e:MouseEvent):void {
//doZoom(e);
}
private function doZoom(e:MouseEvent):void {
var node:DefaultOrgChartNode=e.currentTarget as DefaultOrgChartNode;
if (zoom.isPlaying) {
zoom.reverse();
} else {
zoom.zoomWidthFrom=1;
zoom.zoomWidthTo=1.5;
zoom.zoomHeightFrom=1;
zoom.zoomHeightTo=1.5;
zoom.duration=200;
zoom.play([node], e.type == MouseEvent.ROLL_OUT ? true : false);
}
}
private function nodeMouseOver(e:MouseEvent):void {
if (willTrigger(OrgChartEvent.NODE_MOUSE_OVER)) {
dispatchEvent(new OrgChartEvent(OrgChartEvent.NODE_MOUSE_OVER, e.currentTarget as IOrgChartNode));
}
}
private function nodeMouseOut(e:MouseEvent):void {
if (willTrigger(OrgChartEvent.NODE_MOUSE_OUT)) {
dispatchEvent(new OrgChartEvent(OrgChartEvent.NODE_MOUSE_OUT, e.currentTarget as IOrgChartNode));
}
}
/*private function nodeClick(e:MouseEvent):void {
if (selectable) {
selectedNode=e.currentTarget as IOrgChartNode;
}
if (willTrigger(OrgChartEvent.NODE_CLICK)) {
dispatchEvent(new OrgChartEvent(OrgChartEvent.NODE_CLICK, e.currentTarget as IOrgChartNode));
}
}*/
public function savePolyLine():void{
Alert.yesLabel = ""+resourceManager.getString('resources', 'LM015');
Alert.noLabel = ""+resourceManager.getString('resources', 'LM070');
Alert.show(resourceManager.getString('resources', 'LM071')+"?", resourceManager.getString('resources', 'LM038')+"", 3, this, alertClickHandler);
}
/**
* 保存折线顶点信息
*/
public function savePolyLineInfo():void{
var staticObj :StaticObj=Application.application.staticObj;
var xmlArray:ArrayCollection = new ArrayCollection(new Array());
for(var i:int=0;i<_polyLines.length;i++){
var line:PolylineDescriptor = _polyLines.getItemAt(i) as PolylineDescriptor;
if(line.name==""){
continue;
}
var lineinfo:Object = {name:line.name,points:line.pointToString()};
xmlArray.addItem(lineinfo);
}
//Alert.show(xmlArray.length+"");
var xml:XML = XmlUtil.objectToXML(xmlArray,"root");
//Alert.show(xml.toString());
var http:HTTPService = new HTTPService();
http.addEventListener(ResultEvent.RESULT,success);
http.addEventListener(FaultEvent.FAULT,fault);
http.url="/companygroup/manage/companyoperation.jsp?method=saveLineInfo"
http.method = "post";
//http.resultFormat = "e4x";
var val:URLVariables= new URLVariables();
val.xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+xml.toString();
val.random = Math.random();
val.groupid = staticObj.groupid;
http.send(val);
}
public function clear():void{
var xmlArray:ArrayCollection = new ArrayCollection(new Array());
//Alert.show(_polyLines.length+"");
for(var i:int=0;i<_polyLines.length;i++){
var line:PolylineDescriptor = _polyLines.getItemAt(i) as PolylineDescriptor;
line.clear();
}
}
public function success(xmlContent:ResultEvent):void{
//Alert.show("保存成功!");
//Alert.yesLabel = "确定";
//Alert.noLabel = "取消";
//Alert.show("是否创建版本?", "创建版本", 3, this, alertClickHandler);
}
public function fault(myFaultEvent:FaultEvent):void{ //异常处理函数
Alert.okLabel=""+resourceManager.getString('resources', 'LM015');
Alert.show(resourceManager.getString('resources', 'LM076')+"!");
trace(myFaultEvent.message);
}
private function alertClickHandler(event:CloseEvent):void {
if (event.detail==Alert.YES){
savePolyLineInfo();
Application.application.showGroupVersionPopUp();
}
}
}
}