blob: af61e52790cbadbf071a16b5f7012723ede54109 [file] [log] [blame]
Simon Hunt7ac7be92015-01-06 10:47:56 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Hunt7ac7be92015-01-06 10:47:56 -08003 *
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 */
16
17/*
18 ONOS GUI -- SVG -- Icon Service
Simon Hunt7ac7be92015-01-06 10:47:56 -080019 */
20(function () {
21 'use strict';
22
Steven Burrows1c2a9682017-07-14 16:52:46 +010023 var $log, gs, sus;
Simon Hunt7f172cc2015-01-16 17:43:00 -080024
Simon Hunt97225382015-01-19 13:33:09 -080025 var vboxSize = 50,
26 cornerSize = vboxSize / 10,
27 viewBox = '0 0 ' + vboxSize + ' ' + vboxSize;
Simon Hunt7f172cc2015-01-16 17:43:00 -080028
Simon Huntac4c6f72015-02-03 19:50:53 -080029 // Maps icon ID to the glyph ID it uses.
30 // NOTE: icon ID maps to a CSS class for styling that icon
Simon Hunt7f172cc2015-01-16 17:43:00 -080031 var glyphMapping = {
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070032 active: 'checkMark',
33 inactive: 'xMark',
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070034
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070035 plus: 'plus',
36 minus: 'minus',
37 play: 'play',
38 stop: 'stop',
Simon Huntca5e8872017-08-01 13:33:51 -070039
Simon Hunt83b92a32017-09-11 12:58:04 -070040 upload: 'upload',
Simon Hunte3579e12017-09-08 16:15:02 -070041 download: 'download',
Simon Huntca5e8872017-08-01 13:33:51 -070042 delta: 'delta',
43 nonzero: 'nonzero',
Simon Hunt986b92f2016-06-03 15:46:59 -070044 close: 'xClose',
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070045
Simon Hunt4e412732015-10-27 15:25:39 -070046 topo: 'topo',
47
Bri Prebilic Cole43f17c02015-05-01 10:43:38 -070048 refresh: 'refresh',
chengfan386620e2016-11-09 17:02:40 +080049 query: 'query',
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -070050 garbage: 'garbage',
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070051
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070052 upArrow: 'triangleUp',
53 downArrow: 'triangleDown',
54
55 appInactive: 'unknown',
56
Simon Hunt6b5a9412016-10-26 19:32:04 -070057 node: 'node',
Bri Prebilic Coledea09742015-02-12 15:33:50 -080058 devIcon_SWITCH: 'switch',
Thomas Vachuska58eded62015-03-31 12:04:03 -070059 devIcon_ROADM: 'roadm',
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020060 devIcon_OTN: 'otn',
Simon Huntc9e83212017-10-09 16:52:07 -070061
62 portIcon_DEFAULT: 'm_ports',
63
Steven Burrows7ed60362017-10-20 03:26:05 +010064 meter: 'meterTable', // TODO: m_meter icon?
65
Simon Hunt864333a2015-11-16 17:08:08 -080066 deviceTable: 'switch',
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -070067 flowTable: 'flowTable',
68 portTable: 'portTable',
69 groupTable: 'groupTable',
Jian Li1f544732015-12-30 23:36:37 -080070 meterTable: 'meterTable',
Yi Tsenga87b40c2017-09-10 00:59:03 -070071 pipeconfTable: 'pipeconfTable',
Thomas Vachuska58eded62015-03-31 12:04:03 -070072
73 hostIcon_endstation: 'endstation',
74 hostIcon_router: 'router',
Simon Hunt20e16792015-04-24 14:29:39 -070075 hostIcon_bgpSpeaker: 'bgpSpeaker',
76
Simon Hunt986b92f2016-06-03 15:46:59 -070077 // navigation menu icons...
Simon Hunt20e16792015-04-24 14:29:39 -070078 nav_apps: 'bird',
Simon Hunta758f6d2016-06-23 13:59:33 -070079 nav_settings: 'cog',
Simon Hunt20e16792015-04-24 14:29:39 -070080 nav_cluster: 'node',
Simon Hunt986b92f2016-06-03 15:46:59 -070081 nav_processors: 'allTraffic',
82
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070083 nav_topo: 'topo',
Simon Hunt47f8fb72017-04-11 13:49:31 -070084 nav_topo2: 'm_cloud',
Simon Hunt20e16792015-04-24 14:29:39 -070085 nav_devs: 'switch',
86 nav_links: 'ports',
87 nav_hosts: 'endstation',
Thomas Vachuska3ece3732015-09-22 23:58:50 -070088 nav_intents: 'relatedIntents',
Steven Burrows1c2a9682017-07-14 16:52:46 +010089 nav_tunnels: 'ports', // TODO: use tunnel glyph, when available
90 nav_yang: 'yang',
Simon Huntac4c6f72015-02-03 19:50:53 -080091 };
92
Simon Hunt97225382015-01-19 13:33:09 -080093 function ensureIconLibDefs() {
94 var body = d3.select('body'),
Simon Hunt24d0c5c2016-03-03 00:05:08 -080095 svg = body.select('svg#IconLibDefs');
Simon Hunt97225382015-01-19 13:33:09 -080096
97 if (svg.empty()) {
98 svg = body.append('svg').attr('id', 'IconLibDefs');
Simon Hunt24d0c5c2016-03-03 00:05:08 -080099 svg.append('defs');
Simon Hunt97225382015-01-19 13:33:09 -0800100 }
101 return svg.select('defs');
102 }
103
Simon Huntac4c6f72015-02-03 19:50:53 -0800104 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800105 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -0800106 // size is dimension of icon in pixels. Defaults to 20.
107 // installGlyph, if truthy, will cause the glyph to be added to
108 // well-known defs element. Defaults to false.
109 // svgClass is the CSS class used to identify the SVG layer.
110 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800111 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800112 var dim = size || 20,
113 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800114 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -0800115 svg, g;
116
117 if (installGlyph) {
118 gs.loadDefs(ensureIconLibDefs(), [gid], true);
119 }
120
121 svg = div.append('svg').attr({
122 'class': svgCls,
123 width: dim,
124 height: dim,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100125 viewBox: viewBox,
Simon Huntac4c6f72015-02-03 19:50:53 -0800126 });
127
128 g = svg.append('g').attr({
Steven Burrows1c2a9682017-07-14 16:52:46 +0100129 'class': 'icon',
Simon Huntac4c6f72015-02-03 19:50:53 -0800130 });
131
132 g.append('rect').attr({
133 width: vboxSize,
134 height: vboxSize,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100135 rx: cornerSize,
Simon Huntac4c6f72015-02-03 19:50:53 -0800136 });
137
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800138 g.append('use').attr({
139 width: vboxSize,
140 height: vboxSize,
141 'class': 'glyph',
Steven Burrows1c2a9682017-07-14 16:52:46 +0100142 'xlink:href': '#' + gid,
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800143 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800144 }
145
146 // div is a D3 selection of the <DIV> element into which icon should load
147 // iconCls is the CSS class used to identify the icon
148 // size is dimension of icon in pixels. Defaults to 20.
149 // installGlyph, if truthy, will cause the glyph to be added to
150 // well-known defs element. Defaults to false.
151 // svgClass is the CSS class used to identify the SVG layer.
152 // Defaults to 'embeddedIcon'.
153 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
154 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
155 div.select('svg g').classed(iconCls, true);
156 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800157
158 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800159 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800160 }
Simon Huntca5e8872017-08-01 13:33:51 -0700161
Simon Huntf44d7262016-06-14 14:46:56 -0700162 // Adds a device glyph to the specified element.
163 // Returns the D3 selection of the glyph (use) element.
164 function addDeviceIcon(elem, glyphId, iconDim) {
165 var gid = gs.glyphDefined(glyphId) ? glyphId : 'query';
166 return elem.append('use').attr({
Simon Hunt044f28072015-10-08 12:38:16 -0700167 'xlink:href': '#' + gid,
Simon Huntf44d7262016-06-14 14:46:56 -0700168 width: iconDim,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100169 height: iconDim,
Simon Huntac4c6f72015-02-03 19:50:53 -0800170 });
Simon Huntac4c6f72015-02-03 19:50:53 -0800171 }
172
Simon Hunt1894d792015-02-04 17:09:20 -0800173 function addHostIcon(elem, radius, glyphId) {
174 var dim = radius * 1.5,
175 xlate = -dim / 2,
176 g = elem.append('g')
177 .attr('class', 'svgIcon hostIcon');
178
179 g.append('circle').attr('r', radius);
180
181 g.append('use').attr({
182 'xlink:href': '#' + glyphId,
183 width: dim,
184 height: dim,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100185 transform: sus.translate(xlate, xlate),
Simon Hunt1894d792015-02-04 17:09:20 -0800186 });
187 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800188 }
189
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700190 function sortIcons() {
Simon Hunt051e9fa2016-01-19 15:54:22 -0800191 function _s(div, gid) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800192 div.style('display', 'inline-block');
Simon Hunt051e9fa2016-01-19 15:54:22 -0800193 loadEmbeddedIcon(div, gid, 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700194 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800195 }
196
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800197 return {
Simon Hunt051e9fa2016-01-19 15:54:22 -0800198 asc: function (div) { _s(div, 'upArrow'); },
199 desc: function (div) { _s(div, 'downArrow'); },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100200 none: function (div) { div.remove(); },
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800201 };
202 }
203
Simon Hunt6f7668c2016-02-02 14:27:11 -0800204 function registerIconMapping(iconId, glyphId) {
205 if (glyphMapping[iconId]) {
206 $log.warn('Icon with id', iconId, 'already mapped. Ignoring.');
207 } else {
Simon Hunt24d0c5c2016-03-03 00:05:08 -0800208 // map icon-->glyph
Simon Hunt6f7668c2016-02-02 14:27:11 -0800209 glyphMapping[iconId] = glyphId;
Simon Hunt24d0c5c2016-03-03 00:05:08 -0800210 // make sure definition is installed
211 gs.loadDefs(ensureIconLibDefs(), [glyphId], true);
Simon Hunt6f7668c2016-02-02 14:27:11 -0800212 }
213 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800214
215 // =========================
216 // === DEFINE THE MODULE
217
Simon Hunt7ac7be92015-01-06 10:47:56 -0800218 angular.module('onosSvg')
Bri Prebilic Cole0c41ba22015-07-06 15:09:48 -0700219 .directive('icon', ['IconService', function (is) {
220 return {
221 restrict: 'A',
222 link: function (scope, element, attrs) {
223 attrs.$observe('iconId', function () {
224 var div = d3.select(element[0]);
225 div.selectAll('*').remove();
226 is.loadEmbeddedIcon(div, attrs.iconId, attrs.iconSize);
227 });
Steven Burrows1c2a9682017-07-14 16:52:46 +0100228 },
Bri Prebilic Cole0c41ba22015-07-06 15:09:48 -0700229 };
230 }])
231
Steven Burrows1c2a9682017-07-14 16:52:46 +0100232 .factory('IconService', ['$log', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800233 'SvgUtilService',
234
Steven Burrows1c2a9682017-07-14 16:52:46 +0100235 function (_$log_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800236 $log = _$log_;
Simon Hunt97225382015-01-19 13:33:09 -0800237 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800238 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800239
Simon Hunt7ac7be92015-01-06 10:47:56 -0800240 return {
Simon Hunt97225382015-01-19 13:33:09 -0800241 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800242 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800243 loadEmbeddedIcon: loadEmbeddedIcon,
244 addDeviceIcon: addDeviceIcon,
245 addHostIcon: addHostIcon,
Simon Hunt6f7668c2016-02-02 14:27:11 -0800246 sortIcons: sortIcons,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100247 registerIconMapping: registerIconMapping,
Simon Hunt7ac7be92015-01-06 10:47:56 -0800248 };
249 }]);
250
251}());