-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmyflow.js
1512 lines (1410 loc) · 37 KB
/
myflow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function($) {
var myflow = {};
myflow.config = {
editable : true,
lineHeight : 15,
basePath : '',
rect : {// 状态
attr : {
x : 10,
y : 10,
width : 100,
height : 50,
r : 5,
fill : '90-#fff-#C0C0C0',
stroke : '#000',
"stroke-width" : 1
},
showType : 'image&text',// image,text,image&text
type : 'state',
name : {
text : 'state',
'font-style' : 'italic'
},
text : {
text : '状态',
'font-size' : 13
},
margin : 5,
props : [],
img : {}
},
path : {// 路径转换
attr : {
path : {
path : 'M10 10L100 100',
stroke : '#808080',
fill : "none",
"stroke-width" : 2
},
arrow : {
path : 'M10 10L10 10',
stroke : '#808080',
fill : "#808080",
"stroke-width" : 2,
radius : 4
},
fromDot : {
width : 5,
height : 5,
stroke : '#fff',
fill : '#000',
cursor : "move",
"stroke-width" : 2
},
toDot : {
width : 5,
height : 5,
stroke : '#fff',
fill : '#000',
cursor : "move",
"stroke-width" : 2
},
bigDot : {
width : 5,
height : 5,
stroke : '#fff',
fill : '#000',
cursor : "move",
"stroke-width" : 2
},
smallDot : {
width : 5,
height : 5,
stroke : '#fff',
fill : '#000',
cursor : "move",
"stroke-width" : 3
},
text : {
cursor : "move",
'background' : '#000'
}
},
text : {
patten : 'TO {to}',
textPos : {
x : 0,
y : -10
}
},
props : {
text : {
name : 'text',
label : '显示',
value : '',
editor : function() {
return new myflow.editors.textEditor();
}
}
}
},
tools : {// 工具栏
attr : {
left : 10,
top : 10
},
pointer : {},
path : {},
states : {},
save : {
onclick : function(data) {
alert(data);
}
}
},
props : {// 属性编辑器
attr : {
top : 10,
right : 30
},
props : {}
},
restore : '',
activeRects : {// 当前激活状态
rects : [],
rectAttr : {
stroke : '#ff0000',
"stroke-width" : 2
}
},
historyRects : {// 历史激活状态
rects : [],
pathAttr : {
path : {
stroke : '#00ff00'
},
arrow : {
stroke : '#00ff00',
fill : "#00ff00"
}
}
}
};
myflow.util = {
isLine : function(p1, p2, p3) {// 三个点是否在一条直线上
var s, p2y;
if ((p1.x - p3.x) == 0)
s = 1;
else
s = (p1.y - p3.y) / (p1.x - p3.x);
p2y = (p2.x - p3.x) * s + p3.y;
// $('body').append(p2.y+'-'+p2y+'='+(p2.y-p2y)+', ');
if ((p2.y - p2y) < 10 && (p2.y - p2y) > -10) {
p2.y = p2y;
return true;
}
return false;
},
center : function(p1, p2) {// 两个点的中间点
return {
x : (p1.x - p2.x) / 2 + p2.x,
y : (p1.y - p2.y) / 2 + p2.y
};
},
nextId : (function() {
var uid = 0;
return function() {
return ++uid;
};
})(),
connPoint : function(rect, p) {// 计算矩形中心到p的连线与矩形的交叉点
var start = p, end = {
x : rect.x + rect.width / 2,
y : rect.y + rect.height / 2
};
// 计算正切角度
var tag = (end.y - start.y) / (end.x - start.x);
tag = isNaN(tag) ? 0 : tag;
var rectTag = rect.height / rect.width;
// 计算箭头位置
var xFlag = start.y < end.y ? -1 : 1, yFlag = start.x < end.x
? -1
: 1, arrowTop, arrowLeft;
// 按角度判断箭头位置
if (Math.abs(tag) > rectTag && xFlag == -1) {// top边
arrowTop = end.y - rect.height / 2;
arrowLeft = end.x + xFlag * rect.height / 2 / tag;
} else if (Math.abs(tag) > rectTag && xFlag == 1) {// bottom边
arrowTop = end.y + rect.height / 2;
arrowLeft = end.x + xFlag * rect.height / 2 / tag;
} else if (Math.abs(tag) < rectTag && yFlag == -1) {// left边
arrowTop = end.y + yFlag * rect.width / 2 * tag;
arrowLeft = end.x - rect.width / 2;
} else if (Math.abs(tag) < rectTag && yFlag == 1) {// right边
arrowTop = end.y + rect.width / 2 * tag;
arrowLeft = end.x + rect.width / 2;
}
return {
x : arrowLeft,
y : arrowTop
};
},
arrow : function(p1, p2, r) {// 画箭头,p1 开始位置,p2 结束位置, r前头的边长
var atan = Math.atan2(p1.y - p2.y, p2.x - p1.x) * (180 / Math.PI);
var centerX = p2.x - r * Math.cos(atan * (Math.PI / 180));
var centerY = p2.y + r * Math.sin(atan * (Math.PI / 180));
var x2 = centerX + r * Math.cos((atan + 120) * (Math.PI / 180));
var y2 = centerY - r * Math.sin((atan + 120) * (Math.PI / 180));
var x3 = centerX + r * Math.cos((atan + 240) * (Math.PI / 180));
var y3 = centerY - r * Math.sin((atan + 240) * (Math.PI / 180));
return [p2, {
x : x2,
y : y2
}, {
x : x3,
y : y3
}];
}
}
myflow.rect = function(o, r) {
var _this = this, _uid = myflow.util.nextId(), _o = $.extend(true, {},
myflow.config.rect, o), _id = 'rect' + _uid, _r = r, // Raphael画笔
_rect, _img, // 图标
_name, // 状态名称
_text, // 显示文本
_ox, _oy; // 拖动时,保存起点位置;
_o.text.text += _uid;
_rect = _r.rect(_o.attr.x, _o.attr.y, _o.attr.width, _o.attr.height,
_o.attr.r).hide().attr(_o.attr);
_img = _r.image(myflow.config.basePath + _o.img.src,
_o.attr.x + _o.img.width / 2,
_o.attr.y + (_o.attr.height - _o.img.height) / 2, _o.img.width,
_o.img.height).hide();
_name = _r.text(
_o.attr.x + _o.img.width + (_o.attr.width - _o.img.width) / 2,
_o.attr.y + myflow.config.lineHeight / 2, _o.name.text).hide()
.attr(_o.name);
_text = _r.text(
_o.attr.x + _o.img.width + (_o.attr.width - _o.img.width) / 2,
_o.attr.y + (_o.attr.height - myflow.config.lineHeight) / 2
+ myflow.config.lineHeight, _o.text.text).hide()
.attr(_o.text);// 文本
// 拖动处理----------------------------------------
_rect.drag(function(dx, dy) {
dragMove(dx, dy);
}, function() {
dragStart()
}, function() {
dragUp();
});
_img.drag(function(dx, dy) {
dragMove(dx, dy);
}, function() {
dragStart()
}, function() {
dragUp();
});
_name.drag(function(dx, dy) {
dragMove(dx, dy);
}, function() {
dragStart()
}, function() {
dragUp();
});
_text.drag(function(dx, dy) {
dragMove(dx, dy);
}, function() {
dragStart()
}, function() {
dragUp();
});
var dragMove = function(dx, dy) {// 拖动中
if (!myflow.config.editable)
return;
var x = (_ox + dx);// -((_ox+dx)%10);
var y = (_oy + dy);// -((_oy+dy)%10);
_bbox.x = x - _o.margin;
_bbox.y = y - _o.margin;
resize();
};
var dragStart = function() {// 开始拖动
_ox = _rect.attr("x");
_oy = _rect.attr("y");
_rect.attr({
opacity : 0.5
});
_img.attr({
opacity : 0.5
});
_text.attr({
opacity : 0.5
});
};
var dragUp = function() {// 拖动结束
_rect.attr({
opacity : 1
});
_img.attr({
opacity : 1
});
_text.attr({
opacity : 1
});
};
// 改变大小的边框
var _bpath, _bdots = {}, _bw = 5, _bbox = {
x : _o.attr.x - _o.margin,
y : _o.attr.y - _o.margin,
width : _o.attr.width + _o.margin * 2,
height : _o.attr.height + _o.margin * 2
};
_bpath = _r.path('M0 0L1 1').hide();
_bdots['t'] = _r.rect(0, 0, _bw, _bw).attr({
fill : '#000',
stroke : '#fff',
cursor : 's-resize'
}).hide().drag(function(dx, dy) {
bdragMove(dx, dy, 't');
}, function() {
bdragStart(this.attr('x') + _bw / 2, this.attr('y') + _bw
/ 2, 't');
}, function() {
});// 上
_bdots['lt'] = _r.rect(0, 0, _bw, _bw).attr({
fill : '#000',
stroke : '#fff',
cursor : 'nw-resize'
}).hide().drag(function(dx, dy) {
bdragMove(dx, dy, 'lt');
}, function() {
bdragStart(this.attr('x') + _bw / 2, this.attr('y') + _bw
/ 2, 'lt');
}, function() {
});// 左上
_bdots['l'] = _r.rect(0, 0, _bw, _bw).attr({
fill : '#000',
stroke : '#fff',
cursor : 'w-resize'
}).hide().drag(function(dx, dy) {
bdragMove(dx, dy, 'l');
}, function() {
bdragStart(this.attr('x') + _bw / 2, this.attr('y') + _bw
/ 2, 'l');
}, function() {
});// 左
_bdots['lb'] = _r.rect(0, 0, _bw, _bw).attr({
fill : '#000',
stroke : '#fff',
cursor : 'sw-resize'
}).hide().drag(function(dx, dy) {
bdragMove(dx, dy, 'lb');
}, function() {
bdragStart(this.attr('x') + _bw / 2, this.attr('y') + _bw
/ 2, 'lb');
}, function() {
});// 左下
_bdots['b'] = _r.rect(0, 0, _bw, _bw).attr({
fill : '#000',
stroke : '#fff',
cursor : 's-resize'
}).hide().drag(function(dx, dy) {
bdragMove(dx, dy, 'b');
}, function() {
bdragStart(this.attr('x') + _bw / 2, this.attr('y') + _bw
/ 2, 'b');
}, function() {
});// 下
_bdots['rb'] = _r.rect(0, 0, _bw, _bw).attr({
fill : '#000',
stroke : '#fff',
cursor : 'se-resize'
}).hide().drag(function(dx, dy) {
bdragMove(dx, dy, 'rb');
}, function() {
bdragStart(this.attr('x') + _bw / 2, this.attr('y') + _bw
/ 2, 'rb');
}, function() {
});// 右下
_bdots['r'] = _r.rect(0, 0, _bw, _bw).attr({
fill : '#000',
stroke : '#fff',
cursor : 'w-resize'
}).hide().drag(function(dx, dy) {
bdragMove(dx, dy, 'r');
}, function() {
bdragStart(this.attr('x') + _bw / 2, this.attr('y') + _bw
/ 2, 'r')
}, function() {
});// 右
_bdots['rt'] = _r.rect(0, 0, _bw, _bw).attr({
fill : '#000',
stroke : '#fff',
cursor : 'ne-resize'
}).hide().drag(function(dx, dy) {
bdragMove(dx, dy, 'rt');
}, function() {
bdragStart(this.attr('x') + _bw / 2, this.attr('y') + _bw
/ 2, 'rt')
}, function() {
});// 右上
$([_bdots['t'].node,_bdots['lt'].node,_bdots['l'].node,_bdots['lb'].node,_bdots['b'].node,_bdots['rb'].node,_bdots['r'].node,_bdots['rt'].node]).click(function(){return false;});
var bdragMove = function(dx, dy, t) {
if (!myflow.config.editable)
return;
var x = _bx + dx, y = _by + dy;
switch (t) {
case 't' :
_bbox.height += _bbox.y - y;
_bbox.y = y;
break;
case 'lt' :
_bbox.width += _bbox.x - x;
_bbox.height += _bbox.y - y;
_bbox.x = x;
_bbox.y = y;
break;
case 'l' :
_bbox.width += _bbox.x - x;
_bbox.x = x;
break;
case 'lb' :
_bbox.height = y - _bbox.y;
_bbox.width += _bbox.x - x;
_bbox.x = x;
break;
case 'b' :
_bbox.height = y - _bbox.y;
break;
case 'rb' :
_bbox.height = y - _bbox.y;
_bbox.width = x - _bbox.x;
break;
case 'r' :
_bbox.width = x - _bbox.x;
break;
case 'rt' :
_bbox.width = x - _bbox.x;
_bbox.height += _bbox.y - y;
_bbox.y = y;
break;
}
resize();
// $('body').append(t);
};
var bdragStart = function(ox, oy, t) {
_bx = ox;
_by = oy;
};
// 事件处理--------------------------------
$([_rect.node, _text.node, _name.node, _img.node]).bind('click',
function() {
if (!myflow.config.editable)
return;
showBox();
var mod = $(_r).data('mod');
switch (mod) {
case 'pointer' :
break;
case 'path' :
var pre = $(_r).data('currNode');
if (pre && pre.getId() != _id
&& pre.getId().substring(0, 4) == 'rect') {
$(_r).trigger('addpath', [pre, _this]);
}
break;
}
$(_r).trigger('click', _this);
$(_r).data('currNode', _this);
return false;
});
var clickHandler = function(e, src) {
if (!myflow.config.editable)
return;
if (src.getId() == _id) {
$(_r).trigger('showprops', [_o.props, src]);
} else {
hideBox();
}
};
$(_r).bind('click', clickHandler);
var textchangeHandler = function(e, text, src) {
if (src.getId() == _id) {
_text.attr({
text : text
});
}
};
$(_r).bind('textchange', textchangeHandler);
// 私有函数-----------------------
// 边框路径
function getBoxPathString() {
return 'M' + _bbox.x + ' ' + _bbox.y + 'L' + _bbox.x + ' '
+ (_bbox.y + _bbox.height) + 'L' + (_bbox.x + _bbox.width)
+ ' ' + (_bbox.y + _bbox.height) + 'L'
+ (_bbox.x + _bbox.width) + ' ' + _bbox.y + 'L' + _bbox.x
+ ' ' + _bbox.y;
}
// 显示边框
function showBox() {
_bpath.show();
for (var k in _bdots) {
_bdots[k].show();
}
}
// 隐藏
function hideBox() {
_bpath.hide();
for (var k in _bdots) {
_bdots[k].hide();
}
}
// 根据_bbox,更新位置信息
function resize() {
var rx = _bbox.x + _o.margin, ry = _bbox.y + _o.margin, rw = _bbox.width
- _o.margin * 2, rh = _bbox.height - _o.margin * 2;
_rect.attr({
x : rx,
y : ry,
width : rw,
height : rh
});
switch (_o.showType) {
case 'image' :
_img.attr({
x : rx + (rw - _o.img.width) / 2,
y : ry + (rh - _o.img.height) / 2
}).show();
break;
case 'text' :
_rect.show();
_text.attr({
x : rx + rw / 2,
y : ry + rh / 2
}).show();// 文本
break;
case 'image&text' :
_rect.show();
_name.attr({
x : rx + _o.img.width + (rw - _o.img.width) / 2,
y : ry + myflow.config.lineHeight / 2
}).show();
_text.attr({
x : rx + _o.img.width + (rw - _o.img.width) / 2,
y : ry + (rh - myflow.config.lineHeight) / 2
+ myflow.config.lineHeight
}).show();// 文本
_img.attr({
x : rx + _o.img.width / 2,
y : ry + (rh - _o.img.height) / 2
}).show();
break;
}
_bdots['t'].attr({
x : _bbox.x + _bbox.width / 2 - _bw / 2,
y : _bbox.y - _bw / 2
});// 上
_bdots['lt'].attr({
x : _bbox.x - _bw / 2,
y : _bbox.y - _bw / 2
});// 左上
_bdots['l'].attr({
x : _bbox.x - _bw / 2,
y : _bbox.y - _bw / 2 + _bbox.height / 2
});// 左
_bdots['lb'].attr({
x : _bbox.x - _bw / 2,
y : _bbox.y - _bw / 2 + _bbox.height
});// 左下
_bdots['b'].attr({
x : _bbox.x - _bw / 2 + _bbox.width / 2,
y : _bbox.y - _bw / 2 + _bbox.height
});// 下
_bdots['rb'].attr({
x : _bbox.x - _bw / 2 + _bbox.width,
y : _bbox.y - _bw / 2 + _bbox.height
});// 右下
_bdots['r'].attr({
x : _bbox.x - _bw / 2 + _bbox.width,
y : _bbox.y - _bw / 2 + _bbox.height / 2
});// 右
_bdots['rt'].attr({
x : _bbox.x - _bw / 2 + _bbox.width,
y : _bbox.y - _bw / 2
});// 右上
_bpath.attr({
path : getBoxPathString()
});
$(_r).trigger('rectresize', _this);
};
// 函数----------------
// 转化json字串
this.toJson = function() {
var data = "{type:'" + _o.type + "',text:{text:'"
+ _text.attr('text') + "'}, attr:{ x:"
+ Math.round(_rect.attr('x')) + ", y:"
+ Math.round(_rect.attr('y')) + ", width:"
+ Math.round(_rect.attr('width')) + ", height:"
+ Math.round(_rect.attr('height')) + "}, props:{";
for (var k in _o.props) {
data += k + ":{value:'"
+ _o.props[k].value + "'},";
}
if (data.substring(data.length - 1, data.length) == ',')
data = data.substring(0, data.length - 1);
data += "}}";
return data;
};
// 从数据中恢复图
this.restore = function(data) {
var obj = data;
// if (typeof data === 'string')
// obj = eval(data);
_o = $.extend(true, _o, data);
_text.attr({
text : obj.text.text
});
resize();
};
this.getBBox = function() {
return _bbox;
};
this.getId = function() {
return _id;
};
this.remove = function() {
_rect.remove();
_text.remove();
_name.remove();
_img.remove();
_bpath.remove();
for (var k in _bdots) {
_bdots[k].remove();
}
};
this.text = function() {
return _text.attr('text');
};
this.attr = function(attr) {
if (attr)
_rect.attr(attr);
};
resize();// 初始化位置
};
myflow.path = function(o, r, from, to) {
var _this = this, _r = r, _o = $.extend(true, {}, myflow.config.path), _path, _arrow, _text, _textPos = _o.text.textPos, _ox, _oy, _from = from, _to = to, _id = 'path'
+ myflow.util.nextId(), _dotList, _autoText = true;
// 点
function dot(type, pos, left, right) {
var _this = this, _t = type, _n, _lt = left, _rt = right, _ox, _oy, // 缓存移动前时位置
_pos = pos;// 缓存位置信息{x,y}, 注意:这是计算出中心点
switch (_t) {
case 'from' :
_n = _r.rect(pos.x - _o.attr.fromDot.width / 2,
pos.y - _o.attr.fromDot.height / 2,
_o.attr.fromDot.width, _o.attr.fromDot.height)
.attr(_o.attr.fromDot);
break;
case 'big' :
_n = _r.rect(pos.x - _o.attr.bigDot.width / 2,
pos.y - _o.attr.bigDot.height / 2,
_o.attr.bigDot.width, _o.attr.bigDot.height)
.attr(_o.attr.bigDot);
break;
case 'small' :
_n = _r.rect(pos.x - _o.attr.smallDot.width / 2,
pos.y - _o.attr.smallDot.height / 2,
_o.attr.smallDot.width, _o.attr.smallDot.height)
.attr(_o.attr.smallDot);
break;
case 'to' :
_n = _r.rect(pos.x - _o.attr.toDot.width / 2,
pos.y - _o.attr.toDot.height / 2,
_o.attr.toDot.width, _o.attr.toDot.height)
.attr(_o.attr.toDot);
break;
}
if (_n && (_t == 'big' || _t == 'small')) {
_n.drag(function(dx, dy) {
dragMove(dx, dy);
}, function() {
dragStart()
}, function() {
dragUp();
});// 初始化拖动
var dragMove = function(dx, dy) {// 拖动中
var x = (_ox + dx), y = (_oy + dy);
_this.moveTo(x, y);
};
var dragStart = function() {// 开始拖动
if (_t == 'big') {
_ox = _n.attr("x") + _o.attr.bigDot.width / 2;
_oy = _n.attr("y") + _o.attr.bigDot.height / 2;
}
if (_t == 'small') {
_ox = _n.attr("x") + _o.attr.smallDot.width / 2;
_oy = _n.attr("y") + _o.attr.smallDot.height / 2;
}
};
var dragUp = function() {// 拖动结束
};
}
$(_n.node).click(function(){return false;});
this.type = function(t) {
if (t)
_t = t;
else
return _t;
};
this.node = function(n) {
if (n)
_n = n;
else
return _n;
};
this.left = function(l) {
if (l)
_lt = l;
else
return _lt;
};
this.right = function(r) {
if (r)
_rt = r;
else
return _rt;
};
this.remove = function() {
_lt = null;
_rt = null;
_n.remove();
};
this.pos = function(pos) {
if (pos) {
_pos = pos;
_n.attr({
x : _pos.x - _n.attr('width') / 2,
y : _pos.y - _n.attr('height') / 2
});
return this;
} else {
return _pos
}
};
this.moveTo = function(x, y) {
this.pos({
x : x,
y : y
});
switch (_t) {
case 'from' :
if (_rt && _rt.right() && _rt.right().type() == 'to') {
_rt.right().pos(myflow.util.connPoint(
_to.getBBox(), _pos));
}
if (_rt && _rt.right()) {
_rt
.pos(myflow.util.center(_pos, _rt.right()
.pos()));
}
break;
case 'big' :
if (_rt && _rt.right() && _rt.right().type() == 'to') {
_rt.right().pos(myflow.util.connPoint(
_to.getBBox(), _pos));
}
if (_lt && _lt.left() && _lt.left().type() == 'from') {
_lt.left().pos(myflow.util.connPoint(_from
.getBBox(), _pos));
}
if (_rt && _rt.right()) {
_rt
.pos(myflow.util.center(_pos, _rt.right()
.pos()));
}
if (_lt && _lt.left()) {
_lt.pos(myflow.util.center(_pos, _lt.left().pos()));
}
// 三个大点在一条线上,移除中间的小点
var pos = {
x : _pos.x,
y : _pos.y
};
if (myflow.util.isLine(_lt.left().pos(), pos, _rt
.right().pos())) {
_t = 'small';
_n.attr(_o.attr.smallDot);
this.pos(pos);
var lt = _lt;
_lt.left().right(_lt.right());
_lt = _lt.left();
lt.remove();
var rt = _rt;
_rt.right().left(_rt.left());
_rt = _rt.right();
rt.remove();
// $('body').append('ok.');
}
break;
case 'small' :// 移动小点时,转变为大点,增加俩个小点
if (_lt && _rt && !myflow.util.isLine(_lt.pos(), {
x : _pos.x,
y : _pos.y
}, _rt.pos())) {
_t = 'big';
_n.attr(_o.attr.bigDot);
var lt = new dot('small', myflow.util.center(_lt
.pos(), _pos), _lt, _lt
.right());
_lt.right(lt);
_lt = lt;
var rt = new dot('small', myflow.util.center(_rt
.pos(), _pos), _rt.left(),
_rt);
_rt.left(rt);
_rt = rt;
}
break;
case 'to' :
if (_lt && _lt.left() && _lt.left().type() == 'from') {
_lt.left().pos(myflow.util.connPoint(_from
.getBBox(), _pos));
}
if (_lt && _lt.left()) {
_lt.pos(myflow.util.center(_pos, _lt.left().pos()));
}
break;
}
refreshpath();
};
}
function dotList() {
// if(!_from) throw '没有from节点!';
var _fromDot, _toDot, _fromBB = _from.getBBox(), _toBB = _to
.getBBox(), _fromPos, _toPos;
_fromPos = myflow.util.connPoint(_fromBB, {
x : _toBB.x + _toBB.width / 2,
y : _toBB.y + _toBB.height / 2
});
_toPos = myflow.util.connPoint(_toBB, _fromPos);
_fromDot = new dot('from', _fromPos, null, new dot('small', {
x : (_fromPos.x + _toPos.x) / 2,
y : (_fromPos.y + _toPos.y) / 2
}));
_fromDot.right().left(_fromDot);
_toDot = new dot('to', _toPos, _fromDot.right(), null);
_fromDot.right().right(_toDot);
// 转换为path格式的字串
this.toPathString = function() {
if (!_fromDot)
return '';
var d = _fromDot, p = 'M' + d.pos().x + ' ' + d.pos().y, arr = '';
// 线的路径
while (d.right()) {
d = d.right();
p += 'L' + d.pos().x + ' ' + d.pos().y;
}
// 箭头路径
var arrPos = myflow.util.arrow(d.left().pos(), d.pos(),
_o.attr.arrow.radius);
arr = 'M' + arrPos[0].x + ' ' + arrPos[0].y + 'L' + arrPos[1].x
+ ' ' + arrPos[1].y + 'L' + arrPos[2].x + ' '
+ arrPos[2].y + 'z';
return [p, arr];
};
this.toJson = function() {
var data = "[", d = _fromDot;
while (d) {
if (d.type() == 'big')
data += "{x:" + Math.round(d.pos().x) + ",y:"
+ Math.round(d.pos().y) + "},";
d = d.right();
}
if (data.substring(data.length - 1, data.length) == ',')
data = data.substring(0, data.length - 1);
data += "]";
return data;
};
this.restore = function(data) {
var obj = data, d = _fromDot.right();
for (var i = 0; i < obj.length; i++) {
d.moveTo(obj[i].x, obj[i].y);
d.moveTo(obj[i].x, obj[i].y);
d = d.right();
}
this.hide();
};
this.fromDot = function() {
return _fromDot;
};
this.toDot = function() {
return _toDot;
};
this.midDot = function() {// 返回中间点
var mid = _fromDot.right(), end = _fromDot.right().right();
while (end.right() && end.right().right()) {
end = end.right().right();
mid = mid.right();
}
return mid;
};
this.show = function() {
var d = _fromDot;
while (d) {
d.node().show();
d = d.right();
}
};
this.hide = function() {
var d = _fromDot;
while (d) {
d.node().hide();
d = d.right();
}
};
this.remove = function() {
var d = _fromDot;
while (d) {
if (d.right()) {
d = d.right();
d.left().remove();
} else {
d.remove();
d = null;
}
}
};
}
// 初始化操作
_o = $.extend(true, _o, o);
_path = _r.path(_o.attr.path.path).attr(_o.attr.path);
_arrow = _r.path(_o.attr.arrow.path).attr(_o.attr.arrow);
_dotList = new dotList();
_dotList.hide();
_text = _r.text(0, 0, _o.text.text||_o.text.patten.replace('{from}', _from.text()).replace('{to}',
_to.text())).attr(_o.attr.text);
_text.drag(function(dx, dy) {
if (!myflow.config.editable)