blob: 52ed46d6489b779059f901750da3a065033f23c8 [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 = {
Simon Huntac4c6f72015-02-03 19:50:53 -080032 deviceOnline: 'checkMark',
33 deviceOffline: 'xMark',
Bri Prebilic Coledea09742015-02-12 15:33:50 -080034 devIcon_SWITCH: 'switch',
35
Simon Huntac4c6f72015-02-03 19:50:53 -080036 tableColSortAsc: 'triangleUp',
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080037 tableColSortDesc: 'triangleDown'
Simon Huntac4c6f72015-02-03 19:50:53 -080038 };
39
40
Simon Hunt7ac7be92015-01-06 10:47:56 -080041
Simon Hunt97225382015-01-19 13:33:09 -080042 function ensureIconLibDefs() {
43 var body = d3.select('body'),
44 svg = body.select('svg#IconLibDefs'),
45 defs;
46
47 if (svg.empty()) {
48 svg = body.append('svg').attr('id', 'IconLibDefs');
49 defs = svg.append('defs');
50 }
51 return svg.select('defs');
52 }
53
Simon Huntac4c6f72015-02-03 19:50:53 -080054 // div is a D3 selection of the <DIV> element into which icon should load
55 // iconCls is the CSS class used to identify the icon
56 // size is dimension of icon in pixels. Defaults to 20.
57 // installGlyph, if truthy, will cause the glyph to be added to
58 // well-known defs element. Defaults to false.
59 // svgClass is the CSS class used to identify the SVG layer.
60 // Defaults to 'embeddedIcon'.
61 function loadIcon(div, iconCls, size, installGlyph, svgClass) {
62 var dim = size || 20,
63 svgCls = svgClass || 'embeddedIcon',
64 gid = glyphMapping[iconCls] || 'unknown',
65 svg, g;
66
67 if (installGlyph) {
68 gs.loadDefs(ensureIconLibDefs(), [gid], true);
69 }
70
71 svg = div.append('svg').attr({
72 'class': svgCls,
73 width: dim,
74 height: dim,
75 viewBox: viewBox
76 });
77
78 g = svg.append('g').attr({
79 'class': 'icon ' + iconCls
80 });
81
82 g.append('rect').attr({
83 width: vboxSize,
84 height: vboxSize,
85 rx: cornerSize
86 });
87
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080088 g.append('use').attr({
89 width: vboxSize,
90 height: vboxSize,
91 'class': 'glyph',
92 'xlink:href': '#' + gid
93 });
94}
Simon Huntac4c6f72015-02-03 19:50:53 -080095
96 function loadEmbeddedIcon(div, iconCls, size) {
97 loadIcon(div, iconCls, size, true);
98 }
99
100
101 // configuration for device and host icons in the topology view
102 var config = {
103 device: {
104 dim: 36,
105 rx: 4
106 },
107 host: {
108 radius: {
109 noGlyph: 9,
110 withGlyph: 14
111 },
112 glyphed: {
113 endstation: 1,
114 bgpSpeaker: 1,
115 router: 1
116 }
117 }
118 };
119
120
121 // Adds a device icon to the specified element, using the given glyph.
122 // Returns the D3 selection of the icon.
123 function addDeviceIcon(elem, glyphId) {
124 var cfg = config.device,
125 g = elem.append('g')
126 .attr('class', 'svgIcon deviceIcon');
127
128 g.append('rect').attr({
129 x: 0,
130 y: 0,
131 rx: cfg.rx,
132 width: cfg.dim,
133 height: cfg.dim
134 });
135
136 g.append('use').attr({
137 'xlink:href': '#' + glyphId,
138 width: cfg.dim,
139 height: cfg.dim
140 });
141
142 g.dim = cfg.dim;
143 return g;
144 }
145
Simon Hunt1894d792015-02-04 17:09:20 -0800146 function addHostIcon(elem, radius, glyphId) {
147 var dim = radius * 1.5,
148 xlate = -dim / 2,
149 g = elem.append('g')
150 .attr('class', 'svgIcon hostIcon');
151
152 g.append('circle').attr('r', radius);
153
154 g.append('use').attr({
155 'xlink:href': '#' + glyphId,
156 width: dim,
157 height: dim,
158 transform: sus.translate(xlate,xlate)
159 });
160 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800161 }
162
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800163 function createSortIcon() {
164 function sortAsc(div) {
165 div.style('display', 'inline-block');
166 loadEmbeddedIcon(div, 'tableColSortAsc', 10);
167 }
168
169 function sortDesc(div) {
170 div.style('display', 'inline-block');
171 loadEmbeddedIcon(div, 'tableColSortDesc', 10);
172 }
173
174 function sortNone(div) {
175 div.remove();
176 }
177
178 return {
179 sortAsc: sortAsc,
180 sortDesc: sortDesc,
181 sortNone: sortNone
182 };
183 }
184
Simon Huntac4c6f72015-02-03 19:50:53 -0800185
186 // =========================
187 // === DEFINE THE MODULE
188
Simon Hunt7ac7be92015-01-06 10:47:56 -0800189 angular.module('onosSvg')
Simon Hunt97225382015-01-19 13:33:09 -0800190 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800191 'SvgUtilService',
192
193 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800194 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800195 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800196 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800197 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800198
Simon Hunt7ac7be92015-01-06 10:47:56 -0800199 return {
Simon Hunt97225382015-01-19 13:33:09 -0800200 loadIcon: loadIcon,
Simon Huntac4c6f72015-02-03 19:50:53 -0800201 loadEmbeddedIcon: loadEmbeddedIcon,
202 addDeviceIcon: addDeviceIcon,
203 addHostIcon: addHostIcon,
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800204 iconConfig: function () { return config; },
205 createSortIcon: createSortIcon
Simon Hunt7ac7be92015-01-06 10:47:56 -0800206 };
207 }]);
208
209}());