username@email.com
2025-04-27 15eb82df2d6ec539e9d4245bfe08d531e8eb6379
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
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
var wf_r = null; //画板对象
var wf_steps = []; //步骤数组
var wf_texts = []; //文本数组
var wf_conns = []; //连线数组
var wf_option = ""; //当前操作
var wf_focusObj = null; //当前焦点对象
var wf_width = 108; //步骤宽度
var wf_height = 50; //步骤高度
var wf_rect = 9; //圆角大小
var wf_designer = true; //是否是设计模式(查看流程图时不帮定双击事件)
var wf_connColor = "#898a89"; //连线的常规颜色
var wf_nodeBorderColor = "#23508e"; //节点边框颜色
var wf_noteColor = "#efeff0"; //节点填充颜色
var wf_stepDefaultName = "新步骤";//默认步骤名称
 
var tempArrPath = []; //临时连线
var mouseX = 0;
var mouseY = 0;
 
var wf_json = {}; //设计json
var wf_id = "";//当前流程ID
var links_tables_fields = [];//当前流程的所有连接所有表和字段
 
$(function () {
    wf_r = Raphael("flowdiv", $(window).width(), $(window).height() - 28);
    wf_r.customAttributes.type1 = function () { };
    wf_r.customAttributes.fromid = function () { };
    wf_r.customAttributes.toid = function () { };
});
 
//添加步骤
function addStep(x, y, text, id, addToJson, type1, bordercolor, bgcolor) {
    var guid = getGuid();
    var xy = getNewXY();
    x = x || xy.x;
    y = y || xy.y;
    text = text || wf_stepDefaultName;
    id = id || guid;
    var rect = wf_r.rect(x, y, wf_width, wf_height, wf_rect);
    rect.attr({ "fill": bgcolor || wf_noteColor, "stroke": bordercolor || wf_nodeBorderColor, "stroke-width": 1, "cursor": "default" });
    rect.id = id;
    rect.type1 = type1 ? type1 : "normal";
    rect.drag(move, dragger, up);
    if (wf_designer) {
        rect.click(click);
        if ("normal" === rect.type1) {
            rect.dblclick(stepSetting);
        }
        else if ("chainStep" === rect.type1) {
            rect.dblclick(chainStepSetting);
        }
        else if ("endStep" === rect.type1) {
           //结束节点不能进行设置
        }
    }
    wf_steps.push(rect);
    var text2 = text.length > 8 ? text.substr(0, 7) + "..." : text;
    var text1 = wf_r.text(x + 52, y + 25, text2);
    text1.attr({ "font-size": "12px" });
    if (text.length > 8) text1.attr({ "title": text });
    text1.id = "text_" + id;
    text1.type1 = "text";
    wf_texts.push(text1);
    if (addToJson == undefined || addToJson == null) addToJson = true;
    if (addToJson) {
        var step = {};
        step.id = guid;
        step.type = type1 ? type1 : "normal";
        step.name = text;
        step.position = { x: x, y: y, width: wf_width, height: wf_height };
        if (rect.type1 === "normal") {
            step.opinionDisplay = "";
            step.expiredPrompt = "";
            step.signatureType = "";
            step.workTime = "";
            step.limitTime = "";
            step.otherTime = "";
            step.archives = "";
            step.archivesParams = "";
            step.note = "";
            step.behavior = {
                flowType: "",
                runSelect: "",
                handlerType: "",
                selectRange: "",
                handlerStep: "",
                valuefield: "",
                fieldtype: "",
                defaultHandler: "",
                hanlderModel: "",
                backModel: "",
                backStep: "",
                backType: "",
                percentage: "",
                ignore: ""
            };
            step.forms = [];
            step.buttons = [];
            step.fieldStatus = [];
        }
        else if (rect.type1 === "chainStep") {
            step.opinionDisplay = "";
            step.expiredPrompt = "";
            step.signatureType = "";
            step.workTime = "";
            step.limitTime = "";
            step.otherTime = "";
            step.archives = "";
            step.archivesParams = "";
            step.note = "";
            step.behavior = {
                flowType: "",
                runSelect: "",
                handlerType: "",
                selectRange: "",
                handlerStep: "",
                valuefield: "",
                fieldtype: "",
                defaultHandler: "",
                hanlderModel: "",
                backModel: "",
                backStep: "",
                backType: "",
                percentage: "",
                ignore: ""
            };
            step.gradualapproval = [];
            step.forms = [];
            step.buttons = [];
            step.fieldStatus = [];
        }
        else if (rect.type1 === "endStep") {
            step.opinionDisplay = "";
            step.expiredPrompt = "";
            step.signatureType = "";
            step.workTime = "";
            step.archives = "";
            step.note = "";
           
 
        }
        pushStep(step);
    }
}
 
//添加子流程节点
function addChainNode() {
    addStep(null, null, "逐级审批", null, null, "chainStep", null, null);
}
 
//添加结束节点
function addEndNode() {
    addStep(null, null, "结束节点", null, null, "endStep", null, null);
}
 
//复制当前选中步骤
function copyStep() {
    if (wf_focusObj == null || !isStepObj(wf_focusObj)) {
        alert("请选择要复制的步骤");
        return;
    }
    var json = null;
    var text = "";
    var id = getGuid();
    if (wf_json && wf_json.steps) {
        for (var i = 0; i < wf_json.steps.length; i++) {
            if (wf_json.steps[i].id === wf_focusObj.id) {
                json = wf_json.steps[i];
                json.id = id;
                text = json.name;
                json.name = text;
                break;
            }
        }
    }
    addStep(undefined, undefined, text, id, false);
}
 
//设置步骤文本
function setStepText(id, txt) {
    var stepText = wf_r.getById("text_" + id);
    if (stepText != null) {
        if (txt.length > 8) {
            stepText.attr({ "title": txt });
            txt = txt.substr(0, 7) + "...";
        }
        stepText.attr({ "text": txt });
    }
}
 
//删除当前焦点及其附属对象
function removeObj() {
    if (!wf_focusObj) {
        layer.alert("请选择要删除的对象!");
    } else if (!confirm('您真的要删除选定对象吗?')) {
        return false;
    }
    if (isStepObj(wf_focusObj))//如果选中的是步骤
    {
        if (wf_focusObj.id) {
            for (var i = 0; i < wf_texts.length; i++) {
                if (wf_texts[i].id == "text_" + wf_focusObj.id) {
                    wf_texts.remove(i);
                    var text = wf_r.getById("text_" + wf_focusObj.id);
                    if (text) text.remove();
                }
            }
        }
        var deleteConnIndex = new Array();
        for (var j = 0; j < wf_conns.length; j++) {
            if (wf_conns[j].arrPath && (wf_conns[j].obj1.id == wf_focusObj.id || wf_conns[j].obj2.id == wf_focusObj.id)) {
                deleteLine(wf_conns[j].id);
                deleteConnIndex.push(j);
                wf_conns[j].arrPath.remove();
            }
        }
        for (var m = deleteConnIndex.length; m--;) {
            wf_conns.remove(deleteConnIndex[m]);
        }
        deleteConnIndex = new Array();
 
        for (var k = 0; k < wf_steps.length; k++) {
            if (wf_steps[k].id == wf_focusObj.id) {
                wf_steps.remove(k);
                deleteStep(wf_focusObj.id);
            }
        }
        wf_focusObj.remove();
    }
    else//如果选中的是线
    {
        for (var j = 0; j < wf_conns.length; j++) {
            if (wf_conns[j].arrPath && wf_conns[j].arrPath.id == wf_focusObj.id) {
                deleteLine(wf_conns[j].id);
                wf_conns.remove(j);
            }
        }
        wf_focusObj.remove();
    }
}
 
//得到新步骤的XY
function getNewXY() {
    var x = 10, y = 50;
    if (wf_steps.length > 0) {
        var step = wf_steps[wf_steps.length - 1];
        x = parseInt(step.attr("x")) + 170;
        y = parseInt(step.attr("y"));
        if (x > wf_r.width - wf_width) {
            x = 10;
            y = y + 100;
        }
 
        if (y > wf_r.height - wf_height) {
            y = wf_r.height - wf_height;
        }
    }
    return { x: x, y: y };
}
 
//添加连线
function addConn() {
    if (!wf_focusObj || !isStepObj(wf_focusObj)) {
        layer.alert("请选择要连接的步骤!"); return false;
    }
    wf_option = "line";
    document.body.onmousemove = mouseMove;
    document.body.onmousedown = function () {
        for (var i = 0; i < tempArrPath.length; i++) {
            tempArrPath[i].arrPath.remove();
        }
        tempArrPath = [];
        document.body.onmousemove = null;
    };
}
 
function mouseMove(ev) {
    ev = ev || window.event;
    var mousePos = mouseCoords(ev);
    mouseX = mousePos.x;
    mouseY = mousePos.y;
    var obj = { obj1: wf_focusObj, obj2: null };
    wf_r.drawArr(obj);
}
 
function mouseCoords(ev) {
    if (ev.pageX || ev.pageY) {
        return { x: ev.pageX, y: ev.pageY };
    }
    return {
        x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
        y: ev.clientY + document.body.scrollTop - document.body.clientTop
    };
}
 
//连接对象
function connObj(obj, addToJSON, title) {
    if (addToJSON == undefined || addToJSON == null) addToJSON = true;
    if (isLine(obj)) {
        var newline = wf_r.drawArr(obj);
        wf_conns.push(newline);
        //var path = newline.arrPath;
        //wf_r.text(parseInt(path.attr('x')+40), parseInt(path.attr('y')), "1111111111");
        if (addToJSON) {
            var line = {};
            line.id = obj.id;
            line.from = obj.obj1.id;
            line.to = obj.obj2.id;
            line.customMethod = "";
            line.sql = "";
            line.noaccordMsg = "";
            addLine(line);
        }
    }
}
 
//单击事件执行相关操作
function click() {
    var o = this;
    switch (wf_option) {
        case "line":
            var obj = { id: getGuid(), obj1: wf_focusObj, obj2: o };
            connObj(obj);
            break;
        default:
            changeStyle(o);
            break;
    }
    wf_option = "";
    wf_focusObj = this;
}
 
//连线单击事件
function connClick() {
    for (var i = 0; i < wf_conns.length; i++) {
        if (wf_conns[i].arrPath === this) {
 
            wf_conns[i].arrPath.attr({ "stroke": "#db0f14" });
        }
        else {
            wf_conns[i].arrPath.attr({ "stroke": wf_connColor });
        }
    }
    for (var i = 0; i < wf_steps.length; i++) {
        wf_steps[i].attr("fill", "#efeff0");
        wf_steps[i].attr("stroke", "#23508e");
    }
 
    wf_focusObj = this;
}
 
//判断一个节点与另一个节点之间是否可以连线
function isLine(obj) {
    if (!obj || !obj.obj1 || !obj.obj2) {
        return false;
    }
    if (obj.obj1 === obj.obj2) {
        return false;
    }
    if (!isStepObj(obj.obj1) || !isStepObj(obj.obj2)) {
        return false;
    }
    for (var i = 0; i < wf_conns.length; i++) {
        if (obj.obj1 === obj.obj2 || (wf_conns[i].obj1 === obj.obj1 && wf_conns[i].obj2 === obj.obj2)) {
            return false;
        }
    }
    return true;
}
 
//得到XML DOM
function getXmlDoc() {
    var xmlDoc = RoadUI.Core.getXmlDoc();
    xmlDoc.async = false;
    return xmlDoc
}
 
//得到GUID
function getGuid() {
    return Raphael.createUUID().toLowerCase();
}
 
//改变节点样式
function changeStyle(obj) {
    if (!obj) {
        return;
    }
    for (var i = 0; i < wf_steps.length; i++) {
        if (wf_steps[i].id == obj.id) {
            wf_steps[i].attr("fill", wf_noteColor);
            wf_steps[i].attr("stroke", "#cc0000");
        }
        else {
            wf_steps[i].attr("fill", wf_noteColor);
            wf_steps[i].attr("stroke", wf_nodeBorderColor);
        }
    }
 
    for (var i = 0; i < wf_conns.length; i++) {
        if (wf_conns[i].arrPath) {
            wf_conns[i].arrPath.attr({ "stroke": wf_connColor });
        }
    }
 
    //obj.animate({ }, 500);
}
 
//拖动节点开始时的事件
function dragger() {
    this.ox = this.attr("x");
    this.oy = this.attr("y");
    changeStyle(this);
};
 
//拖动事件
function move(dx, dy) {
    var x = this.ox + dx;
    var y = this.oy + dy;
 
    if (x < 0) {
        x = 0;
    }
    else if (x > wf_r.width - wf_width) {
        x = wf_r.width - wf_width;
    }
 
    if (y < 0) {
        y = 0;
    }
    else if (y > wf_r.height - wf_height) {
        y = wf_r.height - wf_height;
    }
    this.attr("x", x);
    this.attr("y", y);
    if (this.id) {
        var text = wf_r.getById('text_' + this.id);
        if (text) {
            text.attr("x", x + 52);
            text.attr("y", y + 25);
        }
    }
    for (var j = wf_conns.length; j--;) {
        if (wf_conns[j].obj1.id == this.id || wf_conns[j].obj2.id == this.id) {
            wf_r.drawArr(wf_conns[j]);
        }
    }
    wf_r.safari();
};
 
//拖动结束后的事件
function up() {
    changeStyle(this);
    //记录移动后的位置
    if (isStepObj(this)) {
        var bbox = this.getBBox();
        if (bbox) {
            var steps = wf_json.steps;
            if (steps && steps.length > 0) {
                for (var i = 0; i < steps.length; i++) {
                    if (steps[i].id == this.id) {
                        steps[i].position = { "x": bbox.x, "y": bbox.y, "width": bbox.width, "height": bbox.height };
                        break;
                    }
                }
            }
        }
    }
};
 
//随着节点位置的改变动态改变箭头
Raphael.fn.drawArr = function (obj) {
    if (!obj || !obj.obj1) {
        return;
    }
 
    if (!obj.obj2) {
        var point1 = getStartEnd(obj.obj1, obj.obj2);
        var path2 = getArr(point1.start.x, point1.start.y, mouseX, mouseY, 9);
        for (var i = 0; i < tempArrPath.length; i++) {
            tempArrPath[i].arrPath.remove();
        }
        tempArrPath = [];
        obj.arrPath = this.path(path2);
        obj.arrPath.attr({ "stroke-width": 2, "stroke": wf_connColor });
        tempArrPath.push(obj);
        return;
    }
 
    var point = getStartEnd(obj.obj1, obj.obj2);
    var path1 = getArr(point.start.x, point.start.y, point.end.x, point.end.y, 9);
    try {
        if (obj.arrPath) {
            obj.arrPath.attr({ path: path1 });
        }
        else {
            obj.arrPath = this.path(path1);
            obj.arrPath.attr({ "stroke-width": 2, "stroke": wf_connColor, "x": point.start.x, "y": point.start.y, "x1": point.end.x, "y1": point.end.y });
            if (wf_designer) {
                obj.arrPath.click(connClick);
                obj.arrPath.dblclick(connSetting);
                obj.arrPath.id = obj.id;
                obj.arrPath.fromid = obj.obj1.id;
                obj.arrPath.toid = obj.obj2.id;
            }
        }
    } catch (e) { }
    return obj;
};
 
function setLineText(id, txt) {
    var line;
    for (var i = 0; i < wf_conns.length; i++) {
        if (wf_conns[i].id == id) {
            line = wf_conns[i];
            break;
        }
    }
    if (!line) {
        return;
    }
    var bbox = line.arrPath.getBBox();
    var txt_x = (bbox.x + bbox.x2) / 2;
    var txt_y = (bbox.y + bbox.y2) / 2;
 
    var lineText = wf_r.getById("line_" + id);
    if (lineText != null) {
        if (!txt) {
            lineText.remove();
        }
        else {
            lineText.attr("x", txt_x);
            lineText.attr("y", txt_y);
            lineText.attr("text", txt || "");
        }
        return;
    }
 
    if (txt) {
        var textObj = wf_r.text(txt_x, txt_y, txt);
        textObj.type1 = "line";
        textObj.id = "line_" + id;
        wf_texts.push(textObj);
    }
    //line.arrPath.attr("title", txt);
}
 
//删除当前焦点及其附属对象
function removeObj() {
    if (!wf_focusObj) {
        alert("请选择要删除的对象!");
    }
    else if (!confirm('您真的要删除选定对象吗?')) {
        return false;
    }
    if (isStepObj(wf_focusObj))//如果选中的是步骤
    {
        if (wf_focusObj.id) {
            for (var i = 0; i < wf_texts.length; i++) {
                if (wf_texts[i].id == "text_" + wf_focusObj.id) {
                    wf_texts.remove(i);
                    var text = wf_r.getById("text_" + wf_focusObj.id);
                    if (text) text.remove();
                }
            }
        }
        var deleteConnIndex = new Array();
        for (var j = 0; j < wf_conns.length; j++) {
            if (wf_conns[j].arrPath && (wf_conns[j].obj1.id == wf_focusObj.id || wf_conns[j].obj2.id == wf_focusObj.id)) {
                deleteLine(wf_conns[j].id, wf_conns[j].arrPath.id);
                deleteConnIndex.push(j);
                wf_conns[j].arrPath.remove();
            }
        }
        for (var m = deleteConnIndex.length; m--;) {
            wf_conns.remove(deleteConnIndex[m]);
        }
        deleteConnIndex = new Array();
 
        for (var k = 0; k < wf_steps.length; k++) {
            if (wf_steps[k].id == wf_focusObj.id) {
                wf_steps.remove(k);
                deleteStep(wf_focusObj.id);
            }
        }
        wf_focusObj.remove();
    }
    else//如果选中的是线
    {
        for (var j = 0; j < wf_conns.length; j++) {
            if (wf_conns[j].arrPath && wf_conns[j].arrPath.id == wf_focusObj.id) {
                deleteLine(wf_conns[j].id, wf_conns[j].arrPath.id);
                wf_conns.remove(j);
            }
        }
        wf_focusObj.remove();
    }
}
 
//得到新步骤的XY
function getNewXY() {
    var x = 10, y = 50;
    if (wf_steps.length > 0) {
        var step = wf_steps[wf_steps.length - 1];
        x = parseInt(step.attr("x")) + 170;
        y = parseInt(step.attr("y"));
        if (x > wf_r.width - wf_width) {
            x = 10;
            y = y + 100;
        }
 
        if (y > wf_r.height - wf_height) {
            y = wf_r.height - wf_height;
        }
    }
    return { x: x, y: y };
}
 
//添加连线
function addConn() {
    if (!wf_focusObj || !isStepObj(wf_focusObj)) {
        alert("请选择要连接的步骤!"); return false;
    }
    wf_option = "line";
    document.body.onmousemove = mouseMove;
    document.body.onmousedown = function () {
        for (var i = 0; i < tempArrPath.length; i++) {
            tempArrPath[i].arrPath.remove();
        }
        tempArrPath = [];
        document.body.onmousemove = null;
    };
}
 
function mouseMove(ev) {
    ev = ev || window.event;
    var mousePos = mouseCoords(ev);
    mouseX = mousePos.x;
    mouseY = mousePos.y;
    var obj = { obj1: wf_focusObj, obj2: null };
    wf_r.drawArr(obj);
}
 
function mouseCoords(ev) {
    if (ev.pageX || ev.pageY) {
        return { x: ev.pageX, y: ev.pageY };
    }
    return {
        x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
        y: ev.clientY + document.body.scrollTop - document.body.clientTop
    };
}
 
//连接对象
function connObj(obj, addToJSON, title) {
    if (addToJSON == undefined || addToJSON == null) addToJSON = true;
    if (isLine(obj)) {
        var newline = wf_r.drawArr(obj);
        wf_conns.push(newline);
        if (addToJSON) {
            var line = {};
            line.id = obj.id;
            line.text = title || "";
            line.from = obj.obj1.id;
            line.to = obj.obj2.id;
            line.customMethod = "";
            line.sql = "";
            line.noaccordMsg = "";
            addLine(line);
        }
        else {
            setLineText(obj.id, title);
        }
    }
}
 
//单击事件执行相关操作
function click() {
    var o = this;
    switch (wf_option) {
        case "line":
            var obj = { id: getGuid(), obj1: wf_focusObj, obj2: o };
            connObj(obj);
            break;
        default:
            changeStyle(o);
            break;
    }
    wf_option = "";
    wf_focusObj = this;
}
 
//连线单击事件
function connClick() {
    for (var i = 0; i < wf_conns.length; i++) {
        if (wf_conns[i].arrPath === this) {
 
            wf_conns[i].arrPath.attr({ "stroke": "#db0f14", "fill": "#db0f14" });
        }
        else {
            wf_conns[i].arrPath.attr({ "stroke": wf_connColor, "fill": wf_connColor });
        }
    }
    for (var i = 0; i < wf_steps.length; i++) {
        wf_steps[i].attr("fill", "#efeff0");
        wf_steps[i].attr("stroke", "#23508e");
    }
 
    wf_focusObj = this;
}
 
//判断一个节点与另一个节点之间是否可以连线
function isLine(obj) {
    if (!obj || !obj.obj1 || !obj.obj2) {
        return false;
    }
    if (obj.obj1 === obj.obj2) {
        return false;
    }
    if (!isStepObj(obj.obj1) || !isStepObj(obj.obj2)) {
        return false;
    }
    for (var i = 0; i < wf_conns.length; i++) {
        if (obj.obj1 === obj.obj2 || (wf_conns[i].obj1 === obj.obj1 && wf_conns[i].obj2 === obj.obj2)) {
            return false;
        }
    }
    return true;
}
 
//判断一个对象是否是步骤对象
function isStepObj(obj) {
    return obj && obj.type1 && (obj.type1.toString() === "normal" || obj.type1.toString() === "chainStep" || obj.type1.toString() === "endStep");
}
 
//得到XML DOM
function getXmlDoc() {
    var xmlDoc = RoadUI.Core.getXmlDoc();
    xmlDoc.async = false;
    return xmlDoc
}
 
//得到GUID
function getGuid() {
    return Raphael.createUUID().toLowerCase();
}
 
//改变节点样式
function changeStyle(obj) {
    if (!obj) {
        return;
    }
    for (var i = 0; i < wf_steps.length; i++) {
        if (wf_steps[i].id == obj.id) {
            wf_steps[i].attr("fill", wf_noteColor);
            wf_steps[i].attr("stroke", "#cc0000");
        }
        else {
            wf_steps[i].attr("fill", wf_noteColor);
            wf_steps[i].attr("stroke", wf_nodeBorderColor);
        }
    }
 
    for (var i = 0; i < wf_conns.length; i++) {
        if (wf_conns[i].arrPath) {
            wf_conns[i].arrPath.attr({ "stroke": wf_connColor, "fill": wf_connColor });
        }
    }
    //obj.animate({ }, 500);
}
 
//拖动节点开始时的事件
function dragger() {
    this.ox = this.attr("x");
    this.oy = this.attr("y");
    changeStyle(this);
};
 
//拖动事件
function move(dx, dy) {
    var x = this.ox + dx;
    var y = this.oy + dy;
 
    if (x < 0) {
        x = 0;
    }
    else if (x > wf_r.width - wf_width) {
        x = wf_r.width - wf_width;
    }
 
    if (y < 0) {
        y = 0;
    }
    else if (y > wf_r.height - wf_height) {
        y = wf_r.height - wf_height;
    }
    this.attr("x", x);
    this.attr("y", y);
    if (this.id) {
        var text = wf_r.getById('text_' + this.id);
        if (text) {
            text.attr("x", x + 52);
            text.attr("y", y + 25);
        }
    }
    for (var j = wf_conns.length; j--;) {
        if (wf_conns[j].obj1.id == this.id || wf_conns[j].obj2.id == this.id) {
            for (var n = 0; n < wf_json.lines.length; n++) {
                if (wf_json.lines[n].id == wf_conns[j].arrPath.id) {
                    setLineText(wf_json.lines[n].id, wf_json.lines[n].text);
                    break;
                }
            }
            wf_r.drawArr(wf_conns[j]);
        }
    }
    wf_r.safari();
};
 
//拖动结束后的事件
function up() {
    changeStyle(this);
    //记录移动后的位置
    if (isStepObj(this)) {
        var bbox = this.getBBox();
        if (bbox) {
            var steps = wf_json.steps;
            if (steps && steps.length > 0) {
                for (var i = 0; i < steps.length; i++) {
                    if (steps[i].id == this.id) {
                        steps[i].position = { "x": bbox.x, "y": bbox.y, "width": bbox.width, "height": bbox.height };
                        break;
                    }
                }
            }
        }
    }
};
 
//随着节点位置的改变动态改变箭头
Raphael.fn.drawArr = function (obj) {
    if (!obj || !obj.obj1) {
        return;
    }
 
    if (!obj.obj2) {
        var point1 = getStartEnd(obj.obj1, obj.obj2);
        var path2 = getArr(point1.start.x, point1.start.y, mouseX, mouseY, 7);
        for (var i = 0; i < tempArrPath.length; i++) {
            tempArrPath[i].arrPath.remove();
        }
        tempArrPath = [];
        obj.arrPath = this.path(path2);
        obj.arrPath.attr({ "stroke-width": 1.7, "stroke": wf_connColor, "fill": wf_connColor });
        tempArrPath.push(obj);
        return;
    }
 
    var point = getStartEnd(obj.obj1, obj.obj2);
    var path1 = getArr(point.start.x, point.start.y, point.end.x, point.end.y, 7);
    try {
        if (obj.arrPath) {
            obj.arrPath.attr({ path: path1 });
        }
        else {
            obj.arrPath = this.path(path1);
            obj.arrPath.attr({ "stroke-width": 1.7, "stroke": wf_connColor, "fill": wf_connColor, "x": point.start.x, "y": point.start.y, "x1": point.end.x, "y1": point.end.y });
            if (wf_designer) {
                obj.arrPath.click(connClick);
                obj.arrPath.dblclick(connSetting);
                obj.arrPath.id = obj.id;
                obj.arrPath.fromid = obj.obj1.id;
                obj.arrPath.toid = obj.obj2.id;
            }
        }
    } catch (e) { }
    return obj;
};
 
function getStartEnd(obj1, obj2) {
    var bb1 = obj1 ? obj1.getBBox() : null;
    var bb2 = obj2 ? obj2.getBBox() : null;
    var p = [
                { x: bb1.x + bb1.width / 2, y: bb1.y - 1 },
                { x: bb1.x + bb1.width / 2, y: bb1.y + bb1.height + 1 },
                { x: bb1.x - 1, y: bb1.y + bb1.height / 2 },
                { x: bb1.x + bb1.width + 1, y: bb1.y + bb1.height / 2 },
                bb2 ? { x: bb2.x + bb2.width / 2, y: bb2.y - 1 } : {},
                bb2 ? { x: bb2.x + bb2.width / 2, y: bb2.y + bb2.height + 1 } : {},
                bb2 ? { x: bb2.x - 1, y: bb2.y + bb2.height / 2 } : {},
                bb2 ? { x: bb2.x + bb2.width + 1, y: bb2.y + bb2.height / 2 } : {}
    ];
    var d = {}, dis = [];
    for (var i = 0; i < 4; i++) {
        for (var j = 4; j < 8; j++) {
            var dx = Math.abs(p[i].x - p[j].x),
                        dy = Math.abs(p[i].y - p[j].y);
            if (
                    (i == j - 4) ||
                    (((i != 3 && j != 6) || p[i].x < p[j].x) &&
                    ((i != 2 && j != 7) || p[i].x > p[j].x) &&
                    ((i != 0 && j != 5) || p[i].y > p[j].y) &&
                    ((i != 1 && j != 4) || p[i].y < p[j].y))
                ) {
                dis.push(dx + dy);
                d[dis[dis.length - 1]] = [i, j];
            }
        }
    }
    if (dis.length == 0) {
        var res = [0, 4];
    } else {
        res = d[Math.min.apply(Math, dis)];
    }
    var result = {};
    result.start = {};
    result.end = {};
    result.start.x = p[res[0]].x;
    result.start.y = p[res[0]].y;
    result.end.x = p[res[1]].x;
    result.end.y = p[res[1]].y;
    return result;
}
 
function getArr(x1, y1, x2, y2, size) {
    var angle = Raphael.angle(x1, y1, x2, y2);
    var a45 = Raphael.rad(angle - 28);
    var a45m = Raphael.rad(angle + 28);
    var x2a = x2 + Math.cos(a45) * size;
    var y2a = y2 + Math.sin(a45) * size;
    var x2b = x2 + Math.cos(a45m) * size;
    var y2b = y2 + Math.sin(a45m) * size;
    return ["M", x1, y1, "L", x2, y2, "M", x2, y2, "L", x2b, y2b, "L", x2a, y2a, "z"].join(",");
}
 
//初始化画板
function initwf() {
    wf_json = {};
    wf_steps = new Array();
    wf_texts = new Array();
    wf_conns = new Array();
    wf_r.clear();
}
 
 
//删除数组中元素
Array.prototype.remove = function (n) {
    if (isNaN(n) || n > this.length) { return false; }
    this.splice(n, 1);
}
 
function removeArray(array, n) {
    if (isNaN(n) || n > array.length) { return false; }
    array.splice(n, 1);
}
 
//得到一连接的所有表
function getTables(connid) {
    var tables = [];
    $.ajax({
        url: "WorkFlowDesigner/GetTables?connid=" + connid, dataType: "json", async: false, cache: false, success: function (json) {
            for (var i = 0; i < json.length; i++) {
                tables.push(json[i]);
            }
        }
    });
    return tables;
}
 
//得到一个表所有字段
function getFields(table) {
    var fields = [];
    $.ajax({
        url: "/WorkFlowDesigner/FindColumnList?table=" + table, dataType: "json", async: false, cache: false, success: function (json) {
            for (var i = 0; i < json.length; i++) {
                fields.push(json[i]);
            }
        }
    });
    return fields;
}
 
//载入当前数据连接的所有表和字段
function initLinks_Tables_Fields(datatable) {
    if (!datatable)
        return;
    var fields = getFields(datatable);
    for (var k = 0; k < fields.length; k++) {
        links_tables_fields.push({ table: datatable, field: fields[k].name, fieldNote: fields[k].note });
    }
}
 
//添加步骤
function pushStep(step) {
    if (!step) return;
    if (!wf_json.steps) wf_json.steps = [];
    var isup = false;
    for (var i = 0; i < wf_json.steps.length; i++) {
        if (wf_json.steps[i].id === step.id) {
            wf_json.steps[i] = step;
            isup = true;
        }
    }
    if (!isup) {
        wf_json.steps.push(step);
    }
}
 
//添加线
function addLine(line) {
    if (!line || !line.from || !line.to) return;
    if (!wf_json.lines) wf_json.lines = [];
    var isup = false;
    for (var i = 0; i < wf_json.lines.length; i++) {
        if (wf_json.lines[i].id == line.id) {
            wf_json.lines[i] = line;
            isup = true;
        }
    }
    if (!isup) {
        wf_json.lines.push(line);
    }
    setLineText(line.id, line.text);
}
 
//根据当前JSON重载入流程
function reloadFlow(json) {
    if (!json || !json.id || $.trim(json.id) == "") return false;
    wf_json = json;
    wf_id = wf_json.id;
    wf_r.clear();
    wf_steps = [];
    wf_conns = [];
    wf_texts = [];
    var steps = wf_json.steps;
    if (steps && steps.length > 0) {
        for (var i = 0; i < steps.length; i++) {
            addStep(steps[i].position.x, steps[i].position.y, steps[i].name, steps[i].id, false, steps[i].type);
        }
    }
    var lines = wf_json.lines;
    if (lines && lines.length > 0) {
        for (var i = 0; i < lines.length; i++) {
            connObj({ id: lines[i].id, obj1: wf_r.getById(lines[i].from), obj2: wf_r.getById(lines[i].to) }, false, lines[i].text);
        }
    }
    var url = "/WorkFlowDesigner/FindWorkFlowTable?keyValue=" + wf_id;
    window.$.ajax({
        url: url,
        type: "get",
        dataType: "json",
        success: function (data) {
            links_tables_fields = [];
            $.each(data, function (i, item) {
                //初始化数据连接
                initLinks_Tables_Fields(item.Value);
                console.log("数据装载:"+links_tables_fields);
            });
        }
    });
}
 
//从json中删除步骤
function deleteStep(stepid) {
    var steps = wf_json.steps;
    if (steps && steps.length > 0) {
        for (var i = 0; i < steps.length; i++) {
            if (steps[i].id == stepid) {
                removeArray(steps, i);
            }
        }
    }
}
 
//从json中删除线
function deleteLine(lineid, textid) {
    var lines = wf_json.lines;
    if (lines && lines.length > 0) {
        for (var i = 0; i < lines.length; i++) {
            if (lines[i].id == lineid) {
                removeArray(lines, i);
            }
        }
    }
    if (textid) {
        if (wf_texts && wf_texts.length > 0) {
            for (var i = 0; i < wf_texts.length; i++) {
 
                if (wf_texts[i].id === "line_" + textid) {
                    wf_texts[i].remove();
                }
            }
        }
    }
}
 
//步骤属性设置
function stepSetting() {
    var bbox = this.getBBox();
    var url = "/WorkFlowDesigner/InstallNode?flowid=" + wf_id + "&id=" + this.id + "&x=" + bbox.x + "&y=" + bbox.y + "&width=" + bbox.width + "&height=" + bbox.height + "&formtype=1";
    layer.open({
        type: 2,
        title: '节点参数设置',
        shadeClose: true,
        shade: 0.4,
        area: ['800px', '540px'],
        content: url
    });
}
 
//步骤属性设置
function chainStepSetting() {
    var bbox = this.getBBox();
    var url = "/WorkFlowDesigner/InstallNodeBy?flowid=" + wf_id + "&id=" + this.id + "&x=" + bbox.x + "&y=" + bbox.y + "&width=" + bbox.width + "&height=" + bbox.height + "&formtype=1";
    layer.open({
        type: 2,
        title: '节点参数设置',
        shadeClose: true,
        shade: 0.4,
        area: ['800px', '540px'],
        content: url
    });
}
 
//流转条件设置
function connSetting() {
    var url = "/WorkFlowDesigner/InstallLine?flowid=" + wf_id + "&id=" + this.id + "&from=" + this.fromid + "&to=" + this.toid;
    layer.open({
        type: 2,
        title: '流转条件设置',
        shadeClose: true,
        shade: 0.4,
        area: ['700px', '450px'],
        content: url
    });
}
 
//保存流程
function saveFlow(op) {
    if (!wf_json) {
        layer.alert("未设置流程!"); return false;
    }
    else if (!wf_json.id || $.trim(wf_json.id) === "") {
        layer.alert("请先定义流程!"); return false;
    }
    else if (!wf_json.name || $.trim(wf_json.name) === "") {
        layer.alert("流程名称不能为空!"); return false;
    }
    var title = "";
    if (op === "save") title = "保存流程";
    else if (op === "install") title = "安装流程";
    else if (op === "uninstall") title = "卸载流程";
    else if (op === "delete") {
        FlowDelete(wf_json.id);
    }
    var url = "/WorkFlowDesigner/Opation?flowid=" + wf_json.id + "&operate=" + (op || "save");
    layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.4,
        area: ['260px', '150px'],
        content: url
    });
}
 
function FlowDelete(id) {
    var url = "/WorkFlowDesigner/DeleteFlow/" + id;
    layer.confirm('确认删除该流程信息吗?', { icon: 3, title: '用户提示' }, function (index) {
        window.$.ajax({
            url: url,
            type: "get",
            dataType: "json",
            success: function (response) {
                if (response.status === 1) {
                    layer.confirm('删除成功', {
                        btn: ['确认', '取消'] //按钮
                    }, function () {
                        window.location.reload();
                    }, function () {
                        window.location.reload();
                    });
                }
                if (response.status === 0) {
                    var errorMsg = response.error;
                    layer.alert(errorMsg);
                }
            }
        });
        layer.closeAll();
    });
}
 
//另存为
function saveAs() {
    if (!wf_json || !wf_json.id || $.trim(wf_json.id) == "") {
        alert("请先新建或打开一个流程!"); return false;
    }
    var url = top.rootdir + "/WorkFlowDesigner/SaveAs?appid=" + appid + "&flowid=" + wf_json.id;
    dialog.open({ title: "流程另存为", width: 600, height: 230, url: url, openerid: iframeid, resize: false });
}