blob: 756aed575e2707112385fd9efdc4a282c6fe1b13 [file] [log] [blame]
Simon Hunt2052e5d2015-04-13 17:40:44 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Hunt2052e5d2015-04-13 17:40:44 -07003 *
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
Steven Burrows1c2a9682017-07-14 16:52:46 +010026 var $log, 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: ',
Steven Burrows1c2a9682017-07-14 16:52:46 +010030 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 Hunt7f62f7c2015-05-11 17:38:02 -070061 function applyStrokeStyle(s, use) {
62 var style;
63 if (s) {
64 style = {};
65 angular.forEach(s, function (value, key) {
66 style['stroke-' + key] = value;
67 });
68 use.style(style);
69 }
70 }
Simon Huntabf66d92015-04-15 12:57:31 -070071
Simon Hunt7f62f7c2015-05-11 17:38:02 -070072 function applyFillClass(f, use) {
73 use.classed('fill-' + f, true);
74 }
75
76 function doSprite(spr, def, pathmeta) {
77 var pmeta = pathmeta[def.path],
78 c = spr.class || 'gray1',
Steven Burrows1c2a9682017-07-14 16:52:46 +010079 p = spr.pos || [0, 0],
Simon Huntabf66d92015-04-15 12:57:31 -070080 lab = spr.label,
Steven Burrows1c2a9682017-07-14 16:52:46 +010081 dim = def.dim || [40, 40],
Simon Huntabf66d92015-04-15 12:57:31 -070082 w = dim[0],
83 h = dim[1],
Simon Huntb2c4cc82015-04-15 17:16:28 -070084 dy = def.labelyoff || 1,
85 sc = def.scale,
86 xfm = sus.translate(p),
Simon Hunt7f62f7c2015-05-11 17:38:02 -070087 g, attr, use;
Simon Hunt2052e5d2015-04-13 17:40:44 -070088
Simon Huntb2c4cc82015-04-15 17:16:28 -070089 if (sc) {
90 xfm += sus.scale(sc, sc);
91 }
92
93 g = spriteLayer.append('g')
94 .classed(c, true)
95 .attr('transform', xfm);
96
97 attr = {
Simon Huntabf66d92015-04-15 12:57:31 -070098 width: w,
99 height: h,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100100 'xlink:href': '#' + pmeta.u,
Simon Huntb2c4cc82015-04-15 17:16:28 -0700101 };
102
103 use = g.append('use').attr(attr);
Simon Hunt7f62f7c2015-05-11 17:38:02 -0700104 applyStrokeStyle(pmeta.s, use);
105 applyFillClass(def.fill, use);
Simon Huntb2c4cc82015-04-15 17:16:28 -0700106
Simon Hunt7f62f7c2015-05-11 17:38:02 -0700107
108 // add subpaths if they have been defined
109 if (fs.isA(def.subpaths)) {
110 def.subpaths.forEach(function (v) {
111 pmeta = pathmeta[v.path];
112 attr = {
113 width: w,
114 height: h,
115 'xlink:href': '#' + pmeta.u,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100116 transform: sus.translate(v.pos),
Simon Hunt7f62f7c2015-05-11 17:38:02 -0700117 };
118 use = g.append('use').attr(attr);
119 applyStrokeStyle(pmeta.s, use);
120 applyFillClass(def.subpathfill, use);
Simon Huntb2c4cc82015-04-15 17:16:28 -0700121 });
Simon Huntb2c4cc82015-04-15 17:16:28 -0700122 }
Simon Huntabf66d92015-04-15 12:57:31 -0700123
124 if (lab) {
125 g.append('text')
126 .text(lab)
Simon Huntb2c4cc82015-04-15 17:16:28 -0700127 .attr({ x: w / 2, y: h * dy });
Simon Huntabf66d92015-04-15 12:57:31 -0700128 }
Simon Hunt2052e5d2015-04-13 17:40:44 -0700129 }
130
Simon Huntabf66d92015-04-15 12:57:31 -0700131 function doLabel(label) {
132 var c = label.class || 'gray1',
Steven Burrows1c2a9682017-07-14 16:52:46 +0100133 p = label.pos || [0, 0],
134 sz = label.size || 1.0;
135
136 spriteLayer.append('g')
Simon Huntb2c4cc82015-04-15 17:16:28 -0700137 .classed(c, true)
138 .attr('transform', sus.translate(p))
139 .append('text')
140 .text(label.text)
141 .style('font-size', (fontsize * sz)+'pt');
Simon Huntabf66d92015-04-15 12:57:31 -0700142 }
143
144
Simon Huntfd8c7d72015-04-14 17:53:37 -0700145 // ==========================
146 // event handlers
Simon Hunt2052e5d2015-04-13 17:40:44 -0700147
Simon Huntfd8c7d72015-04-14 17:53:37 -0700148 // Handles response from 'spriteListRequest' which lists all the
149 // registered sprite definitions on the server.
150 // (see onos-upload-sprites)
151 function inList(payload) {
152 $log.debug(tssid + 'Registered sprite definitions:', payload.names);
153 // Some day, we will make this list available to the user in
154 // a dropdown selection box...
Simon Hunt2052e5d2015-04-13 17:40:44 -0700155 }
156
Simon Huntfd8c7d72015-04-14 17:53:37 -0700157 // Handles response from 'spriteDataRequest' which provides the
158 // data for the requested sprite definition.
159 function inData(payload) {
160 var data = payload.data,
Simon Hunt017a7c32015-04-15 19:23:27 -0700161 name, desc, pfx, sprites, labels, alpha,
162 paths, defn, load,
163 pathmeta = {},
164 defs = {},
165 warn = [];
Simon Huntfd8c7d72015-04-14 17:53:37 -0700166
167 if (!data) {
Simon Hunt017a7c32015-04-15 19:23:27 -0700168 $log.warn(tssid + 'No sprite data loaded.');
Simon Huntfd8c7d72015-04-14 17:53:37 -0700169 return;
170 }
Simon Huntabf66d92015-04-15 12:57:31 -0700171 name = data.defn_name;
172 desc = data.defn_desc;
Simon Hunt017a7c32015-04-15 19:23:27 -0700173 paths = data.paths;
174 defn = data.defn;
175 load = data.load;
176 pfx = tssid + '[' + name + ']: ';
Simon Huntfd8c7d72015-04-14 17:53:37 -0700177
Steven Burrows1c2a9682017-07-14 16:52:46 +0100178 $log.debug('Loading sprites...[' + name + ']', desc);
Simon Huntfd8c7d72015-04-14 17:53:37 -0700179
Simon Hunt017a7c32015-04-15 19:23:27 -0700180 function no(what) {
181 warn.push(pfx + 'No ' + what + ' property defined');
Simon Huntb2c4cc82015-04-15 17:16:28 -0700182 }
Simon Huntfd8c7d72015-04-14 17:53:37 -0700183
Simon Hunt017a7c32015-04-15 19:23:27 -0700184 if (!paths) no('paths');
185 if (!defn) no('defn');
186 if (!load) no('load');
187
188 if (warn.length) {
189 $log.error(warn.join('\n'));
190 return;
Simon Huntabf66d92015-04-15 12:57:31 -0700191 }
192
Simon Hunt017a7c32015-04-15 19:23:27 -0700193 // any custom paths need to be added to the glyph DB, and imported
194 registerPathsAsGlyphs(paths);
195
196 paths.forEach(function (p) {
197 pathmeta[p.tag] = {
198 s: p.stroke,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100199 u: p.glyph || 'spr_' + p.tag,
Simon Hunt017a7c32015-04-15 19:23:27 -0700200 };
201 });
202
203 defn.forEach(function (d) {
204 defs[d.id] = d;
205 });
206
207 // sprites, labels and alpha are each optional components of the load
208 sprites = load.sprites;
209 labels = load.labels;
210 alpha = load.alpha;
211
212 if (alpha) {
213 spriteLayer.style('opacity', alpha);
Simon Huntabf66d92015-04-15 12:57:31 -0700214 }
215
216 if (sprites) {
217 sprites.forEach(function (spr) {
Simon Hunt7f62f7c2015-05-11 17:38:02 -0700218 var def = defs[spr.id];
219 doSprite(spr, def, pathmeta);
Simon Huntabf66d92015-04-15 12:57:31 -0700220 });
221 }
222
223 if (labels) {
224 labels.forEach(doLabel);
225 }
Simon Huntfd8c7d72015-04-14 17:53:37 -0700226 }
227
228
Simon Huntabf66d92015-04-15 12:57:31 -0700229 function loadSprites(layer, defsElem, defname) {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700230 var name = defname || 'sprites';
231 spriteLayer = layer;
Simon Huntabf66d92015-04-15 12:57:31 -0700232 defsElement = defsElem;
Simon Huntfd8c7d72015-04-14 17:53:37 -0700233
234 $log.info(tssid + 'Requesting sprite definition ['+name+']...');
235
236 wss.sendEvent('spriteListRequest');
Steven Burrows1c2a9682017-07-14 16:52:46 +0100237 wss.sendEvent('spriteDataRequest', { name: name });
Simon Huntfd8c7d72015-04-14 17:53:37 -0700238 }
Simon Hunt2052e5d2015-04-13 17:40:44 -0700239
240 // === -----------------------------------------------------
241 // === MODULE DEFINITION ===
242
243 angular.module('ovTopo')
244 .factory('TopoSpriteService',
Steven Burrows1c2a9682017-07-14 16:52:46 +0100245 ['$log', 'FnService', 'GlyphService',
Simon Huntabf66d92015-04-15 12:57:31 -0700246 'SvgUtilService', 'WebSocketService',
Simon Hunt2052e5d2015-04-13 17:40:44 -0700247
Steven Burrows1c2a9682017-07-14 16:52:46 +0100248 function (_$log_, _fs_, _gs_, _sus_, _wss_) {
Simon Hunt2052e5d2015-04-13 17:40:44 -0700249 $log = _$log_;
Simon Hunt2052e5d2015-04-13 17:40:44 -0700250 fs = _fs_;
Simon Huntabf66d92015-04-15 12:57:31 -0700251 gs = _gs_;
Simon Hunt2052e5d2015-04-13 17:40:44 -0700252 sus = _sus_;
Simon Huntfd8c7d72015-04-14 17:53:37 -0700253 wss = _wss_;
Simon Hunt2052e5d2015-04-13 17:40:44 -0700254
255 return {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700256 loadSprites: loadSprites,
257 spriteListResponse: inList,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100258 spriteDataResponse: inData,
Simon Hunt2052e5d2015-04-13 17:40:44 -0700259 };
260 }]);
261
262}());