blob: 0f9da65968ab825f97a792d46e3f41906566c054 [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'],
Bhavesh Kumard0b8bae2018-07-31 16:56:43 +053082 ['nav_partitions', 'unknown'],
Sean Condon83fc39f2018-04-19 18:56:13 +010083
Sean Condon49e15be2018-05-16 16:58:29 +010084 ['nav_topo', 'topo'],
85 ['nav_topo2', 'm_cloud'],
86 ['nav_devs', 'switch'],
87 ['nav_links', 'ports'],
88 ['nav_hosts', 'endstation'],
89 ['nav_intents', 'relatedIntents'],
90 ['nav_tunnels', 'ports'], // TODO: use tunnel glyph, when available
91 ['nav_yang', 'yang'],
Sean Condon83fc39f2018-04-19 18:56:13 +010092]);
93
94/**
95 * ONOS GUI -- SVG -- Icon Service
96 */
97@Injectable()
98export class IconService {
99
100 constructor(
101 private gs: GlyphService,
102 private log: LogService,
103 private sus: SvgUtilService
104 ) {
105
106 this.log.debug('IconService constructed');
107 }
108
109 ensureIconLibDefs() {
Sean Condon49e15be2018-05-16 16:58:29 +0100110 const body = d3.select('body');
Sean Condon83fc39f2018-04-19 18:56:13 +0100111 let svg = body.select('svg#IconLibDefs');
112 if (svg.empty()) {
113 svg = body.append('svg').attr('id', 'IconLibDefs');
114 svg.append('defs');
115 }
116 return svg.select('defs');
117 }
118
119 /**
120 * Load an icon
121 *
122 * @param div A D3 selection of the '&lt;div&gt;' element into which icon should load
123 * @param glyphId Identifies the glyph to use
124 * @param size The dimension of icon in pixels. Defaults to 20.
125 * @param installGlyph If truthy, will cause the glyph to be added to
126 * well-known defs element. Defaults to false.
127 * @param svgClass The CSS class used to identify the SVG layer.
128 * Defaults to 'embeddedIcon'.
129 */
130 loadIcon(div, glyphId: string = 'unknown', size: number = 20, installGlyph: boolean = true, svgClass: string = 'embeddedIcon') {
Sean Condon49e15be2018-05-16 16:58:29 +0100131 const dim = size || 20;
132 const svgCls = svgClass || 'embeddedIcon';
133 const gid = glyphId || 'unknown';
Sean Condon83fc39f2018-04-19 18:56:13 +0100134 let g;
135 let svgIcon: any;
136
137 if (installGlyph) {
138 this.gs.loadDefs(this.ensureIconLibDefs(), [gid], true);
139 }
140 this.log.warn('loadEmbeddedIcon. install done');
141
142 svgIcon = div
143 .append('svg')
144 .attr('class', svgCls)
145 .attr('width', dim)
146 .attr('height', dim)
147 .attr('viewBox', viewBox);
148
149 g = svgIcon.append('g')
150 .attr('class', 'icon');
151
152 g.append('rect')
153 .attr('width', vboxSize)
154 .attr('height', vboxSize)
155 .attr('rx', cornerSize);
156
157 g.append('use')
158 .attr('width', vboxSize)
159 .attr('height', vboxSize)
160 .attr('class', 'glyph')
161 .attr('xlink:href', '#' + gid);
162 }
163
164 /**
165 * Load an icon by class.
166 * @param div A D3 selection of the <DIV> element into which icon should load
167 * @param iconCls The CSS class used to identify the icon
168 * @param {number} size The dimension of icon in pixels. Defaults to 20.
169 * @param {boolean} installGlyph If truthy, will cause the glyph to be added to
170 * well-known defs element. Defaults to false.
171 * @param svgClass The CSS class used to identify the SVG layer.
172 * Defaults to 'embeddedIcon'.
173 */
Sean Condon49e15be2018-05-16 16:58:29 +0100174 loadIconByClass(div, iconCls: string, size: number, installGlyph: boolean, svgClass= 'embeddedIcon') {
Sean Condon83fc39f2018-04-19 18:56:13 +0100175 this.loadIcon(div, glyphMapping.get(iconCls), size, installGlyph, svgClass);
176 div.select('svg g').classed(iconCls, true);
177 }
178
179 /**
180 * Load an embedded icon.
181 */
182 loadEmbeddedIcon(div, iconCls: string, size: number) {
183 this.loadIconByClass(div, iconCls, size, true);
184 }
185
186 /**
187 * Load an icon only to the svg defs collection
188 *
189 * Note: This is added for use with IconComponent, where the icon's
190 * svg element is defined in the component template (and not built
191 * inline using d3 manipulation
192 *
193 * @param iconCls The icon class as a string
194 */
195 loadIconDef(iconCls: string): void {
196 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphMapping.get(iconCls)], true);
Sean Condon28ecc5f2018-06-25 12:50:16 +0100197 this.log.debug('icon definition', iconCls, 'added to defs');
Sean Condon83fc39f2018-04-19 18:56:13 +0100198 }
199
200
201 /**
202 * Add a device icon
203 *
204 * Adds a device glyph to the specified element.
205 * Returns the D3 selection of the glyph (use) element.
206 */
207 addDeviceIcon(elem, glyphId, iconDim) {
Sean Condon49e15be2018-05-16 16:58:29 +0100208 const gid = this.gs.glyphDefined(glyphId) ? glyphId : 'query';
Sean Condon83fc39f2018-04-19 18:56:13 +0100209 return elem.append('use').attr({
210 'xlink:href': '#' + gid,
211 width: iconDim,
212 height: iconDim,
213 });
214 }
215
216 addHostIcon(elem, radius, glyphId) {
Sean Condon49e15be2018-05-16 16:58:29 +0100217 const dim = radius * 1.5;
218 const xlate = -dim / 2;
219 const g = elem.append('g')
Sean Condon83fc39f2018-04-19 18:56:13 +0100220 .attr('class', 'svgIcon hostIcon');
221
222 g.append('circle').attr('r', radius);
223
224 g.append('use').attr({
225 'xlink:href': '#' + glyphId,
226 width: dim,
227 height: dim,
Sean Condonfd6d11b2018-06-02 20:29:49 +0100228 transform: this.sus.translate([xlate], xlate),
Sean Condon83fc39f2018-04-19 18:56:13 +0100229 });
230 return g;
231 }
232
233 registerIconMapping(iconId, glyphId) {
234 if (glyphMapping[iconId]) {
235 this.log.warn('Icon with id', iconId, 'already mapped. Ignoring.');
236 } else {
237 // map icon-->glyph
238 glyphMapping[iconId] = glyphId;
239 // make sure definition is installed
240 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphId], true);
241 }
242 }
243}