import { Component, ContentChildren, QueryList, ElementRef, ViewChild, OnDestroy, Input, Output, EventEmitter, OnInit } from "@angular/core"; import {VisNetworkNodeDirective} from "./node/vis-network-node.directive"; import {VisNetworkEdgeDirective} from "./edge/vis-network-edge.directive"; import {VisNetworkService} from "./vis-network.service"; import {VisNgNetworkNode} from "./interfaces/vis-ng-network-node.interface"; import {VisNgNetworkEdge} from "./interfaces/vis-ng-network-edge.interface"; import {Subject, Observable, Subscription} from "rxjs"; import {VisNgNetworkOptions} from "./interfaces/vis-ng-network-options.interface"; import {VisNgNetworkEventArgument, VisNgNetwork} from "./interfaces/vis-ng-network.interface"; export function VisNgEvent(eventString?: string): any { return function (target: VisNetworkComponent, key: string) { if (eventString == null) { if (key.startsWith('graph')) { eventString = key.slice('graph'.length); eventString = eventString[0].toLowerCase() + eventString.slice(1); } } if (!target['_events']) { target['_events'] = new Map(); } (>target['_events']).set(key, eventString); } } @Component({ selector: 'vis-network', template: `
`, styles: [` :host { display: block; width: 100%; height: 100%; } .graph { width: 100%; height: 100%; }`] }) export class VisNetworkComponent implements OnInit, OnDestroy { public nodes$: Subject> = new Subject>(); public edges$: Subject> = new Subject>(); public options$: Subject = new Subject(); @ContentChildren(VisNetworkNodeDirective) public set nodes(nodes: QueryList) { this.nodes$.next(nodes); } @ContentChildren(VisNetworkEdgeDirective) public set edges(edges: QueryList) { this.edges$.next(edges); } @Input() public set options(options: VisNgNetworkOptions) { this.options$.next(options); } private _events: Map; // region Events // Events triggered by human interaction, selection, dragging, etc. @VisNgEvent() @Output() public graphClick = new EventEmitter(); @VisNgEvent() @Output() public graphDoubleClick = new EventEmitter(); @VisNgEvent('oncontext') @Output() public graphRightClick = new EventEmitter(); @VisNgEvent() @Output() public graphHold = new EventEmitter(); @VisNgEvent() @Output() public graphRelease = new EventEmitter(); @VisNgEvent() @Output() public graphSelect = new EventEmitter(); @VisNgEvent() @Output() public graphSelectNode = new EventEmitter(); @VisNgEvent() @Output() public graphSelectEdge = new EventEmitter(); @VisNgEvent() @Output() public graphDeselectNode = new EventEmitter(); @VisNgEvent() @Output() public graphDeselectEdge = new EventEmitter(); @VisNgEvent() @Output() public graphDragStart = new EventEmitter(); @VisNgEvent() @Output() public graphDragging = new EventEmitter(); @VisNgEvent() @Output() public graphDragEnd = new EventEmitter(); @VisNgEvent() @Output() public graphHoverNode = new EventEmitter(); @VisNgEvent() @Output() public graphBlurNode = new EventEmitter(); @VisNgEvent() @Output() public graphHoverEdge = new EventEmitter(); @VisNgEvent() @Output() public graphBlurEdge = new EventEmitter(); @VisNgEvent() @Output() public graphZoom = new EventEmitter(); @VisNgEvent() @Output() public graphShowPopup; @VisNgEvent() @Output() public graphHidePopup = new EventEmitter(); // Events triggered the physics simulation. Can be used to trigger GUI updates. @VisNgEvent() @Output() public graphStartStabilizing = new EventEmitter(); @VisNgEvent() @Output() public graphStabilizationProgress = new EventEmitter(); @VisNgEvent() @Output() public graphStabilizationIterationsDone = new EventEmitter(); @VisNgEvent() @Output() public graphStabilized = new EventEmitter(); // Events triggered by the canvas. @VisNgEvent() @Output() public graphResize = new EventEmitter(); // Events triggered by the rendering module. Can be used to draw custom elements on the canvas. @Output() @VisNgEvent() public graphInitRedraw = new EventEmitter(); @Output() @VisNgEvent() public graphBeforeDrawing = new EventEmitter(); @Output() @VisNgEvent() public graphAfterDrawing = new EventEmitter(); // Event triggered by the view module. @VisNgEvent() @Output() public graphAnimationFinished = new EventEmitter(); // Event triggered by the configuration module. @VisNgEvent() @Output() public graphConfigChange = new EventEmitter(); // endregion @ViewChild('graph') public graphElement: ElementRef; public changes$: Observable<{nodes: VisNgNetworkNode[], edges: VisNgNetworkEdge[], options: VisNgNetworkOptions}> = Observable.combineLatest(this.nodes$, this.edges$, this.options$) .map(values => { return { nodes: values[0], edges: values[1], options: values[2], } }) .filter(values => values.options !== null) .map(values => { const nodes: VisNgNetworkNode[] = values.nodes.map(node => node.toObject()); const edges: VisNgNetworkEdge[] = values.edges.map(edge => edge.toObject()); const options: VisNgNetworkOptions = values.options; return {nodes, edges, options}; }); private _subscriptions: Subscription[] = []; public get rawNetworkInstance(): VisNgNetwork { return this.service.network; } public get rawNodes(): any { return this.service.network.body.data.nodes._data; } public get rawEdges(): any { return this.service.network.body.data.edges._data; } public getPositions(param?: string | string[]): any { return this.service.network.getPositions(param); } public storePositions(param: any): void { this.service.network.storePositions(param); } constructor(private service: VisNetworkService) { let sub = this.changes$.take(1).subscribe(values => { this.service.initializeGraph(this.graphElement, values.nodes, values.edges, values.options); }); this._subscriptions.push(sub); sub = this.changes$.skip(1).subscribe(values => { this.service.onChange({ nodes: values.nodes, edges: values.edges, options: values.options }); }); this._subscriptions.push(sub); } ngOnInit() { // We must wait a tick for network to be created in the service. setTimeout(() => { this._events.forEach((eventString, propertyName) => { this.service.attachEvent(eventString, arg => { this[propertyName].emit(arg); }); }); }); } ngOnDestroy() { this._subscriptions.forEach(s => s.unsubscribe()); this.service.destroy(); } }