Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 { |
| 17 | Component, |
| 18 | Input, |
| 19 | OnChanges, |
| 20 | OnInit, |
| 21 | SimpleChanges |
| 22 | } from '@angular/core'; |
| 23 | |
| 24 | /** |
| 25 | * How to fit in to the 1000 by 100 SVG viewbox |
| 26 | */ |
| 27 | export enum FitOption { |
| 28 | FIT1000WIDE = 'fit1000wide', |
| 29 | FIT1000HIGH = 'fit1000high', |
| 30 | FITNONE = 'fitnone'// 1:1 ratio |
| 31 | } |
| 32 | |
| 33 | const SVG_VIEWBOX_CENTRE = 500; // View box is 0,0,1000,1000 |
| 34 | |
| 35 | @Component({ |
| 36 | selector: '[onos-gridsvg]', |
| 37 | templateUrl: './gridsvg.component.html', |
| 38 | styleUrls: ['./gridsvg.component.css'] |
| 39 | }) |
| 40 | export class GridsvgComponent implements OnInit, OnChanges { |
| 41 | @Input() horizLowerLimit: number = 0; |
| 42 | @Input() horizUpperLimit: number = 1000; |
| 43 | @Input() vertLowerLimit: number = 0; |
| 44 | @Input() vertUpperLimit: number = 1000; |
| 45 | @Input() spacing: number = 100; |
| 46 | @Input() invertVertical: boolean = false; |
| 47 | @Input() gridcolor: string = '#e8e7e1'; // If specifying this in a template use [gridcolor]="'#e8e7e1'" |
| 48 | @Input() centre: boolean = true; |
| 49 | @Input() fit: FitOption = FitOption.FITNONE; |
| 50 | @Input() aspectRatio: number = 1.0; |
| 51 | |
| 52 | gridPointsHoriz: number[]; |
| 53 | gridPointsVert: number[]; |
| 54 | horizCentreOffset: number = 0; |
| 55 | vertCentreOffset: number = 0; |
| 56 | gridScaleX: number = 1.0; |
| 57 | gridScaleY: number = 1.0; |
| 58 | |
| 59 | public static calculateGridPoints(lwr: number, upper: number, step: number): number[] { |
| 60 | const gridPoints = new Array<number>(); |
| 61 | for (let i = lwr; i < upper; i += step) { |
| 62 | gridPoints.push(i); |
| 63 | } |
| 64 | return gridPoints; |
| 65 | } |
| 66 | |
| 67 | public static calcOffset(lwr: number, upper: number): number { |
| 68 | return -((upper + lwr) * (upper - lwr) / ((upper - lwr) * 2) - SVG_VIEWBOX_CENTRE); |
| 69 | } |
| 70 | |
| 71 | public static calcScale(lwr: number, upper: number): number { |
| 72 | return SVG_VIEWBOX_CENTRE * 2 / Math.abs(upper - lwr); |
| 73 | } |
| 74 | |
| 75 | constructor() { } |
| 76 | |
| 77 | ngOnInit() { |
| 78 | this.gridPointsHoriz = GridsvgComponent.calculateGridPoints( |
| 79 | this.horizLowerLimit, this.horizUpperLimit, this.spacing); |
| 80 | this.gridPointsVert = GridsvgComponent.calculateGridPoints( |
| 81 | this.vertLowerLimit, this.vertUpperLimit, this.spacing); |
| 82 | this.horizCentreOffset = GridsvgComponent.calcOffset(this.horizUpperLimit, this.horizLowerLimit); |
| 83 | this.vertCentreOffset = GridsvgComponent.calcOffset(this.vertUpperLimit, this.vertLowerLimit); |
| 84 | this.gridScaleX = this.whichScale(this.fit, true); |
| 85 | this.gridScaleY = this.whichScale(this.fit, false); |
| 86 | } |
| 87 | |
| 88 | ngOnChanges(changes: SimpleChanges) { |
| 89 | if (changes['horizLowerLimit'] || |
| 90 | changes['horizUpperLimit'] || |
| 91 | changes['horizSpacing']) { |
| 92 | this.gridPointsHoriz = GridsvgComponent.calculateGridPoints( |
| 93 | this.horizLowerLimit, this.horizUpperLimit, this.spacing); |
| 94 | this.horizCentreOffset = GridsvgComponent.calcOffset(this.horizUpperLimit, this.horizLowerLimit); |
| 95 | } |
| 96 | if (changes['vertLowerLimit'] || |
| 97 | changes['vertUpperLimit'] || |
| 98 | changes['vertSpacing'] ) { |
| 99 | this.gridPointsVert = GridsvgComponent.calculateGridPoints( |
| 100 | this.vertLowerLimit, this.vertUpperLimit, this.spacing); |
| 101 | this.vertCentreOffset = GridsvgComponent.calcOffset(this.vertUpperLimit, this.vertLowerLimit); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | whichScale(fit: FitOption, isX: boolean): number { |
| 106 | if (fit === FitOption.FIT1000HIGH) { |
| 107 | return GridsvgComponent.calcScale( |
| 108 | this.vertUpperLimit, this.vertLowerLimit) * (isX ? this.aspectRatio : 1.0); |
| 109 | } else if (fit === FitOption.FIT1000WIDE) { |
| 110 | return GridsvgComponent.calcScale( |
| 111 | this.horizUpperLimit, this.horizLowerLimit) * (isX ? 1.0 : this.aspectRatio); |
| 112 | } else { |
| 113 | return 1.0; |
| 114 | } |
| 115 | } |
| 116 | } |