blob: cc26360f12a88ba548b08b4b3d60b3412a4bb8fb [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
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070048 loading: 'loading',
49
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070050 appInactive: 'unknown',
51
Bri Prebilic Coledea09742015-02-12 15:33:50 -080052 devIcon_SWITCH: 'switch',
Thomas Vachuska58eded62015-03-31 12:04:03 -070053 devIcon_ROADM: 'roadm',
Simon Hunt864333a2015-11-16 17:08:08 -080054 deviceTable: 'switch',
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -070055 flowTable: 'flowTable',
56 portTable: 'portTable',
57 groupTable: 'groupTable',
Thomas Vachuska58eded62015-03-31 12:04:03 -070058
59 hostIcon_endstation: 'endstation',
60 hostIcon_router: 'router',
Simon Hunt20e16792015-04-24 14:29:39 -070061 hostIcon_bgpSpeaker: 'bgpSpeaker',
62
63 nav_apps: 'bird',
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070064 nav_settings: 'chain',
Simon Hunt20e16792015-04-24 14:29:39 -070065 nav_cluster: 'node',
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070066 nav_topo: 'topo',
Simon Hunt20e16792015-04-24 14:29:39 -070067 nav_devs: 'switch',
68 nav_links: 'ports',
69 nav_hosts: 'endstation',
Thomas Vachuska3ece3732015-09-22 23:58:50 -070070 nav_intents: 'relatedIntents',
71 nav_processors: 'allTraffic'
Simon Huntac4c6f72015-02-03 19:50:53 -080072 };
73
Simon Hunt97225382015-01-19 13:33:09 -080074 function ensureIconLibDefs() {
75 var body = d3.select('body'),
76 svg = body.select('svg#IconLibDefs'),
77 defs;
78
79 if (svg.empty()) {
80 svg = body.append('svg').attr('id', 'IconLibDefs');
81 defs = svg.append('defs');
82 }
83 return svg.select('defs');
84 }
85
Simon Huntac4c6f72015-02-03 19:50:53 -080086 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080087 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080088 // size is dimension of icon in pixels. Defaults to 20.
89 // installGlyph, if truthy, will cause the glyph to be added to
90 // well-known defs element. Defaults to false.
91 // svgClass is the CSS class used to identify the SVG layer.
92 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080093 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -080094 var dim = size || 20,
95 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080096 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -080097 svg, g;
98
99 if (installGlyph) {
100 gs.loadDefs(ensureIconLibDefs(), [gid], true);
101 }
102
103 svg = div.append('svg').attr({
104 'class': svgCls,
105 width: dim,
106 height: dim,
107 viewBox: viewBox
108 });
109
110 g = svg.append('g').attr({
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800111 'class': 'icon'
Simon Huntac4c6f72015-02-03 19:50:53 -0800112 });
113
114 g.append('rect').attr({
115 width: vboxSize,
116 height: vboxSize,
117 rx: cornerSize
118 });
119
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800120 g.append('use').attr({
121 width: vboxSize,
122 height: vboxSize,
123 'class': 'glyph',
124 'xlink:href': '#' + gid
125 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800126 }
127
128 // div is a D3 selection of the <DIV> element into which icon should load
129 // iconCls is the CSS class used to identify the icon
130 // size is dimension of icon in pixels. Defaults to 20.
131 // installGlyph, if truthy, will cause the glyph to be added to
132 // well-known defs element. Defaults to false.
133 // svgClass is the CSS class used to identify the SVG layer.
134 // Defaults to 'embeddedIcon'.
135 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
136 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
137 div.select('svg g').classed(iconCls, true);
138 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800139
140 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800141 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800142 }
143
144
145 // configuration for device and host icons in the topology view
146 var config = {
147 device: {
148 dim: 36,
149 rx: 4
150 },
151 host: {
152 radius: {
153 noGlyph: 9,
154 withGlyph: 14
155 },
156 glyphed: {
157 endstation: 1,
158 bgpSpeaker: 1,
159 router: 1
160 }
161 }
162 };
163
164
165 // Adds a device icon to the specified element, using the given glyph.
166 // Returns the D3 selection of the icon.
167 function addDeviceIcon(elem, glyphId) {
168 var cfg = config.device,
Simon Hunt044f28072015-10-08 12:38:16 -0700169 gid = gs.glyphDefined(glyphId) ? glyphId : 'query',
Simon Huntac4c6f72015-02-03 19:50:53 -0800170 g = elem.append('g')
171 .attr('class', 'svgIcon deviceIcon');
172
173 g.append('rect').attr({
174 x: 0,
175 y: 0,
176 rx: cfg.rx,
177 width: cfg.dim,
178 height: cfg.dim
179 });
180
181 g.append('use').attr({
Simon Hunt044f28072015-10-08 12:38:16 -0700182 'xlink:href': '#' + gid,
Simon Huntac4c6f72015-02-03 19:50:53 -0800183 width: cfg.dim,
184 height: cfg.dim
185 });
186
187 g.dim = cfg.dim;
188 return g;
189 }
190
Simon Hunt1894d792015-02-04 17:09:20 -0800191 function addHostIcon(elem, radius, glyphId) {
192 var dim = radius * 1.5,
193 xlate = -dim / 2,
194 g = elem.append('g')
195 .attr('class', 'svgIcon hostIcon');
196
197 g.append('circle').attr('r', radius);
198
199 g.append('use').attr({
200 'xlink:href': '#' + glyphId,
201 width: dim,
202 height: dim,
203 transform: sus.translate(xlate,xlate)
204 });
205 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800206 }
207
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700208 function sortIcons() {
Simon Hunt3695a622015-03-31 11:52:23 -0700209 function sortAsc(div) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800210 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700211 loadEmbeddedIcon(div, 'upArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700212 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800213 }
214
215 function sortDesc(div) {
216 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700217 loadEmbeddedIcon(div, 'downArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700218 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800219 }
220
221 function sortNone(div) {
222 div.remove();
223 }
224
225 return {
226 sortAsc: sortAsc,
227 sortDesc: sortDesc,
228 sortNone: sortNone
229 };
230 }
231
Simon Huntac4c6f72015-02-03 19:50:53 -0800232
233 // =========================
234 // === DEFINE THE MODULE
235
Simon Hunt7ac7be92015-01-06 10:47:56 -0800236 angular.module('onosSvg')
Bri Prebilic Cole0c41ba22015-07-06 15:09:48 -0700237 .directive('icon', ['IconService', function (is) {
238 return {
239 restrict: 'A',
240 link: function (scope, element, attrs) {
241 attrs.$observe('iconId', function () {
242 var div = d3.select(element[0]);
243 div.selectAll('*').remove();
244 is.loadEmbeddedIcon(div, attrs.iconId, attrs.iconSize);
245 });
246 }
247 };
248 }])
249
Simon Hunt97225382015-01-19 13:33:09 -0800250 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800251 'SvgUtilService',
252
253 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800254 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800255 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800256 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800257 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800258
Simon Hunt7ac7be92015-01-06 10:47:56 -0800259 return {
Simon Hunt97225382015-01-19 13:33:09 -0800260 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800261 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800262 loadEmbeddedIcon: loadEmbeddedIcon,
263 addDeviceIcon: addDeviceIcon,
264 addHostIcon: addHostIcon,
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800265 iconConfig: function () { return config; },
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700266 sortIcons: sortIcons
Simon Hunt7ac7be92015-01-06 10:47:56 -0800267 };
268 }]);
269
270}());