blob: 5b801ab59f8fcdd9e576255667b8089b9f30626b [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
Bhavesh72ead492018-07-19 16:29:18 +053043 ['m_ports', 'm_ports'],
Sean Condon0c577f62018-11-18 22:40:05 +000044 ['m_switch', 'm_switch'],
Sean Condon021f0fa2018-12-06 23:31:11 -080045 ['m_roadm', 'm_roadm'],
46 ['m_router', 'm_router'],
47 ['m_endstation', 'm_endstation'],
Bhavesh72ead492018-07-19 16:29:18 +053048
Sean Condon49e15be2018-05-16 16:58:29 +010049 ['topo', 'topo'],
Sean Condonf4f54a12018-10-10 23:25:46 +010050 ['bird', 'bird'],
Sean Condon83fc39f2018-04-19 18:56:13 +010051
Sean Condon49e15be2018-05-16 16:58:29 +010052 ['refresh', 'refresh'],
53 ['query', 'query'],
54 ['garbage', 'garbage'],
Sean Condon83fc39f2018-04-19 18:56:13 +010055
56
Sean Condon49e15be2018-05-16 16:58:29 +010057 ['upArrow', 'triangleUp'],
58 ['downArrow', 'triangleDown'],
Sean Condon83fc39f2018-04-19 18:56:13 +010059
Sean Condon49e15be2018-05-16 16:58:29 +010060 ['appInactive', 'unknown'],
Sean Condonaa4366d2018-11-02 14:29:01 +000061 ['uiAttached', 'uiAttached'],
Sean Condon83fc39f2018-04-19 18:56:13 +010062
Sean Condon49e15be2018-05-16 16:58:29 +010063 ['node', 'node'],
64 ['devIcon_SWITCH', 'switch'],
65 ['devIcon_ROADM', 'roadm'],
66 ['devIcon_OTN', 'otn'],
Sean Condon83fc39f2018-04-19 18:56:13 +010067
Sean Condon49e15be2018-05-16 16:58:29 +010068 ['portIcon_DEFAULT', 'm_ports'],
Sean Condon83fc39f2018-04-19 18:56:13 +010069
Sean Condon49e15be2018-05-16 16:58:29 +010070 ['meter', 'meterTable'], // TODO: m_meter icon?
Sean Condon83fc39f2018-04-19 18:56:13 +010071
Sean Condon49e15be2018-05-16 16:58:29 +010072 ['deviceTable', 'switch'],
73 ['flowTable', 'flowTable'],
74 ['portTable', 'portTable'],
75 ['groupTable', 'groupTable'],
76 ['meterTable', 'meterTable'],
77 ['pipeconfTable', 'pipeconfTable'],
Sean Condon83fc39f2018-04-19 18:56:13 +010078
Sean Condon49e15be2018-05-16 16:58:29 +010079 ['hostIcon_endstation', 'endstation'],
80 ['hostIcon_router', 'router'],
81 ['hostIcon_bgpSpeaker', 'bgpSpeaker'],
Sean Condon83fc39f2018-04-19 18:56:13 +010082
83 // navigation menu icons...
Sean Condon49e15be2018-05-16 16:58:29 +010084 ['nav_apps', 'bird'],
85 ['nav_settings', 'cog'],
86 ['nav_cluster', 'node'],
87 ['nav_processors', 'allTraffic'],
Bhavesh Kumard0b8bae2018-07-31 16:56:43 +053088 ['nav_partitions', 'unknown'],
Sean Condon83fc39f2018-04-19 18:56:13 +010089
Sean Condon49e15be2018-05-16 16:58:29 +010090 ['nav_topo', 'topo'],
91 ['nav_topo2', 'm_cloud'],
92 ['nav_devs', 'switch'],
93 ['nav_links', 'ports'],
94 ['nav_hosts', 'endstation'],
95 ['nav_intents', 'relatedIntents'],
96 ['nav_tunnels', 'ports'], // TODO: use tunnel glyph, when available
97 ['nav_yang', 'yang'],
Sean Condon83fc39f2018-04-19 18:56:13 +010098]);
99
100/**
101 * ONOS GUI -- SVG -- Icon Service
102 */
Sean Condon5ca00262018-09-06 17:55:25 +0100103@Injectable({
104 providedIn: 'root',
105})
Sean Condon83fc39f2018-04-19 18:56:13 +0100106export class IconService {
107
108 constructor(
109 private gs: GlyphService,
110 private log: LogService,
111 private sus: SvgUtilService
112 ) {
113
114 this.log.debug('IconService constructed');
115 }
116
117 ensureIconLibDefs() {
Sean Condon49e15be2018-05-16 16:58:29 +0100118 const body = d3.select('body');
Sean Condon83fc39f2018-04-19 18:56:13 +0100119 let svg = body.select('svg#IconLibDefs');
120 if (svg.empty()) {
121 svg = body.append('svg').attr('id', 'IconLibDefs');
122 svg.append('defs');
123 }
124 return svg.select('defs');
125 }
126
127 /**
128 * Load an icon
129 *
130 * @param div A D3 selection of the '&lt;div&gt;' element into which icon should load
131 * @param glyphId Identifies the glyph to use
132 * @param size The dimension of icon in pixels. Defaults to 20.
133 * @param installGlyph If truthy, will cause the glyph to be added to
134 * well-known defs element. Defaults to false.
135 * @param svgClass The CSS class used to identify the SVG layer.
136 * Defaults to 'embeddedIcon'.
137 */
138 loadIcon(div, glyphId: string = 'unknown', size: number = 20, installGlyph: boolean = true, svgClass: string = 'embeddedIcon') {
Sean Condon49e15be2018-05-16 16:58:29 +0100139 const dim = size || 20;
140 const svgCls = svgClass || 'embeddedIcon';
141 const gid = glyphId || 'unknown';
Sean Condon83fc39f2018-04-19 18:56:13 +0100142 let g;
143 let svgIcon: any;
144
145 if (installGlyph) {
146 this.gs.loadDefs(this.ensureIconLibDefs(), [gid], true);
147 }
148 this.log.warn('loadEmbeddedIcon. install done');
149
150 svgIcon = div
151 .append('svg')
152 .attr('class', svgCls)
153 .attr('width', dim)
154 .attr('height', dim)
155 .attr('viewBox', viewBox);
156
157 g = svgIcon.append('g')
158 .attr('class', 'icon');
159
160 g.append('rect')
161 .attr('width', vboxSize)
162 .attr('height', vboxSize)
163 .attr('rx', cornerSize);
164
165 g.append('use')
166 .attr('width', vboxSize)
167 .attr('height', vboxSize)
168 .attr('class', 'glyph')
169 .attr('xlink:href', '#' + gid);
170 }
171
172 /**
173 * Load an icon by class.
174 * @param div A D3 selection of the <DIV> element into which icon should load
175 * @param iconCls The CSS class used to identify the icon
Sean Condon5ca00262018-09-06 17:55:25 +0100176 * @param size The dimension of icon in pixels. Defaults to 20.
177 * @param installGlyph If truthy, will cause the glyph to be added to
Sean Condon83fc39f2018-04-19 18:56:13 +0100178 * well-known defs element. Defaults to false.
179 * @param svgClass The CSS class used to identify the SVG layer.
180 * Defaults to 'embeddedIcon'.
181 */
Sean Condon49e15be2018-05-16 16:58:29 +0100182 loadIconByClass(div, iconCls: string, size: number, installGlyph: boolean, svgClass= 'embeddedIcon') {
Sean Condon83fc39f2018-04-19 18:56:13 +0100183 this.loadIcon(div, glyphMapping.get(iconCls), size, installGlyph, svgClass);
184 div.select('svg g').classed(iconCls, true);
185 }
186
187 /**
188 * Load an embedded icon.
189 */
190 loadEmbeddedIcon(div, iconCls: string, size: number) {
191 this.loadIconByClass(div, iconCls, size, true);
192 }
193
194 /**
195 * Load an icon only to the svg defs collection
196 *
197 * Note: This is added for use with IconComponent, where the icon's
198 * svg element is defined in the component template (and not built
199 * inline using d3 manipulation
200 *
201 * @param iconCls The icon class as a string
202 */
203 loadIconDef(iconCls: string): void {
204 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphMapping.get(iconCls)], true);
Sean Condon28ecc5f2018-06-25 12:50:16 +0100205 this.log.debug('icon definition', iconCls, 'added to defs');
Sean Condon83fc39f2018-04-19 18:56:13 +0100206 }
207
208
209 /**
210 * Add a device icon
211 *
212 * Adds a device glyph to the specified element.
213 * Returns the D3 selection of the glyph (use) element.
214 */
215 addDeviceIcon(elem, glyphId, iconDim) {
Sean Condon49e15be2018-05-16 16:58:29 +0100216 const gid = this.gs.glyphDefined(glyphId) ? glyphId : 'query';
Sean Condon83fc39f2018-04-19 18:56:13 +0100217 return elem.append('use').attr({
218 'xlink:href': '#' + gid,
219 width: iconDim,
220 height: iconDim,
221 });
222 }
223
224 addHostIcon(elem, radius, glyphId) {
Sean Condon49e15be2018-05-16 16:58:29 +0100225 const dim = radius * 1.5;
226 const xlate = -dim / 2;
227 const g = elem.append('g')
Sean Condon83fc39f2018-04-19 18:56:13 +0100228 .attr('class', 'svgIcon hostIcon');
229
230 g.append('circle').attr('r', radius);
231
232 g.append('use').attr({
233 'xlink:href': '#' + glyphId,
234 width: dim,
235 height: dim,
Sean Condonfd6d11b2018-06-02 20:29:49 +0100236 transform: this.sus.translate([xlate], xlate),
Sean Condon83fc39f2018-04-19 18:56:13 +0100237 });
238 return g;
239 }
240
241 registerIconMapping(iconId, glyphId) {
242 if (glyphMapping[iconId]) {
243 this.log.warn('Icon with id', iconId, 'already mapped. Ignoring.');
244 } else {
245 // map icon-->glyph
246 glyphMapping[iconId] = glyphId;
247 // make sure definition is installed
248 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphId], true);
249 }
250 }
251}