blob: b2d6f434f5cfb9a72e0492feeb4ec1d442691490 [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 Condon4747ece2019-05-04 20:17:02 +010049 ['switch', 'm_switch'],
Sean Condon021f0fa2018-12-06 23:31:11 -080050 ['m_roadm', 'm_roadm'],
Sean Condon4747ece2019-05-04 20:17:02 +010051 ['roadm', 'm_roadm'],
Sean Condon021f0fa2018-12-06 23:31:11 -080052 ['m_router', 'm_router'],
Sean Condon4747ece2019-05-04 20:17:02 +010053 ['router', 'm_router'],
Sean Condon91481822019-01-01 13:56:14 +000054 ['m_uiAttached', 'm_uiAttached'],
Sean Condon021f0fa2018-12-06 23:31:11 -080055 ['m_endstation', 'm_endstation'],
Sean Condon4747ece2019-05-04 20:17:02 +010056 ['endstation', 'm_endstation'],
Sean Condon91481822019-01-01 13:56:14 +000057 ['m_summary', 'm_summary'],
58 ['m_details', 'm_details'],
59 ['m_oblique', 'm_oblique'],
60 ['m_filters', 'm_filters'],
61 ['m_cycleLabels', 'm_cycleLabels'],
Sean Condon71910542019-02-16 18:16:42 +000062 ['m_cycleGridDisplay', 'm_cycleGridDisplay'],
Sean Condon91481822019-01-01 13:56:14 +000063 ['m_prev', 'm_prev'],
64 ['m_next', 'm_next'],
65 ['m_flows', 'm_flows'],
66 ['m_allTraffic', 'm_allTraffic'],
67 ['m_xMark', 'm_xMark'],
68 ['m_resetZoom', 'm_resetZoom'],
69 ['m_eqMaster', 'm_eqMaster'],
70 ['m_unknown', 'm_unknown'],
71 ['m_controller', 'm_controller'],
72 ['m_eqMaster', 'm_eqMaster'],
73 ['m_virtual', 'm_virtual'],
74 ['m_other', 'm_other'],
75 ['m_bgpSpeaker', 'm_bgpSpeaker'],
Sean Condon4747ece2019-05-04 20:17:02 +010076 ['bgpSpeaker', 'm_bgpSpeaker'],
Sean Condon91481822019-01-01 13:56:14 +000077 ['m_otn', 'm_otn'],
Sean Condon4747ece2019-05-04 20:17:02 +010078 ['otn', 'm_otn'],
79 ['m_terminal_device', 'm_otn'],
80 ['m_ols', 'm_roadm'],
Sean Condon91481822019-01-01 13:56:14 +000081 ['m_roadm_otn', 'm_roadm_otn'],
Sean Condon4747ece2019-05-04 20:17:02 +010082 ['roadm_otn', 'm_roadm_otn'],
Sean Condon91481822019-01-01 13:56:14 +000083 ['m_fiberSwitch', 'm_fiberSwitch'],
Sean Condon4747ece2019-05-04 20:17:02 +010084 ['fiber_switch', 'm_fiberSwitch'],
Sean Condon91481822019-01-01 13:56:14 +000085 ['m_microwave', 'm_microwave'],
Sean Condon4747ece2019-05-04 20:17:02 +010086 ['microwave', 'm_microwave'],
Sean Condon91481822019-01-01 13:56:14 +000087 ['m_relatedIntents', 'm_relatedIntents'],
88 ['m_intentTraffic', 'm_intentTraffic'],
89 ['m_firewall', 'm_firewall'],
90 ['m_balancer', 'm_balancer'],
91 ['m_ips', 'm_ips'],
92 ['m_ids', 'm_ids'],
93 ['m_olt', 'm_olt'],
94 ['m_onu', 'm_onu'],
95 ['m_swap', 'm_swap'],
96 ['m_shortestGeoPath', 'm_shortestGeoPath'],
97 ['m_source', 'm_source'],
98 ['m_destination', 'm_destination'],
99 ['m_topo', 'm_topo'],
100 ['m_shortestPath', 'm_shortestPath'],
101 ['m_disjointPaths', 'm_disjointPaths'],
102 ['m_region', 'm_region'],
Sean Condon4747ece2019-05-04 20:17:02 +0100103 ['virtual', 'cord'],
Bhavesh72ead492018-07-19 16:29:18 +0530104
Sean Condon49e15be2018-05-16 16:58:29 +0100105 ['topo', 'topo'],
Sean Condonf4f54a12018-10-10 23:25:46 +0100106 ['bird', 'bird'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100107
Sean Condon49e15be2018-05-16 16:58:29 +0100108 ['refresh', 'refresh'],
109 ['query', 'query'],
110 ['garbage', 'garbage'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100111
112
Sean Condon49e15be2018-05-16 16:58:29 +0100113 ['upArrow', 'triangleUp'],
114 ['downArrow', 'triangleDown'],
Sean Condon91481822019-01-01 13:56:14 +0000115 ['triangleLeft', 'triangleLeft'],
116 ['triangleRight', 'triangleRight'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100117
Sean Condon49e15be2018-05-16 16:58:29 +0100118 ['appInactive', 'unknown'],
Sean Condonaa4366d2018-11-02 14:29:01 +0000119 ['uiAttached', 'uiAttached'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100120
Sean Condon49e15be2018-05-16 16:58:29 +0100121 ['node', 'node'],
122 ['devIcon_SWITCH', 'switch'],
123 ['devIcon_ROADM', 'roadm'],
124 ['devIcon_OTN', 'otn'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100125
Sean Condon49e15be2018-05-16 16:58:29 +0100126 ['portIcon_DEFAULT', 'm_ports'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100127
Sean Condon49e15be2018-05-16 16:58:29 +0100128 ['meter', 'meterTable'], // TODO: m_meter icon?
Sean Condon83fc39f2018-04-19 18:56:13 +0100129
Sean Condon49e15be2018-05-16 16:58:29 +0100130 ['deviceTable', 'switch'],
131 ['flowTable', 'flowTable'],
132 ['portTable', 'portTable'],
133 ['groupTable', 'groupTable'],
134 ['meterTable', 'meterTable'],
135 ['pipeconfTable', 'pipeconfTable'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100136
Sean Condon49e15be2018-05-16 16:58:29 +0100137 ['hostIcon_endstation', 'endstation'],
138 ['hostIcon_router', 'router'],
139 ['hostIcon_bgpSpeaker', 'bgpSpeaker'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100140
141 // navigation menu icons...
Sean Condon49e15be2018-05-16 16:58:29 +0100142 ['nav_apps', 'bird'],
143 ['nav_settings', 'cog'],
144 ['nav_cluster', 'node'],
145 ['nav_processors', 'allTraffic'],
Bhavesh Kumard0b8bae2018-07-31 16:56:43 +0530146 ['nav_partitions', 'unknown'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100147
Sean Condon49e15be2018-05-16 16:58:29 +0100148 ['nav_topo', 'topo'],
149 ['nav_topo2', 'm_cloud'],
150 ['nav_devs', 'switch'],
151 ['nav_links', 'ports'],
152 ['nav_hosts', 'endstation'],
153 ['nav_intents', 'relatedIntents'],
154 ['nav_tunnels', 'ports'], // TODO: use tunnel glyph, when available
155 ['nav_yang', 'yang'],
Sean Condon83fc39f2018-04-19 18:56:13 +0100156]);
157
158/**
159 * ONOS GUI -- SVG -- Icon Service
160 */
Sean Condon5ca00262018-09-06 17:55:25 +0100161@Injectable({
162 providedIn: 'root',
163})
Sean Condon83fc39f2018-04-19 18:56:13 +0100164export class IconService {
165
166 constructor(
167 private gs: GlyphService,
168 private log: LogService,
169 private sus: SvgUtilService
170 ) {
171
172 this.log.debug('IconService constructed');
173 }
174
175 ensureIconLibDefs() {
Sean Condon49e15be2018-05-16 16:58:29 +0100176 const body = d3.select('body');
Sean Condon83fc39f2018-04-19 18:56:13 +0100177 let svg = body.select('svg#IconLibDefs');
178 if (svg.empty()) {
179 svg = body.append('svg').attr('id', 'IconLibDefs');
180 svg.append('defs');
181 }
182 return svg.select('defs');
183 }
184
185 /**
186 * Load an icon
187 *
188 * @param div A D3 selection of the '&lt;div&gt;' element into which icon should load
189 * @param glyphId Identifies the glyph to use
190 * @param size The dimension of icon in pixels. Defaults to 20.
191 * @param installGlyph If truthy, will cause the glyph to be added to
192 * well-known defs element. Defaults to false.
193 * @param svgClass The CSS class used to identify the SVG layer.
194 * Defaults to 'embeddedIcon'.
195 */
196 loadIcon(div, glyphId: string = 'unknown', size: number = 20, installGlyph: boolean = true, svgClass: string = 'embeddedIcon') {
Sean Condon49e15be2018-05-16 16:58:29 +0100197 const dim = size || 20;
198 const svgCls = svgClass || 'embeddedIcon';
199 const gid = glyphId || 'unknown';
Sean Condon83fc39f2018-04-19 18:56:13 +0100200 let g;
201 let svgIcon: any;
202
203 if (installGlyph) {
204 this.gs.loadDefs(this.ensureIconLibDefs(), [gid], true);
205 }
206 this.log.warn('loadEmbeddedIcon. install done');
207
208 svgIcon = div
209 .append('svg')
210 .attr('class', svgCls)
211 .attr('width', dim)
212 .attr('height', dim)
213 .attr('viewBox', viewBox);
214
215 g = svgIcon.append('g')
216 .attr('class', 'icon');
217
218 g.append('rect')
219 .attr('width', vboxSize)
220 .attr('height', vboxSize)
221 .attr('rx', cornerSize);
222
223 g.append('use')
224 .attr('width', vboxSize)
225 .attr('height', vboxSize)
226 .attr('class', 'glyph')
227 .attr('xlink:href', '#' + gid);
228 }
229
230 /**
231 * Load an icon by class.
232 * @param div A D3 selection of the <DIV> element into which icon should load
233 * @param iconCls The CSS class used to identify the icon
Sean Condon5ca00262018-09-06 17:55:25 +0100234 * @param size The dimension of icon in pixels. Defaults to 20.
235 * @param installGlyph If truthy, will cause the glyph to be added to
Sean Condon83fc39f2018-04-19 18:56:13 +0100236 * well-known defs element. Defaults to false.
237 * @param svgClass The CSS class used to identify the SVG layer.
238 * Defaults to 'embeddedIcon'.
239 */
Sean Condon49e15be2018-05-16 16:58:29 +0100240 loadIconByClass(div, iconCls: string, size: number, installGlyph: boolean, svgClass= 'embeddedIcon') {
Sean Condon83fc39f2018-04-19 18:56:13 +0100241 this.loadIcon(div, glyphMapping.get(iconCls), size, installGlyph, svgClass);
242 div.select('svg g').classed(iconCls, true);
243 }
244
245 /**
246 * Load an embedded icon.
247 */
248 loadEmbeddedIcon(div, iconCls: string, size: number) {
249 this.loadIconByClass(div, iconCls, size, true);
250 }
251
252 /**
253 * Load an icon only to the svg defs collection
254 *
255 * Note: This is added for use with IconComponent, where the icon's
256 * svg element is defined in the component template (and not built
257 * inline using d3 manipulation
258 *
259 * @param iconCls The icon class as a string
260 */
261 loadIconDef(iconCls: string): void {
Sean Condon59d31372019-02-02 20:07:00 +0000262 let glyphName: string = glyphMapping.get(iconCls);
263 if (!glyphName) {
264 glyphName = iconCls;
265 }
Sean Condon4747ece2019-05-04 20:17:02 +0100266 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphName], true, [iconCls]);
Sean Condon83fc39f2018-04-19 18:56:13 +0100267 }
268
269
270 /**
271 * Add a device icon
272 *
273 * Adds a device glyph to the specified element.
274 * Returns the D3 selection of the glyph (use) element.
275 */
276 addDeviceIcon(elem, glyphId, iconDim) {
Sean Condon49e15be2018-05-16 16:58:29 +0100277 const gid = this.gs.glyphDefined(glyphId) ? glyphId : 'query';
Sean Condon83fc39f2018-04-19 18:56:13 +0100278 return elem.append('use').attr({
279 'xlink:href': '#' + gid,
280 width: iconDim,
281 height: iconDim,
282 });
283 }
284
285 addHostIcon(elem, radius, glyphId) {
Sean Condon49e15be2018-05-16 16:58:29 +0100286 const dim = radius * 1.5;
287 const xlate = -dim / 2;
288 const g = elem.append('g')
Sean Condon83fc39f2018-04-19 18:56:13 +0100289 .attr('class', 'svgIcon hostIcon');
290
291 g.append('circle').attr('r', radius);
292
293 g.append('use').attr({
294 'xlink:href': '#' + glyphId,
295 width: dim,
296 height: dim,
Sean Condonfd6d11b2018-06-02 20:29:49 +0100297 transform: this.sus.translate([xlate], xlate),
Sean Condon83fc39f2018-04-19 18:56:13 +0100298 });
299 return g;
300 }
301
302 registerIconMapping(iconId, glyphId) {
303 if (glyphMapping[iconId]) {
304 this.log.warn('Icon with id', iconId, 'already mapped. Ignoring.');
305 } else {
306 // map icon-->glyph
307 glyphMapping[iconId] = glyphId;
308 // make sure definition is installed
309 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphId], true);
310 }
311 }
312}