blob: aabbc170ecf142366cb0208444937aaa0671e768 [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
40 delta: 'delta',
41 nonzero: 'nonzero',
Simon Hunt986b92f2016-06-03 15:46:59 -070042 close: 'xClose',
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070043
Simon Hunt4e412732015-10-27 15:25:39 -070044 topo: 'topo',
45
Bri Prebilic Cole43f17c02015-05-01 10:43:38 -070046 refresh: 'refresh',
chengfan386620e2016-11-09 17:02:40 +080047 query: 'query',
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -070048 garbage: 'garbage',
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070049
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070050 upArrow: 'triangleUp',
51 downArrow: 'triangleDown',
52
53 appInactive: 'unknown',
54
Simon Hunt6b5a9412016-10-26 19:32:04 -070055 node: 'node',
Bri Prebilic Coledea09742015-02-12 15:33:50 -080056 devIcon_SWITCH: 'switch',
Thomas Vachuska58eded62015-03-31 12:04:03 -070057 devIcon_ROADM: 'roadm',
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020058 devIcon_OTN: 'otn',
Simon Hunt864333a2015-11-16 17:08:08 -080059 deviceTable: 'switch',
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -070060 flowTable: 'flowTable',
61 portTable: 'portTable',
62 groupTable: 'groupTable',
Jian Li1f544732015-12-30 23:36:37 -080063 meterTable: 'meterTable',
Thomas Vachuska58eded62015-03-31 12:04:03 -070064
65 hostIcon_endstation: 'endstation',
66 hostIcon_router: 'router',
Simon Hunt20e16792015-04-24 14:29:39 -070067 hostIcon_bgpSpeaker: 'bgpSpeaker',
68
Simon Hunt986b92f2016-06-03 15:46:59 -070069 // navigation menu icons...
Simon Hunt20e16792015-04-24 14:29:39 -070070 nav_apps: 'bird',
Simon Hunta758f6d2016-06-23 13:59:33 -070071 nav_settings: 'cog',
Simon Hunt20e16792015-04-24 14:29:39 -070072 nav_cluster: 'node',
Simon Hunt986b92f2016-06-03 15:46:59 -070073 nav_processors: 'allTraffic',
74
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070075 nav_topo: 'topo',
Simon Hunt47f8fb72017-04-11 13:49:31 -070076 nav_topo2: 'm_cloud',
Simon Hunt20e16792015-04-24 14:29:39 -070077 nav_devs: 'switch',
78 nav_links: 'ports',
79 nav_hosts: 'endstation',
Thomas Vachuska3ece3732015-09-22 23:58:50 -070080 nav_intents: 'relatedIntents',
Steven Burrows1c2a9682017-07-14 16:52:46 +010081 nav_tunnels: 'ports', // TODO: use tunnel glyph, when available
82 nav_yang: 'yang',
Simon Huntac4c6f72015-02-03 19:50:53 -080083 };
84
Simon Hunt97225382015-01-19 13:33:09 -080085 function ensureIconLibDefs() {
86 var body = d3.select('body'),
Simon Hunt24d0c5c2016-03-03 00:05:08 -080087 svg = body.select('svg#IconLibDefs');
Simon Hunt97225382015-01-19 13:33:09 -080088
89 if (svg.empty()) {
90 svg = body.append('svg').attr('id', 'IconLibDefs');
Simon Hunt24d0c5c2016-03-03 00:05:08 -080091 svg.append('defs');
Simon Hunt97225382015-01-19 13:33:09 -080092 }
93 return svg.select('defs');
94 }
95
Simon Huntac4c6f72015-02-03 19:50:53 -080096 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080097 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080098 // size is dimension of icon in pixels. Defaults to 20.
99 // installGlyph, if truthy, will cause the glyph to be added to
100 // well-known defs element. Defaults to false.
101 // svgClass is the CSS class used to identify the SVG layer.
102 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800103 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800104 var dim = size || 20,
105 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800106 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -0800107 svg, g;
108
109 if (installGlyph) {
110 gs.loadDefs(ensureIconLibDefs(), [gid], true);
111 }
112
113 svg = div.append('svg').attr({
114 'class': svgCls,
115 width: dim,
116 height: dim,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100117 viewBox: viewBox,
Simon Huntac4c6f72015-02-03 19:50:53 -0800118 });
119
120 g = svg.append('g').attr({
Steven Burrows1c2a9682017-07-14 16:52:46 +0100121 'class': 'icon',
Simon Huntac4c6f72015-02-03 19:50:53 -0800122 });
123
124 g.append('rect').attr({
125 width: vboxSize,
126 height: vboxSize,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100127 rx: cornerSize,
Simon Huntac4c6f72015-02-03 19:50:53 -0800128 });
129
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800130 g.append('use').attr({
131 width: vboxSize,
132 height: vboxSize,
133 'class': 'glyph',
Steven Burrows1c2a9682017-07-14 16:52:46 +0100134 'xlink:href': '#' + gid,
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800135 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800136 }
137
138 // div is a D3 selection of the <DIV> element into which icon should load
139 // iconCls is the CSS class used to identify the icon
140 // size is dimension of icon in pixels. Defaults to 20.
141 // installGlyph, if truthy, will cause the glyph to be added to
142 // well-known defs element. Defaults to false.
143 // svgClass is the CSS class used to identify the SVG layer.
144 // Defaults to 'embeddedIcon'.
145 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
146 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
147 div.select('svg g').classed(iconCls, true);
148 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800149
150 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800151 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800152 }
Simon Huntca5e8872017-08-01 13:33:51 -0700153
Simon Huntf44d7262016-06-14 14:46:56 -0700154 // Adds a device glyph to the specified element.
155 // Returns the D3 selection of the glyph (use) element.
156 function addDeviceIcon(elem, glyphId, iconDim) {
157 var gid = gs.glyphDefined(glyphId) ? glyphId : 'query';
158 return elem.append('use').attr({
Simon Hunt044f28072015-10-08 12:38:16 -0700159 'xlink:href': '#' + gid,
Simon Huntf44d7262016-06-14 14:46:56 -0700160 width: iconDim,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100161 height: iconDim,
Simon Huntac4c6f72015-02-03 19:50:53 -0800162 });
Simon Huntac4c6f72015-02-03 19:50:53 -0800163 }
164
Simon Hunt1894d792015-02-04 17:09:20 -0800165 function addHostIcon(elem, radius, glyphId) {
166 var dim = radius * 1.5,
167 xlate = -dim / 2,
168 g = elem.append('g')
169 .attr('class', 'svgIcon hostIcon');
170
171 g.append('circle').attr('r', radius);
172
173 g.append('use').attr({
174 'xlink:href': '#' + glyphId,
175 width: dim,
176 height: dim,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100177 transform: sus.translate(xlate, xlate),
Simon Hunt1894d792015-02-04 17:09:20 -0800178 });
179 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800180 }
181
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700182 function sortIcons() {
Simon Hunt051e9fa2016-01-19 15:54:22 -0800183 function _s(div, gid) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800184 div.style('display', 'inline-block');
Simon Hunt051e9fa2016-01-19 15:54:22 -0800185 loadEmbeddedIcon(div, gid, 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700186 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800187 }
188
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800189 return {
Simon Hunt051e9fa2016-01-19 15:54:22 -0800190 asc: function (div) { _s(div, 'upArrow'); },
191 desc: function (div) { _s(div, 'downArrow'); },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100192 none: function (div) { div.remove(); },
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800193 };
194 }
195
Simon Hunt6f7668c2016-02-02 14:27:11 -0800196 function registerIconMapping(iconId, glyphId) {
197 if (glyphMapping[iconId]) {
198 $log.warn('Icon with id', iconId, 'already mapped. Ignoring.');
199 } else {
Simon Hunt24d0c5c2016-03-03 00:05:08 -0800200 // map icon-->glyph
Simon Hunt6f7668c2016-02-02 14:27:11 -0800201 glyphMapping[iconId] = glyphId;
Simon Hunt24d0c5c2016-03-03 00:05:08 -0800202 // make sure definition is installed
203 gs.loadDefs(ensureIconLibDefs(), [glyphId], true);
Simon Hunt6f7668c2016-02-02 14:27:11 -0800204 }
205 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800206
207 // =========================
208 // === DEFINE THE MODULE
209
Simon Hunt7ac7be92015-01-06 10:47:56 -0800210 angular.module('onosSvg')
Bri Prebilic Cole0c41ba22015-07-06 15:09:48 -0700211 .directive('icon', ['IconService', function (is) {
212 return {
213 restrict: 'A',
214 link: function (scope, element, attrs) {
215 attrs.$observe('iconId', function () {
216 var div = d3.select(element[0]);
217 div.selectAll('*').remove();
218 is.loadEmbeddedIcon(div, attrs.iconId, attrs.iconSize);
219 });
Steven Burrows1c2a9682017-07-14 16:52:46 +0100220 },
Bri Prebilic Cole0c41ba22015-07-06 15:09:48 -0700221 };
222 }])
223
Steven Burrows1c2a9682017-07-14 16:52:46 +0100224 .factory('IconService', ['$log', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800225 'SvgUtilService',
226
Steven Burrows1c2a9682017-07-14 16:52:46 +0100227 function (_$log_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800228 $log = _$log_;
Simon Hunt97225382015-01-19 13:33:09 -0800229 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800230 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800231
Simon Hunt7ac7be92015-01-06 10:47:56 -0800232 return {
Simon Hunt97225382015-01-19 13:33:09 -0800233 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800234 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800235 loadEmbeddedIcon: loadEmbeddedIcon,
236 addDeviceIcon: addDeviceIcon,
237 addHostIcon: addHostIcon,
Simon Hunt6f7668c2016-02-02 14:27:11 -0800238 sortIcons: sortIcons,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100239 registerIconMapping: registerIconMapping,
Simon Hunt7ac7be92015-01-06 10:47:56 -0800240 };
241 }]);
242
243}());