Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | import {Directive, ElementRef, Input, OnChanges} from '@angular/core'; |
| 17 | import {LogService} from 'gui2-fw-lib'; |
| 18 | import * as d3 from 'd3'; |
| 19 | |
| 20 | @Directive({ |
| 21 | selector: '[onosZoomableOf]' |
| 22 | }) |
| 23 | export class ZoomableDirective implements OnChanges { |
| 24 | @Input() zoomableOf: ElementRef; |
| 25 | |
| 26 | constructor( |
| 27 | private _element: ElementRef, |
| 28 | private log: LogService, |
| 29 | ) {} |
| 30 | |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 31 | /** |
| 32 | * If the input object is changed then re-establish the zoom |
| 33 | */ |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 34 | 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 Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 50 | /** |
| 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 Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 59 | } |