-
Notifications
You must be signed in to change notification settings - Fork 11
/
demo.html
260 lines (191 loc) · 5.25 KB
/
demo.html
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
<!DOCTYPE>
<html>
<head>
<title>cytoscape-automove.js demo</title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
<script src="https://unpkg.com/cytoscape/dist/cytoscape.min.js"></script>
<!-- for testing with local version of cytoscape.js -->
<!--<script src="../cytoscape.js/build/cytoscape.js"></script>-->
<script src="cytoscape-automove.js"></script>
<style>
body {
font-family: helvetica neue, helvetica, liberation sans, arial, sans-serif;
font-size: 14px;
}
#cy {
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
z-index: 999;
}
h1 {
opacity: 0.5;
font-size: 1em;
font-weight: bold;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function(){
var cy = window.cy = cytoscape({
container: document.getElementById('cy'),
layout: {
name: 'preset'
},
style: [
{
selector: 'node',
style: {
'label': 'data(id)'
}
},
{
selector: '.mid',
style: {
'width': 8,
'height': 8,
'label': ''
}
}
],
elements: [
{ data: { id: 'a' } },
{ data: { id: 'b' } },
{ data: { id: 'c' } },
{ data: { id: 'mid' }, classes: 'mid' },
{ data: { source: 'a', target: 'mid' } },
{ data: { source: 'b', target: 'mid' } },
{ data: { source: 'mid', target: 'c' } },
{ data: { id: 'd' } },
{ data: { id: 'e' } },
{ data: { id: 'f' } },
{ data: { source: 'e', target: 'f' } },
{ data: { id: 'g' } },
{ data: { id: 'h' } },
{ data: { id: 'i' } },
{ data: { source: 'h', target: 'i' } },
{ data: { id: 'j' } },
{ data: { id: 'k' } },
{ data: { id: 'm' } },
{ data: { id: 'n' } },
{ data: { source: 'k', target: 'm' } },
{ data: { source: 'k', target: 'n' } },
{ data: { source: 'm', target: 'n' } },
]
});
// cy.on('automove', function( evt ){
// var target = evt.target || evt.cyTarget; // 3.x || 2.x
//
// console.log('automove event on %s', target.id());
// });
// a, b, c; with mid in the middle
cy.$('#a, #b, #c').makeLayout({
name: 'circle',
boundingBox: {
x1: 0,
y1: 0,
x2: 300,
y2: 300
}
}).run();
cy.automove({
nodesMatching: cy.$('#mid'),
reposition: 'mean',
meanOnSelfPosition: function( node ){ return false; }
});
// dragging mid drags its neighbourhood with it
cy.automove({
nodesMatching: cy.$('#mid').neighbourhood().nodes(),
reposition: 'drag',
dragWith: cy.$('#mid')
});
// d can't go out of a box
cy.automove({
nodesMatching: cy.$('#d'),
reposition: { x1: 350, x2: 450, y1: 100, y2: 200 }
});
cy.$('#d').position({ x: 400, y: 150 });
// e & f have the same y
var eAndF = cy.$('#e, #f');
eAndF.makeLayout({
name: 'grid',
boundingBox: { x1: 0, x2: 300, y1: 300, y2: 400 },
cols: 2,
fit: false
}).run();
cy.automove({
nodesMatching: cy.$('#e, #f'),
reposition: function( node ){
var pos = node.position();
if( node.grabbed() ){ return pos; }
var otherNode = eAndF.not( node );
return {
x: pos.x,
y: otherNode.position('y')
};
},
when: 'matching'
});
// g kept in viewport
cy.$('#g').position({ x: 400, y: 350 });
cy.fit( 100 ); // make sure g is in the viewport for the demo
cy.automove({
nodesMatching: cy.$('#g'),
reposition: 'viewport'
});
// i gets pulled along with h on drag
cy.$('#h').position({ x: 585, y: 195 });
cy.$('#i').position({ x: 510, y: 260 });
cy.automove({
nodesMatching: cy.$('#i'),
reposition: 'drag',
dragWith: cy.$('#h')
});
// j can't go in the box of d
cy.$('#j').position({ x: 585, y: 350 });
cy.automove({
nodesMatching: cy.$('#j'),
reposition: { type: 'outside', x1: 350, x2: 450, y1: 100, y2: 200 }
});
// k, m, n all move together on drag as a unit (e.g. cluster)
cy.$('#k').position({ x: 430, y: -20 });
cy.$('#m').position({ x: 490, y: -110 });
cy.$('#n').position({ x: 550, y: -20 });
cy.automove({
nodesMatching: cy.$('#k, #m, #n'),
reposition: 'drag',
dragWith: cy.$('#k, #m, #n')
});
cy.fit( 100 ); // fit to all the layouts
// .automove-viewport nodes kept in viewport (even if added after this call)
// convenient but less performant than `nodesMatching: collection`
cy.automove({
nodesMatching: '.automove-viewport',
reposition: 'viewport'
});
cy.on('tap', function( evt ){
var tgt = evt.target || evt.cyTarget; // 3.x || 2.x
if( tgt === cy ){
cy.add({
classes: 'automove-viewport',
data: { id: 'new' + Math.round( Math.random() * 100 ) },
position: {
x: evt.position.x,
y: evt.position.y
}
});
}
});
cy.on('cxttap', 'node', function( evt ){
var tgt = evt.target || evt.cyTarget; // 3.x || 2.x
tgt.remove();
});
});
</script>
</head>
<body>
<h1>cytoscape-automove demo</h1>
<div id="cy"></div>
</body>
</html>