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