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