Ability to drag more than one node at once

This commit is contained in:
ziajka 2018-11-07 11:44:33 +01:00
parent 0fe3d0e7ca
commit 2f822580a9
6 changed files with 114 additions and 75 deletions

View File

@ -17,7 +17,7 @@ import { SelectionTool } from '../../tools/selection-tool';
import { MovingTool } from '../../tools/moving-tool';
import { LinksWidget } from '../../widgets/links';
import { MapChangeDetectorRef } from '../../services/map-change-detector-ref';
import { NodeDragging, NodeDragged } from '../../events/nodes';
import { NodeDragging, NodeDragged, NodeClicked } from '../../events/nodes';
import { LinkCreated } from '../../events/links';
import { CanvasSizeDetector } from '../../helpers/canvas-size-detector';
import { SelectionManager } from '../../managers/selection-manager';
@ -40,13 +40,16 @@ export class MapComponent implements OnInit, OnChanges, OnDestroy {
@Input() width = 1500;
@Input() height = 600;
@Output() onNodeDragged: EventEmitter<NodeDragged>;
@Output() onNodeDragged = new EventEmitter<NodeDragged>();
@Output() onLinkCreated = new EventEmitter<LinkCreated>();
private parentNativeElement: any;
private svg: Selection<SVGSVGElement, any, null, undefined>;
private onNodeDraggingSubscription: Subscription;
private onNodeClickedSubscription: Subscription;
private onNodeDraggedSubscription: Subscription;
private onChangesDetected: Subscription;
protected settings = {
@ -67,7 +70,6 @@ export class MapComponent implements OnInit, OnChanges, OnDestroy {
public graphLayout: GraphLayout
) {
this.parentNativeElement = element.nativeElement;
this.onNodeDragged = nodeWidget.onNodeDragged;
}
@Input('show-interface-labels')
@ -118,6 +120,8 @@ export class MapComponent implements OnInit, OnChanges, OnDestroy {
ngOnDestroy() {
this.graphLayout.disconnect(this.svg);
this.onNodeDraggingSubscription.unsubscribe();
this.onNodeClickedSubscription.unsubscribe();
this.onNodeDraggedSubscription.unsubscribe();
this.onChangesDetected.unsubscribe();
}
@ -128,17 +132,19 @@ export class MapComponent implements OnInit, OnChanges, OnDestroy {
this.context.size = this.getSize();
this.onNodeDraggingSubscription = this.nodeWidget.onNodeDragging.subscribe((eventNode: NodeDragging) => {
const nodes = this.selectionManager.getSelectedNodes();
let nodes = this.selectionManager.getSelectedNodes();
if (nodes.filter((n: Node) => n.node_id === eventNode.node.node_id).length === 0) {
this.selectionManager.setSelectedNodes([eventNode.node]);
nodes = this.selectionManager.getSelectedNodes();
}
nodes.forEach((node: Node) => {
node.x += eventNode.event.dx;
node.y += eventNode.event.dy;
this.nodesWidget.redrawNode(this.svg, node);
const links = this.links.filter((link) => link.target.node_id === node.node_id || link.source.node_id === node.node_id);
links.forEach((link) => {
this.linksWidget.redrawLink(this.svg, link);
});
@ -146,6 +152,24 @@ export class MapComponent implements OnInit, OnChanges, OnDestroy {
});
this.onNodeDraggedSubscription = this.nodeWidget.onNodeDragged.subscribe((eventNode: NodeDragged) => {
let nodes = this.selectionManager.getSelectedNodes();
if (nodes.filter((n: Node) => n.node_id === eventNode.node.node_id).length === 0) {
this.selectionManager.setSelectedNodes([eventNode.node]);
nodes = this.selectionManager.getSelectedNodes();
}
nodes.forEach((node) => {
this.onNodeDragged.emit(new NodeDragged(eventNode.event, node));
});
});
this.onNodeClickedSubscription = this.nodeWidget.onNodeClicked.subscribe((nodeClickedEvent: NodeClicked) => {
this.selectionManager.setSelectedNodes([nodeClickedEvent.node]);
});
this.onChangesDetected = this.mapChangeDetectorRef.changesDetected.subscribe(() => {
if (this.mapChangeDetectorRef.hasBeenDrawn) {
this.reload();

View File

@ -16,12 +16,12 @@ describe('LinksWidget', () => {
let widget: LinksWidget;
let layersEnter: Selection<SVGGElement, Layer, SVGGElement, any>;
let layer: Layer;
let mockedLinkWidget: LinkWidget;
let linkWidget: LinkWidget;
beforeEach(() => {
svg = new TestSVGCanvas();
mockedLinkWidget = instance(mock(LinkWidget));
widget = new LinksWidget(new MultiLinkCalculatorHelper(), mockedLinkWidget);
linkWidget = instance(mock(LinkWidget));
widget = new LinksWidget(new MultiLinkCalculatorHelper(), linkWidget);
const node_1 = new Node();
node_1.node_id = "1";
@ -65,10 +65,6 @@ describe('LinksWidget', () => {
});
it('should draw links', () => {
const linkWidgetMock = mock(LinkWidget);
const linkWidget = instance(linkWidgetMock);
spyOn(widget, 'getLinkWidget').and.returnValue(linkWidget);
widget.draw(layersEnter);
const drew = svg.canvas.selectAll<SVGGElement, Link>('g.link');

View File

@ -0,0 +1,70 @@
import { TestSVGCanvas } from "../testing";
import { Node } from "../models/node";
import { Label } from "../models/label";
import { CssFixer } from "../helpers/css-fixer";
import { FontFixer } from "../helpers/font-fixer";
import { NodeWidget } from "./node";
describe('NodesWidget', () => {
let svg: TestSVGCanvas;
let widget: NodeWidget;
beforeEach(() => {
svg = new TestSVGCanvas();
widget = new NodeWidget(new CssFixer(), new FontFixer());
});
afterEach(() => {
svg.destroy();
});
describe('draggable behaviour', () => {
let node: Node;
const tryToDrag = () => {
const drew = svg.canvas.selectAll<SVGGElement, Node>('g.node');
const drewNode = drew.nodes()[0];
drewNode.dispatchEvent(
new MouseEvent('mousedown', {
clientX: 150, clientY: 250, relatedTarget: drewNode,
screenY: 1024, screenX: 1024, view: window
})
);
window.dispatchEvent(new MouseEvent('mousemove', {clientX: 300, clientY: 300}));
window.dispatchEvent(new MouseEvent('mouseup', {clientX: 300, clientY: 300, view: window}));
};
beforeEach(() => {
node = new Node();
node.x = 100;
node.y = 200;
node.width = 100;
node.height = 100;
node.label = new Label();
});
// it('should be draggable when enabled', () => {
// widget.setDraggingEnabled(true);
// widget.draw(svg.canvas);
// tryToDrag();
// expect(node.x).toEqual(250);
// expect(node.y).toEqual(250);
// });
// it('should be not draggable when disabled', () => {
// widget.setDraggingEnabled(false);
// widget.draw(svg.canvas);
// tryToDrag();
// expect(node.x).toEqual(100);
// expect(node.y).toEqual(200);
// });
});
});

View File

@ -1,74 +1,23 @@
import { TestSVGCanvas } from "../testing";
import { NodesWidget } from "./nodes";
import { Node } from "../models/node";
import { Label } from "../models/label";
import { CssFixer } from "../helpers/css-fixer";
import { FontFixer } from "../helpers/font-fixer";
import { NodeWidget } from "./node";
import { instance, mock } from "ts-mockito";
describe('NodesWidget', () => {
let svg: TestSVGCanvas;
let nodeWidget: NodeWidget;
let widget: NodesWidget;
beforeEach(() => {
svg = new TestSVGCanvas();
widget = new NodesWidget(
new CssFixer(),
new FontFixer()
);
nodeWidget = instance(mock(NodeWidget));
widget = new NodesWidget(nodeWidget);
});
afterEach(() => {
svg.destroy();
});
describe('draggable behaviour', () => {
let node: Node;
const tryToDrag = () => {
const drew = svg.canvas.selectAll<SVGGElement, Node>('g.node');
const drewNode = drew.nodes()[0];
drewNode.dispatchEvent(
new MouseEvent('mousedown', {
clientX: 150, clientY: 250, relatedTarget: drewNode,
screenY: 1024, screenX: 1024, view: window
})
);
window.dispatchEvent(new MouseEvent('mousemove', {clientX: 300, clientY: 300}));
window.dispatchEvent(new MouseEvent('mouseup', {clientX: 300, clientY: 300, view: window}));
};
beforeEach(() => {
node = new Node();
node.x = 100;
node.y = 200;
node.width = 100;
node.height = 100;
node.label = new Label();
});
it('should be draggable when enabled', () => {
widget.setDraggingEnabled(true);
widget.draw(svg.canvas, [node]);
tryToDrag();
expect(node.x).toEqual(250);
expect(node.y).toEqual(250);
});
it('should be not draggable when disabled', () => {
widget.setDraggingEnabled(false);
widget.draw(svg.canvas, [node]);
tryToDrag();
expect(node.x).toEqual(100);
expect(node.y).toEqual(200);
});
});
});

View File

@ -41,7 +41,7 @@ describe('ApplianceService', () => {
server.port = 3080;
server.authorization = "none";
service.list(server).subscribe();
service.list(server).subscribe(() => {});
httpTestingController.expectOne('http://127.0.0.1:3080/v2/appliances');

View File

@ -1,4 +1,4 @@
import { TestBed, inject } from '@angular/core/testing';
import { TestBed, inject, fakeAsync } from '@angular/core/testing';
import { PersistenceService, StorageType } from "angular-persistence";
import { Settings, SettingsService } from './settings.service';
@ -23,7 +23,7 @@ describe('SettingsService', () => {
persistenceService = TestBed.get(PersistenceService);
});
afterEach(() => {
beforeEach(() => {
persistenceService.removeAll(StorageType.LOCAL);
});
@ -69,7 +69,7 @@ describe('SettingsService', () => {
});
}));
it('should execute subscriber', inject([SettingsService], (service: SettingsService) => {
it('should execute subscriber', inject([SettingsService], fakeAsync((service: SettingsService) => {
let changedSettings: Settings;
service.set('crash_reports', true);
@ -79,7 +79,7 @@ describe('SettingsService', () => {
service.set('crash_reports', false);
expect(changedSettings.crash_reports).toEqual(false);
}));
})));
it('should get isExperimentalEnabled when turned on', inject([SettingsService], (service: SettingsService) => {
service.set('experimental_features', true);