blob: d5e2a38f1a9ca159bc26cb6980559d4dd7341fbe [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 = {
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070032 appActive: 'checkMark',
33 appInactive: 'unknown',
Simon Hunt3695a622015-03-31 11:52:23 -070034 appPlus: 'plus',
35 appMinus: 'minus',
36 appPlay: 'play',
37 appStop: 'stop',
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070038
Simon Huntac4c6f72015-02-03 19:50:53 -080039 deviceOnline: 'checkMark',
40 deviceOffline: 'xMark',
Bri Prebilic Coledea09742015-02-12 15:33:50 -080041 devIcon_SWITCH: 'switch',
Thomas Vachuska58eded62015-03-31 12:04:03 -070042 devIcon_ROADM: 'roadm',
43
44 hostIcon_endstation: 'endstation',
45 hostIcon_router: 'router',
46 hostIcon_bgpSpeaker: 'bgpSpeaker',
Bri Prebilic Coledea09742015-02-12 15:33:50 -080047
Simon Huntac4c6f72015-02-03 19:50:53 -080048 tableColSortAsc: 'triangleUp',
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080049 tableColSortDesc: 'triangleDown'
Simon Huntac4c6f72015-02-03 19:50:53 -080050 };
51
52
Simon Hunt7ac7be92015-01-06 10:47:56 -080053
Simon Hunt97225382015-01-19 13:33:09 -080054 function ensureIconLibDefs() {
55 var body = d3.select('body'),
56 svg = body.select('svg#IconLibDefs'),
57 defs;
58
59 if (svg.empty()) {
60 svg = body.append('svg').attr('id', 'IconLibDefs');
61 defs = svg.append('defs');
62 }
63 return svg.select('defs');
64 }
65
Simon Huntac4c6f72015-02-03 19:50:53 -080066 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080067 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080068 // size is dimension of icon in pixels. Defaults to 20.
69 // installGlyph, if truthy, will cause the glyph to be added to
70 // well-known defs element. Defaults to false.
71 // svgClass is the CSS class used to identify the SVG layer.
72 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080073 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -080074 var dim = size || 20,
75 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080076 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -080077 svg, g;
78
79 if (installGlyph) {
80 gs.loadDefs(ensureIconLibDefs(), [gid], true);
81 }
82
83 svg = div.append('svg').attr({
84 'class': svgCls,
85 width: dim,
86 height: dim,
87 viewBox: viewBox
88 });
89
90 g = svg.append('g').attr({
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080091 'class': 'icon'
Simon Huntac4c6f72015-02-03 19:50:53 -080092 });
93
94 g.append('rect').attr({
95 width: vboxSize,
96 height: vboxSize,
97 rx: cornerSize
98 });
99
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800100 g.append('use').attr({
101 width: vboxSize,
102 height: vboxSize,
103 'class': 'glyph',
104 'xlink:href': '#' + gid
105 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800106 }
107
108 // div is a D3 selection of the <DIV> element into which icon should load
109 // iconCls is the CSS class used to identify the icon
110 // size is dimension of icon in pixels. Defaults to 20.
111 // installGlyph, if truthy, will cause the glyph to be added to
112 // well-known defs element. Defaults to false.
113 // svgClass is the CSS class used to identify the SVG layer.
114 // Defaults to 'embeddedIcon'.
115 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
116 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
117 div.select('svg g').classed(iconCls, true);
118 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800119
120 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800121 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800122 }
123
124
125 // configuration for device and host icons in the topology view
126 var config = {
127 device: {
128 dim: 36,
129 rx: 4
130 },
131 host: {
132 radius: {
133 noGlyph: 9,
134 withGlyph: 14
135 },
136 glyphed: {
137 endstation: 1,
138 bgpSpeaker: 1,
139 router: 1
140 }
141 }
142 };
143
144
145 // Adds a device icon to the specified element, using the given glyph.
146 // Returns the D3 selection of the icon.
147 function addDeviceIcon(elem, glyphId) {
148 var cfg = config.device,
149 g = elem.append('g')
150 .attr('class', 'svgIcon deviceIcon');
151
152 g.append('rect').attr({
153 x: 0,
154 y: 0,
155 rx: cfg.rx,
156 width: cfg.dim,
157 height: cfg.dim
158 });
159
160 g.append('use').attr({
161 'xlink:href': '#' + glyphId,
162 width: cfg.dim,
163 height: cfg.dim
164 });
165
166 g.dim = cfg.dim;
167 return g;
168 }
169
Simon Hunt1894d792015-02-04 17:09:20 -0800170 function addHostIcon(elem, radius, glyphId) {
171 var dim = radius * 1.5,
172 xlate = -dim / 2,
173 g = elem.append('g')
174 .attr('class', 'svgIcon hostIcon');
175
176 g.append('circle').attr('r', radius);
177
178 g.append('use').attr({
179 'xlink:href': '#' + glyphId,
180 width: dim,
181 height: dim,
182 transform: sus.translate(xlate,xlate)
183 });
184 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800185 }
186
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800187 function createSortIcon() {
Simon Hunt3695a622015-03-31 11:52:23 -0700188 function sortAsc(div) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800189 div.style('display', 'inline-block');
190 loadEmbeddedIcon(div, 'tableColSortAsc', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700191 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800192 }
193
194 function sortDesc(div) {
195 div.style('display', 'inline-block');
196 loadEmbeddedIcon(div, 'tableColSortDesc', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700197 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800198 }
199
200 function sortNone(div) {
201 div.remove();
202 }
203
204 return {
205 sortAsc: sortAsc,
206 sortDesc: sortDesc,
207 sortNone: sortNone
208 };
209 }
210
Simon Huntac4c6f72015-02-03 19:50:53 -0800211
212 // =========================
213 // === DEFINE THE MODULE
214
Simon Hunt7ac7be92015-01-06 10:47:56 -0800215 angular.module('onosSvg')
Simon Hunt97225382015-01-19 13:33:09 -0800216 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800217 'SvgUtilService',
218
219 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800220 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800221 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800222 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800223 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800224
Simon Hunt7ac7be92015-01-06 10:47:56 -0800225 return {
Simon Hunt97225382015-01-19 13:33:09 -0800226 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800227 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800228 loadEmbeddedIcon: loadEmbeddedIcon,
229 addDeviceIcon: addDeviceIcon,
230 addHostIcon: addHostIcon,
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800231 iconConfig: function () { return config; },
232 createSortIcon: createSortIcon
Simon Hunt7ac7be92015-01-06 10:47:56 -0800233 };
234 }]);
235
236}());