blob: bd69db10eb0b7bc8d6be293533e6e1a12f68bb1d [file] [log] [blame]
Sean Condon0c577f62018-11-18 22:40:05 +00001/*
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 {Directive, ElementRef, Input, OnChanges} from '@angular/core';
17import {LogService} from 'gui2-fw-lib';
18import * as d3 from 'd3';
19
20@Directive({
21 selector: '[onosZoomableOf]'
22})
23export class ZoomableDirective implements OnChanges {
24 @Input() zoomableOf: ElementRef;
25
26 constructor(
27 private _element: ElementRef,
28 private log: LogService,
29 ) {}
30
Sean Condon91481822019-01-01 13:56:14 +000031 /**
32 * If the input object is changed then re-establish the zoom
33 */
Sean Condon0c577f62018-11-18 22:40:05 +000034 ngOnChanges() {
35 let zoomed, zoom;
36
37 const svg = d3.select(this.zoomableOf);
38 const container = d3.select(this._element.nativeElement);
39
40 zoomed = () => {
41 const transform = d3.event.transform;
42 container.attr('transform', 'translate(' + transform.x + ',' + transform.y + ') scale(' + transform.k + ')');
43 };
44
45 zoom = d3.zoom().on('zoom', zoomed);
46 svg.call(zoom);
47 this.log.debug('Applying zoomable behaviour on', this.zoomableOf, this._element.nativeElement);
48 }
49
Sean Condon91481822019-01-01 13:56:14 +000050 /**
51 * Reset the zoom when the R key is pressed when in Topology view
52 */
53 resetZoom(): void {
54 const container = d3.select(this._element.nativeElement);
55 container.attr('transform', 'translate(0,0) scale(1.0)');
56 this.log.debug('Pan to 0,0 and zoom to 1.0');
57 }
58
Sean Condon0c577f62018-11-18 22:40:05 +000059}