-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.js
More file actions
149 lines (122 loc) · 4.37 KB
/
state.js
File metadata and controls
149 lines (122 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
jsPlumb.ready(function () {
// setup some defaults for jsPlumb.
var instance = jsPlumb.getInstance({
Endpoint: ["Dot", {radius: 2}],
Connector:"StateMachine",
HoverPaintStyle: {strokeStyle: "#1e8151", lineWidth: 5 },
ConnectionOverlays: [
[ "Arrow", {
location: 1,
id: "arrow",
length: 14,
foldback: 0.8
} ],
[ "Label", {
id: "label",
cssClass: "aLabel"
}]
],
Container: "state-canvas"
});
instance.registerConnectionType("basic", { anchor:"Continuous", connector:"StateMachine" });
window.jsp = instance;
var stateCanvas = document.getElementById("state-canvas");
var windows = jsPlumb.getSelector(".statemachine-demo .w");
//
// initialise element as connection targets and source.
//
var initNode = function(el) {
// initialise draggable elements.
instance.draggable(el);
instance.makeSource(el, {
filter: ".ep",
anchor: "Continuous",
connectorStyle: { strokeStyle: "#5c96bc", lineWidth: 2, outlineColor: "transparent", outlineWidth: 4 },
connectionType:"basic",
extract:{
"action":"the-action"
},
/*maxConnections: 2,
onMaxConnections: function (info, e) {
alert("Maximum connections (" + info.maxConnections + ") reached");
}*/
});
instance.makeTarget(el, {
dropOptions: { hoverClass: "dragHover" },
anchor: "Continuous",
allowLoopback: true
});
// this is not part of the core demo functionality; it is a means for the Toolkit edition's wrapped
// version of this demo to find out about new nodes being added.
//
instance.fire("jsPlumbDemoNodeAdded", el);
};
getAns = function() {
/*$('#myModal').modal('show');*/
let ret = solve();
let convertState = function( st , size ) {
let a = '';
while(st > 0) {
a += '' + (st % 2);
st = st >> 1;
}
while(a.length < size) {
a += '0';
}
return a;
};
let createNode = function( state_num ) { // create nodes in clockwise
let center = { x: 500, y: 300 };
let radius = 200;
// create Node with id="state[id]" & class="w"
for( var i = 0 ; i < (state_num) ; i++ ) {
var st = document.createElement( 'div' );
st.className = 'w';
st.id = 'state' + call + '-' + i;
st.innerHTML = convertState( i, n_reg ); // ep ?
let theta = 2*Math.PI*i/(state_num);
st.style.left = (center.x + radius*Math.sin( theta )) + 'px';
st.style.top = (center.y + radius*Math.cos( theta )) + 'px';
instance.getContainer().appendChild(st);
initNode(st);
}
};
createNode( ret.length );
for( var reg = 0 ; reg < (ret.length) ; reg++ ) { // reg_status
for( var inp = 0 ; inp < ret[reg].length ; inp++ ) { // input_status
instance.connect( {
source: "state" + call + '-' + reg ,
target: "state" + call + '-' + ret[reg][inp][0] ,
type:"basic" ,
overlays:[
[ "Label", {
label:"IN: " + convertState( inp , n_in ) + ' | OUT: ' + convertState( ret[reg][inp][1] , n_out ),
location:0.25 + 0.15*Math.random(),
id:"myLabel",
cssClass: "aLabel" } ]
],
} );
}
}
let order = '';
for( var gate of allGates ) {
let gt = gate.split('-')[0];
if(['JK','RS','T','D'].indexOf( gt ) >= 0)
order += (gate + ' > ');
}
order = order.substring(0, order.length -2);
$('#output').text(order);
call += 1;
};
clearStates = function() {
jsPlumb.detachEveryConnection();
jsPlumb.deleteEveryEndpoint();
jsPlumb.empty('state-canvas');
}
// suspend drawing and initialise.
instance.batch(function () {
});
jsPlumb.fire("jsPlumbDemoLoaded", instance);
});
var getAns, clearStates;
var call = 0;