Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +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 | */ |
| 16 | import { Injectable } from '@angular/core'; |
| 17 | import { GlyphService } from './glyph.service'; |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 18 | import { LogService } from '../log.service'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 19 | import { SvgUtilService } from './svgutil.service'; |
| 20 | import * as d3 from 'd3'; |
| 21 | |
| 22 | const vboxSize = 50; |
| 23 | const cornerSize = vboxSize / 10; |
| 24 | const viewBox = '0 0 ' + vboxSize + ' ' + vboxSize; |
| 25 | |
| 26 | export const glyphMapping = new Map<string, string>([ |
| 27 | // Maps icon ID to the glyph ID it uses. |
| 28 | // NOTE: icon ID maps to a CSS class for styling that icon |
| 29 | ['active', 'checkMark'], |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 30 | ['inactive', 'xMark'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 31 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 32 | ['plus', 'plus'], |
| 33 | ['minus', 'minus'], |
| 34 | ['play', 'play'], |
| 35 | ['stop', 'stop'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 36 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 37 | ['upload', 'upload'], |
| 38 | ['download', 'download'], |
| 39 | ['delta', 'delta'], |
| 40 | ['nonzero', 'nonzero'], |
| 41 | ['close', 'xClose'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 42 | |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 43 | ['m_cloud', 'm_cloud'], |
| 44 | ['m_map', 'm_map'], |
| 45 | ['m_selectMap', 'm_selectMap'], |
| 46 | ['thatsNoMoon', 'thatsNoMoon'], |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 47 | ['m_ports', 'm_ports'], |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 48 | ['m_switch', 'm_switch'], |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 49 | ['switch', 'm_switch'], |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 50 | ['m_roadm', 'm_roadm'], |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 51 | ['roadm', 'm_roadm'], |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 52 | ['m_router', 'm_router'], |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 53 | ['router', 'm_router'], |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 54 | ['m_uiAttached', 'm_uiAttached'], |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 55 | ['m_endstation', 'm_endstation'], |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 56 | ['endstation', 'm_endstation'], |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 57 | ['m_summary', 'm_summary'], |
| 58 | ['m_details', 'm_details'], |
| 59 | ['m_oblique', 'm_oblique'], |
| 60 | ['m_filters', 'm_filters'], |
| 61 | ['m_cycleLabels', 'm_cycleLabels'], |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 62 | ['m_cycleGridDisplay', 'm_cycleGridDisplay'], |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 63 | ['m_prev', 'm_prev'], |
| 64 | ['m_next', 'm_next'], |
| 65 | ['m_flows', 'm_flows'], |
| 66 | ['m_allTraffic', 'm_allTraffic'], |
| 67 | ['m_xMark', 'm_xMark'], |
| 68 | ['m_resetZoom', 'm_resetZoom'], |
| 69 | ['m_eqMaster', 'm_eqMaster'], |
| 70 | ['m_unknown', 'm_unknown'], |
| 71 | ['m_controller', 'm_controller'], |
| 72 | ['m_eqMaster', 'm_eqMaster'], |
| 73 | ['m_virtual', 'm_virtual'], |
| 74 | ['m_other', 'm_other'], |
| 75 | ['m_bgpSpeaker', 'm_bgpSpeaker'], |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 76 | ['bgpSpeaker', 'm_bgpSpeaker'], |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 77 | ['m_otn', 'm_otn'], |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 78 | ['otn', 'm_otn'], |
| 79 | ['m_terminal_device', 'm_otn'], |
| 80 | ['m_ols', 'm_roadm'], |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 81 | ['m_roadm_otn', 'm_roadm_otn'], |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 82 | ['roadm_otn', 'm_roadm_otn'], |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 83 | ['m_fiberSwitch', 'm_fiberSwitch'], |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 84 | ['fiber_switch', 'm_fiberSwitch'], |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 85 | ['m_microwave', 'm_microwave'], |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 86 | ['microwave', 'm_microwave'], |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 87 | ['m_relatedIntents', 'm_relatedIntents'], |
| 88 | ['m_intentTraffic', 'm_intentTraffic'], |
| 89 | ['m_firewall', 'm_firewall'], |
| 90 | ['m_balancer', 'm_balancer'], |
| 91 | ['m_ips', 'm_ips'], |
| 92 | ['m_ids', 'm_ids'], |
| 93 | ['m_olt', 'm_olt'], |
| 94 | ['m_onu', 'm_onu'], |
| 95 | ['m_swap', 'm_swap'], |
| 96 | ['m_shortestGeoPath', 'm_shortestGeoPath'], |
| 97 | ['m_source', 'm_source'], |
| 98 | ['m_destination', 'm_destination'], |
| 99 | ['m_topo', 'm_topo'], |
| 100 | ['m_shortestPath', 'm_shortestPath'], |
| 101 | ['m_disjointPaths', 'm_disjointPaths'], |
| 102 | ['m_region', 'm_region'], |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 103 | ['virtual', 'cord'], |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 104 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 105 | ['topo', 'topo'], |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 106 | ['bird', 'bird'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 107 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 108 | ['refresh', 'refresh'], |
| 109 | ['query', 'query'], |
| 110 | ['garbage', 'garbage'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 111 | |
| 112 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 113 | ['upArrow', 'triangleUp'], |
| 114 | ['downArrow', 'triangleDown'], |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 115 | ['triangleLeft', 'triangleLeft'], |
| 116 | ['triangleRight', 'triangleRight'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 117 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 118 | ['appInactive', 'unknown'], |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 119 | ['uiAttached', 'uiAttached'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 120 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 121 | ['node', 'node'], |
| 122 | ['devIcon_SWITCH', 'switch'], |
| 123 | ['devIcon_ROADM', 'roadm'], |
| 124 | ['devIcon_OTN', 'otn'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 125 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 126 | ['portIcon_DEFAULT', 'm_ports'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 127 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 128 | ['meter', 'meterTable'], // TODO: m_meter icon? |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 129 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 130 | ['deviceTable', 'switch'], |
| 131 | ['flowTable', 'flowTable'], |
| 132 | ['portTable', 'portTable'], |
| 133 | ['groupTable', 'groupTable'], |
| 134 | ['meterTable', 'meterTable'], |
| 135 | ['pipeconfTable', 'pipeconfTable'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 136 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 137 | ['hostIcon_endstation', 'endstation'], |
| 138 | ['hostIcon_router', 'router'], |
| 139 | ['hostIcon_bgpSpeaker', 'bgpSpeaker'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 140 | |
| 141 | // navigation menu icons... |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 142 | ['nav_apps', 'bird'], |
| 143 | ['nav_settings', 'cog'], |
| 144 | ['nav_cluster', 'node'], |
| 145 | ['nav_processors', 'allTraffic'], |
Bhavesh Kumar | d0b8bae | 2018-07-31 16:56:43 +0530 | [diff] [blame] | 146 | ['nav_partitions', 'unknown'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 147 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 148 | ['nav_topo', 'topo'], |
| 149 | ['nav_topo2', 'm_cloud'], |
| 150 | ['nav_devs', 'switch'], |
| 151 | ['nav_links', 'ports'], |
| 152 | ['nav_hosts', 'endstation'], |
| 153 | ['nav_intents', 'relatedIntents'], |
| 154 | ['nav_tunnels', 'ports'], // TODO: use tunnel glyph, when available |
| 155 | ['nav_yang', 'yang'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 156 | ]); |
| 157 | |
| 158 | /** |
| 159 | * ONOS GUI -- SVG -- Icon Service |
| 160 | */ |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 161 | @Injectable({ |
| 162 | providedIn: 'root', |
| 163 | }) |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 164 | export class IconService { |
| 165 | |
| 166 | constructor( |
| 167 | private gs: GlyphService, |
| 168 | private log: LogService, |
| 169 | private sus: SvgUtilService |
| 170 | ) { |
| 171 | |
| 172 | this.log.debug('IconService constructed'); |
| 173 | } |
| 174 | |
| 175 | ensureIconLibDefs() { |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 176 | const body = d3.select('body'); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 177 | let svg = body.select('svg#IconLibDefs'); |
| 178 | if (svg.empty()) { |
| 179 | svg = body.append('svg').attr('id', 'IconLibDefs'); |
| 180 | svg.append('defs'); |
| 181 | } |
| 182 | return svg.select('defs'); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Load an icon |
| 187 | * |
| 188 | * @param div A D3 selection of the '<div>' element into which icon should load |
| 189 | * @param glyphId Identifies the glyph to use |
| 190 | * @param size The dimension of icon in pixels. Defaults to 20. |
| 191 | * @param installGlyph If truthy, will cause the glyph to be added to |
| 192 | * well-known defs element. Defaults to false. |
| 193 | * @param svgClass The CSS class used to identify the SVG layer. |
| 194 | * Defaults to 'embeddedIcon'. |
| 195 | */ |
| 196 | loadIcon(div, glyphId: string = 'unknown', size: number = 20, installGlyph: boolean = true, svgClass: string = 'embeddedIcon') { |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 197 | const dim = size || 20; |
| 198 | const svgCls = svgClass || 'embeddedIcon'; |
| 199 | const gid = glyphId || 'unknown'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 200 | let g; |
| 201 | let svgIcon: any; |
| 202 | |
| 203 | if (installGlyph) { |
| 204 | this.gs.loadDefs(this.ensureIconLibDefs(), [gid], true); |
| 205 | } |
| 206 | this.log.warn('loadEmbeddedIcon. install done'); |
| 207 | |
| 208 | svgIcon = div |
| 209 | .append('svg') |
| 210 | .attr('class', svgCls) |
| 211 | .attr('width', dim) |
| 212 | .attr('height', dim) |
| 213 | .attr('viewBox', viewBox); |
| 214 | |
| 215 | g = svgIcon.append('g') |
| 216 | .attr('class', 'icon'); |
| 217 | |
| 218 | g.append('rect') |
| 219 | .attr('width', vboxSize) |
| 220 | .attr('height', vboxSize) |
| 221 | .attr('rx', cornerSize); |
| 222 | |
| 223 | g.append('use') |
| 224 | .attr('width', vboxSize) |
| 225 | .attr('height', vboxSize) |
| 226 | .attr('class', 'glyph') |
| 227 | .attr('xlink:href', '#' + gid); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Load an icon by class. |
| 232 | * @param div A D3 selection of the <DIV> element into which icon should load |
| 233 | * @param iconCls The CSS class used to identify the icon |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 234 | * @param size The dimension of icon in pixels. Defaults to 20. |
| 235 | * @param installGlyph If truthy, will cause the glyph to be added to |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 236 | * well-known defs element. Defaults to false. |
| 237 | * @param svgClass The CSS class used to identify the SVG layer. |
| 238 | * Defaults to 'embeddedIcon'. |
| 239 | */ |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 240 | loadIconByClass(div, iconCls: string, size: number, installGlyph: boolean, svgClass= 'embeddedIcon') { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 241 | this.loadIcon(div, glyphMapping.get(iconCls), size, installGlyph, svgClass); |
| 242 | div.select('svg g').classed(iconCls, true); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Load an embedded icon. |
| 247 | */ |
| 248 | loadEmbeddedIcon(div, iconCls: string, size: number) { |
| 249 | this.loadIconByClass(div, iconCls, size, true); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Load an icon only to the svg defs collection |
| 254 | * |
| 255 | * Note: This is added for use with IconComponent, where the icon's |
| 256 | * svg element is defined in the component template (and not built |
| 257 | * inline using d3 manipulation |
| 258 | * |
| 259 | * @param iconCls The icon class as a string |
| 260 | */ |
| 261 | loadIconDef(iconCls: string): void { |
Sean Condon | 59d3137 | 2019-02-02 20:07:00 +0000 | [diff] [blame] | 262 | let glyphName: string = glyphMapping.get(iconCls); |
| 263 | if (!glyphName) { |
| 264 | glyphName = iconCls; |
| 265 | } |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 266 | this.gs.loadDefs(this.ensureIconLibDefs(), [glyphName], true, [iconCls]); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | |
| 270 | /** |
| 271 | * Add a device icon |
| 272 | * |
| 273 | * Adds a device glyph to the specified element. |
| 274 | * Returns the D3 selection of the glyph (use) element. |
| 275 | */ |
| 276 | addDeviceIcon(elem, glyphId, iconDim) { |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 277 | const gid = this.gs.glyphDefined(glyphId) ? glyphId : 'query'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 278 | return elem.append('use').attr({ |
| 279 | 'xlink:href': '#' + gid, |
| 280 | width: iconDim, |
| 281 | height: iconDim, |
| 282 | }); |
| 283 | } |
| 284 | |
| 285 | addHostIcon(elem, radius, glyphId) { |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 286 | const dim = radius * 1.5; |
| 287 | const xlate = -dim / 2; |
| 288 | const g = elem.append('g') |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 289 | .attr('class', 'svgIcon hostIcon'); |
| 290 | |
| 291 | g.append('circle').attr('r', radius); |
| 292 | |
| 293 | g.append('use').attr({ |
| 294 | 'xlink:href': '#' + glyphId, |
| 295 | width: dim, |
| 296 | height: dim, |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 297 | transform: this.sus.translate([xlate], xlate), |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 298 | }); |
| 299 | return g; |
| 300 | } |
| 301 | |
| 302 | registerIconMapping(iconId, glyphId) { |
| 303 | if (glyphMapping[iconId]) { |
| 304 | this.log.warn('Icon with id', iconId, 'already mapped. Ignoring.'); |
| 305 | } else { |
| 306 | // map icon-->glyph |
| 307 | glyphMapping[iconId] = glyphId; |
| 308 | // make sure definition is installed |
| 309 | this.gs.loadDefs(this.ensureIconLibDefs(), [glyphId], true); |
| 310 | } |
| 311 | } |
| 312 | } |