Sean Condon | 50855cf | 2018-12-23 15:37:42 +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 { |
| 17 | ChangeDetectorRef, |
| 18 | Component, EventEmitter, |
| 19 | Input, OnChanges, Output, SimpleChanges, |
| 20 | } from '@angular/core'; |
| 21 | import {Link, LinkHighlight, UiElement} from '../../models'; |
| 22 | import {LogService} from 'gui2-fw-lib'; |
Sean Condon | d88f366 | 2019-04-03 16:35:30 +0100 | [diff] [blame] | 23 | import {NodeVisual, SelectedEvent} from '../nodevisual'; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 24 | import {animate, state, style, transition, trigger} from '@angular/animations'; |
| 25 | |
| 26 | interface Point { |
| 27 | x: number; |
| 28 | y: number; |
| 29 | } |
| 30 | |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 31 | @Component({ |
| 32 | selector: '[onos-linksvg]', |
| 33 | templateUrl: './linksvg.component.html', |
| 34 | styleUrls: ['./linksvg.component.css'], |
| 35 | animations: [ |
| 36 | trigger('linkLabelVisible', [ |
| 37 | state('true', style( { |
| 38 | opacity: 1.0, |
| 39 | })), |
| 40 | state( 'false', style({ |
| 41 | opacity: 0 |
| 42 | })), |
| 43 | transition('false => true', animate('500ms ease-in')), |
| 44 | transition('true => false', animate('1000ms ease-out')) |
| 45 | ]) |
| 46 | ] |
| 47 | }) |
| 48 | export class LinkSvgComponent extends NodeVisual implements OnChanges { |
| 49 | @Input() link: Link; |
| 50 | @Input() highlighted: string = ''; |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 51 | @Input() highlightsEnabled: boolean = true; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 52 | @Input() label: string; |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 53 | @Input() scale = 1.0; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 54 | isHighlighted: boolean = false; |
Sean Condon | d88f366 | 2019-04-03 16:35:30 +0100 | [diff] [blame] | 55 | @Output() selectedEvent = new EventEmitter<SelectedEvent>(); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 56 | @Output() enhancedEvent = new EventEmitter<Link>(); |
| 57 | enhanced: boolean = false; |
| 58 | labelPosSrc: Point = {x: 0, y: 0}; |
| 59 | labelPosTgt: Point = {x: 0, y: 0}; |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 60 | lastTimer: any; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 61 | |
| 62 | constructor( |
| 63 | protected log: LogService, |
| 64 | private ref: ChangeDetectorRef |
| 65 | ) { |
| 66 | super(); |
| 67 | } |
| 68 | |
| 69 | ngOnChanges(changes: SimpleChanges) { |
| 70 | if (changes['linkHighlight']) { |
| 71 | const hl: LinkHighlight = changes['linkHighlight'].currentValue; |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 72 | clearTimeout(this.lastTimer); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 73 | this.highlighted = hl.css; |
| 74 | this.label = hl.label; |
| 75 | this.isHighlighted = true; |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 76 | this.log.debug('Link hightlighted', this.link.id, this.highlighted); |
Sean Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 77 | if (hl.fadems > 0) { |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 78 | this.lastTimer = setTimeout(() => { |
Sean Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 79 | this.isHighlighted = false; |
| 80 | this.highlighted = ''; |
| 81 | this.ref.markForCheck(); |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 82 | }, hl.fadems); // Disappear slightly before next one comes in |
Sean Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 83 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 84 | |
| 85 | } |
| 86 | |
| 87 | this.ref.markForCheck(); |
| 88 | } |
| 89 | |
| 90 | enhance() { |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 91 | if (!this.highlightsEnabled) { |
| 92 | return; |
| 93 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 94 | this.enhancedEvent.emit(this.link); |
| 95 | this.enhanced = true; |
| 96 | this.repositionLabels(); |
| 97 | setTimeout(() => { |
| 98 | this.enhanced = false; |
| 99 | this.ref.markForCheck(); |
| 100 | }, 1000); |
| 101 | } |
| 102 | |
| 103 | /** |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 104 | * We want to place the label for the port about 40 px from the node. |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 105 | * If the distance between the nodes is less than 100, then just place the |
| 106 | * label 1/3 of the way from the node |
| 107 | */ |
| 108 | repositionLabels(): void { |
| 109 | const x1: number = this.link.source.x; |
| 110 | const y1: number = this.link.source.y; |
| 111 | const x2: number = this.link.target.x; |
| 112 | const y2: number = this.link.target.y; |
| 113 | |
| 114 | const dist = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2)); |
| 115 | const offset = dist > 100 ? 40 : dist / 3; |
| 116 | this.labelPosSrc = <Point>{ |
| 117 | x: x1 + (x2 - x1) * offset / dist, |
| 118 | y: y1 + (y2 - y1) * offset / dist |
| 119 | }; |
| 120 | |
| 121 | this.labelPosTgt = <Point>{ |
| 122 | x: x2 - (x2 - x1) * offset / dist, |
| 123 | y: y2 - (y2 - y1) * offset / dist |
| 124 | }; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * For the 14pt font we are using, the average width seems to be about 8px |
| 129 | * @param text The string we want to calculate a width for |
| 130 | */ |
| 131 | textLength(text: string) { |
| 132 | return text.length * 8; |
| 133 | } |
| 134 | } |