Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 2 | * Copyright 2019-present Open Networking Foundation |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 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 | */ |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 16 | import { |
| 17 | ChangeDetectionStrategy, |
| 18 | ChangeDetectorRef, |
| 19 | Component, |
| 20 | EventEmitter, |
| 21 | HostListener, |
| 22 | Input, |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 23 | OnChanges, OnDestroy, |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 24 | OnInit, |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 25 | Output, |
| 26 | QueryList, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 27 | SimpleChange, |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 28 | SimpleChanges, |
| 29 | ViewChildren |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 30 | } from '@angular/core'; |
Sean Condon | 3dd062f | 2020-04-14 09:25:00 +0100 | [diff] [blame] | 31 | import {LocMeta, LogService, MetaUi, WebSocketService, ZoomUtils} from 'org_onosproject_onos/web/gui2-fw-lib/public_api'; |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 32 | import { |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 33 | Badge, |
| 34 | Device, DeviceHighlight, |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 35 | DeviceProps, |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 36 | ForceDirectedGraph, |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 37 | Host, HostHighlight, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 38 | HostLabelToggle, |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 39 | LabelToggle, |
| 40 | LayerType, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 41 | Link, |
| 42 | LinkHighlight, |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 43 | Location, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 44 | ModelEventMemo, |
| 45 | ModelEventType, |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 46 | Node, |
Sean Condon | d88f366 | 2019-04-03 16:35:30 +0100 | [diff] [blame] | 47 | Options, |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 48 | Region, |
| 49 | RegionLink, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 50 | SubRegion, |
| 51 | UiElement |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 52 | } from './models'; |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 53 | import {LocationType} from '../backgroundsvg/backgroundsvg.component'; |
Sean Condon | ff85fbe | 2019-03-16 14:28:46 +0000 | [diff] [blame] | 54 | import {DeviceNodeSvgComponent} from './visuals/devicenodesvg/devicenodesvg.component'; |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 55 | import {HostNodeSvgComponent} from './visuals/hostnodesvg/hostnodesvg.component'; |
| 56 | import {LinkSvgComponent} from './visuals/linksvg/linksvg.component'; |
Sean Condon | d88f366 | 2019-04-03 16:35:30 +0100 | [diff] [blame] | 57 | import {SelectedEvent} from './visuals/nodevisual'; |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 58 | |
| 59 | interface UpdateMeta { |
| 60 | id: string; |
| 61 | class: string; |
| 62 | memento: MetaUi; |
| 63 | } |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 64 | |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 65 | const SVGCANVAS = <Options>{ |
| 66 | width: 1000, |
| 67 | height: 1000 |
| 68 | }; |
| 69 | |
| 70 | interface ChangeSummary { |
| 71 | numChanges: number; |
| 72 | locationChanged: boolean; |
| 73 | } |
| 74 | |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 75 | /** |
| 76 | * ONOS GUI -- Topology Forces Graph Layer View. |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 77 | * |
| 78 | * The regionData is set by Topology Service on WebSocket topo2CurrentRegion callback |
| 79 | * This drives the whole Force graph |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 80 | */ |
| 81 | @Component({ |
| 82 | selector: '[onos-forcesvg]', |
| 83 | templateUrl: './forcesvg.component.html', |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 84 | styleUrls: ['./forcesvg.component.css'], |
| 85 | changeDetection: ChangeDetectionStrategy.OnPush, |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 86 | }) |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 87 | export class ForceSvgComponent implements OnInit, OnDestroy, OnChanges { |
Sean Condon | ff85fbe | 2019-03-16 14:28:46 +0000 | [diff] [blame] | 88 | @Input() deviceLabelToggle: LabelToggle.Enum = LabelToggle.Enum.NONE; |
| 89 | @Input() hostLabelToggle: HostLabelToggle.Enum = HostLabelToggle.Enum.NONE; |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 90 | @Input() showHosts: boolean = false; |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 91 | @Input() showAlarms: boolean = false; |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 92 | @Input() highlightPorts: boolean = true; |
| 93 | @Input() onosInstMastership: string = ''; |
| 94 | @Input() visibleLayer: LayerType = LayerType.LAYER_DEFAULT; |
| 95 | @Input() selectedLink: RegionLink = null; |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 96 | @Input() scale: number = 1; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 97 | @Input() regionData: Region = <Region>{devices: [ [], [], [] ], hosts: [ [], [], [] ], links: []}; |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 98 | @Output() linkSelected = new EventEmitter<RegionLink>(); |
Sean Condon | d88f366 | 2019-04-03 16:35:30 +0100 | [diff] [blame] | 99 | @Output() selectedNodeEvent = new EventEmitter<UiElement[]>(); |
Sean Condon | ff85fbe | 2019-03-16 14:28:46 +0000 | [diff] [blame] | 100 | public graph: ForceDirectedGraph; |
Sean Condon | d88f366 | 2019-04-03 16:35:30 +0100 | [diff] [blame] | 101 | private selectedNodes: UiElement[] = []; |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 102 | viewInitialized: boolean = false; |
Sean Condon | 00e56d0 | 2020-02-28 09:50:04 +0000 | [diff] [blame] | 103 | linksHighlighted: Map<string, LinkHighlight>; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 104 | |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 105 | // References to the children of this component - these are created in the |
| 106 | // template view with the *ngFor and we get them by a query here |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 107 | @ViewChildren(DeviceNodeSvgComponent) devices: QueryList<DeviceNodeSvgComponent>; |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 108 | @ViewChildren(HostNodeSvgComponent) hosts: QueryList<HostNodeSvgComponent>; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 109 | @ViewChildren(LinkSvgComponent) links: QueryList<LinkSvgComponent>; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 110 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 111 | constructor( |
| 112 | protected log: LogService, |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 113 | private ref: ChangeDetectorRef, |
| 114 | protected wss: WebSocketService |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 115 | ) { |
| 116 | this.selectedLink = null; |
| 117 | this.log.debug('ForceSvgComponent constructed'); |
Sean Condon | 00e56d0 | 2020-02-28 09:50:04 +0000 | [diff] [blame] | 118 | this.linksHighlighted = new Map<string, LinkHighlight>(); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 122 | * Utility for extracting a node name from an endpoint string |
| 123 | * In some cases - have to remove the port number from the end of a device |
| 124 | * name |
| 125 | * @param endPtStr The end point name |
| 126 | */ |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 127 | static extractNodeName(endPtStr: string, portStr: string): string { |
| 128 | if (portStr === undefined || endPtStr === undefined) { |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 129 | return endPtStr; |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 130 | } else if (endPtStr.includes('/')) { |
| 131 | return endPtStr.substr(0, endPtStr.length - portStr.length - 1); |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 132 | } |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 133 | return endPtStr; |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 136 | /** |
| 137 | * Recursive method to compare 2 objects attribute by attribute and update |
| 138 | * the first where a change is detected |
| 139 | * @param existingNode 1st object |
| 140 | * @param updatedNode 2nd object |
| 141 | */ |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 142 | private static updateObject(existingNode: Object, updatedNode: Object): ChangeSummary { |
| 143 | const changed = <ChangeSummary>{numChanges: 0, locationChanged: false}; |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 144 | for (const key of Object.keys(updatedNode)) { |
| 145 | const o = updatedNode[key]; |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 146 | if (['id', 'x', 'y', 'fx', 'fy', 'vx', 'vy', 'index'].some(k => k === key)) { |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 147 | continue; |
| 148 | } else if (o && typeof o === 'object' && o.constructor === Object) { |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 149 | const subChanged = ForceSvgComponent.updateObject(existingNode[key], updatedNode[key]); |
| 150 | changed.numChanges += subChanged.numChanges; |
| 151 | changed.locationChanged = subChanged.locationChanged ? true : changed.locationChanged; |
| 152 | } else if (existingNode === undefined) { |
| 153 | // Copy the whole object |
| 154 | existingNode = updatedNode; |
| 155 | changed.locationChanged = true; |
| 156 | changed.numChanges++; |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 157 | } else if (existingNode[key] !== updatedNode[key]) { |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 158 | if (['locType', 'latOrY', 'longOrX', 'latitude', 'longitude', 'gridX', 'gridY'].some(k => k === key)) { |
| 159 | changed.locationChanged = true; |
| 160 | } |
| 161 | changed.numChanges++; |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 162 | existingNode[key] = updatedNode[key]; |
| 163 | } |
| 164 | } |
| 165 | return changed; |
| 166 | } |
| 167 | |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 168 | @HostListener('window:resize', ['$event']) |
| 169 | onResize(event) { |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 170 | this.graph.restartSimulation(); |
| 171 | this.log.debug('Simulation restart after resize', event); |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 175 | * After the component is initialized create the Force simulation |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 176 | * The list of devices, hosts and links will not have been receieved back |
| 177 | * from the WebSocket yet as this time - they will be updated later through |
| 178 | * ngOnChanges() |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 179 | */ |
| 180 | ngOnInit() { |
| 181 | // Receiving an initialized simulated graph from our custom d3 service |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 182 | this.graph = new ForceDirectedGraph(SVGCANVAS, this.log); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 183 | |
| 184 | /** Binding change detection check on each tick |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 185 | * This along with an onPush change detection strategy should enforce |
| 186 | * checking only when relevant! This improves scripting computation |
| 187 | * duration in a couple of tests I've made, consistently. Also, it makes |
| 188 | * sense to avoid unnecessary checks when we are dealing only with |
| 189 | * simulations data binding. |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 190 | */ |
| 191 | this.graph.ticker.subscribe((simulation) => { |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 192 | // this.log.debug("Force simulation has ticked. Alpha", |
| 193 | // Math.round(simulation.alpha() * 1000) / 1000); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 194 | this.ref.markForCheck(); |
| 195 | }); |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 196 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 197 | this.log.debug('ForceSvgComponent initialized - waiting for nodes and links'); |
| 198 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 202 | * When any one of the inputs get changed by a containing component, this |
| 203 | * gets called automatically. In addition this is called manually by |
| 204 | * topology.service when a response is received from the WebSocket from the |
| 205 | * server |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 206 | * |
| 207 | * The Devices, Hosts and SubRegions are all added to the Node list for the simulation |
| 208 | * The Links are added to the Link list of the simulation. |
| 209 | * Before they are added the Links are associated with Nodes based on their endPt |
| 210 | * |
| 211 | * @param changes - a list of changed @Input(s) |
| 212 | */ |
| 213 | ngOnChanges(changes: SimpleChanges) { |
| 214 | if (changes['regionData']) { |
| 215 | const devices: Device[] = |
| 216 | changes['regionData'].currentValue.devices[this.visibleLayerIdx()]; |
| 217 | const hosts: Host[] = |
| 218 | changes['regionData'].currentValue.hosts[this.visibleLayerIdx()]; |
| 219 | const subRegions: SubRegion[] = changes['regionData'].currentValue.subRegion; |
| 220 | this.graph.nodes = []; |
| 221 | if (devices) { |
| 222 | this.graph.nodes = devices; |
| 223 | } |
| 224 | if (hosts) { |
| 225 | this.graph.nodes = this.graph.nodes.concat(hosts); |
| 226 | } |
| 227 | if (subRegions) { |
| 228 | this.graph.nodes = this.graph.nodes.concat(subRegions); |
| 229 | } |
| 230 | |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 231 | this.graph.nodes.forEach((n) => this.fixPosition(n)); |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 232 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 233 | // Associate the endpoints of each link with a real node |
| 234 | this.graph.links = []; |
| 235 | for (const linkIdx of Object.keys(this.regionData.links)) { |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 236 | const link = this.regionData.links[linkIdx]; |
| 237 | const epA = ForceSvgComponent.extractNodeName(link.epA, link.portA); |
| 238 | if (!this.graph.nodes.find((node) => node.id === epA)) { |
| 239 | this.log.error('ngOnChange Could not find endpoint A', epA, 'for', link); |
| 240 | continue; |
| 241 | } |
| 242 | const epB = ForceSvgComponent.extractNodeName( |
| 243 | link.epB, link.portB); |
| 244 | if (!this.graph.nodes.find((node) => node.id === epB)) { |
| 245 | this.log.error('ngOnChange Could not find endpoint B', epB, 'for', link); |
| 246 | continue; |
| 247 | } |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 248 | this.regionData.links[linkIdx].source = |
| 249 | this.graph.nodes.find((node) => |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 250 | node.id === epA); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 251 | this.regionData.links[linkIdx].target = |
| 252 | this.graph.nodes.find((node) => |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 253 | node.id === epB); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 254 | this.regionData.links[linkIdx].index = Number(linkIdx); |
| 255 | } |
| 256 | |
| 257 | this.graph.links = this.regionData.links; |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 258 | if (this.graph.nodes.length > 0) { |
| 259 | this.graph.reinitSimulation(); |
| 260 | } |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 261 | this.log.debug('ForceSvgComponent input changed', |
| 262 | this.graph.nodes.length, 'nodes,', this.graph.links.length, 'links'); |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 263 | if (!this.viewInitialized) { |
| 264 | this.viewInitialized = true; |
| 265 | if (this.showAlarms) { |
| 266 | this.wss.sendEvent('alarmTopovDisplayStart', {}); |
| 267 | } |
| 268 | } |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 269 | } |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 270 | |
| 271 | if (changes['showAlarms'] && this.viewInitialized) { |
| 272 | if (this.showAlarms) { |
| 273 | this.wss.sendEvent('alarmTopovDisplayStart', {}); |
| 274 | } else { |
| 275 | this.wss.sendEvent('alarmTopovDisplayStop', {}); |
| 276 | this.cancelAllDeviceHighlightsNow(); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | ngOnDestroy(): void { |
| 282 | if (this.showAlarms) { |
| 283 | this.wss.sendEvent('alarmTopovDisplayStop', {}); |
| 284 | this.cancelAllDeviceHighlightsNow(); |
| 285 | } |
| 286 | this.viewInitialized = false; |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 287 | } |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 288 | |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 289 | /** |
Sean Condon | 058804c | 2019-04-16 09:41:52 +0100 | [diff] [blame] | 290 | * If instance has a value then mute colors of devices not connected to it |
| 291 | * Otherwise if instance does not have a value unmute all |
| 292 | * @param instanceName name of the selected instance |
| 293 | */ |
| 294 | changeInstSelection(instanceName: string) { |
| 295 | this.log.debug('Mastership changed', instanceName); |
| 296 | this.devices.filter((d) => d.device.master !== instanceName) |
| 297 | .forEach((d) => { |
| 298 | const isMuted = Boolean(instanceName); |
| 299 | d.ngOnChanges({'colorMuted': new SimpleChange(!isMuted, isMuted, true)}); |
| 300 | } |
| 301 | ); |
| 302 | } |
| 303 | |
| 304 | /** |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 305 | * If a node has a fixed location then assign it to fx and fy so |
| 306 | * that it doesn't get affected by forces |
| 307 | * @param graphNode The node whose location should be processed |
| 308 | */ |
| 309 | private fixPosition(graphNode: Node): void { |
| 310 | const loc: Location = <Location>graphNode['location']; |
| 311 | const props: DeviceProps = <DeviceProps>graphNode['props']; |
| 312 | const metaUi = <MetaUi>graphNode['metaUi']; |
| 313 | if (loc && loc.locType === LocationType.GEO) { |
| 314 | const position: MetaUi = |
| 315 | ZoomUtils.convertGeoToCanvas( |
| 316 | <LocMeta>{lng: loc.longOrX, lat: loc.latOrY}); |
| 317 | graphNode.fx = position.x; |
| 318 | graphNode.fy = position.y; |
| 319 | this.log.debug('Found node', graphNode.id, 'with', loc.locType); |
| 320 | } else if (loc && loc.locType === LocationType.GRID) { |
| 321 | graphNode.fx = loc.longOrX; |
| 322 | graphNode.fy = loc.latOrY; |
| 323 | this.log.debug('Found node', graphNode.id, 'with', loc.locType); |
| 324 | } else if (props && props.locType === LocationType.NONE && metaUi) { |
| 325 | graphNode.fx = metaUi.x; |
| 326 | graphNode.fy = metaUi.y; |
| 327 | this.log.debug('Found node', graphNode.id, 'with locType=none and metaUi'); |
| 328 | } else { |
| 329 | graphNode.fx = null; |
| 330 | graphNode.fy = null; |
| 331 | } |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Get the index of LayerType so it can drive the visibility of nodes and |
| 336 | * hosts on layers |
| 337 | */ |
| 338 | visibleLayerIdx(): number { |
| 339 | const layerKeys: string[] = Object.keys(LayerType); |
| 340 | for (const idx in layerKeys) { |
| 341 | if (LayerType[layerKeys[idx]] === this.visibleLayer) { |
| 342 | return Number(idx); |
| 343 | } |
| 344 | } |
| 345 | return -1; |
| 346 | } |
| 347 | |
| 348 | selectLink(link: RegionLink): void { |
| 349 | this.selectedLink = link; |
| 350 | this.linkSelected.emit(link); |
| 351 | } |
| 352 | |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 353 | /** |
Sean Condon | d88f366 | 2019-04-03 16:35:30 +0100 | [diff] [blame] | 354 | * Iterate through all hosts and devices and links to deselect the previously selected |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 355 | * node. The emit an event to the parent that lets it know the selection has |
| 356 | * changed. |
Sean Condon | d88f366 | 2019-04-03 16:35:30 +0100 | [diff] [blame] | 357 | * |
| 358 | * This function collates all of the nodes that have been selected and passes |
| 359 | * a collection of nodes up to the topology component |
| 360 | * |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 361 | * @param selectedNode the newly selected node |
| 362 | */ |
Sean Condon | d88f366 | 2019-04-03 16:35:30 +0100 | [diff] [blame] | 363 | updateSelected(selectedNode: SelectedEvent): void { |
| 364 | this.log.debug('Node or link ', |
Sean Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 365 | selectedNode.uiElement ? selectedNode.uiElement.id : '--', |
Sean Condon | d88f366 | 2019-04-03 16:35:30 +0100 | [diff] [blame] | 366 | selectedNode.deselecting ? 'deselected' : 'selected', |
| 367 | selectedNode.isShift ? 'Multiple' : ''); |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 368 | |
Sean Condon | d88f366 | 2019-04-03 16:35:30 +0100 | [diff] [blame] | 369 | if (selectedNode.isShift && selectedNode.deselecting) { |
| 370 | const idx = this.selectedNodes.findIndex((n) => |
| 371 | n.id === selectedNode.uiElement.id |
| 372 | ); |
| 373 | this.selectedNodes.splice(idx, 1); |
| 374 | this.log.debug('Removed node', idx); |
| 375 | |
| 376 | } else if (selectedNode.isShift) { |
| 377 | this.selectedNodes.push(selectedNode.uiElement); |
| 378 | |
| 379 | } else if (selectedNode.deselecting) { |
Sean Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 380 | this.devices |
| 381 | .forEach((d) => d.deselect()); |
| 382 | this.hosts |
| 383 | .forEach((h) => h.deselect()); |
| 384 | this.links |
| 385 | .forEach((l) => l.deselect()); |
Sean Condon | d88f366 | 2019-04-03 16:35:30 +0100 | [diff] [blame] | 386 | this.selectedNodes = []; |
| 387 | |
| 388 | } else { |
| 389 | const selNodeId = selectedNode.uiElement.id; |
| 390 | // Otherwise if shift was not pressed deselect previous |
| 391 | this.devices |
| 392 | .filter((d) => d.device.id !== selNodeId) |
| 393 | .forEach((d) => d.deselect()); |
| 394 | this.hosts |
| 395 | .filter((h) => h.host.id !== selNodeId) |
| 396 | .forEach((h) => h.deselect()); |
| 397 | |
| 398 | this.links |
| 399 | .filter((l) => l.link.id !== selNodeId) |
| 400 | .forEach((l) => l.deselect()); |
| 401 | |
| 402 | this.selectedNodes = [selectedNode.uiElement]; |
| 403 | } |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 404 | // Push the changes back up to parent (Topology Component) |
Sean Condon | d88f366 | 2019-04-03 16:35:30 +0100 | [diff] [blame] | 405 | this.selectedNodeEvent.emit(this.selectedNodes); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 408 | /** |
| 409 | * We want to filter links to show only those not related to hosts if the |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 410 | * 'showHosts' flag has been switched off. If 'showHosts' is true, then |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 411 | * display all links. |
| 412 | */ |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 413 | filteredLinks(): Link[] { |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 414 | return this.regionData.links.filter((h) => |
| 415 | this.showHosts || |
| 416 | ((<Host>h.source).nodeType !== 'host' && |
| 417 | (<Host>h.target).nodeType !== 'host')); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 418 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 419 | |
| 420 | /** |
| 421 | * When changes happen in the model, then model events are sent up through the |
| 422 | * Web Socket |
| 423 | * @param type - the type of the change |
| 424 | * @param memo - a qualifier on the type |
| 425 | * @param subject - the item that the update is for |
| 426 | * @param data - the new definition of the item |
| 427 | */ |
| 428 | handleModelEvent(type: ModelEventType, memo: ModelEventMemo, subject: string, data: UiElement): void { |
| 429 | switch (type) { |
| 430 | case ModelEventType.DEVICE_ADDED_OR_UPDATED: |
| 431 | if (memo === ModelEventMemo.ADDED) { |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 432 | this.fixPosition(<Device>data); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 433 | this.graph.nodes.push(<Device>data); |
Sean Condon | ee5d4b9 | 2019-03-11 19:57:34 +0000 | [diff] [blame] | 434 | this.regionData.devices[this.visibleLayerIdx()].push(<Device>data); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 435 | this.log.debug('Device added', (<Device>data).id); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 436 | } else if (memo === ModelEventMemo.UPDATED) { |
| 437 | const oldDevice: Device = |
| 438 | this.regionData.devices[this.visibleLayerIdx()] |
| 439 | .find((d) => d.id === subject); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 440 | const changes = ForceSvgComponent.updateObject(oldDevice, <Device>data); |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 441 | if (changes.numChanges > 0) { |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 442 | this.log.debug('Device ', oldDevice.id, memo, ' - ', changes, 'changes'); |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 443 | if (changes.locationChanged) { |
| 444 | this.fixPosition(oldDevice); |
| 445 | } |
Sean Condon | 058804c | 2019-04-16 09:41:52 +0100 | [diff] [blame] | 446 | const svgDevice: DeviceNodeSvgComponent = |
| 447 | this.devices.find((svgdevice) => svgdevice.device.id === subject); |
| 448 | svgDevice.ngOnChanges({'device': |
| 449 | new SimpleChange(<Device>{}, oldDevice, true) |
| 450 | }); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 451 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 452 | } else { |
| 453 | this.log.warn('Device ', memo, ' - not yet implemented', data); |
| 454 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 455 | break; |
| 456 | case ModelEventType.HOST_ADDED_OR_UPDATED: |
| 457 | if (memo === ModelEventMemo.ADDED) { |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 458 | this.fixPosition(<Host>data); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 459 | this.graph.nodes.push(<Host>data); |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 460 | this.regionData.hosts[this.visibleLayerIdx()].push(<Host>data); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 461 | this.log.debug('Host added', (<Host>data).id); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 462 | } else if (memo === ModelEventMemo.UPDATED) { |
| 463 | const oldHost: Host = this.regionData.hosts[this.visibleLayerIdx()] |
| 464 | .find((h) => h.id === subject); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 465 | const changes = ForceSvgComponent.updateObject(oldHost, <Host>data); |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 466 | if (changes.numChanges > 0) { |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 467 | this.log.debug('Host ', oldHost.id, memo, ' - ', changes, 'changes'); |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 468 | if (changes.locationChanged) { |
| 469 | this.fixPosition(oldHost); |
| 470 | } |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 471 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 472 | } else { |
| 473 | this.log.warn('Host change', memo, ' - unexpected'); |
| 474 | } |
| 475 | break; |
| 476 | case ModelEventType.DEVICE_REMOVED: |
| 477 | if (memo === ModelEventMemo.REMOVED || memo === undefined) { |
| 478 | const removeIdx: number = |
| 479 | this.regionData.devices[this.visibleLayerIdx()] |
| 480 | .findIndex((d) => d.id === subject); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 481 | this.regionData.devices[this.visibleLayerIdx()].splice(removeIdx, 1); |
| 482 | this.removeRelatedLinks(subject); |
| 483 | this.log.debug('Device ', subject, 'removed. Links', this.regionData.links); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 484 | } else { |
| 485 | this.log.warn('Device removed - unexpected memo', memo); |
| 486 | } |
| 487 | break; |
| 488 | case ModelEventType.HOST_REMOVED: |
| 489 | if (memo === ModelEventMemo.REMOVED || memo === undefined) { |
| 490 | const removeIdx: number = |
| 491 | this.regionData.hosts[this.visibleLayerIdx()] |
| 492 | .findIndex((h) => h.id === subject); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 493 | this.regionData.hosts[this.visibleLayerIdx()].splice(removeIdx, 1); |
| 494 | this.removeRelatedLinks(subject); |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 495 | this.log.debug('Host ', subject, 'removed'); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 496 | } else { |
| 497 | this.log.warn('Host removed - unexpected memo', memo); |
| 498 | } |
| 499 | break; |
| 500 | case ModelEventType.LINK_ADDED_OR_UPDATED: |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 501 | if (memo === ModelEventMemo.ADDED && |
| 502 | this.regionData.links.findIndex((l) => l.id === subject) === -1) { |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 503 | const newLink = <RegionLink>data; |
| 504 | |
| 505 | |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 506 | const epA = ForceSvgComponent.extractNodeName( |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 507 | newLink.epA, newLink.portA); |
| 508 | if (!this.graph.nodes.find((node) => node.id === epA)) { |
| 509 | this.log.error('Could not find endpoint A', epA, 'of', newLink); |
| 510 | break; |
| 511 | } |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 512 | const epB = ForceSvgComponent.extractNodeName( |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 513 | newLink.epB, newLink.portB); |
| 514 | if (!this.graph.nodes.find((node) => node.id === epB)) { |
| 515 | this.log.error('Could not find endpoint B', epB, 'of link', newLink); |
| 516 | break; |
| 517 | } |
| 518 | |
| 519 | const listLen = this.regionData.links.push(<RegionLink>data); |
| 520 | this.regionData.links[listLen - 1].source = |
| 521 | this.graph.nodes.find((node) => node.id === epA); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 522 | this.regionData.links[listLen - 1].target = |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 523 | this.graph.nodes.find((node) => node.id === epB); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 524 | this.log.debug('Link added', subject); |
| 525 | } else if (memo === ModelEventMemo.UPDATED) { |
| 526 | const oldLink = this.regionData.links.find((l) => l.id === subject); |
| 527 | const changes = ForceSvgComponent.updateObject(oldLink, <RegionLink>data); |
| 528 | this.log.debug('Link ', subject, '. Updated', changes, 'items'); |
| 529 | } else { |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 530 | this.log.warn('Link event ignored', subject, data); |
| 531 | } |
| 532 | break; |
| 533 | case ModelEventType.LINK_REMOVED: |
| 534 | if (memo === ModelEventMemo.REMOVED) { |
| 535 | const removeIdx = this.regionData.links.findIndex((l) => l.id === subject); |
| 536 | this.regionData.links.splice(removeIdx, 1); |
| 537 | this.log.debug('Link ', subject, 'removed'); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 538 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 539 | break; |
| 540 | default: |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 541 | this.log.error('Unexpected model event', type, 'for', subject, 'Data', data); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 542 | } |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 543 | this.graph.links = this.regionData.links; |
| 544 | this.graph.reinitSimulation(); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 547 | private removeRelatedLinks(subject: string) { |
| 548 | const len = this.regionData.links.length; |
| 549 | for (let i = 0; i < len; i++) { |
| 550 | const linkIdx = this.regionData.links.findIndex((l) => |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 551 | (ForceSvgComponent.extractNodeName(l.epA, l.portA) === subject || |
| 552 | ForceSvgComponent.extractNodeName(l.epB, l.portB) === subject)); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 553 | if (linkIdx >= 0) { |
| 554 | this.regionData.links.splice(linkIdx, 1); |
| 555 | this.log.debug('Link ', linkIdx, 'removed on attempt', i); |
| 556 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * When traffic monitoring is turned on (A key) highlights will be sent back |
| 562 | * from the WebSocket through the Traffic Service |
Sean Condon | 4e55c80 | 2019-12-03 22:13:34 +0000 | [diff] [blame] | 563 | * Also handles Intent highlights in case one is selected |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 564 | * @param devices - an array of device highlights |
| 565 | * @param hosts - an array of host highlights |
| 566 | * @param links - an array of link highlights |
| 567 | */ |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 568 | handleHighlights(devices: DeviceHighlight[], hosts: HostHighlight[], links: LinkHighlight[], fadeMs: number = 0): void { |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 569 | |
| 570 | if (devices.length > 0) { |
| 571 | this.log.debug(devices.length, 'Devices highlighted'); |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 572 | devices.forEach((dh: DeviceHighlight) => { |
| 573 | this.devices.forEach((d: DeviceNodeSvgComponent) => { |
| 574 | if (d.device.id === dh.id) { |
| 575 | d.badge = dh.badge; |
| 576 | this.ref.markForCheck(); // Forces ngOnChange in the DeviceSvgComponent |
| 577 | this.log.debug('Highlighting device', dh.id); |
| 578 | } |
| 579 | }); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 580 | }); |
| 581 | } |
| 582 | if (hosts.length > 0) { |
| 583 | this.log.debug(hosts.length, 'Hosts highlighted'); |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 584 | hosts.forEach((hh: HostHighlight) => { |
| 585 | this.hosts.forEach((h) => { |
| 586 | if (h.host.id === hh.id) { |
| 587 | h.badge = hh.badge; |
| 588 | this.ref.markForCheck(); // Forces ngOnChange in the HostSvgComponent |
| 589 | this.log.debug('Highlighting host', hh.id); |
| 590 | } |
| 591 | }); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 592 | }); |
| 593 | } |
| 594 | if (links.length > 0) { |
| 595 | this.log.debug(links.length, 'Links highlighted'); |
Sean Condon | 4e55c80 | 2019-12-03 22:13:34 +0000 | [diff] [blame] | 596 | links.forEach((lh: LinkHighlight) => { |
| 597 | if (fadeMs > 0) { |
| 598 | lh.fadems = fadeMs; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 599 | } |
Sean Condon | 00e56d0 | 2020-02-28 09:50:04 +0000 | [diff] [blame] | 600 | this.linksHighlighted.set(Link.linkIdFromShowHighlights(lh.id), lh); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 601 | }); |
Sean Condon | 00e56d0 | 2020-02-28 09:50:04 +0000 | [diff] [blame] | 602 | this.ref.detectChanges(); // Forces ngOnChange in the LinkSvgComponent |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 605 | |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 606 | cancelAllHostHighlightsNow() { |
| 607 | this.hosts.forEach((host: HostNodeSvgComponent) => { |
| 608 | host.badge = undefined; |
| 609 | this.ref.markForCheck(); // Forces ngOnChange in the HostSvgComponent |
| 610 | }); |
| 611 | } |
| 612 | |
| 613 | cancelAllDeviceHighlightsNow() { |
| 614 | this.devices.forEach((device: DeviceNodeSvgComponent) => { |
| 615 | device.badge = undefined; |
| 616 | this.ref.markForCheck(); // Forces ngOnChange in the DeviceSvgComponent |
| 617 | }); |
| 618 | } |
| 619 | |
Sean Condon | 4e55c80 | 2019-12-03 22:13:34 +0000 | [diff] [blame] | 620 | cancelAllLinkHighlightsNow() { |
| 621 | this.links.forEach((link: LinkSvgComponent) => { |
| 622 | link.linkHighlight = <LinkHighlight>{}; |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 623 | this.ref.markForCheck(); // Forces ngOnChange in the LinkSvgComponent |
Sean Condon | 4e55c80 | 2019-12-03 22:13:34 +0000 | [diff] [blame] | 624 | }); |
| 625 | } |
| 626 | |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 627 | /** |
| 628 | * As nodes are dragged around the graph, their new location should be sent |
| 629 | * back to server |
| 630 | * @param klass The class of node e.g. 'host' or 'device' |
| 631 | * @param id - the ID of the node |
| 632 | * @param newLocation - the new Location of the node |
| 633 | */ |
| 634 | nodeMoved(klass: string, id: string, newLocation: MetaUi) { |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 635 | this.wss.sendEvent('updateMeta2', <UpdateMeta>{ |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 636 | id: id, |
| 637 | class: klass, |
| 638 | memento: newLocation |
| 639 | }); |
| 640 | this.log.debug(klass, id, 'has been moved to', newLocation); |
| 641 | } |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 642 | |
Sean Condon | 9de2135 | 2019-04-06 19:22:27 +0100 | [diff] [blame] | 643 | /** |
| 644 | * If any nodes with fixed positions had been dragged out of place |
| 645 | * then put back where they belong |
| 646 | * If there are some devices selected reset only these |
| 647 | */ |
| 648 | resetNodeLocations(): number { |
| 649 | let numbernodes = 0; |
| 650 | if (this.selectedNodes.length > 0) { |
| 651 | this.devices |
| 652 | .filter((d) => this.selectedNodes.some((s) => s.id === d.device.id)) |
| 653 | .forEach((dev) => { |
| 654 | Node.resetNodeLocation(<Node>dev.device); |
| 655 | numbernodes++; |
| 656 | }); |
| 657 | this.hosts |
| 658 | .filter((h) => this.selectedNodes.some((s) => s.id === h.host.id)) |
| 659 | .forEach((h) => { |
| 660 | Host.resetNodeLocation(<Host>h.host); |
| 661 | numbernodes++; |
| 662 | }); |
| 663 | } else { |
| 664 | this.devices.forEach((dev) => { |
| 665 | Node.resetNodeLocation(<Node>dev.device); |
| 666 | numbernodes++; |
| 667 | }); |
| 668 | this.hosts.forEach((h) => { |
| 669 | Host.resetNodeLocation(<Host>h.host); |
| 670 | numbernodes++; |
| 671 | }); |
| 672 | } |
| 673 | this.graph.reinitSimulation(); |
| 674 | return numbernodes; |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 675 | } |
Sean Condon | 9de2135 | 2019-04-06 19:22:27 +0100 | [diff] [blame] | 676 | |
| 677 | /** |
| 678 | * Toggle floating nodes between unpinned and frozen |
| 679 | * There may be frozen and unpinned in the selection |
| 680 | * |
| 681 | * If there are nodes selected toggle only these |
| 682 | */ |
| 683 | unpinOrFreezeNodes(freeze: boolean): number { |
| 684 | let numbernodes = 0; |
| 685 | if (this.selectedNodes.length > 0) { |
| 686 | this.devices |
| 687 | .filter((d) => this.selectedNodes.some((s) => s.id === d.device.id)) |
| 688 | .forEach((d) => { |
| 689 | Node.unpinOrFreezeNode(<Node>d.device, freeze); |
| 690 | numbernodes++; |
| 691 | }); |
| 692 | this.hosts |
| 693 | .filter((h) => this.selectedNodes.some((s) => s.id === h.host.id)) |
| 694 | .forEach((h) => { |
| 695 | Node.unpinOrFreezeNode(<Node>h.host, freeze); |
| 696 | numbernodes++; |
| 697 | }); |
| 698 | } else { |
| 699 | this.devices.forEach((d) => { |
| 700 | Node.unpinOrFreezeNode(<Node>d.device, freeze); |
| 701 | numbernodes++; |
| 702 | }); |
| 703 | this.hosts.forEach((h) => { |
| 704 | Node.unpinOrFreezeNode(<Node>h.host, freeze); |
| 705 | numbernodes++; |
| 706 | }); |
| 707 | } |
| 708 | this.graph.reinitSimulation(); |
| 709 | return numbernodes; |
| 710 | } |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 711 | } |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 712 | |