blob: 737a8a86fb5e3832f50067f8076cfe9c4b6b04ec [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
40 upArrow: 'triangleUp',
41 downArrow: 'triangleDown',
42
43 appInactive: 'unknown',
44
Bri Prebilic Coledea09742015-02-12 15:33:50 -080045 devIcon_SWITCH: 'switch',
Thomas Vachuska58eded62015-03-31 12:04:03 -070046 devIcon_ROADM: 'roadm',
47
48 hostIcon_endstation: 'endstation',
49 hostIcon_router: 'router',
Simon Hunt20e16792015-04-24 14:29:39 -070050 hostIcon_bgpSpeaker: 'bgpSpeaker',
51
52 nav_apps: 'bird',
53 nav_cluster: 'node',
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070054 nav_topo: 'topo',
Simon Hunt20e16792015-04-24 14:29:39 -070055 nav_devs: 'switch',
56 nav_links: 'ports',
57 nav_hosts: 'endstation',
58 nav_intents: 'relatedIntents'
Simon Huntac4c6f72015-02-03 19:50:53 -080059 };
60
Simon Hunt97225382015-01-19 13:33:09 -080061 function ensureIconLibDefs() {
62 var body = d3.select('body'),
63 svg = body.select('svg#IconLibDefs'),
64 defs;
65
66 if (svg.empty()) {
67 svg = body.append('svg').attr('id', 'IconLibDefs');
68 defs = svg.append('defs');
69 }
70 return svg.select('defs');
71 }
72
Simon Huntac4c6f72015-02-03 19:50:53 -080073 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080074 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080075 // size is dimension of icon in pixels. Defaults to 20.
76 // installGlyph, if truthy, will cause the glyph to be added to
77 // well-known defs element. Defaults to false.
78 // svgClass is the CSS class used to identify the SVG layer.
79 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080080 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -080081 var dim = size || 20,
82 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080083 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -080084 svg, g;
85
86 if (installGlyph) {
87 gs.loadDefs(ensureIconLibDefs(), [gid], true);
88 }
89
90 svg = div.append('svg').attr({
91 'class': svgCls,
92 width: dim,
93 height: dim,
94 viewBox: viewBox
95 });
96
97 g = svg.append('g').attr({
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080098 'class': 'icon'
Simon Huntac4c6f72015-02-03 19:50:53 -080099 });
100
101 g.append('rect').attr({
102 width: vboxSize,
103 height: vboxSize,
104 rx: cornerSize
105 });
106
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800107 g.append('use').attr({
108 width: vboxSize,
109 height: vboxSize,
110 'class': 'glyph',
111 'xlink:href': '#' + gid
112 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800113 }
114
115 // div is a D3 selection of the <DIV> element into which icon should load
116 // iconCls is the CSS class used to identify the icon
117 // size is dimension of icon in pixels. Defaults to 20.
118 // installGlyph, if truthy, will cause the glyph to be added to
119 // well-known defs element. Defaults to false.
120 // svgClass is the CSS class used to identify the SVG layer.
121 // Defaults to 'embeddedIcon'.
122 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
123 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
124 div.select('svg g').classed(iconCls, true);
125 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800126
127 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800128 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800129 }
130
131
132 // configuration for device and host icons in the topology view
133 var config = {
134 device: {
135 dim: 36,
136 rx: 4
137 },
138 host: {
139 radius: {
140 noGlyph: 9,
141 withGlyph: 14
142 },
143 glyphed: {
144 endstation: 1,
145 bgpSpeaker: 1,
146 router: 1
147 }
148 }
149 };
150
151
152 // Adds a device icon to the specified element, using the given glyph.
153 // Returns the D3 selection of the icon.
154 function addDeviceIcon(elem, glyphId) {
155 var cfg = config.device,
156 g = elem.append('g')
157 .attr('class', 'svgIcon deviceIcon');
158
159 g.append('rect').attr({
160 x: 0,
161 y: 0,
162 rx: cfg.rx,
163 width: cfg.dim,
164 height: cfg.dim
165 });
166
167 g.append('use').attr({
168 'xlink:href': '#' + glyphId,
169 width: cfg.dim,
170 height: cfg.dim
171 });
172
173 g.dim = cfg.dim;
174 return g;
175 }
176
Simon Hunt1894d792015-02-04 17:09:20 -0800177 function addHostIcon(elem, radius, glyphId) {
178 var dim = radius * 1.5,
179 xlate = -dim / 2,
180 g = elem.append('g')
181 .attr('class', 'svgIcon hostIcon');
182
183 g.append('circle').attr('r', radius);
184
185 g.append('use').attr({
186 'xlink:href': '#' + glyphId,
187 width: dim,
188 height: dim,
189 transform: sus.translate(xlate,xlate)
190 });
191 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800192 }
193
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800194 function createSortIcon() {
Simon Hunt3695a622015-03-31 11:52:23 -0700195 function sortAsc(div) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800196 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700197 loadEmbeddedIcon(div, 'upArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700198 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800199 }
200
201 function sortDesc(div) {
202 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700203 loadEmbeddedIcon(div, 'downArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700204 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800205 }
206
207 function sortNone(div) {
208 div.remove();
209 }
210
211 return {
212 sortAsc: sortAsc,
213 sortDesc: sortDesc,
214 sortNone: sortNone
215 };
216 }
217
Simon Huntac4c6f72015-02-03 19:50:53 -0800218
219 // =========================
220 // === DEFINE THE MODULE
221
Simon Hunt7ac7be92015-01-06 10:47:56 -0800222 angular.module('onosSvg')
Simon Hunt97225382015-01-19 13:33:09 -0800223 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800224 'SvgUtilService',
225
226 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800227 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800228 fs = _fs_;
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,
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800238 iconConfig: function () { return config; },
239 createSortIcon: createSortIcon
Simon Hunt7ac7be92015-01-06 10:47:56 -0800240 };
241 }]);
242
243}());