blob: de3672ce28bbe11838527b211f17d014848c72db [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
2 * Copyright 2015-present Open Networking Foundation
3 *
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';
18import { LogService } from '../../log.service';
19import { 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 Condon83fc39f2018-04-19 18:56:13 +010046
Sean Condon49e15be2018-05-16 16:58:29 +010047 ['refresh', 'refresh'],
48 ['query', 'query'],
49 ['garbage', 'garbage'],
Sean Condon83fc39f2018-04-19 18:56:13 +010050
51
Sean Condon49e15be2018-05-16 16:58:29 +010052 ['upArrow', 'triangleUp'],
53 ['downArrow', 'triangleDown'],
Sean Condon83fc39f2018-04-19 18:56:13 +010054
Sean Condon49e15be2018-05-16 16:58:29 +010055 ['appInactive', 'unknown'],
Sean Condon83fc39f2018-04-19 18:56:13 +010056
Sean Condon49e15be2018-05-16 16:58:29 +010057 ['node', 'node'],
58 ['devIcon_SWITCH', 'switch'],
59 ['devIcon_ROADM', 'roadm'],
60 ['devIcon_OTN', 'otn'],
Sean Condon83fc39f2018-04-19 18:56:13 +010061
Sean Condon49e15be2018-05-16 16:58:29 +010062 ['portIcon_DEFAULT', 'm_ports'],
Sean Condon83fc39f2018-04-19 18:56:13 +010063
Sean Condon49e15be2018-05-16 16:58:29 +010064 ['meter', 'meterTable'], // TODO: m_meter icon?
Sean Condon83fc39f2018-04-19 18:56:13 +010065
Sean Condon49e15be2018-05-16 16:58:29 +010066 ['deviceTable', 'switch'],
67 ['flowTable', 'flowTable'],
68 ['portTable', 'portTable'],
69 ['groupTable', 'groupTable'],
70 ['meterTable', 'meterTable'],
71 ['pipeconfTable', 'pipeconfTable'],
Sean Condon83fc39f2018-04-19 18:56:13 +010072
Sean Condon49e15be2018-05-16 16:58:29 +010073 ['hostIcon_endstation', 'endstation'],
74 ['hostIcon_router', 'router'],
75 ['hostIcon_bgpSpeaker', 'bgpSpeaker'],
Sean Condon83fc39f2018-04-19 18:56:13 +010076
77 // navigation menu icons...
Sean Condon49e15be2018-05-16 16:58:29 +010078 ['nav_apps', 'bird'],
79 ['nav_settings', 'cog'],
80 ['nav_cluster', 'node'],
81 ['nav_processors', 'allTraffic'],
Sean Condon83fc39f2018-04-19 18:56:13 +010082
Sean Condon49e15be2018-05-16 16:58:29 +010083 ['nav_topo', 'topo'],
84 ['nav_topo2', 'm_cloud'],
85 ['nav_devs', 'switch'],
86 ['nav_links', 'ports'],
87 ['nav_hosts', 'endstation'],
88 ['nav_intents', 'relatedIntents'],
89 ['nav_tunnels', 'ports'], // TODO: use tunnel glyph, when available
90 ['nav_yang', 'yang'],
Sean Condon83fc39f2018-04-19 18:56:13 +010091]);
92
93/**
94 * ONOS GUI -- SVG -- Icon Service
95 */
96@Injectable()
97export class IconService {
98
99 constructor(
100 private gs: GlyphService,
101 private log: LogService,
102 private sus: SvgUtilService
103 ) {
104
105 this.log.debug('IconService constructed');
106 }
107
108 ensureIconLibDefs() {
Sean Condon49e15be2018-05-16 16:58:29 +0100109 const body = d3.select('body');
Sean Condon83fc39f2018-04-19 18:56:13 +0100110 let svg = body.select('svg#IconLibDefs');
111 if (svg.empty()) {
112 svg = body.append('svg').attr('id', 'IconLibDefs');
113 svg.append('defs');
114 }
115 return svg.select('defs');
116 }
117
118 /**
119 * Load an icon
120 *
121 * @param div A D3 selection of the '&lt;div&gt;' element into which icon should load
122 * @param glyphId Identifies the glyph to use
123 * @param size The dimension of icon in pixels. Defaults to 20.
124 * @param installGlyph If truthy, will cause the glyph to be added to
125 * well-known defs element. Defaults to false.
126 * @param svgClass The CSS class used to identify the SVG layer.
127 * Defaults to 'embeddedIcon'.
128 */
129 loadIcon(div, glyphId: string = 'unknown', size: number = 20, installGlyph: boolean = true, svgClass: string = 'embeddedIcon') {
Sean Condon49e15be2018-05-16 16:58:29 +0100130 const dim = size || 20;
131 const svgCls = svgClass || 'embeddedIcon';
132 const gid = glyphId || 'unknown';
Sean Condon83fc39f2018-04-19 18:56:13 +0100133 let g;
134 let svgIcon: any;
135
136 if (installGlyph) {
137 this.gs.loadDefs(this.ensureIconLibDefs(), [gid], true);
138 }
139 this.log.warn('loadEmbeddedIcon. install done');
140
141 svgIcon = div
142 .append('svg')
143 .attr('class', svgCls)
144 .attr('width', dim)
145 .attr('height', dim)
146 .attr('viewBox', viewBox);
147
148 g = svgIcon.append('g')
149 .attr('class', 'icon');
150
151 g.append('rect')
152 .attr('width', vboxSize)
153 .attr('height', vboxSize)
154 .attr('rx', cornerSize);
155
156 g.append('use')
157 .attr('width', vboxSize)
158 .attr('height', vboxSize)
159 .attr('class', 'glyph')
160 .attr('xlink:href', '#' + gid);
161 }
162
163 /**
164 * Load an icon by class.
165 * @param div A D3 selection of the <DIV> element into which icon should load
166 * @param iconCls The CSS class used to identify the icon
167 * @param {number} size The dimension of icon in pixels. Defaults to 20.
168 * @param {boolean} installGlyph If truthy, will cause the glyph to be added to
169 * well-known defs element. Defaults to false.
170 * @param svgClass The CSS class used to identify the SVG layer.
171 * Defaults to 'embeddedIcon'.
172 */
Sean Condon49e15be2018-05-16 16:58:29 +0100173 loadIconByClass(div, iconCls: string, size: number, installGlyph: boolean, svgClass= 'embeddedIcon') {
Sean Condon83fc39f2018-04-19 18:56:13 +0100174 this.loadIcon(div, glyphMapping.get(iconCls), size, installGlyph, svgClass);
175 div.select('svg g').classed(iconCls, true);
176 }
177
178 /**
179 * Load an embedded icon.
180 */
181 loadEmbeddedIcon(div, iconCls: string, size: number) {
182 this.loadIconByClass(div, iconCls, size, true);
183 }
184
185 /**
186 * Load an icon only to the svg defs collection
187 *
188 * Note: This is added for use with IconComponent, where the icon's
189 * svg element is defined in the component template (and not built
190 * inline using d3 manipulation
191 *
192 * @param iconCls The icon class as a string
193 */
194 loadIconDef(iconCls: string): void {
195 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphMapping.get(iconCls)], true);
Sean Condon28ecc5f2018-06-25 12:50:16 +0100196 this.log.debug('icon definition', iconCls, 'added to defs');
Sean Condon83fc39f2018-04-19 18:56:13 +0100197 }
198
199
200 /**
201 * Add a device icon
202 *
203 * Adds a device glyph to the specified element.
204 * Returns the D3 selection of the glyph (use) element.
205 */
206 addDeviceIcon(elem, glyphId, iconDim) {
Sean Condon49e15be2018-05-16 16:58:29 +0100207 const gid = this.gs.glyphDefined(glyphId) ? glyphId : 'query';
Sean Condon83fc39f2018-04-19 18:56:13 +0100208 return elem.append('use').attr({
209 'xlink:href': '#' + gid,
210 width: iconDim,
211 height: iconDim,
212 });
213 }
214
215 addHostIcon(elem, radius, glyphId) {
Sean Condon49e15be2018-05-16 16:58:29 +0100216 const dim = radius * 1.5;
217 const xlate = -dim / 2;
218 const g = elem.append('g')
Sean Condon83fc39f2018-04-19 18:56:13 +0100219 .attr('class', 'svgIcon hostIcon');
220
221 g.append('circle').attr('r', radius);
222
223 g.append('use').attr({
224 'xlink:href': '#' + glyphId,
225 width: dim,
226 height: dim,
Sean Condonfd6d11b2018-06-02 20:29:49 +0100227 transform: this.sus.translate([xlate], xlate),
Sean Condon83fc39f2018-04-19 18:56:13 +0100228 });
229 return g;
230 }
231
232 registerIconMapping(iconId, glyphId) {
233 if (glyphMapping[iconId]) {
234 this.log.warn('Icon with id', iconId, 'already mapped. Ignoring.');
235 } else {
236 // map icon-->glyph
237 glyphMapping[iconId] = glyphId;
238 // make sure definition is installed
239 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphId], true);
240 }
241 }
242}