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 | */ |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 16 | import { |
| 17 | Directive, |
| 18 | ElementRef, |
| 19 | Input, |
| 20 | OnChanges, |
| 21 | OnInit, |
| 22 | SimpleChanges |
| 23 | } from '@angular/core'; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 24 | import * as d3 from 'd3'; |
Sean Condon | 14d442f | 2019-12-09 14:16:19 +0000 | [diff] [blame] | 25 | import {TopoZoomPrefs} from './zoomutils'; |
| 26 | import {LogService} from '../log.service'; |
| 27 | import {PrefsService} from '../util/prefs.service'; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 28 | |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 29 | const TOPO_ZOOM_PREFS = 'topo_zoom'; |
| 30 | |
| 31 | const ZOOM_PREFS_DEFAULT: TopoZoomPrefs = <TopoZoomPrefs>{ |
| 32 | tx: 0, ty: 0, sc: 1.0 |
| 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * A directive that takes care of Zooming and Panning the Topology view |
| 37 | * |
| 38 | * It wraps the D3 Pan and Zoom functionality |
| 39 | * See https://github.com/d3/d3-zoom/blob/master/README.md |
| 40 | */ |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 41 | @Directive({ |
| 42 | selector: '[onosZoomableOf]' |
| 43 | }) |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 44 | export class ZoomableDirective implements OnChanges, OnInit { |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 45 | @Input() zoomableOf: ElementRef; |
| 46 | |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 47 | zoom: any; // The d3 zoom behaviour |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 48 | zoomCached: TopoZoomPrefs = <TopoZoomPrefs>{tx: 0, ty: 0, sc: 1.0}; |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 49 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 50 | constructor( |
| 51 | private _element: ElementRef, |
| 52 | private log: LogService, |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 53 | private ps: PrefsService |
| 54 | ) { |
| 55 | const container = d3.select(this._element.nativeElement); |
| 56 | |
| 57 | const zoomed = () => { |
| 58 | const transform = d3.event.transform; |
Sean Condon | b0a196a | 2019-04-19 09:50:44 +0100 | [diff] [blame] | 59 | container.attr('transform', 'translate(' + transform.x + ',' + transform.y + ') scale(' + transform.k + ')'); |
| 60 | this.updateZoomState(<TopoZoomPrefs>{tx: transform.x, ty: transform.y, sc: transform.k}); |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | this.zoom = d3.zoom().on('zoom', zoomed); |
| 64 | } |
| 65 | |
| 66 | ngOnInit() { |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 67 | this.zoomCached = this.ps.getPrefs(TOPO_ZOOM_PREFS, ZOOM_PREFS_DEFAULT); |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 68 | const svg = d3.select(this.zoomableOf); |
| 69 | |
| 70 | svg.call(this.zoom); |
| 71 | |
| 72 | svg.transition().call(this.zoom.transform, |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 73 | d3.zoomIdentity.translate(this.zoomCached.tx, this.zoomCached.ty).scale(this.zoomCached.sc)); |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 74 | this.log.debug('Loaded topo_zoom_prefs', |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 75 | this.zoomCached.tx, this.zoomCached.ty, this.zoomCached.sc); |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 76 | |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Updates the cache of zoom preferences locally and onwards to the PrefsService |
| 81 | */ |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 82 | updateZoomState(zoomPrefs: TopoZoomPrefs): void { |
| 83 | this.zoomCached = zoomPrefs; |
| 84 | this.ps.setPrefs(TOPO_ZOOM_PREFS, zoomPrefs); |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 85 | } |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 86 | |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 87 | /** |
| 88 | * If the input object is changed then re-establish the zoom |
| 89 | */ |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 90 | ngOnChanges(changes: SimpleChanges): void { |
| 91 | if (changes['zoomableOf']) { |
| 92 | const svg = d3.select(changes['zoomableOf'].currentValue); |
| 93 | svg.call(this.zoom); |
| 94 | this.log.debug('Applying zoomable behaviour on', this.zoomableOf, this._element.nativeElement); |
| 95 | } |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 98 | /** |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 99 | * Change the zoom level when a map is chosen in Topology view |
| 100 | * |
| 101 | * Animated to run over 750ms |
| 102 | */ |
Sean Condon | ff85fbe | 2019-03-16 14:28:46 +0000 | [diff] [blame] | 103 | changeZoomLevel(zoomState: TopoZoomPrefs, fast?: boolean): void { |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 104 | const svg = d3.select(this.zoomableOf); |
Sean Condon | ff85fbe | 2019-03-16 14:28:46 +0000 | [diff] [blame] | 105 | svg.transition().duration(fast ? 0 : 750).call(this.zoom.transform, |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 106 | d3.zoomIdentity.translate(zoomState.tx, zoomState.ty).scale(zoomState.sc)); |
| 107 | this.updateZoomState(zoomState); |
| 108 | this.log.debug('Pan to', zoomState.tx, zoomState.ty, 'and zoom to', zoomState.sc); |
| 109 | } |
| 110 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 111 | } |