blob: 813589f1fb797861178e441316568b601f40309b [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
Sean Condon49e15be2018-05-16 16:58:29 +010043 ['topo', 'topo'],
Sean Condon83fc39f2018-04-19 18:56:13 +010044
Sean Condon49e15be2018-05-16 16:58:29 +010045 ['refresh', 'refresh'],
46 ['query', 'query'],
47 ['garbage', 'garbage'],
Sean Condon83fc39f2018-04-19 18:56:13 +010048
49
Sean Condon49e15be2018-05-16 16:58:29 +010050 ['upArrow', 'triangleUp'],
51 ['downArrow', 'triangleDown'],
Sean Condon83fc39f2018-04-19 18:56:13 +010052
Sean Condon49e15be2018-05-16 16:58:29 +010053 ['appInactive', 'unknown'],
Sean Condon83fc39f2018-04-19 18:56:13 +010054
Sean Condon49e15be2018-05-16 16:58:29 +010055 ['node', 'node'],
56 ['devIcon_SWITCH', 'switch'],
57 ['devIcon_ROADM', 'roadm'],
58 ['devIcon_OTN', 'otn'],
Sean Condon83fc39f2018-04-19 18:56:13 +010059
Sean Condon49e15be2018-05-16 16:58:29 +010060 ['portIcon_DEFAULT', 'm_ports'],
Sean Condon83fc39f2018-04-19 18:56:13 +010061
Sean Condon49e15be2018-05-16 16:58:29 +010062 ['meter', 'meterTable'], // TODO: m_meter icon?
Sean Condon83fc39f2018-04-19 18:56:13 +010063
Sean Condon49e15be2018-05-16 16:58:29 +010064 ['deviceTable', 'switch'],
65 ['flowTable', 'flowTable'],
66 ['portTable', 'portTable'],
67 ['groupTable', 'groupTable'],
68 ['meterTable', 'meterTable'],
69 ['pipeconfTable', 'pipeconfTable'],
Sean Condon83fc39f2018-04-19 18:56:13 +010070
Sean Condon49e15be2018-05-16 16:58:29 +010071 ['hostIcon_endstation', 'endstation'],
72 ['hostIcon_router', 'router'],
73 ['hostIcon_bgpSpeaker', 'bgpSpeaker'],
Sean Condon83fc39f2018-04-19 18:56:13 +010074
75 // navigation menu icons...
Sean Condon49e15be2018-05-16 16:58:29 +010076 ['nav_apps', 'bird'],
77 ['nav_settings', 'cog'],
78 ['nav_cluster', 'node'],
79 ['nav_processors', 'allTraffic'],
Sean Condon83fc39f2018-04-19 18:56:13 +010080
Sean Condon49e15be2018-05-16 16:58:29 +010081 ['nav_topo', 'topo'],
82 ['nav_topo2', 'm_cloud'],
83 ['nav_devs', 'switch'],
84 ['nav_links', 'ports'],
85 ['nav_hosts', 'endstation'],
86 ['nav_intents', 'relatedIntents'],
87 ['nav_tunnels', 'ports'], // TODO: use tunnel glyph, when available
88 ['nav_yang', 'yang'],
Sean Condon83fc39f2018-04-19 18:56:13 +010089]);
90
91/**
92 * ONOS GUI -- SVG -- Icon Service
93 */
94@Injectable()
95export class IconService {
96
97 constructor(
98 private gs: GlyphService,
99 private log: LogService,
100 private sus: SvgUtilService
101 ) {
102
103 this.log.debug('IconService constructed');
104 }
105
106 ensureIconLibDefs() {
Sean Condon49e15be2018-05-16 16:58:29 +0100107 const body = d3.select('body');
Sean Condon83fc39f2018-04-19 18:56:13 +0100108 let svg = body.select('svg#IconLibDefs');
109 if (svg.empty()) {
110 svg = body.append('svg').attr('id', 'IconLibDefs');
111 svg.append('defs');
112 }
113 return svg.select('defs');
114 }
115
116 /**
117 * Load an icon
118 *
119 * @param div A D3 selection of the '&lt;div&gt;' element into which icon should load
120 * @param glyphId Identifies the glyph to use
121 * @param size The dimension of icon in pixels. Defaults to 20.
122 * @param installGlyph If truthy, will cause the glyph to be added to
123 * well-known defs element. Defaults to false.
124 * @param svgClass The CSS class used to identify the SVG layer.
125 * Defaults to 'embeddedIcon'.
126 */
127 loadIcon(div, glyphId: string = 'unknown', size: number = 20, installGlyph: boolean = true, svgClass: string = 'embeddedIcon') {
Sean Condon49e15be2018-05-16 16:58:29 +0100128 const dim = size || 20;
129 const svgCls = svgClass || 'embeddedIcon';
130 const gid = glyphId || 'unknown';
Sean Condon83fc39f2018-04-19 18:56:13 +0100131 let g;
132 let svgIcon: any;
133
134 if (installGlyph) {
135 this.gs.loadDefs(this.ensureIconLibDefs(), [gid], true);
136 }
137 this.log.warn('loadEmbeddedIcon. install done');
138
139 svgIcon = div
140 .append('svg')
141 .attr('class', svgCls)
142 .attr('width', dim)
143 .attr('height', dim)
144 .attr('viewBox', viewBox);
145
146 g = svgIcon.append('g')
147 .attr('class', 'icon');
148
149 g.append('rect')
150 .attr('width', vboxSize)
151 .attr('height', vboxSize)
152 .attr('rx', cornerSize);
153
154 g.append('use')
155 .attr('width', vboxSize)
156 .attr('height', vboxSize)
157 .attr('class', 'glyph')
158 .attr('xlink:href', '#' + gid);
159 }
160
161 /**
162 * Load an icon by class.
163 * @param div A D3 selection of the <DIV> element into which icon should load
164 * @param iconCls The CSS class used to identify the icon
165 * @param {number} size The dimension of icon in pixels. Defaults to 20.
166 * @param {boolean} installGlyph If truthy, will cause the glyph to be added to
167 * well-known defs element. Defaults to false.
168 * @param svgClass The CSS class used to identify the SVG layer.
169 * Defaults to 'embeddedIcon'.
170 */
Sean Condon49e15be2018-05-16 16:58:29 +0100171 loadIconByClass(div, iconCls: string, size: number, installGlyph: boolean, svgClass= 'embeddedIcon') {
Sean Condon83fc39f2018-04-19 18:56:13 +0100172 this.loadIcon(div, glyphMapping.get(iconCls), size, installGlyph, svgClass);
173 div.select('svg g').classed(iconCls, true);
174 }
175
176 /**
177 * Load an embedded icon.
178 */
179 loadEmbeddedIcon(div, iconCls: string, size: number) {
180 this.loadIconByClass(div, iconCls, size, true);
181 }
182
183 /**
184 * Load an icon only to the svg defs collection
185 *
186 * Note: This is added for use with IconComponent, where the icon's
187 * svg element is defined in the component template (and not built
188 * inline using d3 manipulation
189 *
190 * @param iconCls The icon class as a string
191 */
192 loadIconDef(iconCls: string): void {
193 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphMapping.get(iconCls)], true);
Sean Condon28ecc5f2018-06-25 12:50:16 +0100194 this.log.debug('icon definition', iconCls, 'added to defs');
Sean Condon83fc39f2018-04-19 18:56:13 +0100195 }
196
197
198 /**
199 * Add a device icon
200 *
201 * Adds a device glyph to the specified element.
202 * Returns the D3 selection of the glyph (use) element.
203 */
204 addDeviceIcon(elem, glyphId, iconDim) {
Sean Condon49e15be2018-05-16 16:58:29 +0100205 const gid = this.gs.glyphDefined(glyphId) ? glyphId : 'query';
Sean Condon83fc39f2018-04-19 18:56:13 +0100206 return elem.append('use').attr({
207 'xlink:href': '#' + gid,
208 width: iconDim,
209 height: iconDim,
210 });
211 }
212
213 addHostIcon(elem, radius, glyphId) {
Sean Condon49e15be2018-05-16 16:58:29 +0100214 const dim = radius * 1.5;
215 const xlate = -dim / 2;
216 const g = elem.append('g')
Sean Condon83fc39f2018-04-19 18:56:13 +0100217 .attr('class', 'svgIcon hostIcon');
218
219 g.append('circle').attr('r', radius);
220
221 g.append('use').attr({
222 'xlink:href': '#' + glyphId,
223 width: dim,
224 height: dim,
Sean Condonfd6d11b2018-06-02 20:29:49 +0100225 transform: this.sus.translate([xlate], xlate),
Sean Condon83fc39f2018-04-19 18:56:13 +0100226 });
227 return g;
228 }
229
230 registerIconMapping(iconId, glyphId) {
231 if (glyphMapping[iconId]) {
232 this.log.warn('Icon with id', iconId, 'already mapped. Ignoring.');
233 } else {
234 // map icon-->glyph
235 glyphMapping[iconId] = glyphId;
236 // make sure definition is installed
237 this.gs.loadDefs(this.ensureIconLibDefs(), [glyphId], true);
238 }
239 }
240}