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