From 5da90118c716c57a63735a84bbb2903f4b9be043 Mon Sep 17 00:00:00 2001 From: ziajka Date: Tue, 6 Nov 2018 15:09:14 +0100 Subject: [PATCH] Canvas size detector --- src/app/cartography/cartography.module.ts | 2 ++ .../components/map/map.component.ts | 14 ++++---------- .../helpers/canvas-size-detector.ts | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 src/app/cartography/helpers/canvas-size-detector.ts diff --git a/src/app/cartography/cartography.module.ts b/src/app/cartography/cartography.module.ts index 502ae205..c276d253 100644 --- a/src/app/cartography/cartography.module.ts +++ b/src/app/cartography/cartography.module.ts @@ -15,6 +15,7 @@ import { LayersManager } from './managers/layers-manager'; import { MapChangeDetectorRef } from './services/map-change-detector-ref'; import { Context } from './models/context'; import { D3_MAP_IMPORTS } from './d3-map.imports'; +import { CanvasSizeDetector } from './helpers/canvas-size-detector'; @NgModule({ @@ -36,6 +37,7 @@ import { D3_MAP_IMPORTS } from './d3-map.imports'; QtDasharrayFixer, LayersManager, MapChangeDetectorRef, + CanvasSizeDetector, Context, ...D3_MAP_IMPORTS ], diff --git a/src/app/cartography/components/map/map.component.ts b/src/app/cartography/components/map/map.component.ts index dc9f8b33..81de01e3 100644 --- a/src/app/cartography/components/map/map.component.ts +++ b/src/app/cartography/components/map/map.component.ts @@ -19,6 +19,7 @@ import { LinksWidget } from '../../widgets/links'; import { MapChangeDetectorRef } from '../../services/map-change-detector-ref'; import { NodeDragging, NodeDragged } from '../../events/nodes'; import { LinkCreated } from '../../events/links'; +import { CanvasSizeDetector } from '../../helpers/canvas-size-detector'; @Component({ @@ -51,6 +52,7 @@ export class MapComponent implements OnInit, OnChanges, OnDestroy { constructor( private context: Context, private mapChangeDetectorRef: MapChangeDetectorRef, + private canvasSizeDetector: CanvasSizeDetector, protected element: ElementRef, protected nodesWidget: NodesWidget, protected linksWidget: LinksWidget, @@ -121,7 +123,7 @@ export class MapComponent implements OnInit, OnChanges, OnDestroy { this.context.size = this.getSize(); this.onNodeDraggingSubscription = this.nodesWidget.onNodeDragging.subscribe((eventNode: NodeDragging) => { - const links = this.links.filter((link) => link.target.node_id === eventNode.node.node_id || link.source.node_id === eventNode.node.node_id) + const links = this.links.filter((link) => link.target.node_id === eventNode.node.node_id || link.source.node_id === eventNode.node.node_id); links.forEach((link) => { this.linksWidget.redrawLink(this.svg, link); @@ -144,15 +146,7 @@ export class MapComponent implements OnInit, OnChanges, OnDestroy { } public getSize(): Size { - let width = document.documentElement.clientWidth; - let height = document.documentElement.clientHeight; - if (this.width > width) { - width = this.width; - } - if (this.height > height) { - height = this.height; - } - return new Size(width, height); + return this.canvasSizeDetector.getOptimalSize(this.width, this.height); } protected linkCreated(evt) { diff --git a/src/app/cartography/helpers/canvas-size-detector.ts b/src/app/cartography/helpers/canvas-size-detector.ts new file mode 100644 index 00000000..0efa347a --- /dev/null +++ b/src/app/cartography/helpers/canvas-size-detector.ts @@ -0,0 +1,18 @@ +import { Injectable } from "@angular/core"; +import { Size } from "../models/size"; + + +@Injectable() +export class CanvasSizeDetector { + public getOptimalSize(minWidth: number, minHeight: number) { + let width = document.documentElement.clientWidth; + let height = document.documentElement.clientHeight; + if (minWidth > width) { + width = minWidth; + } + if (minHeight > height) { + height = minHeight; + } + return new Size(width, height); + } +} \ No newline at end of file