Map change detector ref

This commit is contained in:
ziajka 2018-11-05 15:24:14 +01:00
parent e296dd004f
commit caca22edb6
4 changed files with 41 additions and 26 deletions

View File

@ -17,6 +17,7 @@ import { InterfaceLabelWidget } from '../../widgets/interface-label';
import { SelectionTool } from '../../tools/selection-tool'; import { SelectionTool } from '../../tools/selection-tool';
import { MovingTool } from '../../tools/moving-tool'; import { MovingTool } from '../../tools/moving-tool';
import { LinksWidget } from '../../widgets/links'; import { LinksWidget } from '../../widgets/links';
import { MapChangeDetectorRef } from '../../services/map-change-detector-ref';
@Component({ @Component({
@ -42,6 +43,7 @@ export class MapComponent implements OnInit, OnChanges, OnDestroy {
private isReady = false; private isReady = false;
private onNodeDraggingSubscription: Subscription; private onNodeDraggingSubscription: Subscription;
private onChangesDetected: Subscription;
protected settings = { protected settings = {
'show_interface_labels': true 'show_interface_labels': true
@ -49,6 +51,7 @@ export class MapComponent implements OnInit, OnChanges, OnDestroy {
constructor( constructor(
private context: Context, private context: Context,
private mapChangeDetectorRef: MapChangeDetectorRef,
protected element: ElementRef, protected element: ElementRef,
protected d3Service: D3Service, protected d3Service: D3Service,
protected nodesWidget: NodesWidget, protected nodesWidget: NodesWidget,
@ -67,27 +70,19 @@ export class MapComponent implements OnInit, OnChanges, OnDestroy {
set showInterfaceLabels(value) { set showInterfaceLabels(value) {
this.settings.show_interface_labels = value; this.settings.show_interface_labels = value;
this.interfaceLabelWidget.setEnabled(value); this.interfaceLabelWidget.setEnabled(value);
if (this.isReady) { this.mapChangeDetectorRef.detectChanges();
this.redraw();
}
} }
@Input('moving-tool') @Input('moving-tool')
set movingTool(value) { set movingTool(value) {
this.movingToolWidget.setEnabled(value); this.movingToolWidget.setEnabled(value);
this.mapChangeDetectorRef.detectChanges();
if(this.isReady) {
this.redraw();
}
} }
@Input('selection-tool') @Input('selection-tool')
set selectionTool(value) { set selectionTool(value) {
this.selectionToolWidget.setEnabled(value); this.selectionToolWidget.setEnabled(value);
this.mapChangeDetectorRef.detectChanges();
if(this.isReady) {
this.redraw();
}
} }
ngOnChanges(changes: { [propKey: string]: SimpleChange }) { ngOnChanges(changes: { [propKey: string]: SimpleChange }) {
@ -117,6 +112,7 @@ export class MapComponent implements OnInit, OnChanges, OnDestroy {
ngOnDestroy() { ngOnDestroy() {
this.graphLayout.disconnect(this.svg); this.graphLayout.disconnect(this.svg);
this.onNodeDraggingSubscription.unsubscribe(); this.onNodeDraggingSubscription.unsubscribe();
this.onChangesDetected.unsubscribe();
} }
ngOnInit() { ngOnInit() {
@ -132,6 +128,12 @@ export class MapComponent implements OnInit, OnChanges, OnDestroy {
this.linksWidget.redrawLink(this.svg, link); this.linksWidget.redrawLink(this.svg, link);
}); });
}); });
this.onChangesDetected = this.mapChangeDetectorRef.changesDetected.subscribe(() => {
if (this.isReady) {
this.reload();
}
});
} }
public createGraph(domElement: HTMLElement) { public createGraph(domElement: HTMLElement) {

View File

@ -0,0 +1,15 @@
import { MapChangeDetectorRef } from "./map-change-detector-ref";
describe('MapChangeDetectorRef', () => {
let detector: MapChangeDetectorRef;
beforeEach(() => {
detector = new MapChangeDetectorRef();
});
it("should emit event", () => {
spyOn(detector.changesDetected, 'emit');
detector.detectChanges();
expect(detector.changesDetected.emit).toHaveBeenCalledWith(true);
})
});

View File

@ -1,9 +1,11 @@
import { Injectable } from "@angular/core"; import { Injectable, EventEmitter } from "@angular/core";
@Injectable() @Injectable()
export class MapChangeDetectorRef { export class MapChangeDetectorRef {
public detectChanges() { public changesDetected = new EventEmitter<boolean>();
public detectChanges() {
this.changesDetected.emit(true);
} }
} }

View File

@ -29,6 +29,7 @@ import { InRectangleHelper } from "../../cartography/helpers/in-rectangle-helper
import { DrawingsDataSource } from "../../cartography/datasources/drawings-datasource"; import { DrawingsDataSource } from "../../cartography/datasources/drawings-datasource";
import { ProgressService } from "../../common/progress/progress.service"; import { ProgressService } from "../../common/progress/progress.service";
import { NodeEvent } from '../../cartography/widgets/nodes'; import { NodeEvent } from '../../cartography/widgets/nodes';
import { MapChangeDetectorRef } from '../../cartography/services/map-change-detector-ref';
@Component({ @Component({
@ -74,6 +75,7 @@ export class ProjectMapComponent implements OnInit, OnDestroy {
private linkService: LinkService, private linkService: LinkService,
private progressService: ProgressService, private progressService: ProgressService,
private projectWebServiceHandler: ProjectWebServiceHandler, private projectWebServiceHandler: ProjectWebServiceHandler,
private mapChangeDetectorRef: MapChangeDetectorRef,
protected nodesDataSource: NodesDataSource, protected nodesDataSource: NodesDataSource,
protected linksDataSource: LinksDataSource, protected linksDataSource: LinksDataSource,
protected drawingsDataSource: DrawingsDataSource, protected drawingsDataSource: DrawingsDataSource,
@ -130,27 +132,21 @@ export class ProjectMapComponent implements OnInit, OnDestroy {
this.subscriptions.push( this.subscriptions.push(
this.drawingsDataSource.changes.subscribe((drawings: Drawing[]) => { this.drawingsDataSource.changes.subscribe((drawings: Drawing[]) => {
this.drawings = drawings; this.drawings = drawings;
if (this.mapChild) { this.mapChangeDetectorRef.detectChanges();
this.mapChild.reload();
}
}) })
); );
this.subscriptions.push( this.subscriptions.push(
this.nodesDataSource.changes.subscribe((nodes: Node[]) => { this.nodesDataSource.changes.subscribe((nodes: Node[]) => {
this.nodes = nodes; this.nodes = nodes;
if (this.mapChild) { this.mapChangeDetectorRef.detectChanges();
this.mapChild.reload();
}
}) })
); );
this.subscriptions.push( this.subscriptions.push(
this.linksDataSource.changes.subscribe((links: Link[]) => { this.linksDataSource.changes.subscribe((links: Link[]) => {
this.links = links; this.links = links;
if (this.mapChild) { this.mapChangeDetectorRef.detectChanges();
this.mapChild.reload();
}
}) })
); );
} }
@ -217,7 +213,7 @@ export class ProjectMapComponent implements OnInit, OnDestroy {
this.mapChild.graphLayout.getSelectionTool().rectangleSelected) this.mapChild.graphLayout.getSelectionTool().rectangleSelected)
); );
this.mapChild.reload(); this.mapChangeDetectorRef.detectChanges();
} }
onNodeCreation(appliance: Appliance) { onNodeCreation(appliance: Appliance) {