-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.js
1191 lines (982 loc) · 39.1 KB
/
lib.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 reset(ar)
{
for (var i in ar) return ar[i];
};
// If current editor is set to edit State Machines, return valSM
// If current editor is set to edit Procedures, return valPR
// If current editor is set to edit something else, print message
function editorSwitch(valSM, valPR, eType)
{
if (!eType) eType=g.fwprop.editorType;
switch (eType)
{
case 'Sm':
return valSM;
case 'Pr':
return valPR;
default:
msg("Current editor type is not set properly, that may indicate an internal application error. This should never happen. Further work may lead to undefined results.","error");
return;
}
}
// return true if editor is set to SM or PR respectively
function editorIsSM(eType) { return editorSwitch(true,false,eType); }
function editorIsPR(eType) { return editorSwitch(false,true,eType); }
function htmlspecialchars(text)
{
text=text+"";
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, function(m) { return map[m]; }); //"
}
function extraSpacing(state,rad)
{
// this crazy stuff is just a quick approximation, needed to make the transition arrow to float nicely around choice pseudo state
if (state.fwprop && state.fwprop.type=='choice') return 10-40*Math.abs(Math.sqrt(Math.abs(Math.sin(rad)*Math.cos(rad)))*Math.sin(rad)*Math.cos(rad));
else return 0;
}
// returns an array of transition coordinates from the start to the end including intermediate vertexes
function full_coordinates(conn)
{
var coord=[];
var i=0;
var spacing=5;
var arrowsize=3;
var start,end,rad;
var s1=conn.stateFrom;
var s2=conn.stateTo;
var x1=s1.attr("x")-spacing,
y1=s1.attr("y")-spacing,
x2=s2.attr("x")-spacing-arrowsize,
y2=s2.attr("y")-spacing-arrowsize,
w1=s1.attr("width")+2*spacing,
h1=s1.attr("height")+2*spacing,
w2=s2.attr("width")+2*spacing+2*arrowsize,
h2=s2.attr("height")+2*spacing+2*arrowsize;
if (conn.movingCoord && !$.isEmptyObject(conn.movingCoord))
{
if (conn.movingEnd)
{
x2=conn.movingCoord.x;
y2=conn.movingCoord.y;
w2=0;
h2=0;
}
else
{
x1=conn.movingCoord.x;
y1=conn.movingCoord.y;
w1=0;
h1=0;
}
}
if (conn.vertexes.length>0)
{
rad=Raphael.rad(Raphael.angle(x1+w1/2,y1+h1/2,conn.vertexes[0].x,conn.vertexes[0].y));
start = angle_spot(x1 - extraSpacing(s1,rad), y1 - extraSpacing(s1,rad), w1 + 2*extraSpacing(s1,rad), h1 + 2*extraSpacing(s1,rad), (rad + Math.PI) % (2 * Math.PI));
rad=Raphael.rad(Raphael.angle(conn.vertexes[conn.vertexes.length-1].x,conn.vertexes[conn.vertexes.length-1].y,x2+w2/2,y2+h2/2));
end = angle_spot(x2 - extraSpacing(s2,rad), y2 - extraSpacing(s2,rad), w2 + 2*extraSpacing(s2,rad), h2 + 2*extraSpacing(s2,rad), rad);
}
else
{
// if we are not using any vertexes, join objects from centers if they overlap or are too close
if ( (x2 >= x1 && x2 <= x1+w1 && y2 >= y1 && y2 <= y1+h1) ||
(x2+w2 >= x1 && x2+w2 <= x1+w1 && y2 >= y1 && y2 <= y1+h1) ||
(x2+w2 >= x1 && x2+w2 <= x1+w1 && y2+h2 >= y1 && y2+h2 <= y1+h1) ||
(x2 >= x1 && x2 <= x1+w1 && y2+h2 >= y1 && y2+h2 <= y1+h1) ||
(x1 >= x2 && x1 <= x2+w2 && y1 >= y2 && y1 <= y2+h2) ||
(x1+w1 >= x2 && x1+w1 <= x2+w2 && y1 >= y2 && y1 <= y2+h2) ||
(x1+w1 >= x2 && x1+w1 <= x2+w2 && y1+h1 >= y2 && y1+h1 <= y2+h2) ||
(x1 >= x2 && x1 <= x2+w2 && y1+h1 >= y2 && y1+h1 <= y2+h2) )
{
x1=x1+w1/2; y1=y1+h1/2; x2=x2+w2/2; y2=y2+h2/2;
w1=1; h1=1; w2=1; h2=1;
}
rad=Raphael.rad(Raphael.angle(x1+w1/2,y1+h1/2,x2+w2/2,y2+h2/2));
start = angle_spot(x1 - extraSpacing(s1,rad), y1 - extraSpacing(s1,rad), w1 + 2*extraSpacing(s1,rad), h1 + 2*extraSpacing(s1,rad), (rad + Math.PI) % (2 * Math.PI));
end = angle_spot(x2 - extraSpacing(s2,rad), y2 - extraSpacing(s2,rad), w2 + 2*extraSpacing(s2,rad), h2 + 2*extraSpacing(s2,rad), rad);
}
coord.push({x: start[0], y: start[1]});
for (i=0; i<conn.vertexes.length; i++) coord.push({x: conn.vertexes[i].x, y: conn.vertexes[i].y});
coord.push({x: end[0], y: end[1]});
return coord;
}
function arrow_path_string()
{
return "M10,-1L0,5L10,11L7,6L7,4z"
}
// calculate spot on element which is going to be starting / ending point of transition path
function angle_spot(x, y, w, h, rad)
{
var cang = Math.atan(h/w);
var dxy = new Array(2);
if (rad > 2*Math.PI-cang || rad < cang)
{
dxy[0] = x + w;
dxy[1] = y + h/2 + Math.tan(rad) * w/2;
}
else if (rad >= cang && rad <= (Math.PI - cang))
{
dxy[0] = x + w/2 + Math.tan(Math.PI / 2 - rad) * h/2;
dxy[1] = y + h;
}
else if (rad > (Math.PI - cang) && rad < (Math.PI + cang))
{
dxy[0] = x;
dxy[1] = y + h/2 - Math.tan(rad) * w/2;
}
else
{
dxy[0] = x + w/2 - Math.tan(Math.PI/2 - (rad - Math.PI)) * h/2;
dxy[1] = y;
}
return dxy;
}
// ------------------------------------
// functions for calculation of length on transtition arrow on mouseover
// ------------------------------------
// linear algebra projection needs a line which is 90 degrees rotated of the original direction
function rotateOrigin(x,y,angle)
{
var rotxy = new Array();
var radangle = Raphael.rad(angle);
rotxy[0] = Math.cos(radangle)*x+Math.sin(radangle)*y;
rotxy[1] = Math.cos(radangle)*y-Math.sin(radangle)*x;
return rotxy;
}
// returns best approximation of length of given zigzag line (defined by dots) to the x/y mouse coordinates
function getLengthAtPoint(dots, x, y)
{
var diff = 10000;
var xsect_d = new Array();
var length = 0;
var length_und = 0;
for(var n = 0; n < dots.length-1; n++)
xsect_d[n] = new Array();
for(var a = 0; a < dots.length-1; a++)
{
var angle = Raphael.angle(dots[a+1].x,dots[a+1].y,dots[a].x,dots[a].y);
var pt1 = rotateOrigin(dots[a].x,dots[a].y,angle);
var pt2 = rotateOrigin(x,y,angle);
var xsect = rotateOrigin(pt2[0],pt1[1],-1*angle);
if(!(Math.max(dots[a].x,dots[a+1].x) +.1 >= xsect[0] && Math.min(dots[a].x,dots[a+1].x) -.1 <= xsect[0] && Math.max(dots[a].y,dots[a+1].y) +.1 >= xsect[1] && Math.min(dots[a].y,dots[a+1].y) -.1 <= xsect[1]))
{
var dist1 = Math.sqrt(Math.pow(dots[a].x-x,2)+Math.pow(dots[a].y-y,2));
var dist2 = Math.sqrt(Math.pow(dots[a+1].x-x,2)+Math.pow(dots[a+1].y-y,2));
if(dist1 < dist2)
{
xsect[0] = dots[a].x;
xsect[1] = dots[a].y;
}
else
{
xsect[0] = dots[a+1].x;
xsect[1] = dots[a+1].y;
}
}
xsect_d[a][0] = xsect[0];
xsect_d[a][1] = xsect[1];
xsect_d[a][2] = Math.sqrt(Math.pow(xsect[0]-x,2)+Math.pow(xsect[1]-y,2));
}
for(var t = 0; t < dots.length-1; t++)
{
length_und += Math.sqrt(Math.pow(dots[t].x-xsect_d[t][0],2)+Math.pow(dots[t].y-xsect_d[t][1],2));
if(xsect_d[t][2] < diff)
{
length += length_und;
length_und = 0;
diff = xsect_d[t][2];
}
length_und += Math.sqrt(Math.pow(dots[t+1].x-xsect_d[t][0],2)+Math.pow(dots[t+1].y-xsect_d[t][1],2));
}
return length;
}
function str_repeat(s,n)
{
// way faster than for() cycle
return new Array(Math.floor(n+1)).join(s);
}
// -------------------------------------------
//
// new state element
//
// -------------------------------------------
function newstate(type,x,y)
{
var n,m; var bbox; end=false;
x+=g.paperPanX;
y+=g.paperPanY;
var nextID=getNextStateAutoID(type);
switch (type)
{
case "state":
n=g.states.push(paper.rect(x,y, 100, editorSwitch(100,30), 10).attr({fill: "#fff", "stroke-width": 1, "stroke": "#666", "fill-opacity": 100 }).transform("t0.5,0.5"));
break;
case "choice":
n=g.states.push(paper.rect(x,y, editorSwitch(30,24),editorSwitch(30,24)).attr({fill: "#fff", "stroke-width": 1, "stroke": "#666", "fill-opacity": 100 }).transform("r45t0.5,0.5"));
break;
case "init":
n=g.states.push(paper.rect(x,y,30,30,15).attr({fill: "#fff", "stroke-width": 1, "stroke": "#666", "fill-opacity": 100 }).transform("t0.5,0.5"));
break;
case "final":
n=g.states.push(paper.rect(x,y,26,26,13).attr({fill: "#000", "stroke-width": 1, "stroke": "#000", "fill-opacity": 100 }).transform("t0.5,0.5"));
break;
case "note":
n=g.states.push(paper.rect(x,y, 100, 33, 0).attr({fill: "#fff", "stroke-width": 1, "stroke": "#666", "fill-opacity": 100, 'stroke-dasharray': '--' }).transform("t0.5,0.5"));
break;
case "notedot":
n=g.states.push(paper.rect(x,y,6,6,6).attr({fill: "#000", "stroke-width": 1, "stroke": "#000", "fill-opacity": 100 }).transform("t0.5,0.5"));
}
g.states[n-1].fwprop={};
g.states[n-1].fwprop.autoid=nextID;
g.states[n-1].fwprop.type=type;
g.states[n-1].fwprop.note="";
g.states[n-1].parentState=false;
switch (type)
{
case "state":
g.states[n-1].text=paper.text(x,y, "").attr({"font-size": 10, "cursor": "default", "text-anchor":"start"});
g.states[n-1].text.parent=g.states[n-1];
g.states[n-1].text.drag(statedragmove,statedragstart,statedragup);
g.states[n-1].text.dblclick(editboxOn);
if (editorIsSM())
{
g.states[n-1].underline=paper.path("M0,0").attr({"fill":"#000"});
g.states[n-1].underline.parent=g.states[n-1];
g.states[n-1].underline.drag(statedragmove,statedragstart,statedragup);
g.states[n-1].underline.dblclick(editboxOn);
}
g.states[n-1].fwprop.identifier=editorSwitch("STATE","NODE")+nextID;
g.states[n-1].fwprop.entryFunc="";
g.states[n-1].fwprop.doFunc="";
g.states[n-1].fwprop.exitFunc="";
g.states[n-1].fwprop.entryType="function";
g.states[n-1].fwprop.doType="function";
g.states[n-1].fwprop.exitType="function";
g.states[n-1].fwprop.entryCode="";
g.states[n-1].fwprop.doCode="";
g.states[n-1].fwprop.exitCode="";
g.states[n-1].fwprop.entryDesc="";
g.states[n-1].fwprop.doDesc="";
g.states[n-1].fwprop.exitDesc="";
updateStateText(g.states[n-1]);
break;
case "choice":
g.states[n-1].fwprop.identifier=editorSwitch("CHOICE","DECISION")+nextID;
break;
case "init":
break;
case "final":
break;
case "note":
g.states[n-1].text=paper.text(x,y, "").attr({"font-size": 10, "cursor": "default", "text-anchor":"start"});
g.states[n-1].text.parent=g.states[n-1];
g.states[n-1].text.drag(statedragmove,statedragstart,statedragup);
g.states[n-1].text.dblclick(editboxOn);
updateStateText(g.states[n-1]);
break;
}
g.states[n-1].drag(statedragmove,statedragstart,statedragup);
g.states[n-1].dblclick(editboxOn);
g.states[n-1].mousemove(statemousemove);
return g.states[n-1];
}
function addNoteConnector()
{
if (g.selected.length!=1) return;
var state=g.selected[0];
if (!stateIsNote(state)) return;
var dot=newstate('notedot',state.attr("x")+Math.floor(Math.random()*state.attr("width")),state.attr("y")+state.attr("height")+50+Math.floor(Math.random()*50));
connect(state,dot);
}
function filterOutRemovedStates() // remove from states array all empty (deleted) states
{
var i;
for (i=0; i<g.states.length; i++)
{
if (g.states[i].removed)
g.states.splice(i--,1);
}
}
function getConnectionsIndex(obj)
{
var i;
for (i=0; i<g.connections.length; i++)
if (g.connections[i].id===obj.id) return i;
return false;
}
function destroySelectors()
{
if (g.connector) { g.connector.remove(); g.connector=false; }
if (g.connectorArrow) { g.connectorArrow.arrowend.remove(); g.connectorArrow.remove(); g.connectorArrow=false; }
if (g.enlarger) { g.enlarger.remove(); g.enlarger=false; }
}
function deleteConnection(i)
{
reorderTransitions(g.connections[i].stateFrom, g.connections[i].fwprop.order, countOutgoingTransitions(g.connections[i].stateFrom) );
g.connections[i].arrowend.remove();
g.connections[i].text.remove();
g.connections[i].remove();
g.connections.splice(i,1);
g.selectedCon=false;
}
// function optimized to delete all states and connections at once
function deleteAllStates()
{
var i;
for(i=0;i<g.states.length;i++)
{
if (g.states[i].text) g.states[i].text.remove();
if (g.states[i].underline) g.states[i].underline.remove();
g.states[i].remove();
}
for(i=0;i<g.connections.length;i++)
{
g.connections[i].text.remove();
g.connections[i].arrowend.remove();
g.connections[i].remove();
}
g.states=[];
g.connections=[];
g.selected=[];
g.selectedCon=false;
}
function deleteState(state)
{
// make sure there is nothing left
destroySelectors();
var dots=findConnectionTargets(state);
var i;
for (i=0; i<g.connections.length; i++)
if (g.connections[i].stateFrom.id===state.id || g.connections[i].stateTo.id===state.id)
deleteConnection(i--);
if (state.text) state.text.remove();
if (state.underline) state.underline.remove();
if (stateIsNote(state) && dots.length>0)
for (i=0; i<dots.length; i++)
dots[i].remove();
state.remove();
filterOutRemovedStates();
refreshConnections();
}
function deleteSelectedStates()
{
var i;
for (i=0; i<g.selected.length; i++)
deleteState(g.selected[i]);
g.selected=[];
refreshToolbars();
}
function deleteSelectedConnection()
{
if (g.selectedCon)
{
if (stateIsNoteDot(g.selectedCon.stateTo)) deleteState(g.selectedCon.stateTo);
else deleteConnection(getConnectionsIndex(g.selectedCon));
}
refreshConnections();
refreshToolbars();
}
function connectionIndex(s1,s2)
{
var i;
for (i=0; i<g.connections.length; i++)
{
if (g.connections[i].stateFrom.id===s1.id && g.connections[i].stateTo.id===s2.id)
return i;
}
return false;
}
function isConnected(s1,s2)
{
return connectionIndex(s1,s2)!==false;
}
function findConnectionSource(target)
{
var i;
for (i=0; i<g.states.length; i++)
if (isConnected(g.states[i],target))
return g.states[i];
return false;
}
function findConnectionTargets(source)
{
var i;
var ret=[];
for (i=0; i<g.states.length; i++)
if (isConnected(source,g.states[i]))
ret.push(g.states[i]);
return ret;
}
function connectionsCountFrom(state)
{
var i, res=0;
for (i=0; i<g.connections.length; i++)
if (g.connections[i].stateFrom.id==state.id) res++;
return res;
}
// -------------------------------------------
// connect one state with other one by an arrow
// -------------------------------------------
function connect(s1,s2,tshiftx,tshifty,verts,sh)
{
if (editorIsSM())
{
if (s2.fwprop.type=='init') { msg("Initial state doesn't accept incomming transitions","error"); return false; }
if (s2.fwprop.type==s1.fwprop.type && s1.fwprop.type=='choice') { msg("The source and target of a transition cannot both be choice pseudo-states","error"); return false; }
if (s1.fwprop.type=='init' && connectionsCountFrom(s1)>0) { msg("Only one transition is allowed from initial state","error"); return false; }
if (s1.fwprop.type=='init' && s2.fwprop.type=='final') { msg("Transition from initial to final state makes no sense","error"); return false; }
}
if (editorIsPR())
{
if (s2.fwprop.type=='init') { msg("Initial node doesn't accept incomming control flow","error"); return false; }
if (s1.fwprop.type=='init' && connectionsCountFrom(s1)>0) { msg("Only one control flow is allowed from initial node","error"); return false; }
if (s1.fwprop.type=='state' && connectionsCountFrom(s1)>0) { msg("Only one control flow is allowed from action node","error"); return false; }
if (s1.fwprop.type=='init' && s2.fwprop.type=='final') { msg("Transition from initial to final node makes no sense","error"); return false; }
}
var order=getNextOrderID(s1);
var m=g.connections.push(paper.path("M0,0").attr({"stroke-width": 2, "stroke-linecap": "round", "stroke-linejoin": "round", color: '#000'})); // dummy path, will be updated later
g.connections[m-1].stateFrom=s1;
g.connections[m-1].stateTo=s2;
g.connections[m-1].coord=[]; // last known coordinates for full transition path, in {x,y} pairs
if (!verts) verts=[];
g.connections[m-1].vertexes=verts; // relative coordinates of points through which the arrow passes, array of {x,y} pairs
if (!sh) sh={x:0,y:0};
g.connections[m-1].shiftxy=sh; // relative shift of connection arrow
g.connections[m-1].fwprop={};
g.connections[m-1].fwprop.order=order;
g.connections[m-1].fwprop.identifier= editorSwitch( s1.fwprop.type=='state' ? "Trigger"+getNextTriggerAutoID() : "" ,"");
g.connections[m-1].fwprop.guardFunc = editorSwitch( s1.fwprop.type=='choice' ? 'Guard'+getNextGuardAutoID() : "" ,"");
g.connections[m-1].fwprop.actionFunc= editorSwitch( s1.fwprop.type=='init' ? 'Action'+getNextActionAutoID() : "" ,"");
g.connections[m-1].fwprop.guardType="function";
g.connections[m-1].fwprop.actionType="function";
g.connections[m-1].fwprop.guardCode="";
g.connections[m-1].fwprop.actionCode="";
// make connection's label somewhere on the paper, coordinates will update later
g.connections[m-1].shiftx= tshiftx ? tshiftx : 0;
g.connections[m-1].shifty= tshifty ? tshifty : 0;
g.connections[m-1].arrowend=paper.path(arrow_path_string()).attr({color: '#000', fill: '#000'});
g.connections[m-1].arrowend.parentPath=g.connections[m-1];
g.connections[m-1].arrowend.mousemove(mouseOverArrow);
g.connections[m-1].arrowend.drag(shifterdragmove,shifterdragstart,shifterdragend);
g.connections[m-1].arrowend.dblclick(editboxOn);
g.connections[m-1].text=paper.text(0,0,"").attr({"cursor": "move"});
g.connections[m-1].text.parent=g.connections[m-1];
g.connections[m-1].drag(labeldragmove,labeldragstart,labeldragup); // to handle click for selection
g.connections[m-1].dblclick(editboxOn);
g.connections[m-1].mouseover(mouseOverArrow);
g.connections[m-1].mousemove(mouseOverArrow);
g.connections[m-1].text.drag(labeldragmove,labeldragstart,labeldragup);
g.connections[m-1].text.dblclick(editboxOn);
// now update the connection on screen
refreshConnection(m-1);
return g.connections[m-1];
}
function selfConnect(state)
{
var i;
if (!state)
{
for(i=0;i<g.selected.length;i++)
if (g.selected[i].fwprop.type=='state')
{
state=g.selected[i];
break;
}
}
if (!state) return;
var X=state.attr("x"),
Y=state.attr("y");
connect(state,state,20,-15,[{x:X-20,y:Y+10},{x:X+10,y:Y-20}]);
}
function refreshBackground()
{
paper.setViewBox(g.paperPanX,g.paperPanY, paper.width*g.zoom, paper.height*g.zoom, false);
g.bg.attr({x: g.paperPanX, y: g.paperPanY, width: paper.width, height: paper.height});
}
function paperScroll(e)
{
var delta = e.type=="mousewheel" ? e.originalEvent.wheelDelta : -e.originalEvent.detail;
g.paperPanY -= delta > 0 ? 60 : -60;
refreshBackground();
}
function doZoom(zoom,x,y)
{
g.paperPanX-=x-x*g.zoom;
g.paperPanY-=y-y*g.zoom;
g.zoom=zoom;
g.paperPanX+=x-x*g.zoom;
g.paperPanY+=y-y*g.zoom;
if (g.selectionbox) g.selectionbox.attr({opacity: zoom/8});
refreshBackground();
}
function snap_area(snapx,snapy, reqcx,reqcy, snap)
{
if (!snap) snap=10;
var x=0,y=0;
if (Math.abs(snapx-reqcx)<snap) x=snapx-reqcx;
if (Math.abs(snapy-reqcy)<snap) y=snapy-reqcy;
return [x,y];
}
function build_path_string(dots)
{
var path = "";
for (var i=0; i<dots.length;i++) path = path + (i<1 ? "M" : "L") + dots[i].x + "," + dots[i].y;
return path;
}
function lastPoint(coord,dx,dy)
{
return (coord[coord.length-1].x+dx)+","+(coord[coord.length-1].y+dy);
}
function lastPointAngle(coord)
{
return Raphael.angle(coord[coord.length-2].x,coord[coord.length-2].y,coord[coord.length-1].x,coord[coord.length-1].y);
}
function window_open(url,id,forceExternal)
{
if (isChromeApp() && !forceExternal)
{
// open help in a single window all the time
if (id=='help')
{
chrome.app.window.create(url,{"id":"help","bounds":{"width":window.screen.availWidth/2,"height":window.screen.availHeight/2}},
function(w) { if (w.contentWindow.gotoHash) w.contentWindow.gotoHash(url.replace(/.*#/,"#")); }
);
return;
}
// check if a window with given id hash already exists
var allWindows=chrome.app.window.getAll();
for(var i=0; i<allWindows.length; i++)
if (allWindows[i].contentWindow.location.hash==url)
return allWindows[i].show();
// if we are so far, document is not open yet. Open it now.
return chrome.app.window.create((url.match("/")?url:"index.html"+url),{"id":"w"+(new Date).getTime(),"bounds":{"width":window.screen.availWidth,"height":window.screen.availHeight}});
}
else
return window.open(url,id);
}
function openHelp(hash)
{
window_open('./doc/'+(offline()?"index.html":"")+(hash?"#"+hash:"#"),'help');
}
function deleteKeyPressed()
{
historyAddPrepare();
deleteSelectedStates();
deleteSelectedConnection();
historyAddFinish();
refreshToolbars();
}
function updateConnectionText(conn, color)
{
if (!conn.fwprop) return;
if (stateIsNote(conn.stateFrom)) return;
stereotypeAp = svgDoubleLt()+"AP"+svgDoubleGt()+" ";
sStereotypeApGuard = conn.fwprop.guardAp?stereotypeAp:"";
sStereotypeApAction = conn.fwprop.actionAp?stereotypeAp:"";
var text= (conn.fwprop.identifier!='' ? conn.fwprop.identifier : "") +
( (conn.fwprop.guardFunc && conn.fwprop.guardFunc!='')
|| (conn.fwprop.guardCode && conn.fwprop.guardCode!='')
|| (conn.fwprop.guardDesc && conn.fwprop.guardDesc!='') ? " [ "+sStereotypeApGuard+svgNewlines(displayInfoText(conn.fwprop.guardFunc?conn.fwprop.guardFunc:conn.fwprop.guardCode,conn.fwprop.guardDesc))+" ] " : "");
text = text + ( (conn.fwprop.actionFunc && conn.fwprop.actionFunc!='')
|| (conn.fwprop.actionCode && conn.fwprop.actionCode!='')
|| (conn.fwprop.actionDesc && conn.fwprop.actionDesc!='') ? " / " + sStereotypeApAction+svgNewlines(displayInfoText(conn.fwprop.actionFunc?conn.fwprop.actionFunc:conn.fwprop.actionCode,conn.fwprop.actionDesc)) : "");
if (countOutgoingTransitions(conn.stateFrom)>1 && g.fwprop.displayOrder) text = "" + conn.fwprop.order+": "+text;
if (conn.text)
{
if (color) conn.text.attr({ "fill": color });
if (conn.text.str!=text) // update the text only if it differs, it's CPU intensive
{
conn.text.str=text;
conn.text.attr({ "text": text });
var tspans=conn.text.node.childNodes;
if (tspans.length>0) autocompleteTooltips(tspans);
}
}
}
function arrowend_refresh(conn)
{
conn.arrowend.transform("t"+lastPoint(conn.coord,0,-5)+"r"+lastPointAngle(conn.coord)+",0,5t-4.5,0");
}
function updateConnectionAttrs(conn, color)
{
var sx=0; var sy=0;
conn.coord=full_coordinates(conn);
// shift all coordinates
if (conn.shiftxy)
for (var i=0; i<conn.coord.length; i++)
{
conn.coord[i].x+=conn.shiftxy.x;
conn.coord[i].y+=conn.shiftxy.y;
}
conn.attr( { path: build_path_string(conn.coord) } );
arrowend_refresh(conn);
conn.attr({'stroke-dasharray': ""});
if (stateIsNote(conn.stateFrom))
{
conn.attr({'stroke-dasharray': "-"});
conn.arrowend.hide();
}
if (!color && conn.fwprop) color=conn.fwprop.color;
if (!color) color='#000';
conn.attr({stroke: color});
conn.arrowend.attr({stroke: color, fill: color});
updateConnectionText(conn,color);
}
function refreshConnection(i, color)
{
if (!g.connections || i===false || !g.connections[i]) return;
var border=2;
var shiftx=g.connections[i].shiftx ? g.connections[i].shiftx : 0;
var shifty=g.connections[i].shifty ? g.connections[i].shifty : 0;
updateConnectionAttrs(g.connections[i],color);
var middle=g.connections[i].getPointAtLength(g.connections[i].getTotalLength()/2);
var newx=Math.floor(middle.x + shiftx);
var newy=Math.floor(middle.y -10 + shifty);
if (g.connections[i].text.attr("x")!=newx || g.connections[i].text.attr("y")!=newy) // update only if differs
{
g.connections[i].text.attr({ x: newx, y: newy })
}
}
function updateTitle()
{
var t="FCP Editor for State Machines and Procedures";
if (g.fwprop.smName!='') t=g.fwprop.smName+" :: "+editorSwitch("State Machine","Procedure");
document.title=t;
}
function refreshConnections(obj)
{
for (var i=0; i<g.connections.length; i++)
{
if (!obj || g.connections[i].stateFrom.id===obj.id || g.connections[i].stateTo.id===obj.id)
refreshConnection(i);
}
}
function refreshState(state)
{
if (state.text) updateStateText(state);
if (g.connector) g.connector.attr({x:state.attr("x")+state.attr("width")+(state.attr("height")>40?-5:-15),y: state.attr("y")+(state.attr("height")>40?15:-5)});
if (g.enlarger) g.enlarger.transform("t"+(state.attr("x")+state.attr("width")-9-0.5)+","+(state.attr("y")+state.attr("height")+0.5));
}
function refreshStates()
{
for (var i=0; i<g.states.length; i++)
refreshState(g.states[i]);
}
function stateIsNote(state)
{
if (state.fwprop && state.fwprop.type=='note') return true;
return false;
}
function stateIsNoteDot(state)
{
if (state.fwprop && state.fwprop.type=='notedot') return true;
return false;
}
// return javascript character 160, which is unicode's nonbreaking blank space
function svgEmptyChar()
{
return String.fromCharCode(160);
}
function svgDoubleLt()
{
return "<<";
}
function svgDoubleGt()
{
return ">>";
}
function textPFX(text,prefix,suffix,nonbsp)
{
if (text && text.replace(/\s/g,'')!='') return (prefix?prefix:"")+text+(suffix?suffix:"");
else if (!nonbsp) return svgEmptyChar();
return "";
}
function svgNewlines(text,padchars)
{
if (padchars) padchars = new Array(padchars).join(svgEmptyChar()); else padchars='';
if (isEmpty(text)) return "";
return text.replace(/\n\n/g,svgEmptyChar()+"\n"+svgEmptyChar()+"\n").replace(/\n/g,"\n"+padchars);
}
function displayInfoText(fnc,desc)
{
if (g.fwprop.displayInfo==1) return isEmpty(fnc)?"":fnc;
else if (g.fwprop.displayInfo==2) return isEmpty(desc)?"":desc;
else
{
if (!isEmpty(desc)) return desc;
else if (!isEmpty(fnc)) return fnc;
}
return "";
}
function buildStateText(state)
{
if (stateIsNote(state)) return svgNewlines(state.fwprop.note);
stereotypeAp = svgDoubleLt()+"AP"+svgDoubleGt();
if (editorIsSM())
{
sStereotypeApEntry = state.fwprop.entryAp?stereotypeAp:svgEmptyChar();
sStereotypeApDo = state.fwprop.doAp?stereotypeAp:svgEmptyChar();
sStereotypeApExit = state.fwprop.exitAp?stereotypeAp:svgEmptyChar();
var ret=svgEmptyChar()+textPFX(state.fwprop.identifier)+svgEmptyChar()+"\n"
+svgEmptyChar("")+"\n"
+svgEmptyChar("")+"\n"
+textPFX(fileNameByID(state.fwprop.embedSmId),svgEmptyChar()+'eSM: ',"\n",true)
+textPFX(sStereotypeApEntry+displayInfoText(state.fwprop.entryFunc?state.fwprop.entryFunc:svgNewlines(state.fwprop.entryCode,15),svgNewlines(state.fwprop.entryDesc,15)),svgEmptyChar("")+'Entry: ',"\n",true)
+textPFX(sStereotypeApDo+displayInfoText(state.fwprop.doFunc?state.fwprop.doFunc:svgNewlines(state.fwprop.doCode,15),svgNewlines(state.fwprop.doDesc,15)),svgEmptyChar("")+'Do: ',"\n",true)
+textPFX(sStereotypeApExit+displayInfoText(state.fwprop.exitFunc?state.fwprop.exitFunc:state.fwprop.exitCode,svgNewlines(state.fwprop.exitDesc,15)),svgEmptyChar("")+'Exit: ',"\n",true);
return ret.replace(new RegExp("("+svgEmptyChar("")+"\n)+"+'$'), '');
}
if (editorIsPR())
{
sStereotype = state.fwprop.entryAp?stereotypeAp:svgEmptyChar();
return textPFX(state.fwprop.identifier)+": "+sStereotype
+textPFX(displayInfoText(state.fwprop.entryFunc?state.fwprop.entryFunc:state.fwprop.entryCode,svgNewlines(state.fwprop.entryDesc,2)),svgEmptyChar(),"",true);
}
}
function updateStateBoxByText(state,margin,onlyWidth)
{
var el=state.text;
var w=Math.floor(el.getBBox().width/2)*2;
var h=Math.floor(el.getBBox().height/2)*2;
if (w+margin>state.attr("width"))
{
state.attr({ "width": w+margin });
refreshConnections();
}
// onlyWidth is needed to workaround some glitch when height is sometimes returned bigger on subsequent calls in one run
if (onlyWidth) return;
if (h+margin>state.attr("height"))
{
state.attr({ "height": h+margin });
refreshConnections();
}
return h;
}
function updateStateText(state,updateOnlyPosition)
{
var margin=20;
if (state.text)
{
var tspans=state.text.node.childNodes;
var y=$(tspans[0]).attr("dy");
var newtext=buildStateText(state);
if (!updateOnlyPosition)
{
// force pre-formated whitespace for Notes
if (stateIsNote(state)) $(state.text.node).css("white-space","pre");
if (state.text.str!=newtext)
{
state.text.str=newtext;
state.text.attr("text", newtext);
tspans=state.text.node.childNodes;
if (tspans)
{
$(tspans[0]).css("font-weight","bold");
y=$(tspans[0]).attr("dy");
autocompleteTooltips(tspans);
}
}
}
var h=updateStateBoxByText(state,margin);
state.text.attr({ x:state.attr("x")+(margin/2), y: state.attr("y")+(stateIsNote(state)?state.attr("height")/2:(h+margin)/2) });
var words=[];
var tspan;
if (tspans)
{
for (var i=0; i<tspans.length; i++)
{
tspan=tspans[i];
if (!$(tspan).text().match(/^.(Entry|Do|Exit|eSM):/)) continue;
words=$(tspan).text().split(" ");
if (words.length>1)
{
$(tspan).text(words.shift());
$(tspan).after($(tspan).clone().attr("dx",42).removeAttr("dy").text(words.join(" ")));
i++;
} else ($(tspan).next().attr("dy",0));
$(tspan).css("font-weight","bold");
}
$(tspans[0]).attr("dy",y);
}
updateStateBoxByText(state,margin,false);
}
if (state.underline)
{
state.underline.attr("path","M"+(state.attr("x")+(margin/2))+","+(state.attr("y")+33.5)+"l"+(state.attr("width")-margin)+",0");
if (state.attr("height")>40) state.underline.show(); else state.underline.hide();
}
}
// make sure that closest parents are updated
// accordingly to the current state.
function updateParents(states,parent)
{
var i,j;
if (!states) states=g.states;
if (!parent) parent=false;
for (i=0; i<states.length; i++)
states[i].parentState=parent;