blob: 85ef536ab11e7294a494f97d382147f6eb8e78ee [file] [log] [blame]
Sean Condon50855cf2018-12-23 15:37:42 +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 {
17 ChangeDetectorRef,
18 Component, EventEmitter,
19 Input, OnChanges, Output, SimpleChanges,
20} from '@angular/core';
21import {Link, LinkHighlight, UiElement} from '../../models';
22import {LogService} from 'gui2-fw-lib';
Sean Condond88f3662019-04-03 16:35:30 +010023import {NodeVisual, SelectedEvent} from '../nodevisual';
Sean Condon50855cf2018-12-23 15:37:42 +000024import {animate, state, style, transition, trigger} from '@angular/animations';
25
26interface Point {
27 x: number;
28 y: number;
29}
30
Sean Condon50855cf2018-12-23 15:37:42 +000031@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})
48export class LinkSvgComponent extends NodeVisual implements OnChanges {
49 @Input() link: Link;
50 @Input() highlighted: string = '';
Sean Condon91481822019-01-01 13:56:14 +000051 @Input() highlightsEnabled: boolean = true;
Sean Condon50855cf2018-12-23 15:37:42 +000052 @Input() label: string;
Sean Condon1ae15802019-03-02 09:07:18 +000053 @Input() scale = 1.0;
Sean Condon50855cf2018-12-23 15:37:42 +000054 isHighlighted: boolean = false;
Sean Condond88f3662019-04-03 16:35:30 +010055 @Output() selectedEvent = new EventEmitter<SelectedEvent>();
Sean Condon50855cf2018-12-23 15:37:42 +000056 @Output() enhancedEvent = new EventEmitter<Link>();
57 enhanced: boolean = false;
58 labelPosSrc: Point = {x: 0, y: 0};
59 labelPosTgt: Point = {x: 0, y: 0};
60
61 constructor(
62 protected log: LogService,
63 private ref: ChangeDetectorRef
64 ) {
65 super();
66 }
67
68 ngOnChanges(changes: SimpleChanges) {
69 if (changes['linkHighlight']) {
70 const hl: LinkHighlight = changes['linkHighlight'].currentValue;
71 this.highlighted = hl.css;
72 this.label = hl.label;
73 this.isHighlighted = true;
Sean Condon64ea7d22019-04-12 19:39:13 +010074 this.log.debug('Link hightlighted', hl);
75 if (hl.fadems > 0) {
76 setTimeout(() => {
77 this.isHighlighted = false;
78 this.highlighted = '';
79 this.ref.markForCheck();
80 }, hl.fadems);
81 }
Sean Condon50855cf2018-12-23 15:37:42 +000082
83 }
84
85 this.ref.markForCheck();
86 }
87
88 enhance() {
Sean Condon91481822019-01-01 13:56:14 +000089 if (!this.highlightsEnabled) {
90 return;
91 }
Sean Condon50855cf2018-12-23 15:37:42 +000092 this.enhancedEvent.emit(this.link);
93 this.enhanced = true;
94 this.repositionLabels();
95 setTimeout(() => {
96 this.enhanced = false;
97 this.ref.markForCheck();
98 }, 1000);
99 }
100
101 /**
Sean Condon1ae15802019-03-02 09:07:18 +0000102 * We want to place the label for the port about 40 px from the node.
Sean Condon50855cf2018-12-23 15:37:42 +0000103 * If the distance between the nodes is less than 100, then just place the
104 * label 1/3 of the way from the node
105 */
106 repositionLabels(): void {
107 const x1: number = this.link.source.x;
108 const y1: number = this.link.source.y;
109 const x2: number = this.link.target.x;
110 const y2: number = this.link.target.y;
111
112 const dist = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
113 const offset = dist > 100 ? 40 : dist / 3;
114 this.labelPosSrc = <Point>{
115 x: x1 + (x2 - x1) * offset / dist,
116 y: y1 + (y2 - y1) * offset / dist
117 };
118
119 this.labelPosTgt = <Point>{
120 x: x2 - (x2 - x1) * offset / dist,
121 y: y2 - (y2 - y1) * offset / dist
122 };
123 }
124
125 /**
126 * For the 14pt font we are using, the average width seems to be about 8px
127 * @param text The string we want to calculate a width for
128 */
129 textLength(text: string) {
130 return text.length * 8;
131 }
132}