blob: 69392cf0505536ab84eeb32a3a05e34c4a1a8d92 [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'],
44
Sean Condon49e15be2018-05-16 16:58:29 +010045 ['topo', 'topo'],
Sean Condonf4f54a12018-10-10 23:25:46 +010046 ['bird', 'bird'],
Sean Condon83fc39f2018-04-19 18:56:13 +010047
Sean Condon49e15be2018-05-16 16:58:29 +010048 ['refresh', 'refresh'],
49 ['query', 'query'],
50 ['garbage', 'garbage'],
Sean Condon83fc39f2018-04-19 18:56:13 +010051
52
Sean Condon49e15be2018-05-16 16:58:29 +010053 ['upArrow', 'triangleUp'],
54 ['downArrow', 'triangleDown'],
Sean Condon83fc39f2018-04-19 18:56:13 +010055
Sean Condon49e15be2018-05-16 16:58:29 +010056 ['appInactive', 'unknown'],
Sean Condonaa4366d2018-11-02 14:29:01 +000057 ['uiAttached', 'uiAttached'],
Sean Condon83fc39f2018-04-19 18:56:13 +010058
Sean Condon49e15be2018-05-16 16:58:29 +010059 ['node', 'node'],
60 ['devIcon_SWITCH', 'switch'],
61 ['devIcon_ROADM', 'roadm'],
62 ['devIcon_OTN', 'otn'],
Sean Condon83fc39f2018-04-19 18:56:13 +010063
Sean Condon49e15be2018-05-16 16:58:29 +010064 ['portIcon_DEFAULT', 'm_ports'],
Sean Condon83fc39f2018-04-19 18:56:13 +010065
Sean Condon49e15be2018-05-16 16:58:29 +010066 ['meter', 'meterTable'], // TODO: m_meter icon?
Sean Condon83fc39f2018-04-19 18:56:13 +010067
Sean Condon49e15be2018-05-16 16:58:29 +010068 ['deviceTable', 'switch'],
69 ['flowTable', 'flowTable'],
70 ['portTable', 'portTable'],
71 ['groupTable', 'groupTable'],
72 ['meterTable', 'meterTable'],
73 ['pipeconfTable', 'pipeconfTable'],
Sean Condon83fc39f2018-04-19 18:56:13 +010074
Sean Condon49e15be2018-05-16 16:58:29 +010075 ['hostIcon_endstation', 'endstation'],
76 ['hostIcon_router', 'router'],
77 ['hostIcon_bgpSpeaker', 'bgpSpeaker'],
Sean Condon83fc39f2018-04-19 18:56:13 +010078
79 // navigation menu icons...
Sean Condon49e15be2018-05-16 16:58:29 +010080 ['nav_apps', 'bird'],
81 ['nav_settings', 'cog'],
82 ['nav_cluster', 'node'],
83 ['nav_processors', 'allTraffic'],
Bhavesh Kumard0b8bae2018-07-31 16:56:43 +053084 ['nav_partitions', 'unknown'],
Sean Condon83fc39f2018-04-19 18:56:13 +010085
Sean Condon49e15be2018-05-16 16:58:29 +010086 ['nav_topo', 'topo'],
87 ['nav_topo2', 'm_cloud'],
88 ['nav_devs', 'switch'],
89 ['nav_links', 'ports'],
90 ['nav_hosts', 'endstation'],
91 ['nav_intents', 'relatedIntents'],
92 ['nav_tunnels', 'ports'], // TODO: use tunnel glyph, when available
93 ['nav_yang', 'yang'],
Sean Condon83fc39f2018-04-19 18:56:13 +010094]);
95
96/**
97 * ONOS GUI -- SVG -- Icon Service
98 */
Sean Condon5ca00262018-09-06 17:55:25 +010099@Injectable({
100 providedIn: 'root',
101})
Sean Condon83fc39f2018-04-19 18:56:13 +0100102export class IconService {
103
104 constructor(
105 private gs: GlyphService,
106 private log: LogService,
107 private sus: SvgUtilService
108 ) {
109
110 this.log.debug('IconService constructed');
111 }
112
113 ensureIconLibDefs() {
Sean Condon49e15be2018-05-16 16:58:29 +0100114 const body = d3.select('body');
Sean Condon83fc39f2018-04-19 18:56:13 +0100115 let svg = body.select('svg#IconLibDefs');
116 if (svg.empty()) {
117 svg = body.append('svg').attr('id', 'IconLibDefs');
118 svg.append('defs');
119 }
120 return svg.select('defs');
121 }
122
123 /**
124 * Load an icon
125 *
126 * @param div A D3 selection of the '&lt;div&gt;' element into which icon should load
127 * @param glyphId Identifies the glyph to use
128 * @param size The dimension of icon in pixels. Defaults to 20.
129 * @param installGlyph If truthy, will cause the glyph to be added to
130 * well-known defs element. Defaults to false.
131 * @param svgClass The CSS class used to identify the SVG layer.
132 * Defaults to 'embeddedIcon'.
133 */
134 loadIcon(div, glyphId: string = 'unknown', size: number = 20, installGlyph: boolean = true, svgClass: string = 'embeddedIcon') {
Sean Condon49e15be2018-05-16 16:58:29 +0100135 const dim = size || 20;
136 const svgCls = svgClass || 'embeddedIcon';
137 const gid = glyphId || 'unknown';
Sean Condon83fc39f2018-04-19 18:56:13 +0100138 let g;
139 let svgIcon: any;
140
141 if (installGlyph) {
142 this.gs.loadDefs(this.ensureIconLibDefs(), [gid], true);
143 }
144 this.log.warn('loadEmbeddedIcon. install done');
145
146 svgIcon = div
147 .append('svg')
148 .attr('class', svgCls)
149 .attr('width', dim)
150 .attr('height', dim)
151 .attr('viewBox', viewBox);
152
153 g = svgIcon.append('g')
154 .attr('class', 'icon');
155
156 g.append('rect')
157 .attr('width', vboxSize)
158 .attr('height', vboxSize)
159 .attr('rx', cornerSize);
160
161 g.append('use')
162 .attr('width', vboxSize)
163 .attr('height', vboxSize)
164 .attr('class', 'glyph')
165 .attr('xlink:href', '#' + gid);
166 }
167
168 /**
169 * Load an icon by class.
170 * @param div A D3 selection of the <DIV> element into which icon should load
171 * @param iconCls The CSS class used to identify the icon
Sean Condon5ca00262018-09-06 17:55:25 +0100172 * @param size The dimension of icon in pixels. Defaults to 20.
173 * @param installGlyph If truthy, will cause the glyph to be added to
Sean Condon83fc39f2018-04-19 18:56:13 +0100174 * well-known defs element. Defaults to false.
175 * @param svgClass The CSS class used to identify the SVG layer.
176 * Defaults to 'embeddedIcon'.
177 */
Sean Condon49e15be2018-05-16 16:58:29 +0100178 loadIconByClass(div, iconCls: string, size: number, installGlyph: boolean, svgClass= 'embeddedIcon') {
Sean Condon83fc39f2018-04-19 18:56:13 +0100179 this.loadIcon(div, glyphMapping.get(iconCls), size, installGlyph, svgClass);
180 div.select('svg g').classed(iconCls, true);
181 }
182
183 /**
184 * Load an embedded icon.
185 */
186 loadEmbeddedIcon(div, iconCls: string, size: number) {
187 this.loadIconByClass(div, iconCls, size, true);
188 }
189
190 /**
191 * Load an icon only to the svg defs collection
192 *
193 * Note: This is added for use with IconComponent, where the icon's
194 * svg element is defined in the component template (and not built
195 * inline using d3 manipulation
196 *
197 * @param iconCls The icon class as a string
198 */
199 loadIconDef(iconCls: string): void {
200 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphMapping.get(iconCls)], true);
Sean Condon28ecc5f2018-06-25 12:50:16 +0100201 this.log.debug('icon definition', iconCls, 'added to defs');
Sean Condon83fc39f2018-04-19 18:56:13 +0100202 }
203
204
205 /**
206 * Add a device icon
207 *
208 * Adds a device glyph to the specified element.
209 * Returns the D3 selection of the glyph (use) element.
210 */
211 addDeviceIcon(elem, glyphId, iconDim) {
Sean Condon49e15be2018-05-16 16:58:29 +0100212 const gid = this.gs.glyphDefined(glyphId) ? glyphId : 'query';
Sean Condon83fc39f2018-04-19 18:56:13 +0100213 return elem.append('use').attr({
214 'xlink:href': '#' + gid,
215 width: iconDim,
216 height: iconDim,
217 });
218 }
219
220 addHostIcon(elem, radius, glyphId) {
Sean Condon49e15be2018-05-16 16:58:29 +0100221 const dim = radius * 1.5;
222 const xlate = -dim / 2;
223 const g = elem.append('g')
Sean Condon83fc39f2018-04-19 18:56:13 +0100224 .attr('class', 'svgIcon hostIcon');
225
226 g.append('circle').attr('r', radius);
227
228 g.append('use').attr({
229 'xlink:href': '#' + glyphId,
230 width: dim,
231 height: dim,
Sean Condonfd6d11b2018-06-02 20:29:49 +0100232 transform: this.sus.translate([xlate], xlate),
Sean Condon83fc39f2018-04-19 18:56:13 +0100233 });
234 return g;
235 }
236
237 registerIconMapping(iconId, glyphId) {
238 if (glyphMapping[iconId]) {
239 this.log.warn('Icon with id', iconId, 'already mapped. Ignoring.');
240 } else {
241 // map icon-->glyph
242 glyphMapping[iconId] = glyphId;
243 // make sure definition is installed
244 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphId], true);
245 }
246 }
247}