UNPKG

19.6 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('vis'), require('rxjs')) :
3 typeof define === 'function' && define.amd ? define(['exports', '@angular/core', '@angular/common', 'vis', 'rxjs'], factory) :
4 (factory((global.vis-ng = global.vis-ng || {}),global.ng.core,global.ng.common,global.vis,global.rxjs));
5}(this, (function (exports,_angular_core,_angular_common,vis,rxjs) { 'use strict';
6
7class VisNetworkService {
8 constructor() {
9 }
10 get network() {
11 return this._network;
12 }
13 getVisNode(nodeId) {
14 return this.nodes.get(nodeId);
15 }
16 /**
17 * Attach vis event to the current _network.
18 * @param eventName
19 * @param callback
20 */
21 attachEvent(eventName, callback) {
22 this._network.on(eventName, callback);
23 }
24 addNode(node) {
25 this.nodes.add(node);
26 }
27 updateNode(node) {
28 this.nodes.update(node);
29 }
30 removeNode(nodeId) {
31 this.nodes.remove(nodeId);
32 }
33 addEdge(edge) {
34 this.edges.add(edge);
35 }
36 updateEdge(edge) {
37 this.edges.update(edge);
38 }
39 removeEdge(edgeId) {
40 this.edges.remove(edgeId);
41 }
42 onChange(newState) {
43 this._network.setOptions(newState.options);
44 const diffNodes = this.diff(this.nodes.get(), newState.nodes);
45 const diffEdges = this.diff(this.edges.get(), newState.edges);
46 diffNodes.added.forEach(n => this.nodes.add(n));
47 diffNodes.changed.forEach(n => this.nodes.update(n));
48 diffNodes.deleted.forEach(n => this.nodes.remove({ id: n }));
49 diffEdges.added.forEach(e => this.edges.add(e));
50 diffEdges.changed.forEach(e => this.edges.update(e));
51 diffEdges.deleted.forEach(e => this.edges.remove({ id: e }));
52 this.network.setOptions(newState.options);
53 }
54 diff(oldValue, newValue) {
55 let diffResult = { added: [], deleted: [], changed: [] };
56 diffResult.deleted = oldValue
57 .filter(oldV => newValue.map(e => e.id).indexOf(oldV.id) == -1)
58 .map(oldV => oldV.id);
59 diffResult.added = newValue.filter(newV => oldValue.map(e => e.id).indexOf(newV.id) == -1);
60 diffResult.changed = newValue.filter(newV => {
61 let el = oldValue.find(oldV => oldV.id == newV.id);
62 return el ? Object.keys(el).some(key => newV[key] != el[key]) : false;
63 });
64 return diffResult;
65 }
66 initDraw() {
67 let data = {
68 nodes: this.nodes,
69 edges: this.edges
70 };
71 let options = this.options;
72 this._network = new vis.Network(this.container.nativeElement, data, options);
73 }
74 initializeGraph(container, nodes, edges, options) {
75 this.container = container;
76 this.nodes = new vis.DataSet(nodes);
77 this.edges = new vis.DataSet(edges);
78 this.options = options;
79 this.initDraw();
80 }
81 destroy() {
82 if (this._network) {
83 this._network.destroy();
84 }
85 }
86 getPositions() {
87 let map = new Map();
88 const positions = this.network.getPositions();
89 Object.keys(positions).forEach(nodeId => {
90 map.set(nodeId, positions[nodeId]);
91 });
92 return map;
93 }
94 setPositions(positions) {
95 positions.forEach((position, nodeId) => {
96 this.network.moveNode(nodeId, position.x, position.y);
97 });
98 }
99}
100VisNetworkService.decorators = [
101 { type: _angular_core.Injectable },
102];
103/** @nocollapse */
104VisNetworkService.ctorParameters = [];
105
106class VisNetworkNodeDirective {
107 constructor(service) {
108 this.service = service;
109 this.select = new _angular_core.EventEmitter();
110 }
111 set options(options) {
112 this._options = options;
113 setTimeout(() => this.service.updateNode(this.toObject()));
114 }
115 get options() {
116 return this._options;
117 }
118 onSelectNode(arg) {
119 if (arg.nodes.indexOf(this.id) != -1) {
120 this.select.emit(arg);
121 }
122 }
123 toObject() {
124 return Object.assign({
125 id: this.id,
126 label: this.label,
127 }, this.options);
128 }
129 ngOnInit() {
130 // We must wait a tick for network to be created in the service.
131 setTimeout(() => {
132 this.service.attachEvent('selectNode', arg => this.onSelectNode(arg));
133 });
134 }
135}
136VisNetworkNodeDirective.decorators = [
137 { type: _angular_core.Directive, args: [{ selector: 'vis-network-node' },] },
138];
139/** @nocollapse */
140VisNetworkNodeDirective.ctorParameters = [
141 { type: VisNetworkService, },
142];
143VisNetworkNodeDirective.propDecorators = {
144 'id': [{ type: _angular_core.Input },],
145 'label': [{ type: _angular_core.Input },],
146 'options': [{ type: _angular_core.Input },],
147 'select': [{ type: _angular_core.Output },],
148};
149
150class VisNetworkEdgeDirective {
151 constructor(service) {
152 this.service = service;
153 }
154 set options(options) {
155 this._options = options;
156 setTimeout(() => this.service.updateEdge(this.toObject()));
157 }
158 get options() {
159 return this._options;
160 }
161 toObject() {
162 return Object.assign({
163 id: this.id,
164 from: this.from,
165 to: this.to,
166 name: this.name,
167 label: this.label,
168 }, this.options);
169 }
170 ngOnInit() {
171 }
172}
173VisNetworkEdgeDirective.decorators = [
174 { type: _angular_core.Directive, args: [{ selector: 'vis-network-edge' },] },
175];
176/** @nocollapse */
177VisNetworkEdgeDirective.ctorParameters = [
178 { type: VisNetworkService, },
179];
180VisNetworkEdgeDirective.propDecorators = {
181 'id': [{ type: _angular_core.Input },],
182 'from': [{ type: _angular_core.Input },],
183 'to': [{ type: _angular_core.Input },],
184 'name': [{ type: _angular_core.Input },],
185 'label': [{ type: _angular_core.Input },],
186 'options': [{ type: _angular_core.Input },],
187};
188
189var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
190 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
191 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
192 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
193 return c > 3 && r && Object.defineProperty(target, key, r), r;
194};
195var __metadata = (undefined && undefined.__metadata) || function (k, v) {
196 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
197};
198function VisNgEvent(eventString) {
199 return function (target, key) {
200 if (eventString == null) {
201 if (key.startsWith('graph')) {
202 eventString = key.slice('graph'.length);
203 eventString = eventString[0].toLowerCase() + eventString.slice(1);
204 }
205 }
206 if (!target['_events']) {
207 target['_events'] = new Map();
208 }
209 target['_events'].set(key, eventString);
210 };
211}
212class VisNetworkComponent {
213 constructor(service) {
214 this.service = service;
215 this.nodes$ = new rxjs.Subject();
216 this.edges$ = new rxjs.Subject();
217 this.options$ = new rxjs.Subject();
218 // region Events
219 // Events triggered by human interaction, selection, dragging, etc.
220 this.graphClick = new _angular_core.EventEmitter();
221 this.graphDoubleClick = new _angular_core.EventEmitter();
222 this.graphRightClick = new _angular_core.EventEmitter();
223 this.graphHold = new _angular_core.EventEmitter();
224 this.graphRelease = new _angular_core.EventEmitter();
225 this.graphSelect = new _angular_core.EventEmitter();
226 this.graphSelectNode = new _angular_core.EventEmitter();
227 this.graphSelectEdge = new _angular_core.EventEmitter();
228 this.graphDeselectNode = new _angular_core.EventEmitter();
229 this.graphDeselectEdge = new _angular_core.EventEmitter();
230 this.graphDragStart = new _angular_core.EventEmitter();
231 this.graphDragging = new _angular_core.EventEmitter();
232 this.graphDragEnd = new _angular_core.EventEmitter();
233 this.graphHoverNode = new _angular_core.EventEmitter();
234 this.graphBlurNode = new _angular_core.EventEmitter();
235 this.graphHoverEdge = new _angular_core.EventEmitter();
236 this.graphBlurEdge = new _angular_core.EventEmitter();
237 this.graphZoom = new _angular_core.EventEmitter();
238 this.graphHidePopup = new _angular_core.EventEmitter();
239 // Events triggered the physics simulation. Can be used to trigger GUI updates.
240 this.graphStartStabilizing = new _angular_core.EventEmitter();
241 this.graphStabilizationProgress = new _angular_core.EventEmitter();
242 this.graphStabilizationIterationsDone = new _angular_core.EventEmitter();
243 this.graphStabilized = new _angular_core.EventEmitter();
244 // Events triggered by the canvas.
245 this.graphResize = new _angular_core.EventEmitter();
246 // Events triggered by the rendering module. Can be used to draw custom elements on the canvas.
247 this.graphInitRedraw = new _angular_core.EventEmitter();
248 this.graphBeforeDrawing = new _angular_core.EventEmitter();
249 this.graphAfterDrawing = new _angular_core.EventEmitter();
250 // Event triggered by the view module.
251 this.graphAnimationFinished = new _angular_core.EventEmitter();
252 // Event triggered by the configuration module.
253 this.graphConfigChange = new _angular_core.EventEmitter();
254 this.changes$ = rxjs.Observable.combineLatest(this.nodes$, this.edges$, this.options$)
255 .map(values => {
256 return {
257 nodes: values[0],
258 edges: values[1],
259 options: values[2],
260 };
261 })
262 .filter(values => values.options !== null)
263 .map(values => {
264 const nodes = values.nodes.map(node => node.toObject());
265 const edges = values.edges.map(edge => edge.toObject());
266 const options = values.options;
267 return { nodes, edges, options };
268 });
269 this._subscriptions = [];
270 let sub = this.changes$.take(1).subscribe(values => {
271 this.service.initializeGraph(this.graphElement, values.nodes, values.edges, values.options);
272 });
273 this._subscriptions.push(sub);
274 sub = this.changes$.skip(1).subscribe(values => {
275 this.service.onChange({
276 nodes: values.nodes,
277 edges: values.edges,
278 options: values.options
279 });
280 });
281 this._subscriptions.push(sub);
282 }
283 set nodes(nodes) {
284 this.nodes$.next(nodes);
285 }
286 set edges(edges) {
287 this.edges$.next(edges);
288 }
289 set options(options) {
290 this.options$.next(options);
291 }
292 get rawNetworkInstance() {
293 return this.service.network;
294 }
295 get rawNodes() {
296 return this.service.network.body.data.nodes._data;
297 }
298 get rawEdges() {
299 return this.service.network.body.data.edges._data;
300 }
301 getPositions(param) {
302 return this.service.network.getPositions(param);
303 }
304 storePositions(param) {
305 this.service.network.storePositions(param);
306 }
307 ngOnInit() {
308 // We must wait a tick for network to be created in the service.
309 setTimeout(() => {
310 this._events.forEach((eventString, propertyName) => {
311 this.service.attachEvent(eventString, arg => {
312 this[propertyName].emit(arg);
313 });
314 });
315 });
316 }
317 ngOnDestroy() {
318 this._subscriptions.forEach(s => s.unsubscribe());
319 this.service.destroy();
320 }
321}
322VisNetworkComponent.decorators = [
323 { type: _angular_core.Component, args: [{
324 selector: 'vis-network',
325 template: `
326<div class="graph" #graph></div>
327`,
328 styles: [`
329:host {
330 display: block;
331 width: 100%;
332 height: 100%;
333}
334
335.graph {
336 width: 100%;
337 height: 100%;
338}`]
339 },] },
340];
341/** @nocollapse */
342VisNetworkComponent.ctorParameters = [
343 { type: VisNetworkService, },
344];
345VisNetworkComponent.propDecorators = {
346 'nodes': [{ type: _angular_core.ContentChildren, args: [VisNetworkNodeDirective,] },],
347 'edges': [{ type: _angular_core.ContentChildren, args: [VisNetworkEdgeDirective,] },],
348 'options': [{ type: _angular_core.Input },],
349 'graphClick': [{ type: _angular_core.Output },],
350 'graphDoubleClick': [{ type: _angular_core.Output },],
351 'graphRightClick': [{ type: _angular_core.Output },],
352 'graphHold': [{ type: _angular_core.Output },],
353 'graphRelease': [{ type: _angular_core.Output },],
354 'graphSelect': [{ type: _angular_core.Output },],
355 'graphSelectNode': [{ type: _angular_core.Output },],
356 'graphSelectEdge': [{ type: _angular_core.Output },],
357 'graphDeselectNode': [{ type: _angular_core.Output },],
358 'graphDeselectEdge': [{ type: _angular_core.Output },],
359 'graphDragStart': [{ type: _angular_core.Output },],
360 'graphDragging': [{ type: _angular_core.Output },],
361 'graphDragEnd': [{ type: _angular_core.Output },],
362 'graphHoverNode': [{ type: _angular_core.Output },],
363 'graphBlurNode': [{ type: _angular_core.Output },],
364 'graphHoverEdge': [{ type: _angular_core.Output },],
365 'graphBlurEdge': [{ type: _angular_core.Output },],
366 'graphZoom': [{ type: _angular_core.Output },],
367 'graphShowPopup': [{ type: _angular_core.Output },],
368 'graphHidePopup': [{ type: _angular_core.Output },],
369 'graphStartStabilizing': [{ type: _angular_core.Output },],
370 'graphStabilizationProgress': [{ type: _angular_core.Output },],
371 'graphStabilizationIterationsDone': [{ type: _angular_core.Output },],
372 'graphStabilized': [{ type: _angular_core.Output },],
373 'graphResize': [{ type: _angular_core.Output },],
374 'graphInitRedraw': [{ type: _angular_core.Output },],
375 'graphBeforeDrawing': [{ type: _angular_core.Output },],
376 'graphAfterDrawing': [{ type: _angular_core.Output },],
377 'graphAnimationFinished': [{ type: _angular_core.Output },],
378 'graphConfigChange': [{ type: _angular_core.Output },],
379 'graphElement': [{ type: _angular_core.ViewChild, args: ['graph',] },],
380};
381__decorate([
382 VisNgEvent(),
383 __metadata('design:type', Object)
384], VisNetworkComponent.prototype, "graphClick", void 0);
385__decorate([
386 VisNgEvent(),
387 __metadata('design:type', Object)
388], VisNetworkComponent.prototype, "graphDoubleClick", void 0);
389__decorate([
390 VisNgEvent('oncontext'),
391 __metadata('design:type', Object)
392], VisNetworkComponent.prototype, "graphRightClick", void 0);
393__decorate([
394 VisNgEvent(),
395 __metadata('design:type', Object)
396], VisNetworkComponent.prototype, "graphHold", void 0);
397__decorate([
398 VisNgEvent(),
399 __metadata('design:type', Object)
400], VisNetworkComponent.prototype, "graphRelease", void 0);
401__decorate([
402 VisNgEvent(),
403 __metadata('design:type', Object)
404], VisNetworkComponent.prototype, "graphSelect", void 0);
405__decorate([
406 VisNgEvent(),
407 __metadata('design:type', Object)
408], VisNetworkComponent.prototype, "graphSelectNode", void 0);
409__decorate([
410 VisNgEvent(),
411 __metadata('design:type', Object)
412], VisNetworkComponent.prototype, "graphSelectEdge", void 0);
413__decorate([
414 VisNgEvent(),
415 __metadata('design:type', Object)
416], VisNetworkComponent.prototype, "graphDeselectNode", void 0);
417__decorate([
418 VisNgEvent(),
419 __metadata('design:type', Object)
420], VisNetworkComponent.prototype, "graphDeselectEdge", void 0);
421__decorate([
422 VisNgEvent(),
423 __metadata('design:type', Object)
424], VisNetworkComponent.prototype, "graphDragStart", void 0);
425__decorate([
426 VisNgEvent(),
427 __metadata('design:type', Object)
428], VisNetworkComponent.prototype, "graphDragging", void 0);
429__decorate([
430 VisNgEvent(),
431 __metadata('design:type', Object)
432], VisNetworkComponent.prototype, "graphDragEnd", void 0);
433__decorate([
434 VisNgEvent(),
435 __metadata('design:type', Object)
436], VisNetworkComponent.prototype, "graphHoverNode", void 0);
437__decorate([
438 VisNgEvent(),
439 __metadata('design:type', Object)
440], VisNetworkComponent.prototype, "graphBlurNode", void 0);
441__decorate([
442 VisNgEvent(),
443 __metadata('design:type', Object)
444], VisNetworkComponent.prototype, "graphHoverEdge", void 0);
445__decorate([
446 VisNgEvent(),
447 __metadata('design:type', Object)
448], VisNetworkComponent.prototype, "graphBlurEdge", void 0);
449__decorate([
450 VisNgEvent(),
451 __metadata('design:type', Object)
452], VisNetworkComponent.prototype, "graphZoom", void 0);
453__decorate([
454 VisNgEvent(),
455 __metadata('design:type', Object)
456], VisNetworkComponent.prototype, "graphShowPopup", void 0);
457__decorate([
458 VisNgEvent(),
459 __metadata('design:type', Object)
460], VisNetworkComponent.prototype, "graphHidePopup", void 0);
461__decorate([
462 VisNgEvent(),
463 __metadata('design:type', Object)
464], VisNetworkComponent.prototype, "graphStartStabilizing", void 0);
465__decorate([
466 VisNgEvent(),
467 __metadata('design:type', Object)
468], VisNetworkComponent.prototype, "graphStabilizationProgress", void 0);
469__decorate([
470 VisNgEvent(),
471 __metadata('design:type', Object)
472], VisNetworkComponent.prototype, "graphStabilizationIterationsDone", void 0);
473__decorate([
474 VisNgEvent(),
475 __metadata('design:type', Object)
476], VisNetworkComponent.prototype, "graphStabilized", void 0);
477__decorate([
478 VisNgEvent(),
479 __metadata('design:type', Object)
480], VisNetworkComponent.prototype, "graphResize", void 0);
481__decorate([
482 VisNgEvent(),
483 __metadata('design:type', Object)
484], VisNetworkComponent.prototype, "graphInitRedraw", void 0);
485__decorate([
486 VisNgEvent(),
487 __metadata('design:type', Object)
488], VisNetworkComponent.prototype, "graphBeforeDrawing", void 0);
489__decorate([
490 VisNgEvent(),
491 __metadata('design:type', Object)
492], VisNetworkComponent.prototype, "graphAfterDrawing", void 0);
493__decorate([
494 VisNgEvent(),
495 __metadata('design:type', Object)
496], VisNetworkComponent.prototype, "graphAnimationFinished", void 0);
497__decorate([
498 VisNgEvent(),
499 __metadata('design:type', Object)
500], VisNetworkComponent.prototype, "graphConfigChange", void 0);
501
502class VisNetworkModule {
503 static forRoot() {
504 return {
505 ngModule: VisNetworkModule,
506 providers: [
507 VisNetworkService,
508 ],
509 };
510 }
511}
512VisNetworkModule.decorators = [
513 { type: _angular_core.NgModule, args: [{
514 imports: [
515 _angular_common.CommonModule,
516 ],
517 //providers: [
518 //VisNetworkService,
519 //],
520 declarations: [
521 VisNetworkComponent,
522 VisNetworkNodeDirective,
523 VisNetworkEdgeDirective,
524 ],
525 exports: [
526 VisNetworkComponent,
527 VisNetworkNodeDirective,
528 VisNetworkEdgeDirective,
529 ],
530 },] },
531];
532/** @nocollapse */
533VisNetworkModule.ctorParameters = [];
534
535exports.VisNetworkService = VisNetworkService;
536exports.VisNetworkModule = VisNetworkModule;
537
538Object.defineProperty(exports, '__esModule', { value: true });
539
540})));
541//# sourceMappingURL=core.umd.js.map