blob: 73480ed77b7b7356ea40d889b5d9c957cf92e271 [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 Condon83fc39f2018-04-19 18:56:13 +010057
Sean Condon49e15be2018-05-16 16:58:29 +010058 ['node', 'node'],
59 ['devIcon_SWITCH', 'switch'],
60 ['devIcon_ROADM', 'roadm'],
61 ['devIcon_OTN', 'otn'],
Sean Condon83fc39f2018-04-19 18:56:13 +010062
Sean Condon49e15be2018-05-16 16:58:29 +010063 ['portIcon_DEFAULT', 'm_ports'],
Sean Condon83fc39f2018-04-19 18:56:13 +010064
Sean Condon49e15be2018-05-16 16:58:29 +010065 ['meter', 'meterTable'], // TODO: m_meter icon?
Sean Condon83fc39f2018-04-19 18:56:13 +010066
Sean Condon49e15be2018-05-16 16:58:29 +010067 ['deviceTable', 'switch'],
68 ['flowTable', 'flowTable'],
69 ['portTable', 'portTable'],
70 ['groupTable', 'groupTable'],
71 ['meterTable', 'meterTable'],
72 ['pipeconfTable', 'pipeconfTable'],
Sean Condon83fc39f2018-04-19 18:56:13 +010073
Sean Condon49e15be2018-05-16 16:58:29 +010074 ['hostIcon_endstation', 'endstation'],
75 ['hostIcon_router', 'router'],
76 ['hostIcon_bgpSpeaker', 'bgpSpeaker'],
Sean Condon83fc39f2018-04-19 18:56:13 +010077
78 // navigation menu icons...
Sean Condon49e15be2018-05-16 16:58:29 +010079 ['nav_apps', 'bird'],
80 ['nav_settings', 'cog'],
81 ['nav_cluster', 'node'],
82 ['nav_processors', 'allTraffic'],
Bhavesh Kumard0b8bae2018-07-31 16:56:43 +053083 ['nav_partitions', 'unknown'],
Sean Condon83fc39f2018-04-19 18:56:13 +010084
Sean Condon49e15be2018-05-16 16:58:29 +010085 ['nav_topo', 'topo'],
86 ['nav_topo2', 'm_cloud'],
87 ['nav_devs', 'switch'],
88 ['nav_links', 'ports'],
89 ['nav_hosts', 'endstation'],
90 ['nav_intents', 'relatedIntents'],
91 ['nav_tunnels', 'ports'], // TODO: use tunnel glyph, when available
92 ['nav_yang', 'yang'],
Sean Condon83fc39f2018-04-19 18:56:13 +010093]);
94
95/**
96 * ONOS GUI -- SVG -- Icon Service
97 */
Sean Condon5ca00262018-09-06 17:55:25 +010098@Injectable({
99 providedIn: 'root',
100})
Sean Condon83fc39f2018-04-19 18:56:13 +0100101export class IconService {
102
103 constructor(
104 private gs: GlyphService,
105 private log: LogService,
106 private sus: SvgUtilService
107 ) {
108
109 this.log.debug('IconService constructed');
110 }
111
112 ensureIconLibDefs() {
Sean Condon49e15be2018-05-16 16:58:29 +0100113 const body = d3.select('body');
Sean Condon83fc39f2018-04-19 18:56:13 +0100114 let svg = body.select('svg#IconLibDefs');
115 if (svg.empty()) {
116 svg = body.append('svg').attr('id', 'IconLibDefs');
117 svg.append('defs');
118 }
119 return svg.select('defs');
120 }
121
122 /**
123 * Load an icon
124 *
125 * @param div A D3 selection of the '&lt;div&gt;' element into which icon should load
126 * @param glyphId Identifies the glyph to use
127 * @param size The dimension of icon in pixels. Defaults to 20.
128 * @param installGlyph If truthy, will cause the glyph to be added to
129 * well-known defs element. Defaults to false.
130 * @param svgClass The CSS class used to identify the SVG layer.
131 * Defaults to 'embeddedIcon'.
132 */
133 loadIcon(div, glyphId: string = 'unknown', size: number = 20, installGlyph: boolean = true, svgClass: string = 'embeddedIcon') {
Sean Condon49e15be2018-05-16 16:58:29 +0100134 const dim = size || 20;
135 const svgCls = svgClass || 'embeddedIcon';
136 const gid = glyphId || 'unknown';
Sean Condon83fc39f2018-04-19 18:56:13 +0100137 let g;
138 let svgIcon: any;
139
140 if (installGlyph) {
141 this.gs.loadDefs(this.ensureIconLibDefs(), [gid], true);
142 }
143 this.log.warn('loadEmbeddedIcon. install done');
144
145 svgIcon = div
146 .append('svg')
147 .attr('class', svgCls)
148 .attr('width', dim)
149 .attr('height', dim)
150 .attr('viewBox', viewBox);
151
152 g = svgIcon.append('g')
153 .attr('class', 'icon');
154
155 g.append('rect')
156 .attr('width', vboxSize)
157 .attr('height', vboxSize)
158 .attr('rx', cornerSize);
159
160 g.append('use')
161 .attr('width', vboxSize)
162 .attr('height', vboxSize)
163 .attr('class', 'glyph')
164 .attr('xlink:href', '#' + gid);
165 }
166
167 /**
168 * Load an icon by class.
169 * @param div A D3 selection of the <DIV> element into which icon should load
170 * @param iconCls The CSS class used to identify the icon
Sean Condon5ca00262018-09-06 17:55:25 +0100171 * @param size The dimension of icon in pixels. Defaults to 20.
172 * @param installGlyph If truthy, will cause the glyph to be added to
Sean Condon83fc39f2018-04-19 18:56:13 +0100173 * well-known defs element. Defaults to false.
174 * @param svgClass The CSS class used to identify the SVG layer.
175 * Defaults to 'embeddedIcon'.
176 */
Sean Condon49e15be2018-05-16 16:58:29 +0100177 loadIconByClass(div, iconCls: string, size: number, installGlyph: boolean, svgClass= 'embeddedIcon') {
Sean Condon83fc39f2018-04-19 18:56:13 +0100178 this.loadIcon(div, glyphMapping.get(iconCls), size, installGlyph, svgClass);
179 div.select('svg g').classed(iconCls, true);
180 }
181
182 /**
183 * Load an embedded icon.
184 */
185 loadEmbeddedIcon(div, iconCls: string, size: number) {
186 this.loadIconByClass(div, iconCls, size, true);
187 }
188
189 /**
190 * Load an icon only to the svg defs collection
191 *
192 * Note: This is added for use with IconComponent, where the icon's
193 * svg element is defined in the component template (and not built
194 * inline using d3 manipulation
195 *
196 * @param iconCls The icon class as a string
197 */
198 loadIconDef(iconCls: string): void {
199 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphMapping.get(iconCls)], true);
Sean Condon28ecc5f2018-06-25 12:50:16 +0100200 this.log.debug('icon definition', iconCls, 'added to defs');
Sean Condon83fc39f2018-04-19 18:56:13 +0100201 }
202
203
204 /**
205 * Add a device icon
206 *
207 * Adds a device glyph to the specified element.
208 * Returns the D3 selection of the glyph (use) element.
209 */
210 addDeviceIcon(elem, glyphId, iconDim) {
Sean Condon49e15be2018-05-16 16:58:29 +0100211 const gid = this.gs.glyphDefined(glyphId) ? glyphId : 'query';
Sean Condon83fc39f2018-04-19 18:56:13 +0100212 return elem.append('use').attr({
213 'xlink:href': '#' + gid,
214 width: iconDim,
215 height: iconDim,
216 });
217 }
218
219 addHostIcon(elem, radius, glyphId) {
Sean Condon49e15be2018-05-16 16:58:29 +0100220 const dim = radius * 1.5;
221 const xlate = -dim / 2;
222 const g = elem.append('g')
Sean Condon83fc39f2018-04-19 18:56:13 +0100223 .attr('class', 'svgIcon hostIcon');
224
225 g.append('circle').attr('r', radius);
226
227 g.append('use').attr({
228 'xlink:href': '#' + glyphId,
229 width: dim,
230 height: dim,
Sean Condonfd6d11b2018-06-02 20:29:49 +0100231 transform: this.sus.translate([xlate], xlate),
Sean Condon83fc39f2018-04-19 18:56:13 +0100232 });
233 return g;
234 }
235
236 registerIconMapping(iconId, glyphId) {
237 if (glyphMapping[iconId]) {
238 this.log.warn('Icon with id', iconId, 'already mapped. Ignoring.');
239 } else {
240 // map icon-->glyph
241 glyphMapping[iconId] = glyphId;
242 // make sure definition is installed
243 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphId], true);
244 }
245 }
246}