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 | |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 43 | ['m_ports', 'm_ports'], |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 44 | ['m_switch', 'm_switch'], |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 45 | ['m_roadm', 'm_roadm'], |
| 46 | ['m_router', 'm_router'], |
| 47 | ['m_endstation', 'm_endstation'], |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 48 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 49 | ['topo', 'topo'], |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 50 | ['bird', 'bird'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 51 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 52 | ['refresh', 'refresh'], |
| 53 | ['query', 'query'], |
| 54 | ['garbage', 'garbage'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 55 | |
| 56 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 57 | ['upArrow', 'triangleUp'], |
| 58 | ['downArrow', 'triangleDown'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 59 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 60 | ['appInactive', 'unknown'], |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 61 | ['uiAttached', 'uiAttached'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 62 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 63 | ['node', 'node'], |
| 64 | ['devIcon_SWITCH', 'switch'], |
| 65 | ['devIcon_ROADM', 'roadm'], |
| 66 | ['devIcon_OTN', 'otn'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 67 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 68 | ['portIcon_DEFAULT', 'm_ports'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 69 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 70 | ['meter', 'meterTable'], // TODO: m_meter icon? |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 71 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 72 | ['deviceTable', 'switch'], |
| 73 | ['flowTable', 'flowTable'], |
| 74 | ['portTable', 'portTable'], |
| 75 | ['groupTable', 'groupTable'], |
| 76 | ['meterTable', 'meterTable'], |
| 77 | ['pipeconfTable', 'pipeconfTable'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 78 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 79 | ['hostIcon_endstation', 'endstation'], |
| 80 | ['hostIcon_router', 'router'], |
| 81 | ['hostIcon_bgpSpeaker', 'bgpSpeaker'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 82 | |
| 83 | // navigation menu icons... |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 84 | ['nav_apps', 'bird'], |
| 85 | ['nav_settings', 'cog'], |
| 86 | ['nav_cluster', 'node'], |
| 87 | ['nav_processors', 'allTraffic'], |
Bhavesh Kumar | d0b8bae | 2018-07-31 16:56:43 +0530 | [diff] [blame] | 88 | ['nav_partitions', 'unknown'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 89 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 90 | ['nav_topo', 'topo'], |
| 91 | ['nav_topo2', 'm_cloud'], |
| 92 | ['nav_devs', 'switch'], |
| 93 | ['nav_links', 'ports'], |
| 94 | ['nav_hosts', 'endstation'], |
| 95 | ['nav_intents', 'relatedIntents'], |
| 96 | ['nav_tunnels', 'ports'], // TODO: use tunnel glyph, when available |
| 97 | ['nav_yang', 'yang'], |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 98 | ]); |
| 99 | |
| 100 | /** |
| 101 | * ONOS GUI -- SVG -- Icon Service |
| 102 | */ |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 103 | @Injectable({ |
| 104 | providedIn: 'root', |
| 105 | }) |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 106 | export class IconService { |
| 107 | |
| 108 | constructor( |
| 109 | private gs: GlyphService, |
| 110 | private log: LogService, |
| 111 | private sus: SvgUtilService |
| 112 | ) { |
| 113 | |
| 114 | this.log.debug('IconService constructed'); |
| 115 | } |
| 116 | |
| 117 | ensureIconLibDefs() { |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 118 | const body = d3.select('body'); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 119 | let svg = body.select('svg#IconLibDefs'); |
| 120 | if (svg.empty()) { |
| 121 | svg = body.append('svg').attr('id', 'IconLibDefs'); |
| 122 | svg.append('defs'); |
| 123 | } |
| 124 | return svg.select('defs'); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Load an icon |
| 129 | * |
| 130 | * @param div A D3 selection of the '<div>' element into which icon should load |
| 131 | * @param glyphId Identifies the glyph to use |
| 132 | * @param size The dimension of icon in pixels. Defaults to 20. |
| 133 | * @param installGlyph If truthy, will cause the glyph to be added to |
| 134 | * well-known defs element. Defaults to false. |
| 135 | * @param svgClass The CSS class used to identify the SVG layer. |
| 136 | * Defaults to 'embeddedIcon'. |
| 137 | */ |
| 138 | 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] | 139 | const dim = size || 20; |
| 140 | const svgCls = svgClass || 'embeddedIcon'; |
| 141 | const gid = glyphId || 'unknown'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 142 | let g; |
| 143 | let svgIcon: any; |
| 144 | |
| 145 | if (installGlyph) { |
| 146 | this.gs.loadDefs(this.ensureIconLibDefs(), [gid], true); |
| 147 | } |
| 148 | this.log.warn('loadEmbeddedIcon. install done'); |
| 149 | |
| 150 | svgIcon = div |
| 151 | .append('svg') |
| 152 | .attr('class', svgCls) |
| 153 | .attr('width', dim) |
| 154 | .attr('height', dim) |
| 155 | .attr('viewBox', viewBox); |
| 156 | |
| 157 | g = svgIcon.append('g') |
| 158 | .attr('class', 'icon'); |
| 159 | |
| 160 | g.append('rect') |
| 161 | .attr('width', vboxSize) |
| 162 | .attr('height', vboxSize) |
| 163 | .attr('rx', cornerSize); |
| 164 | |
| 165 | g.append('use') |
| 166 | .attr('width', vboxSize) |
| 167 | .attr('height', vboxSize) |
| 168 | .attr('class', 'glyph') |
| 169 | .attr('xlink:href', '#' + gid); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Load an icon by class. |
| 174 | * @param div A D3 selection of the <DIV> element into which icon should load |
| 175 | * @param iconCls The CSS class used to identify the icon |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 176 | * @param size The dimension of icon in pixels. Defaults to 20. |
| 177 | * @param installGlyph If truthy, will cause the glyph to be added to |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 178 | * well-known defs element. Defaults to false. |
| 179 | * @param svgClass The CSS class used to identify the SVG layer. |
| 180 | * Defaults to 'embeddedIcon'. |
| 181 | */ |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 182 | loadIconByClass(div, iconCls: string, size: number, installGlyph: boolean, svgClass= 'embeddedIcon') { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 183 | this.loadIcon(div, glyphMapping.get(iconCls), size, installGlyph, svgClass); |
| 184 | div.select('svg g').classed(iconCls, true); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Load an embedded icon. |
| 189 | */ |
| 190 | loadEmbeddedIcon(div, iconCls: string, size: number) { |
| 191 | this.loadIconByClass(div, iconCls, size, true); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Load an icon only to the svg defs collection |
| 196 | * |
| 197 | * Note: This is added for use with IconComponent, where the icon's |
| 198 | * svg element is defined in the component template (and not built |
| 199 | * inline using d3 manipulation |
| 200 | * |
| 201 | * @param iconCls The icon class as a string |
| 202 | */ |
| 203 | loadIconDef(iconCls: string): void { |
| 204 | this.gs.loadDefs(this.ensureIconLibDefs(), [glyphMapping.get(iconCls)], true); |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 205 | this.log.debug('icon definition', iconCls, 'added to defs'); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | |
| 209 | /** |
| 210 | * Add a device icon |
| 211 | * |
| 212 | * Adds a device glyph to the specified element. |
| 213 | * Returns the D3 selection of the glyph (use) element. |
| 214 | */ |
| 215 | addDeviceIcon(elem, glyphId, iconDim) { |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 216 | const gid = this.gs.glyphDefined(glyphId) ? glyphId : 'query'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 217 | return elem.append('use').attr({ |
| 218 | 'xlink:href': '#' + gid, |
| 219 | width: iconDim, |
| 220 | height: iconDim, |
| 221 | }); |
| 222 | } |
| 223 | |
| 224 | addHostIcon(elem, radius, glyphId) { |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 225 | const dim = radius * 1.5; |
| 226 | const xlate = -dim / 2; |
| 227 | const g = elem.append('g') |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 228 | .attr('class', 'svgIcon hostIcon'); |
| 229 | |
| 230 | g.append('circle').attr('r', radius); |
| 231 | |
| 232 | g.append('use').attr({ |
| 233 | 'xlink:href': '#' + glyphId, |
| 234 | width: dim, |
| 235 | height: dim, |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 236 | transform: this.sus.translate([xlate], xlate), |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 237 | }); |
| 238 | return g; |
| 239 | } |
| 240 | |
| 241 | registerIconMapping(iconId, glyphId) { |
| 242 | if (glyphMapping[iconId]) { |
| 243 | this.log.warn('Icon with id', iconId, 'already mapped. Ignoring.'); |
| 244 | } else { |
| 245 | // map icon-->glyph |
| 246 | glyphMapping[iconId] = glyphId; |
| 247 | // make sure definition is installed |
| 248 | this.gs.loadDefs(this.ensureIconLibDefs(), [glyphId], true); |
| 249 | } |
| 250 | } |
| 251 | } |