blob: 9b5dbab968daa60ce7741bad4e07a095bb47b776 [file] [log] [blame]
Simon Hunt2052e5d2015-04-13 17:40:44 -07001/*
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 -- Topology Sprite Module.
Simon Huntabf66d92015-04-15 12:57:31 -070019 Defines behavior for loading sprites into the sprite layer.
Simon Hunt2052e5d2015-04-13 17:40:44 -070020 */
21
22(function () {
23 'use strict';
24
25 // injected refs
Simon Huntabf66d92015-04-15 12:57:31 -070026 var $log, $http, fs, gs, sus, wss;
Simon Huntfd8c7d72015-04-14 17:53:37 -070027
Simon Huntb2c4cc82015-04-15 17:16:28 -070028 // constants
29 var tssid = 'TopoSpriteService: ',
30 fontsize = 20; // default font size 20pt.
Simon Hunt2052e5d2015-04-13 17:40:44 -070031
32 // internal state
Simon Huntabf66d92015-04-15 12:57:31 -070033 var spriteLayer, defsElement;
Simon Hunt2052e5d2015-04-13 17:40:44 -070034
Simon Hunt017a7c32015-04-15 19:23:27 -070035
Simon Huntabf66d92015-04-15 12:57:31 -070036 function registerPathsAsGlyphs(paths) {
37 var custom = {},
38 ids = [];
Simon Hunt2052e5d2015-04-13 17:40:44 -070039
Simon Huntabf66d92015-04-15 12:57:31 -070040 function mkd(d) {
41 return fs.isA(d) ? d.join('') : d;
Simon Hunt2052e5d2015-04-13 17:40:44 -070042 }
43
Simon Hunt017a7c32015-04-15 19:23:27 -070044 paths.forEach(function (path) {
45 var tag = 'spr_' + path.tag;
Simon Hunt2052e5d2015-04-13 17:40:44 -070046
Simon Hunt017a7c32015-04-15 19:23:27 -070047 if (path.glyph) {
48 // assumption is that we are using a built-in glyph
49 return;
50 }
51
52 custom['_' + tag] = path.viewbox || '0 0 1000 1000';
53 custom[tag] = mkd(path.d);
54 ids.push(tag);
55 });
56
57 gs.registerGlyphs(custom);
58 gs.loadDefs(defsElement, ids, true);
Simon Huntabf66d92015-04-15 12:57:31 -070059 }
60
Simon Huntabf66d92015-04-15 12:57:31 -070061
Simon Hunt017a7c32015-04-15 19:23:27 -070062 function doSprite(spr, def, pmeta) {
Simon Huntabf66d92015-04-15 12:57:31 -070063 var c = spr.class || 'gray1',
64 p = spr.pos || [0,0],
65 lab = spr.label,
Simon Huntb2c4cc82015-04-15 17:16:28 -070066 dim = def.dim || [40,40],
Simon Huntabf66d92015-04-15 12:57:31 -070067 w = dim[0],
68 h = dim[1],
Simon Huntb2c4cc82015-04-15 17:16:28 -070069 dy = def.labelyoff || 1,
70 sc = def.scale,
71 xfm = sus.translate(p),
Simon Huntb2c4cc82015-04-15 17:16:28 -070072 g, attr, use, style;
Simon Hunt2052e5d2015-04-13 17:40:44 -070073
Simon Huntb2c4cc82015-04-15 17:16:28 -070074 if (sc) {
75 xfm += sus.scale(sc, sc);
76 }
77
78 g = spriteLayer.append('g')
79 .classed(c, true)
80 .attr('transform', xfm);
81
82 attr = {
Simon Huntabf66d92015-04-15 12:57:31 -070083 width: w,
84 height: h,
Simon Hunt017a7c32015-04-15 19:23:27 -070085 'xlink:href': '#' + pmeta.u
Simon Huntb2c4cc82015-04-15 17:16:28 -070086 };
87
88 use = g.append('use').attr(attr);
89
Simon Hunt017a7c32015-04-15 19:23:27 -070090 if (pmeta.s) {
Simon Huntb2c4cc82015-04-15 17:16:28 -070091 style = {};
Simon Hunt017a7c32015-04-15 19:23:27 -070092 angular.forEach(pmeta.s, function (value, key) {
Simon Huntb2c4cc82015-04-15 17:16:28 -070093 style['stroke-' + key] = value;
94 });
95 use.style(style);
96 }
Simon Huntabf66d92015-04-15 12:57:31 -070097
98 if (lab) {
99 g.append('text')
100 .text(lab)
Simon Huntb2c4cc82015-04-15 17:16:28 -0700101 .attr({ x: w / 2, y: h * dy });
Simon Huntabf66d92015-04-15 12:57:31 -0700102 }
Simon Hunt2052e5d2015-04-13 17:40:44 -0700103 }
104
Simon Huntabf66d92015-04-15 12:57:31 -0700105 function doLabel(label) {
106 var c = label.class || 'gray1',
Simon Huntb2c4cc82015-04-15 17:16:28 -0700107 p = label.pos || [0,0],
108 sz = label.size || 1.0,
109 g = spriteLayer.append('g')
110 .classed(c, true)
111 .attr('transform', sus.translate(p))
112 .append('text')
113 .text(label.text)
114 .style('font-size', (fontsize * sz)+'pt');
Simon Huntabf66d92015-04-15 12:57:31 -0700115 }
116
117
Simon Huntfd8c7d72015-04-14 17:53:37 -0700118 // ==========================
119 // event handlers
Simon Hunt2052e5d2015-04-13 17:40:44 -0700120
Simon Huntfd8c7d72015-04-14 17:53:37 -0700121 // Handles response from 'spriteListRequest' which lists all the
122 // registered sprite definitions on the server.
123 // (see onos-upload-sprites)
124 function inList(payload) {
125 $log.debug(tssid + 'Registered sprite definitions:', payload.names);
126 // Some day, we will make this list available to the user in
127 // a dropdown selection box...
Simon Hunt2052e5d2015-04-13 17:40:44 -0700128 }
129
Simon Huntfd8c7d72015-04-14 17:53:37 -0700130 // Handles response from 'spriteDataRequest' which provides the
131 // data for the requested sprite definition.
132 function inData(payload) {
133 var data = payload.data,
Simon Hunt017a7c32015-04-15 19:23:27 -0700134 name, desc, pfx, sprites, labels, alpha,
135 paths, defn, load,
136 pathmeta = {},
137 defs = {},
138 warn = [];
Simon Huntfd8c7d72015-04-14 17:53:37 -0700139
140 if (!data) {
Simon Hunt017a7c32015-04-15 19:23:27 -0700141 $log.warn(tssid + 'No sprite data loaded.');
Simon Huntfd8c7d72015-04-14 17:53:37 -0700142 return;
143 }
Simon Huntabf66d92015-04-15 12:57:31 -0700144 name = data.defn_name;
145 desc = data.defn_desc;
Simon Hunt017a7c32015-04-15 19:23:27 -0700146 paths = data.paths;
147 defn = data.defn;
148 load = data.load;
149 pfx = tssid + '[' + name + ']: ';
Simon Huntfd8c7d72015-04-14 17:53:37 -0700150
151 $log.debug("Loading sprites...[" + name + "]", desc);
152
Simon Hunt017a7c32015-04-15 19:23:27 -0700153 function no(what) {
154 warn.push(pfx + 'No ' + what + ' property defined');
Simon Huntb2c4cc82015-04-15 17:16:28 -0700155 }
Simon Huntfd8c7d72015-04-14 17:53:37 -0700156
Simon Hunt017a7c32015-04-15 19:23:27 -0700157 if (!paths) no('paths');
158 if (!defn) no('defn');
159 if (!load) no('load');
160
161 if (warn.length) {
162 $log.error(warn.join('\n'));
163 return;
Simon Huntabf66d92015-04-15 12:57:31 -0700164 }
165
Simon Hunt017a7c32015-04-15 19:23:27 -0700166 // any custom paths need to be added to the glyph DB, and imported
167 registerPathsAsGlyphs(paths);
168
169 paths.forEach(function (p) {
170 pathmeta[p.tag] = {
171 s: p.stroke,
172 u: p.glyph || 'spr_' + p.tag
173 };
174 });
175
176 defn.forEach(function (d) {
177 defs[d.id] = d;
178 });
179
180 // sprites, labels and alpha are each optional components of the load
181 sprites = load.sprites;
182 labels = load.labels;
183 alpha = load.alpha;
184
185 if (alpha) {
186 spriteLayer.style('opacity', alpha);
Simon Huntabf66d92015-04-15 12:57:31 -0700187 }
188
189 if (sprites) {
190 sprites.forEach(function (spr) {
Simon Huntb2c4cc82015-04-15 17:16:28 -0700191 var def = defs[spr.id],
Simon Hunt017a7c32015-04-15 19:23:27 -0700192 pmeta = pathmeta[def.path];
193 doSprite(spr, def, pmeta);
Simon Huntabf66d92015-04-15 12:57:31 -0700194 });
195 }
196
197 if (labels) {
198 labels.forEach(doLabel);
199 }
Simon Huntfd8c7d72015-04-14 17:53:37 -0700200 }
201
202
Simon Huntabf66d92015-04-15 12:57:31 -0700203 function loadSprites(layer, defsElem, defname) {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700204 var name = defname || 'sprites';
205 spriteLayer = layer;
Simon Huntabf66d92015-04-15 12:57:31 -0700206 defsElement = defsElem;
Simon Huntfd8c7d72015-04-14 17:53:37 -0700207
208 $log.info(tssid + 'Requesting sprite definition ['+name+']...');
209
210 wss.sendEvent('spriteListRequest');
211 wss.sendEvent('spriteDataRequest', {name: name});
212 }
Simon Hunt2052e5d2015-04-13 17:40:44 -0700213
214 // === -----------------------------------------------------
215 // === MODULE DEFINITION ===
216
217 angular.module('ovTopo')
218 .factory('TopoSpriteService',
Simon Huntabf66d92015-04-15 12:57:31 -0700219 ['$log', '$http', 'FnService', 'GlyphService',
220 'SvgUtilService', 'WebSocketService',
Simon Hunt2052e5d2015-04-13 17:40:44 -0700221
Simon Huntabf66d92015-04-15 12:57:31 -0700222 function (_$log_, _$http_, _fs_, _gs_, _sus_, _wss_) {
Simon Hunt2052e5d2015-04-13 17:40:44 -0700223 $log = _$log_;
224 $http = _$http_;
225 fs = _fs_;
Simon Huntabf66d92015-04-15 12:57:31 -0700226 gs = _gs_;
Simon Hunt2052e5d2015-04-13 17:40:44 -0700227 sus = _sus_;
Simon Huntfd8c7d72015-04-14 17:53:37 -0700228 wss = _wss_;
Simon Hunt2052e5d2015-04-13 17:40:44 -0700229
230 return {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700231 loadSprites: loadSprites,
232 spriteListResponse: inList,
233 spriteDataResponse: inData
Simon Hunt2052e5d2015-04-13 17:40:44 -0700234 };
235 }]);
236
237}());