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 | */ |
| 16 | import { |
| 17 | ChangeDetectorRef, |
| 18 | Component, |
| 19 | ElementRef, EventEmitter, |
| 20 | Input, |
| 21 | OnChanges, Output, |
| 22 | SimpleChanges, |
| 23 | ViewChild |
| 24 | } from '@angular/core'; |
| 25 | import {Device, LabelToggle} from '../../models'; |
| 26 | import {LogService} from 'gui2-fw-lib'; |
| 27 | |
| 28 | /** |
| 29 | * The Device node in the force graph |
| 30 | * |
| 31 | * Note: here the selector is given square brackets [] so that it can be |
| 32 | * inserted in SVG element like a directive |
| 33 | */ |
| 34 | @Component({ |
| 35 | selector: '[onos-devicenodesvg]', |
| 36 | templateUrl: './devicenodesvg.component.html', |
| 37 | styleUrls: ['./devicenodesvg.component.css'], |
| 38 | // changeDetection: ChangeDetectionStrategy.Default, |
| 39 | // animations: [ |
| 40 | // trigger('deviceLabelToggle', [ |
| 41 | // state('0', style({ // none |
| 42 | // width: '36px', |
| 43 | // })), |
| 44 | // state('1, 2', // id |
| 45 | // style({ width: '{{ txtWidth }}'}), |
| 46 | // { params: {'txtWidth': '36px'}} |
| 47 | // ), // default |
| 48 | // transition('0 => *', animate('1000ms ease-in')), |
| 49 | // transition('* => 0', animate('1000ms ease-out')) |
| 50 | // ]) |
| 51 | // ] |
| 52 | }) |
| 53 | export class DeviceNodeSvgComponent implements OnChanges { |
| 54 | @Input() device: Device; |
| 55 | @Input() scale: number = 1.0; |
| 56 | @Input() labelToggle: LabelToggle = LabelToggle.NONE; |
| 57 | selected: boolean; |
| 58 | @Output() selectedEvent = new EventEmitter<string>(); |
| 59 | textWidth: number = 36; |
| 60 | @ViewChild('idTxt') idTxt: ElementRef; |
| 61 | |
| 62 | constructor( |
| 63 | protected log: LogService, |
| 64 | private ref: ChangeDetectorRef |
| 65 | ) { |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Called by parent (forcesvg) when a change happens |
| 70 | * |
| 71 | * There is a difficulty in passing the SVG text object to the animation |
| 72 | * directly, to get its width, so we capture it here and update textWidth |
| 73 | * local variable here and use it in the animation |
| 74 | */ |
| 75 | ngOnChanges(changes: SimpleChanges) { |
| 76 | if (changes['device']) { |
| 77 | if (!this.device.x) { |
| 78 | this.device.x = 0; |
| 79 | this.device.y = 0; |
| 80 | } |
| 81 | } |
| 82 | if (changes['labelToggle']) { |
| 83 | this.labelToggle = changes['labelToggle'].currentValue; |
| 84 | } |
| 85 | this.ref.markForCheck(); |
| 86 | } |
| 87 | |
| 88 | toggleSelected() { |
| 89 | this.selected = !this.selected; |
| 90 | if (this.selected) { |
| 91 | this.selectedEvent.emit(this.device.id); |
| 92 | } else { |
| 93 | this.selectedEvent.emit(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | deselect() { |
| 98 | this.selected = false; |
| 99 | } |
| 100 | |
| 101 | } |