blob: 766cc56f5fb6ad4dcb7666aee38f7faaaeae2902 [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
Sean Condon5ca00262018-09-06 17:55:25 +01002 * Copyright 2018-present Open Networking Foundation
Sean Condon83fc39f2018-04-19 18:56:13 +01003 *
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 */
16import { Injectable } from '@angular/core';
17import { GlyphService } from './glyph.service';
Sean Condon5ca00262018-09-06 17:55:25 +010018import { LogService } from '../log.service';
Sean Condon83fc39f2018-04-19 18:56:13 +010019import { SvgUtilService } from './svgutil.service';
20import * as d3 from 'd3';
21
22const vboxSize = 50;
23const cornerSize = vboxSize / 10;
24const viewBox = '0 0 ' + vboxSize + ' ' + vboxSize;
25
26export 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 Condon49e15be2018-05-16 16:58:29 +010030 ['inactive', 'xMark'],
Sean Condon83fc39f2018-04-19 18:56:13 +010031
Sean Condon49e15be2018-05-16 16:58:29 +010032 ['plus', 'plus'],
33 ['minus', 'minus'],
34 ['play', 'play'],
35 ['stop', 'stop'],
Sean Condon83fc39f2018-04-19 18:56:13 +010036
Sean Condon49e15be2018-05-16 16:58:29 +010037 ['upload', 'upload'],
38 ['download', 'download'],
39 ['delta', 'delta'],
40 ['nonzero', 'nonzero'],
41 ['close', 'xClose'],
Sean Condon83fc39f2018-04-19 18:56:13 +010042
Sean Condon91481822019-01-01 13:56:14 +000043 ['m_cloud', 'm_cloud'],
44 ['m_map', 'm_map'],
45 ['m_selectMap', 'm_selectMap'],
46 ['thatsNoMoon', 'thatsNoMoon'],
Bhavesh72ead492018-07-19 16:29:18 +053047 ['m_ports', 'm_ports'],
Sean Condon0c577f62018-11-18 22:40:05 +000048 ['m_switch', 'm_switch'],
Sean Condon021f0fa2018-12-06 23:31:11 -080049 ['m_roadm', 'm_roadm'],
50 ['m_router', 'm_router'],
Sean Condon91481822019-01-01 13:56:14 +000051 ['m_uiAttached', 'm_uiAttached'],
Sean Condon021f0fa2018-12-06 23:31:11 -080052 ['m_endstation', 'm_endstation'],
Sean Condon91481822019-01-01 13:56:14 +000053 ['m_summary', 'm_summary'],
54 ['m_details', 'm_details'],
55 ['m_oblique', 'm_oblique'],
56 ['m_filters', 'm_filters'],
57 ['m_cycleLabels', 'm_cycleLabels'],
58 ['m_prev', 'm_prev'],
59 ['m_next', 'm_next'],
60 ['m_flows', 'm_flows'],
61 ['m_allTraffic', 'm_allTraffic'],
62 ['m_xMark', 'm_xMark'],
63 ['m_resetZoom', 'm_resetZoom'],
64 ['m_eqMaster', 'm_eqMaster'],
65 ['m_unknown', 'm_unknown'],
66 ['m_controller', 'm_controller'],
67 ['m_eqMaster', 'm_eqMaster'],
68 ['m_virtual', 'm_virtual'],
69 ['m_other', 'm_other'],
70 ['m_bgpSpeaker', 'm_bgpSpeaker'],
71 ['m_otn', 'm_otn'],
72 ['m_roadm_otn', 'm_roadm_otn'],
73 ['m_fiberSwitch', 'm_fiberSwitch'],
74 ['m_microwave', 'm_microwave'],
75 ['m_relatedIntents', 'm_relatedIntents'],
76 ['m_intentTraffic', 'm_intentTraffic'],
77 ['m_firewall', 'm_firewall'],
78 ['m_balancer', 'm_balancer'],
79 ['m_ips', 'm_ips'],
80 ['m_ids', 'm_ids'],
81 ['m_olt', 'm_olt'],
82 ['m_onu', 'm_onu'],
83 ['m_swap', 'm_swap'],
84 ['m_shortestGeoPath', 'm_shortestGeoPath'],
85 ['m_source', 'm_source'],
86 ['m_destination', 'm_destination'],
87 ['m_topo', 'm_topo'],
88 ['m_shortestPath', 'm_shortestPath'],
89 ['m_disjointPaths', 'm_disjointPaths'],
90 ['m_region', 'm_region'],
Bhavesh72ead492018-07-19 16:29:18 +053091
Sean Condon49e15be2018-05-16 16:58:29 +010092 ['topo', 'topo'],
Sean Condonf4f54a12018-10-10 23:25:46 +010093 ['bird', 'bird'],
Sean Condon83fc39f2018-04-19 18:56:13 +010094
Sean Condon49e15be2018-05-16 16:58:29 +010095 ['refresh', 'refresh'],
96 ['query', 'query'],
97 ['garbage', 'garbage'],
Sean Condon83fc39f2018-04-19 18:56:13 +010098
99
Sean Condon49e15be2018-05-16 16:58:29 +0100100 ['upArrow', 'triangleUp'],
101 ['downArrow', 'triangleDown'],
Sean Condon91481822019-01-01 13:56:14 +0000102 ['triangleLeft', 'triangleLeft'],
103 ['triangleRight', 'triangleRight'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100104
Sean Condon49e15be2018-05-16 16:58:29 +0100105 ['appInactive', 'unknown'],
Sean Condonaa4366d2018-11-02 14:29:01 +0000106 ['uiAttached', 'uiAttached'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100107
Sean Condon49e15be2018-05-16 16:58:29 +0100108 ['node', 'node'],
109 ['devIcon_SWITCH', 'switch'],
110 ['devIcon_ROADM', 'roadm'],
111 ['devIcon_OTN', 'otn'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100112
Sean Condon49e15be2018-05-16 16:58:29 +0100113 ['portIcon_DEFAULT', 'm_ports'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100114
Sean Condon49e15be2018-05-16 16:58:29 +0100115 ['meter', 'meterTable'], // TODO: m_meter icon?
Sean Condon83fc39f2018-04-19 18:56:13 +0100116
Sean Condon49e15be2018-05-16 16:58:29 +0100117 ['deviceTable', 'switch'],
118 ['flowTable', 'flowTable'],
119 ['portTable', 'portTable'],
120 ['groupTable', 'groupTable'],
121 ['meterTable', 'meterTable'],
122 ['pipeconfTable', 'pipeconfTable'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100123
Sean Condon49e15be2018-05-16 16:58:29 +0100124 ['hostIcon_endstation', 'endstation'],
125 ['hostIcon_router', 'router'],
126 ['hostIcon_bgpSpeaker', 'bgpSpeaker'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100127
128 // navigation menu icons...
Sean Condon49e15be2018-05-16 16:58:29 +0100129 ['nav_apps', 'bird'],
130 ['nav_settings', 'cog'],
131 ['nav_cluster', 'node'],
132 ['nav_processors', 'allTraffic'],
Bhavesh Kumard0b8bae2018-07-31 16:56:43 +0530133 ['nav_partitions', 'unknown'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100134
Sean Condon49e15be2018-05-16 16:58:29 +0100135 ['nav_topo', 'topo'],
136 ['nav_topo2', 'm_cloud'],
137 ['nav_devs', 'switch'],
138 ['nav_links', 'ports'],
139 ['nav_hosts', 'endstation'],
140 ['nav_intents', 'relatedIntents'],
141 ['nav_tunnels', 'ports'], // TODO: use tunnel glyph, when available
142 ['nav_yang', 'yang'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100143]);
144
145/**
146 * ONOS GUI -- SVG -- Icon Service
147 */
Sean Condon5ca00262018-09-06 17:55:25 +0100148@Injectable({
149 providedIn: 'root',
150})
Sean Condon83fc39f2018-04-19 18:56:13 +0100151export class IconService {
152
153 constructor(
154 private gs: GlyphService,
155 private log: LogService,
156 private sus: SvgUtilService
157 ) {
158
159 this.log.debug('IconService constructed');
160 }
161
162 ensureIconLibDefs() {
Sean Condon49e15be2018-05-16 16:58:29 +0100163 const body = d3.select('body');
Sean Condon83fc39f2018-04-19 18:56:13 +0100164 let svg = body.select('svg#IconLibDefs');
165 if (svg.empty()) {
166 svg = body.append('svg').attr('id', 'IconLibDefs');
167 svg.append('defs');
168 }
169 return svg.select('defs');
170 }
171
172 /**
173 * Load an icon
174 *
175 * @param div A D3 selection of the '&lt;div&gt;' element into which icon should load
176 * @param glyphId Identifies the glyph to use
177 * @param size The dimension of icon in pixels. Defaults to 20.
178 * @param installGlyph If truthy, will cause the glyph to be added to
179 * well-known defs element. Defaults to false.
180 * @param svgClass The CSS class used to identify the SVG layer.
181 * Defaults to 'embeddedIcon'.
182 */
183 loadIcon(div, glyphId: string = 'unknown', size: number = 20, installGlyph: boolean = true, svgClass: string = 'embeddedIcon') {
Sean Condon49e15be2018-05-16 16:58:29 +0100184 const dim = size || 20;
185 const svgCls = svgClass || 'embeddedIcon';
186 const gid = glyphId || 'unknown';
Sean Condon83fc39f2018-04-19 18:56:13 +0100187 let g;
188 let svgIcon: any;
189
190 if (installGlyph) {
191 this.gs.loadDefs(this.ensureIconLibDefs(), [gid], true);
192 }
193 this.log.warn('loadEmbeddedIcon. install done');
194
195 svgIcon = div
196 .append('svg')
197 .attr('class', svgCls)
198 .attr('width', dim)
199 .attr('height', dim)
200 .attr('viewBox', viewBox);
201
202 g = svgIcon.append('g')
203 .attr('class', 'icon');
204
205 g.append('rect')
206 .attr('width', vboxSize)
207 .attr('height', vboxSize)
208 .attr('rx', cornerSize);
209
210 g.append('use')
211 .attr('width', vboxSize)
212 .attr('height', vboxSize)
213 .attr('class', 'glyph')
214 .attr('xlink:href', '#' + gid);
215 }
216
217 /**
218 * Load an icon by class.
219 * @param div A D3 selection of the <DIV> element into which icon should load
220 * @param iconCls The CSS class used to identify the icon
Sean Condon5ca00262018-09-06 17:55:25 +0100221 * @param size The dimension of icon in pixels. Defaults to 20.
222 * @param installGlyph If truthy, will cause the glyph to be added to
Sean Condon83fc39f2018-04-19 18:56:13 +0100223 * well-known defs element. Defaults to false.
224 * @param svgClass The CSS class used to identify the SVG layer.
225 * Defaults to 'embeddedIcon'.
226 */
Sean Condon49e15be2018-05-16 16:58:29 +0100227 loadIconByClass(div, iconCls: string, size: number, installGlyph: boolean, svgClass= 'embeddedIcon') {
Sean Condon83fc39f2018-04-19 18:56:13 +0100228 this.loadIcon(div, glyphMapping.get(iconCls), size, installGlyph, svgClass);
229 div.select('svg g').classed(iconCls, true);
230 }
231
232 /**
233 * Load an embedded icon.
234 */
235 loadEmbeddedIcon(div, iconCls: string, size: number) {
236 this.loadIconByClass(div, iconCls, size, true);
237 }
238
239 /**
240 * Load an icon only to the svg defs collection
241 *
242 * Note: This is added for use with IconComponent, where the icon's
243 * svg element is defined in the component template (and not built
244 * inline using d3 manipulation
245 *
246 * @param iconCls The icon class as a string
247 */
248 loadIconDef(iconCls: string): void {
249 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphMapping.get(iconCls)], true);
Sean Condon28ecc5f2018-06-25 12:50:16 +0100250 this.log.debug('icon definition', iconCls, 'added to defs');
Sean Condon83fc39f2018-04-19 18:56:13 +0100251 }
252
253
254 /**
255 * Add a device icon
256 *
257 * Adds a device glyph to the specified element.
258 * Returns the D3 selection of the glyph (use) element.
259 */
260 addDeviceIcon(elem, glyphId, iconDim) {
Sean Condon49e15be2018-05-16 16:58:29 +0100261 const gid = this.gs.glyphDefined(glyphId) ? glyphId : 'query';
Sean Condon83fc39f2018-04-19 18:56:13 +0100262 return elem.append('use').attr({
263 'xlink:href': '#' + gid,
264 width: iconDim,
265 height: iconDim,
266 });
267 }
268
269 addHostIcon(elem, radius, glyphId) {
Sean Condon49e15be2018-05-16 16:58:29 +0100270 const dim = radius * 1.5;
271 const xlate = -dim / 2;
272 const g = elem.append('g')
Sean Condon83fc39f2018-04-19 18:56:13 +0100273 .attr('class', 'svgIcon hostIcon');
274
275 g.append('circle').attr('r', radius);
276
277 g.append('use').attr({
278 'xlink:href': '#' + glyphId,
279 width: dim,
280 height: dim,
Sean Condonfd6d11b2018-06-02 20:29:49 +0100281 transform: this.sus.translate([xlate], xlate),
Sean Condon83fc39f2018-04-19 18:56:13 +0100282 });
283 return g;
284 }
285
286 registerIconMapping(iconId, glyphId) {
287 if (glyphMapping[iconId]) {
288 this.log.warn('Icon with id', iconId, 'already mapped. Ignoring.');
289 } else {
290 // map icon-->glyph
291 glyphMapping[iconId] = glyphId;
292 // make sure definition is installed
293 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphId], true);
294 }
295 }
296}