-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathwidget.ts
411 lines (374 loc) · 12.7 KB
/
widget.ts
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
// Copyright (c) 2020, QuantStack, Mariana Meireles and ipycytoscape Contributors
//
// Distributed under the terms of the Modified BSD License.
//
// The full license is in the file LICENSE, distributed with this software.
import {
DOMWidgetModel,
DOMWidgetView,
ISerializers,
Dict,
} from '@jupyter-widgets/base';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const widgets = require('@jupyter-widgets/base');
import { MODULE_NAME, MODULE_VERSION } from './version';
// Import the CSS
import '../css/widget.css';
import cytoscape, { Core } from 'cytoscape';
// @ts-ignore
import cola from 'cytoscape-cola';
// @ts-ignore
import popper from 'cytoscape-popper';
// @ts-ignore
import Tippy, { Instance } from 'tippy.js';
// @ts-ignore
import dagre from 'cytoscape-dagre';
// @ts-ignore
import klay from 'cytoscape-klay';
import 'tippy.js/themes/material.css';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { NodeModel, EdgeModel } from './graph';
cytoscape.use(popper);
cytoscape.use(dagre);
cytoscape.use(klay);
cytoscape.use(cola);
export class CytoscapeModel extends DOMWidgetModel {
defaults() {
return {
...super.defaults(),
_model_name: CytoscapeModel.model_name,
_model_module: CytoscapeModel.model_module,
_model_module_version: CytoscapeModel.model_module_version,
_view_name: CytoscapeModel.view_name,
_view_module: CytoscapeModel.view_module,
_view_module_version: CytoscapeModel.view_module_version,
auto_unselectify: true,
box_selection_enabled: false,
cytoscape_layout: {},
cytoscape_style: [],
elements: [],
zoom: 0,
rendered_position: {},
tooltip_source: '',
graph: null,
};
}
initialize(attributes: any, options: any) {
super.initialize(attributes, options);
this.on('msg:custom', this.processMessage.bind(this));
}
static serializers: ISerializers = {
graph: { deserialize: widgets.unpack_models },
...DOMWidgetModel.serializers,
};
private processMessage(command: any, buffers: any) {
if (command.name === 'layout') {
this.forEachView((view) => {
view.cytoscape_obj.layout(this.get('cytoscape_layout')).run();
});
}
}
private forEachView(callback: (view: CytoscapeView) => void) {
for (const view_id in this.views) {
this.views[view_id].then((view: CytoscapeView) => {
callback(view);
});
}
}
static model_name = 'CytoscapeModel';
static model_module = MODULE_NAME;
static model_module_version = MODULE_VERSION;
static view_name = 'CytoscapeView';
static view_module = MODULE_NAME;
static view_module_version = MODULE_VERSION;
views: Dict<Promise<CytoscapeView>>;
}
export class CytoscapeView extends DOMWidgetView {
cytoscape_obj: Core;
is_rendered = false;
nodeViews: any = [];
edgeViews: any = [];
monitored: any = {};
render() {
this.el.classList.add('custom-widget');
this.displayed.then(() => {
this.init_render();
this.cytoscape_obj.startBatch();
this.nodeViews = new widgets.ViewList(
this.addNodeModel,
this.removeNodeView,
this
);
this.nodeViews.update(this.model.get('graph').get('nodes'));
this.edgeViews = new widgets.ViewList(
this.addEdgeModel,
this.removeEdgeView,
this
);
this.edgeViews.update(this.model.get('graph').get('edges'));
this.cytoscape_obj.endBatch();
this.cytoscape_obj
.elements()
.layout(this.model.get('cytoscape_layout'))
.run();
});
this.model
.get('graph')
.on_some_change(['nodes', 'edges'], this._updateViewLists, this);
//Python attributes that must be sync. with frontend
this.model.on('change:min_zoom', this._updateMinZoom, this);
this.model.on('change:max_zoom', this._updateMaxZoom, this);
this.model.on('change:zooming_enabled', this._updateZoomingEnabled, this);
this.model.on(
'change:user_zooming_enabled',
this._updateUserZoomingEnabled,
this
);
this.model.on('change:panning_enabled', this._updatePanningEnabled, this);
this.model.on(
'change:user_panning_enabled',
this._updateUserPanningEnabled,
this
);
this.model.on(
'change:box_selection_enabled',
this._updateBoxSelectionEnabled,
this
);
this.model.on('change:selection_type', this._updateSelectionType, this);
this.model.on('change:touch_tap_threshold', this.value_changed, this);
this.model.on('change:desktop_tap_threshold', this.value_changed, this);
this.model.on('change:autolock', this._updateAutolock, this);
this.model.on('change:auto_ungrabify', this._updateAutoUngrabify, this);
this.model.on('change:auto_unselectify', this._updateAutoUnselectify, this);
this.model.on('change:cytoscape_layout', this._updateLayout, this);
this.model.on('change:cytoscape_style', this._updateStyle, this);
this.model.on('change:elements', this.value_changed, this);
this.model.on('change:pixel_ratio', this.value_changed, this);
this.model.on(
'change:_interaction_handlers',
this.listenForUserEvents,
this
);
const layout = this.model.get('layout');
if (layout !== null) {
layout.on_some_change(['width', 'height'], this._resize, this);
}
}
value_changed() {
if (this.is_rendered) {
// Rerendering creates a new cytoscape object, so we will need to re-add
// interaction handlers. Set `monitored` to empty to trigger this.
this.monitored = {};
this.init_render();
}
}
private _updateViewLists() {
this.nodeViews.update(this.model.get('graph').get('nodes'));
this.edgeViews.update(this.model.get('graph').get('edges'));
this.cytoscape_obj
.elements()
.layout(this.model.get('cytoscape_layout'))
.run();
console.log('whole cytoscape relayout');
}
listenForUserEvents() {
const new_monitored = this.model.get('_interaction_handlers');
// If the plot hasn't been displayed yet, we can't add handlers yet. By
// returning immediately, we avoid marking them as set, so we'll end up
// setting them when the graph is finally displayed.
if (!this.cytoscape_obj) {
return;
}
for (const widgtype in new_monitored) {
if (Object.prototype.hasOwnProperty.call(new_monitored, widgtype)) {
for (let i = 0; i < new_monitored[widgtype].length; i++) {
const evnttype = new_monitored[widgtype][i];
if (this.monitored[widgtype]) {
if (this.monitored[widgtype].includes(evnttype)) {
return;
} else {
this.monitored[widgtype].push(evnttype);
}
} else {
this.monitored[widgtype] = [evnttype];
}
this.cytoscape_obj.on(evnttype, widgtype, (e: any) => {
this.send({
event: evnttype,
widget: widgtype,
data: e.target.json(),
});
});
}
}
}
}
init_render() {
if (this.model.get('graph') !== null) {
this.is_rendered = true;
this.cytoscape_obj = cytoscape({
container: this.el,
minZoom: this.model.get('min_zoom'),
maxZoom: this.model.get('max_zoom'),
zoomingEnabled: this.model.get('zooming_enabled'),
userZoomingEnabled: this.model.get('user_zooming_enabled'),
panningEnabled: this.model.get('panning_enabled'),
boxSelectionEnabled: this.model.get('box_selection_enabled'),
selectionType: this.model.get('selection_type'),
touchTapThreshold: this.model.get('touch_tap_threshold'),
desktopTapThreshold: this.model.get('desktop_tap_threshold'),
autolock: this.model.get('autolock'),
autoungrabify: this.model.get('auto_ungrabify'),
autounselectify: this.model.get('auto_unselectify'),
headless: this.model.get('headless'),
styleEnabled: this.model.get('style_enabled'),
hideEdgesOnViewport: this.model.get('hide_edges_on_viewport'),
textureOnViewport: this.model.get('texture_on_viewport'),
motionBlur: this.model.get('motion_blur'),
motionBlurOpacity: this.model.get('motion_blur_opacity'),
wheelSensitivity: this.model.get('wheel_sensitivity'),
pixelRatio: this.model.get('pixel_ratio'),
style: this.model.get('cytoscape_style'),
elements: [],
});
// we need to set listeners at initial render in case interaction was
// added before the graph was displayed.
// const monitored = this.model.get('monitored');
this.listenForUserEvents();
this.cytoscape_obj.on('click', (e: any) => {
const node = e.target;
const ref = node.popperRef();
const dummyDomEle = document.createElement('div');
const tooltip_source = this.model.get('tooltip_source');
if (node.data()[tooltip_source]) {
const tip = Tippy(dummyDomEle, {
//TODO: add a pretty tippy
trigger: 'manual',
lazy: false,
arrow: true,
theme: 'material',
placement: 'bottom',
content: () => {
const content = document.createElement('div');
content.innerHTML = node
.data()
[tooltip_source].replace(/(?:\r\n|\r|\n)/g, '<br>');
return content;
},
onCreate: (instance: Instance | undefined) => {
if (instance && instance.popperInstance) {
instance.popperInstance.reference = ref;
}
},
});
tip.show();
}
});
}
}
private _updateMinZoom() {
this.cytoscape_obj.minZoom(this.model.get('min_zoom'));
}
private _updateMaxZoom() {
this.cytoscape_obj.maxZoom(this.model.get('max_zoom'));
}
private _updateZoomingEnabled() {
this.cytoscape_obj.zoomingEnabled(this.model.get('zooming_enabled'));
}
private _updateUserZoomingEnabled() {
this.cytoscape_obj.userZoomingEnabled(
this.model.get('user_zooming_enabled')
);
}
private _updatePanningEnabled() {
this.cytoscape_obj.panningEnabled(this.model.get('panning_enabled'));
}
private _updateUserPanningEnabled() {
this.cytoscape_obj.userPanningEnabled(
this.model.get('user_panning_enabled')
);
}
private _updateBoxSelectionEnabled() {
this.cytoscape_obj.boxSelectionEnabled(
this.model.get('box_selection_enabled')
);
}
private _updateSelectionType() {
// I think that @types may have gotten this wrong?
(this.cytoscape_obj as any).selectionType(this.model.get('selection_type'));
}
private _updateAutolock() {
this.cytoscape_obj.autolock(this.model.get('autolock'));
}
private _updateAutoUngrabify() {
this.cytoscape_obj.autoungrabify(this.model.get('auto_ungrabify'));
}
private _updateAutoUnselectify() {
this.cytoscape_obj.autounselectify(this.model.get('auto_unselectify'));
}
private _updateLayout() {
this.cytoscape_obj.layout(this.model.get('cytoscape_layout')).run();
}
private _updateStyle() {
this.cytoscape_obj.style(this.model.get('cytoscape_style'));
}
private _resize() {
if (this.cytoscape_obj) {
this.cytoscape_obj.resize();
this.cytoscape_obj.fit();
}
}
/**
* Add the listeners for traits that are common to nodes and edges
*/
_addElementListeners(
ele: cytoscape.CollectionReturnValue,
view: DOMWidgetView
) {
ele.on('select', (event) => {
view.model.set('selected', true);
view.model.save_changes();
});
ele.on('unselect', (event) => {
view.model.set('selected', false);
view.model.save_changes();
});
ele.on('remove', (event) => {
view.model.set('removed', true);
view.model.save_changes();
});
}
async addNodeModel(NodeModel: NodeModel) {
const node = this.cytoscape_obj.add(NodeModel.asCyObj());
const child = await this.create_child_view(NodeModel, {
cytoscapeView: this,
});
this._addElementListeners(node, child);
node.on('grab', (event) => {
child.model.set('grabbed', true);
child.model.save_changes();
});
node.on('free', (event) => {
child.model.set('grabbed', false);
child.model.save_changes();
});
return child;
}
removeNodeView(nodeView: any) {
nodeView.model.set('removed', true);
nodeView.remove();
}
async addEdgeModel(EdgeModel: EdgeModel) {
const edge = this.cytoscape_obj.add(EdgeModel.asCyObj());
const child = await this.create_child_view(EdgeModel, {
cytoscapeView: this,
});
this._addElementListeners(edge, child);
return child;
}
removeEdgeView(edgeView: any) {
edgeView.model.set('removed', true);
edgeView.remove();
}
}