blob: e0c85ed61380c6c41a7c5f43b1ddaf98b9d06010 [file] [log] [blame]
Sean Condonf4f54a12018-10-10 23:25:46 +01001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the 'License');
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an 'AS IS' BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { Component, OnInit } from '@angular/core';
17import {
18 FnService,
19 LogService,
20 ZoomService, Zoomer, ZoomOpts
21} from 'gui2-fw-lib';
22
23/**
24 * ONOS GUI -- Topology Zoom Layer View.
25 * View that contains the 'Force graph' message
26 *
27 * This component is an SVG snippet that expects to be in an SVG element with a view box of 1000x1000
28 *
29 * It should be added to a template with a tag like <svg:g onos-zoomlayer />
30 */
31@Component({
32 selector: '[onos-zoomlayer]',
33 templateUrl: './zoomlayersvg.component.html',
34 styleUrls: ['./zoomlayersvg.component.css']
35})
36export class ZoomLayerSvgComponent implements OnInit {
37 zoomer: Zoomer;
38 zoomEventListeners: any[];
39
40 constructor(
41 protected fs: FnService,
42 protected log: LogService,
43 protected zs: ZoomService
44 ) {
45 this.log.debug('ZoomLayerSvgComponent constructed');
46 }
47
48 ngOnInit() {
49
50 }
51
52 createZoomer(options: ZoomOpts) {
53 // need to wrap the original zoom callback to extend its behavior
54 const origCallback = this.fs.isF(options.zoomCallback) ? options.zoomCallback : () => {};
55
56 options.zoomCallback = () => {
57 origCallback([0, 0], 1);
58
59 this.zoomEventListeners.forEach((ev) => ev(this.zoomer));
60 };
61
62 this.zoomer = this.zs.createZoomer(options);
63 return this.zoomer;
64 }
65
66 getZoomer() {
67 return this.zoomer;
68 }
69
70 findZoomEventListener(ev) {
71 for (let i = 0, len = this.zoomEventListeners.length; i < len; i++) {
72 if (this.zoomEventListeners[i] === ev) {
73 return i;
74 }
75 }
76 return -1;
77 }
78
79 addZoomEventListener(callback) {
80 this.zoomEventListeners.push(callback);
81 }
82
83 removeZoomEventListener(callback) {
84 const evIndex = this.findZoomEventListener(callback);
85
86 if (evIndex !== -1) {
87 this.zoomEventListeners.splice(evIndex);
88 }
89 }
90
91 adjustmentScale(min: number, max: number): number {
92 let _scale = 1;
93 const size = (min + max) / 2;
94
95 if (size * this.scale() < max) {
96 _scale = min / (size * this.scale());
97 } else if (size * this.scale() > max) {
98 _scale = min / (size * this.scale());
99 }
100
101 return _scale;
102 }
103
104 scale(): number {
105 return this.zoomer.scale();
106 }
107
108 panAndZoom(translate: number[], scale: number, transition?: number) {
109 this.zoomer.panZoom(translate, scale, transition);
110 }
111
112}