blob: c536a6648afadc29c87532fde54200d556bee3f8 [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
Bri Prebilic Cole43f17c02015-05-01 10:43:38 -070040 refresh: 'refresh',
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -070041 garbage: 'garbage',
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070042
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070043 upArrow: 'triangleUp',
44 downArrow: 'triangleDown',
45
46 appInactive: 'unknown',
47
Bri Prebilic Coledea09742015-02-12 15:33:50 -080048 devIcon_SWITCH: 'switch',
Thomas Vachuska58eded62015-03-31 12:04:03 -070049 devIcon_ROADM: 'roadm',
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -070050 flowTable: 'flowTable',
51 portTable: 'portTable',
52 groupTable: 'groupTable',
Thomas Vachuska58eded62015-03-31 12:04:03 -070053
54 hostIcon_endstation: 'endstation',
55 hostIcon_router: 'router',
Simon Hunt20e16792015-04-24 14:29:39 -070056 hostIcon_bgpSpeaker: 'bgpSpeaker',
57
58 nav_apps: 'bird',
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070059 nav_settings: 'chain',
Simon Hunt20e16792015-04-24 14:29:39 -070060 nav_cluster: 'node',
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070061 nav_topo: 'topo',
Simon Hunt20e16792015-04-24 14:29:39 -070062 nav_devs: 'switch',
63 nav_links: 'ports',
64 nav_hosts: 'endstation',
65 nav_intents: 'relatedIntents'
Simon Huntac4c6f72015-02-03 19:50:53 -080066 };
67
Simon Hunt97225382015-01-19 13:33:09 -080068 function ensureIconLibDefs() {
69 var body = d3.select('body'),
70 svg = body.select('svg#IconLibDefs'),
71 defs;
72
73 if (svg.empty()) {
74 svg = body.append('svg').attr('id', 'IconLibDefs');
75 defs = svg.append('defs');
76 }
77 return svg.select('defs');
78 }
79
Simon Huntac4c6f72015-02-03 19:50:53 -080080 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080081 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080082 // size is dimension of icon in pixels. Defaults to 20.
83 // installGlyph, if truthy, will cause the glyph to be added to
84 // well-known defs element. Defaults to false.
85 // svgClass is the CSS class used to identify the SVG layer.
86 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080087 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -080088 var dim = size || 20,
89 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080090 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -080091 svg, g;
92
93 if (installGlyph) {
94 gs.loadDefs(ensureIconLibDefs(), [gid], true);
95 }
96
97 svg = div.append('svg').attr({
98 'class': svgCls,
99 width: dim,
100 height: dim,
101 viewBox: viewBox
102 });
103
104 g = svg.append('g').attr({
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800105 'class': 'icon'
Simon Huntac4c6f72015-02-03 19:50:53 -0800106 });
107
108 g.append('rect').attr({
109 width: vboxSize,
110 height: vboxSize,
111 rx: cornerSize
112 });
113
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800114 g.append('use').attr({
115 width: vboxSize,
116 height: vboxSize,
117 'class': 'glyph',
118 'xlink:href': '#' + gid
119 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800120 }
121
122 // div is a D3 selection of the <DIV> element into which icon should load
123 // iconCls is the CSS class used to identify the icon
124 // size is dimension of icon in pixels. Defaults to 20.
125 // installGlyph, if truthy, will cause the glyph to be added to
126 // well-known defs element. Defaults to false.
127 // svgClass is the CSS class used to identify the SVG layer.
128 // Defaults to 'embeddedIcon'.
129 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
130 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
131 div.select('svg g').classed(iconCls, true);
132 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800133
134 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800135 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800136 }
137
138
139 // configuration for device and host icons in the topology view
140 var config = {
141 device: {
142 dim: 36,
143 rx: 4
144 },
145 host: {
146 radius: {
147 noGlyph: 9,
148 withGlyph: 14
149 },
150 glyphed: {
151 endstation: 1,
152 bgpSpeaker: 1,
153 router: 1
154 }
155 }
156 };
157
158
159 // Adds a device icon to the specified element, using the given glyph.
160 // Returns the D3 selection of the icon.
161 function addDeviceIcon(elem, glyphId) {
162 var cfg = config.device,
163 g = elem.append('g')
164 .attr('class', 'svgIcon deviceIcon');
165
166 g.append('rect').attr({
167 x: 0,
168 y: 0,
169 rx: cfg.rx,
170 width: cfg.dim,
171 height: cfg.dim
172 });
173
174 g.append('use').attr({
175 'xlink:href': '#' + glyphId,
176 width: cfg.dim,
177 height: cfg.dim
178 });
179
180 g.dim = cfg.dim;
181 return g;
182 }
183
Simon Hunt1894d792015-02-04 17:09:20 -0800184 function addHostIcon(elem, radius, glyphId) {
185 var dim = radius * 1.5,
186 xlate = -dim / 2,
187 g = elem.append('g')
188 .attr('class', 'svgIcon hostIcon');
189
190 g.append('circle').attr('r', radius);
191
192 g.append('use').attr({
193 'xlink:href': '#' + glyphId,
194 width: dim,
195 height: dim,
196 transform: sus.translate(xlate,xlate)
197 });
198 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800199 }
200
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700201 function sortIcons() {
Simon Hunt3695a622015-03-31 11:52:23 -0700202 function sortAsc(div) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800203 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700204 loadEmbeddedIcon(div, 'upArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700205 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800206 }
207
208 function sortDesc(div) {
209 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700210 loadEmbeddedIcon(div, 'downArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700211 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800212 }
213
214 function sortNone(div) {
215 div.remove();
216 }
217
218 return {
219 sortAsc: sortAsc,
220 sortDesc: sortDesc,
221 sortNone: sortNone
222 };
223 }
224
Simon Huntac4c6f72015-02-03 19:50:53 -0800225
226 // =========================
227 // === DEFINE THE MODULE
228
Simon Hunt7ac7be92015-01-06 10:47:56 -0800229 angular.module('onosSvg')
Bri Prebilic Cole0c41ba22015-07-06 15:09:48 -0700230 .directive('icon', ['IconService', function (is) {
231 return {
232 restrict: 'A',
233 link: function (scope, element, attrs) {
234 attrs.$observe('iconId', function () {
235 var div = d3.select(element[0]);
236 div.selectAll('*').remove();
237 is.loadEmbeddedIcon(div, attrs.iconId, attrs.iconSize);
238 });
239 }
240 };
241 }])
242
Simon Hunt97225382015-01-19 13:33:09 -0800243 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800244 'SvgUtilService',
245
246 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800247 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800248 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800249 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800250 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800251
Simon Hunt7ac7be92015-01-06 10:47:56 -0800252 return {
Simon Hunt97225382015-01-19 13:33:09 -0800253 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800254 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800255 loadEmbeddedIcon: loadEmbeddedIcon,
256 addDeviceIcon: addDeviceIcon,
257 addHostIcon: addHostIcon,
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800258 iconConfig: function () { return config; },
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700259 sortIcons: sortIcons
Simon Hunt7ac7be92015-01-06 10:47:56 -0800260 };
261 }]);
262
263}());