blob: b63cf6408b99866990eb5032a94bead23be0101f [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',
42
Simon Huntac4c6f72015-02-03 19:50:53 -080043 tableColSortAsc: 'triangleUp',
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080044 tableColSortDesc: 'triangleDown'
Simon Huntac4c6f72015-02-03 19:50:53 -080045 };
46
47
Simon Hunt7ac7be92015-01-06 10:47:56 -080048
Simon Hunt97225382015-01-19 13:33:09 -080049 function ensureIconLibDefs() {
50 var body = d3.select('body'),
51 svg = body.select('svg#IconLibDefs'),
52 defs;
53
54 if (svg.empty()) {
55 svg = body.append('svg').attr('id', 'IconLibDefs');
56 defs = svg.append('defs');
57 }
58 return svg.select('defs');
59 }
60
Simon Huntac4c6f72015-02-03 19:50:53 -080061 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080062 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080063 // size is dimension of icon in pixels. Defaults to 20.
64 // installGlyph, if truthy, will cause the glyph to be added to
65 // well-known defs element. Defaults to false.
66 // svgClass is the CSS class used to identify the SVG layer.
67 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080068 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -080069 var dim = size || 20,
70 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080071 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -080072 svg, g;
73
74 if (installGlyph) {
75 gs.loadDefs(ensureIconLibDefs(), [gid], true);
76 }
77
78 svg = div.append('svg').attr({
79 'class': svgCls,
80 width: dim,
81 height: dim,
82 viewBox: viewBox
83 });
84
85 g = svg.append('g').attr({
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080086 'class': 'icon'
Simon Huntac4c6f72015-02-03 19:50:53 -080087 });
88
89 g.append('rect').attr({
90 width: vboxSize,
91 height: vboxSize,
92 rx: cornerSize
93 });
94
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080095 g.append('use').attr({
96 width: vboxSize,
97 height: vboxSize,
98 'class': 'glyph',
99 'xlink:href': '#' + gid
100 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800101 }
102
103 // div is a D3 selection of the <DIV> element into which icon should load
104 // iconCls is the CSS class used to identify the icon
105 // size is dimension of icon in pixels. Defaults to 20.
106 // installGlyph, if truthy, will cause the glyph to be added to
107 // well-known defs element. Defaults to false.
108 // svgClass is the CSS class used to identify the SVG layer.
109 // Defaults to 'embeddedIcon'.
110 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
111 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
112 div.select('svg g').classed(iconCls, true);
113 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800114
115 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800116 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800117 }
118
119
120 // configuration for device and host icons in the topology view
121 var config = {
122 device: {
123 dim: 36,
124 rx: 4
125 },
126 host: {
127 radius: {
128 noGlyph: 9,
129 withGlyph: 14
130 },
131 glyphed: {
132 endstation: 1,
133 bgpSpeaker: 1,
134 router: 1
135 }
136 }
137 };
138
139
140 // Adds a device icon to the specified element, using the given glyph.
141 // Returns the D3 selection of the icon.
142 function addDeviceIcon(elem, glyphId) {
143 var cfg = config.device,
144 g = elem.append('g')
145 .attr('class', 'svgIcon deviceIcon');
146
147 g.append('rect').attr({
148 x: 0,
149 y: 0,
150 rx: cfg.rx,
151 width: cfg.dim,
152 height: cfg.dim
153 });
154
155 g.append('use').attr({
156 'xlink:href': '#' + glyphId,
157 width: cfg.dim,
158 height: cfg.dim
159 });
160
161 g.dim = cfg.dim;
162 return g;
163 }
164
Simon Hunt1894d792015-02-04 17:09:20 -0800165 function addHostIcon(elem, radius, glyphId) {
166 var dim = radius * 1.5,
167 xlate = -dim / 2,
168 g = elem.append('g')
169 .attr('class', 'svgIcon hostIcon');
170
171 g.append('circle').attr('r', radius);
172
173 g.append('use').attr({
174 'xlink:href': '#' + glyphId,
175 width: dim,
176 height: dim,
177 transform: sus.translate(xlate,xlate)
178 });
179 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800180 }
181
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800182 function createSortIcon() {
Simon Hunt3695a622015-03-31 11:52:23 -0700183 function sortAsc(div) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800184 div.style('display', 'inline-block');
185 loadEmbeddedIcon(div, 'tableColSortAsc', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700186 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800187 }
188
189 function sortDesc(div) {
190 div.style('display', 'inline-block');
191 loadEmbeddedIcon(div, 'tableColSortDesc', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700192 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800193 }
194
195 function sortNone(div) {
196 div.remove();
197 }
198
199 return {
200 sortAsc: sortAsc,
201 sortDesc: sortDesc,
202 sortNone: sortNone
203 };
204 }
205
Simon Huntac4c6f72015-02-03 19:50:53 -0800206
207 // =========================
208 // === DEFINE THE MODULE
209
Simon Hunt7ac7be92015-01-06 10:47:56 -0800210 angular.module('onosSvg')
Simon Hunt97225382015-01-19 13:33:09 -0800211 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800212 'SvgUtilService',
213
214 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800215 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800216 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800217 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800218 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800219
Simon Hunt7ac7be92015-01-06 10:47:56 -0800220 return {
Simon Hunt97225382015-01-19 13:33:09 -0800221 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800222 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800223 loadEmbeddedIcon: loadEmbeddedIcon,
224 addDeviceIcon: addDeviceIcon,
225 addHostIcon: addHostIcon,
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800226 iconConfig: function () { return config; },
227 createSortIcon: createSortIcon
Simon Hunt7ac7be92015-01-06 10:47:56 -0800228 };
229 }]);
230
231}());