blob: 6ced1622b918ca00ed7b7ebdc337a8fa604308e7 [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 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 */
Sean Condon5ca00262018-09-06 17:55:25 +010097@Injectable({
98 providedIn: 'root',
99})
Sean Condon83fc39f2018-04-19 18:56:13 +0100100export class IconService {
101
102 constructor(
103 private gs: GlyphService,
104 private log: LogService,
105 private sus: SvgUtilService
106 ) {
107
108 this.log.debug('IconService constructed');
109 }
110
111 ensureIconLibDefs() {
Sean Condon49e15be2018-05-16 16:58:29 +0100112 const body = d3.select('body');
Sean Condon83fc39f2018-04-19 18:56:13 +0100113 let svg = body.select('svg#IconLibDefs');
114 if (svg.empty()) {
115 svg = body.append('svg').attr('id', 'IconLibDefs');
116 svg.append('defs');
117 }
118 return svg.select('defs');
119 }
120
121 /**
122 * Load an icon
123 *
124 * @param div A D3 selection of the '&lt;div&gt;' element into which icon should load
125 * @param glyphId Identifies the glyph to use
126 * @param size The dimension of icon in pixels. Defaults to 20.
127 * @param installGlyph If truthy, will cause the glyph to be added to
128 * well-known defs element. Defaults to false.
129 * @param svgClass The CSS class used to identify the SVG layer.
130 * Defaults to 'embeddedIcon'.
131 */
132 loadIcon(div, glyphId: string = 'unknown', size: number = 20, installGlyph: boolean = true, svgClass: string = 'embeddedIcon') {
Sean Condon49e15be2018-05-16 16:58:29 +0100133 const dim = size || 20;
134 const svgCls = svgClass || 'embeddedIcon';
135 const gid = glyphId || 'unknown';
Sean Condon83fc39f2018-04-19 18:56:13 +0100136 let g;
137 let svgIcon: any;
138
139 if (installGlyph) {
140 this.gs.loadDefs(this.ensureIconLibDefs(), [gid], true);
141 }
142 this.log.warn('loadEmbeddedIcon. install done');
143
144 svgIcon = div
145 .append('svg')
146 .attr('class', svgCls)
147 .attr('width', dim)
148 .attr('height', dim)
149 .attr('viewBox', viewBox);
150
151 g = svgIcon.append('g')
152 .attr('class', 'icon');
153
154 g.append('rect')
155 .attr('width', vboxSize)
156 .attr('height', vboxSize)
157 .attr('rx', cornerSize);
158
159 g.append('use')
160 .attr('width', vboxSize)
161 .attr('height', vboxSize)
162 .attr('class', 'glyph')
163 .attr('xlink:href', '#' + gid);
164 }
165
166 /**
167 * Load an icon by class.
168 * @param div A D3 selection of the <DIV> element into which icon should load
169 * @param iconCls The CSS class used to identify the icon
Sean Condon5ca00262018-09-06 17:55:25 +0100170 * @param size The dimension of icon in pixels. Defaults to 20.
171 * @param installGlyph If truthy, will cause the glyph to be added to
Sean Condon83fc39f2018-04-19 18:56:13 +0100172 * well-known defs element. Defaults to false.
173 * @param svgClass The CSS class used to identify the SVG layer.
174 * Defaults to 'embeddedIcon'.
175 */
Sean Condon49e15be2018-05-16 16:58:29 +0100176 loadIconByClass(div, iconCls: string, size: number, installGlyph: boolean, svgClass= 'embeddedIcon') {
Sean Condon83fc39f2018-04-19 18:56:13 +0100177 this.loadIcon(div, glyphMapping.get(iconCls), size, installGlyph, svgClass);
178 div.select('svg g').classed(iconCls, true);
179 }
180
181 /**
182 * Load an embedded icon.
183 */
184 loadEmbeddedIcon(div, iconCls: string, size: number) {
185 this.loadIconByClass(div, iconCls, size, true);
186 }
187
188 /**
189 * Load an icon only to the svg defs collection
190 *
191 * Note: This is added for use with IconComponent, where the icon's
192 * svg element is defined in the component template (and not built
193 * inline using d3 manipulation
194 *
195 * @param iconCls The icon class as a string
196 */
197 loadIconDef(iconCls: string): void {
198 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphMapping.get(iconCls)], true);
Sean Condon28ecc5f2018-06-25 12:50:16 +0100199 this.log.debug('icon definition', iconCls, 'added to defs');
Sean Condon83fc39f2018-04-19 18:56:13 +0100200 }
201
202
203 /**
204 * Add a device icon
205 *
206 * Adds a device glyph to the specified element.
207 * Returns the D3 selection of the glyph (use) element.
208 */
209 addDeviceIcon(elem, glyphId, iconDim) {
Sean Condon49e15be2018-05-16 16:58:29 +0100210 const gid = this.gs.glyphDefined(glyphId) ? glyphId : 'query';
Sean Condon83fc39f2018-04-19 18:56:13 +0100211 return elem.append('use').attr({
212 'xlink:href': '#' + gid,
213 width: iconDim,
214 height: iconDim,
215 });
216 }
217
218 addHostIcon(elem, radius, glyphId) {
Sean Condon49e15be2018-05-16 16:58:29 +0100219 const dim = radius * 1.5;
220 const xlate = -dim / 2;
221 const g = elem.append('g')
Sean Condon83fc39f2018-04-19 18:56:13 +0100222 .attr('class', 'svgIcon hostIcon');
223
224 g.append('circle').attr('r', radius);
225
226 g.append('use').attr({
227 'xlink:href': '#' + glyphId,
228 width: dim,
229 height: dim,
Sean Condonfd6d11b2018-06-02 20:29:49 +0100230 transform: this.sus.translate([xlate], xlate),
Sean Condon83fc39f2018-04-19 18:56:13 +0100231 });
232 return g;
233 }
234
235 registerIconMapping(iconId, glyphId) {
236 if (glyphMapping[iconId]) {
237 this.log.warn('Icon with id', iconId, 'already mapped. Ignoring.');
238 } else {
239 // map icon-->glyph
240 glyphMapping[iconId] = glyphId;
241 // make sure definition is installed
242 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphId], true);
243 }
244 }
245}