blob: e5b8bb47700fae4088497ff7477fe3631bf54165 [file] [log] [blame]
Simon Hunt7ac7be92015-01-06 10:47:56 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16
17/*
18 ONOS GUI -- SVG -- Icon Service
Simon Hunt7ac7be92015-01-06 10:47:56 -080019 */
20(function () {
21 'use strict';
22
Simon Hunt1894d792015-02-04 17:09:20 -080023 var $log, fs, 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',
39
Simon Hunt4e412732015-10-27 15:25:39 -070040 topo: 'topo',
41
Bri Prebilic Cole43f17c02015-05-01 10:43:38 -070042 refresh: 'refresh',
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -070043 garbage: 'garbage',
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070044
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070045 upArrow: 'triangleUp',
46 downArrow: 'triangleDown',
47
48 appInactive: 'unknown',
49
Bri Prebilic Coledea09742015-02-12 15:33:50 -080050 devIcon_SWITCH: 'switch',
Thomas Vachuska58eded62015-03-31 12:04:03 -070051 devIcon_ROADM: 'roadm',
Simon Hunt864333a2015-11-16 17:08:08 -080052 deviceTable: 'switch',
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -070053 flowTable: 'flowTable',
54 portTable: 'portTable',
55 groupTable: 'groupTable',
Thomas Vachuska58eded62015-03-31 12:04:03 -070056
57 hostIcon_endstation: 'endstation',
58 hostIcon_router: 'router',
Simon Hunt20e16792015-04-24 14:29:39 -070059 hostIcon_bgpSpeaker: 'bgpSpeaker',
60
61 nav_apps: 'bird',
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070062 nav_settings: 'chain',
Simon Hunt20e16792015-04-24 14:29:39 -070063 nav_cluster: 'node',
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070064 nav_topo: 'topo',
Simon Hunt20e16792015-04-24 14:29:39 -070065 nav_devs: 'switch',
66 nav_links: 'ports',
67 nav_hosts: 'endstation',
Thomas Vachuska3ece3732015-09-22 23:58:50 -070068 nav_intents: 'relatedIntents',
69 nav_processors: 'allTraffic'
Simon Huntac4c6f72015-02-03 19:50:53 -080070 };
71
Simon Hunt97225382015-01-19 13:33:09 -080072 function ensureIconLibDefs() {
73 var body = d3.select('body'),
74 svg = body.select('svg#IconLibDefs'),
75 defs;
76
77 if (svg.empty()) {
78 svg = body.append('svg').attr('id', 'IconLibDefs');
79 defs = svg.append('defs');
80 }
81 return svg.select('defs');
82 }
83
Simon Huntac4c6f72015-02-03 19:50:53 -080084 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080085 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080086 // size is dimension of icon in pixels. Defaults to 20.
87 // installGlyph, if truthy, will cause the glyph to be added to
88 // well-known defs element. Defaults to false.
89 // svgClass is the CSS class used to identify the SVG layer.
90 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080091 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -080092 var dim = size || 20,
93 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080094 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -080095 svg, g;
96
97 if (installGlyph) {
98 gs.loadDefs(ensureIconLibDefs(), [gid], true);
99 }
100
101 svg = div.append('svg').attr({
102 'class': svgCls,
103 width: dim,
104 height: dim,
105 viewBox: viewBox
106 });
107
108 g = svg.append('g').attr({
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800109 'class': 'icon'
Simon Huntac4c6f72015-02-03 19:50:53 -0800110 });
111
112 g.append('rect').attr({
113 width: vboxSize,
114 height: vboxSize,
115 rx: cornerSize
116 });
117
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800118 g.append('use').attr({
119 width: vboxSize,
120 height: vboxSize,
121 'class': 'glyph',
122 'xlink:href': '#' + gid
123 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800124 }
125
126 // div is a D3 selection of the <DIV> element into which icon should load
127 // iconCls is the CSS class used to identify the icon
128 // size is dimension of icon in pixels. Defaults to 20.
129 // installGlyph, if truthy, will cause the glyph to be added to
130 // well-known defs element. Defaults to false.
131 // svgClass is the CSS class used to identify the SVG layer.
132 // Defaults to 'embeddedIcon'.
133 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
134 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
135 div.select('svg g').classed(iconCls, true);
136 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800137
138 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800139 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800140 }
141
142
143 // configuration for device and host icons in the topology view
144 var config = {
145 device: {
146 dim: 36,
147 rx: 4
148 },
149 host: {
Simon Huntc2bfe332015-12-04 11:01:24 -0800150 badge: {
151 dx: 14,
152 dy: -14
153 },
Simon Huntac4c6f72015-02-03 19:50:53 -0800154 radius: {
155 noGlyph: 9,
156 withGlyph: 14
157 },
158 glyphed: {
159 endstation: 1,
160 bgpSpeaker: 1,
161 router: 1
162 }
163 }
164 };
165
166
167 // Adds a device icon to the specified element, using the given glyph.
168 // Returns the D3 selection of the icon.
169 function addDeviceIcon(elem, glyphId) {
170 var cfg = config.device,
Simon Hunt044f28072015-10-08 12:38:16 -0700171 gid = gs.glyphDefined(glyphId) ? glyphId : 'query',
Simon Huntac4c6f72015-02-03 19:50:53 -0800172 g = elem.append('g')
173 .attr('class', 'svgIcon deviceIcon');
174
175 g.append('rect').attr({
176 x: 0,
177 y: 0,
178 rx: cfg.rx,
179 width: cfg.dim,
180 height: cfg.dim
181 });
182
183 g.append('use').attr({
Simon Hunt044f28072015-10-08 12:38:16 -0700184 'xlink:href': '#' + gid,
Simon Huntac4c6f72015-02-03 19:50:53 -0800185 width: cfg.dim,
186 height: cfg.dim
187 });
188
189 g.dim = cfg.dim;
190 return g;
191 }
192
Simon Hunt1894d792015-02-04 17:09:20 -0800193 function addHostIcon(elem, radius, glyphId) {
194 var dim = radius * 1.5,
195 xlate = -dim / 2,
196 g = elem.append('g')
197 .attr('class', 'svgIcon hostIcon');
198
199 g.append('circle').attr('r', radius);
200
201 g.append('use').attr({
202 'xlink:href': '#' + glyphId,
203 width: dim,
204 height: dim,
205 transform: sus.translate(xlate,xlate)
206 });
207 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800208 }
209
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700210 function sortIcons() {
Simon Hunt3695a622015-03-31 11:52:23 -0700211 function sortAsc(div) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800212 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700213 loadEmbeddedIcon(div, 'upArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700214 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800215 }
216
217 function sortDesc(div) {
218 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700219 loadEmbeddedIcon(div, 'downArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700220 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800221 }
222
223 function sortNone(div) {
224 div.remove();
225 }
226
227 return {
228 sortAsc: sortAsc,
229 sortDesc: sortDesc,
230 sortNone: sortNone
231 };
232 }
233
Simon Huntac4c6f72015-02-03 19:50:53 -0800234
235 // =========================
236 // === DEFINE THE MODULE
237
Simon Hunt7ac7be92015-01-06 10:47:56 -0800238 angular.module('onosSvg')
Bri Prebilic Cole0c41ba22015-07-06 15:09:48 -0700239 .directive('icon', ['IconService', function (is) {
240 return {
241 restrict: 'A',
242 link: function (scope, element, attrs) {
243 attrs.$observe('iconId', function () {
244 var div = d3.select(element[0]);
245 div.selectAll('*').remove();
246 is.loadEmbeddedIcon(div, attrs.iconId, attrs.iconSize);
247 });
248 }
249 };
250 }])
251
Simon Hunt97225382015-01-19 13:33:09 -0800252 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800253 'SvgUtilService',
254
255 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800256 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800257 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800258 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800259 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800260
Simon Hunt7ac7be92015-01-06 10:47:56 -0800261 return {
Simon Hunt97225382015-01-19 13:33:09 -0800262 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800263 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800264 loadEmbeddedIcon: loadEmbeddedIcon,
265 addDeviceIcon: addDeviceIcon,
266 addHostIcon: addHostIcon,
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800267 iconConfig: function () { return config; },
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700268 sortIcons: sortIcons
Simon Hunt7ac7be92015-01-06 10:47:56 -0800269 };
270 }]);
271
272}());