blob: e7e40f08544d4a76a57d898fb2af74f10dd0e195 [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',
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070050 hostIcon_bgpSpeaker: 'bgpSpeaker'
Simon Huntac4c6f72015-02-03 19:50:53 -080051 };
52
Simon Hunt97225382015-01-19 13:33:09 -080053 function ensureIconLibDefs() {
54 var body = d3.select('body'),
55 svg = body.select('svg#IconLibDefs'),
56 defs;
57
58 if (svg.empty()) {
59 svg = body.append('svg').attr('id', 'IconLibDefs');
60 defs = svg.append('defs');
61 }
62 return svg.select('defs');
63 }
64
Simon Huntac4c6f72015-02-03 19:50:53 -080065 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080066 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080067 // size is dimension of icon in pixels. Defaults to 20.
68 // installGlyph, if truthy, will cause the glyph to be added to
69 // well-known defs element. Defaults to false.
70 // svgClass is the CSS class used to identify the SVG layer.
71 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080072 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -080073 var dim = size || 20,
74 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080075 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -080076 svg, g;
77
78 if (installGlyph) {
79 gs.loadDefs(ensureIconLibDefs(), [gid], true);
80 }
81
82 svg = div.append('svg').attr({
83 'class': svgCls,
84 width: dim,
85 height: dim,
86 viewBox: viewBox
87 });
88
89 g = svg.append('g').attr({
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080090 'class': 'icon'
Simon Huntac4c6f72015-02-03 19:50:53 -080091 });
92
93 g.append('rect').attr({
94 width: vboxSize,
95 height: vboxSize,
96 rx: cornerSize
97 });
98
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080099 g.append('use').attr({
100 width: vboxSize,
101 height: vboxSize,
102 'class': 'glyph',
103 'xlink:href': '#' + gid
104 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800105 }
106
107 // div is a D3 selection of the <DIV> element into which icon should load
108 // iconCls is the CSS class used to identify the icon
109 // size is dimension of icon in pixels. Defaults to 20.
110 // installGlyph, if truthy, will cause the glyph to be added to
111 // well-known defs element. Defaults to false.
112 // svgClass is the CSS class used to identify the SVG layer.
113 // Defaults to 'embeddedIcon'.
114 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
115 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
116 div.select('svg g').classed(iconCls, true);
117 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800118
119 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800120 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800121 }
122
123
124 // configuration for device and host icons in the topology view
125 var config = {
126 device: {
127 dim: 36,
128 rx: 4
129 },
130 host: {
131 radius: {
132 noGlyph: 9,
133 withGlyph: 14
134 },
135 glyphed: {
136 endstation: 1,
137 bgpSpeaker: 1,
138 router: 1
139 }
140 }
141 };
142
143
144 // Adds a device icon to the specified element, using the given glyph.
145 // Returns the D3 selection of the icon.
146 function addDeviceIcon(elem, glyphId) {
147 var cfg = config.device,
148 g = elem.append('g')
149 .attr('class', 'svgIcon deviceIcon');
150
151 g.append('rect').attr({
152 x: 0,
153 y: 0,
154 rx: cfg.rx,
155 width: cfg.dim,
156 height: cfg.dim
157 });
158
159 g.append('use').attr({
160 'xlink:href': '#' + glyphId,
161 width: cfg.dim,
162 height: cfg.dim
163 });
164
165 g.dim = cfg.dim;
166 return g;
167 }
168
Simon Hunt1894d792015-02-04 17:09:20 -0800169 function addHostIcon(elem, radius, glyphId) {
170 var dim = radius * 1.5,
171 xlate = -dim / 2,
172 g = elem.append('g')
173 .attr('class', 'svgIcon hostIcon');
174
175 g.append('circle').attr('r', radius);
176
177 g.append('use').attr({
178 'xlink:href': '#' + glyphId,
179 width: dim,
180 height: dim,
181 transform: sus.translate(xlate,xlate)
182 });
183 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800184 }
185
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800186 function createSortIcon() {
Simon Hunt3695a622015-03-31 11:52:23 -0700187 function sortAsc(div) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800188 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700189 loadEmbeddedIcon(div, 'upArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700190 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800191 }
192
193 function sortDesc(div) {
194 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700195 loadEmbeddedIcon(div, 'downArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700196 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800197 }
198
199 function sortNone(div) {
200 div.remove();
201 }
202
203 return {
204 sortAsc: sortAsc,
205 sortDesc: sortDesc,
206 sortNone: sortNone
207 };
208 }
209
Simon Huntac4c6f72015-02-03 19:50:53 -0800210
211 // =========================
212 // === DEFINE THE MODULE
213
Simon Hunt7ac7be92015-01-06 10:47:56 -0800214 angular.module('onosSvg')
Simon Hunt97225382015-01-19 13:33:09 -0800215 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800216 'SvgUtilService',
217
218 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800219 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800220 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800221 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800222 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800223
Simon Hunt7ac7be92015-01-06 10:47:56 -0800224 return {
Simon Hunt97225382015-01-19 13:33:09 -0800225 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800226 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800227 loadEmbeddedIcon: loadEmbeddedIcon,
228 addDeviceIcon: addDeviceIcon,
229 addHostIcon: addHostIcon,
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800230 iconConfig: function () { return config; },
231 createSortIcon: createSortIcon
Simon Hunt7ac7be92015-01-06 10:47:56 -0800232 };
233 }]);
234
235}());