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