blob: 3b1115a86a7de35fdaf9eab4e9bc8f08c5e172ab [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 Huntabf66d92015-04-15 12:57:31 -070035 function registerPathsAsGlyphs(paths) {
36 var custom = {},
37 ids = [];
Simon Hunt2052e5d2015-04-13 17:40:44 -070038
Simon Huntabf66d92015-04-15 12:57:31 -070039 function mkd(d) {
40 return fs.isA(d) ? d.join('') : d;
Simon Hunt2052e5d2015-04-13 17:40:44 -070041 }
42
Simon Huntabf66d92015-04-15 12:57:31 -070043 if (paths) {
44 paths.forEach(function (path) {
45 var tag = 'spr_' + path.tag;
46 custom['_' + tag] = path.viewbox || '0 0 1000 1000';
47 custom[tag] = mkd(path.d);
48 ids.push(tag);
49 });
Simon Hunt2052e5d2015-04-13 17:40:44 -070050
Simon Huntabf66d92015-04-15 12:57:31 -070051 gs.registerGlyphs(custom);
52 gs.loadDefs(defsElement, ids, true);
Simon Hunt2052e5d2015-04-13 17:40:44 -070053 }
Simon Huntabf66d92015-04-15 12:57:31 -070054 }
55
Simon Huntabf66d92015-04-15 12:57:31 -070056
Simon Huntb2c4cc82015-04-15 17:16:28 -070057 function doSprite(spr, def, pstrk) {
Simon Huntabf66d92015-04-15 12:57:31 -070058 var c = spr.class || 'gray1',
59 p = spr.pos || [0,0],
60 lab = spr.label,
Simon Huntb2c4cc82015-04-15 17:16:28 -070061 dim = def.dim || [40,40],
Simon Huntabf66d92015-04-15 12:57:31 -070062 w = dim[0],
63 h = dim[1],
Simon Huntb2c4cc82015-04-15 17:16:28 -070064 dy = def.labelyoff || 1,
65 sc = def.scale,
66 xfm = sus.translate(p),
67 useId = def.glyph || 'spr_' + def.path,
68 g, attr, use, style;
Simon Hunt2052e5d2015-04-13 17:40:44 -070069
Simon Huntb2c4cc82015-04-15 17:16:28 -070070 if (sc) {
71 xfm += sus.scale(sc, sc);
72 }
73
74 g = spriteLayer.append('g')
75 .classed(c, true)
76 .attr('transform', xfm);
77
78 attr = {
Simon Huntabf66d92015-04-15 12:57:31 -070079 width: w,
80 height: h,
Simon Huntb2c4cc82015-04-15 17:16:28 -070081 'xlink:href': '#' + useId
82 };
83
84 use = g.append('use').attr(attr);
85
86 if (pstrk) {
87 style = {};
88 angular.forEach(pstrk, function (value, key) {
89 style['stroke-' + key] = value;
90 });
91 use.style(style);
92 }
Simon Huntabf66d92015-04-15 12:57:31 -070093
94 if (lab) {
95 g.append('text')
96 .text(lab)
Simon Huntb2c4cc82015-04-15 17:16:28 -070097 .attr({ x: w / 2, y: h * dy });
Simon Huntabf66d92015-04-15 12:57:31 -070098 }
Simon Hunt2052e5d2015-04-13 17:40:44 -070099 }
100
Simon Huntabf66d92015-04-15 12:57:31 -0700101 function doLabel(label) {
102 var c = label.class || 'gray1',
Simon Huntb2c4cc82015-04-15 17:16:28 -0700103 p = label.pos || [0,0],
104 sz = label.size || 1.0,
105 g = spriteLayer.append('g')
106 .classed(c, true)
107 .attr('transform', sus.translate(p))
108 .append('text')
109 .text(label.text)
110 .style('font-size', (fontsize * sz)+'pt');
Simon Huntabf66d92015-04-15 12:57:31 -0700111 }
112
113
Simon Huntfd8c7d72015-04-14 17:53:37 -0700114 // ==========================
115 // event handlers
Simon Hunt2052e5d2015-04-13 17:40:44 -0700116
Simon Huntfd8c7d72015-04-14 17:53:37 -0700117 // Handles response from 'spriteListRequest' which lists all the
118 // registered sprite definitions on the server.
119 // (see onos-upload-sprites)
120 function inList(payload) {
121 $log.debug(tssid + 'Registered sprite definitions:', payload.names);
122 // Some day, we will make this list available to the user in
123 // a dropdown selection box...
Simon Hunt2052e5d2015-04-13 17:40:44 -0700124 }
125
Simon Huntfd8c7d72015-04-14 17:53:37 -0700126 // Handles response from 'spriteDataRequest' which provides the
127 // data for the requested sprite definition.
128 function inData(payload) {
129 var data = payload.data,
Simon Huntb2c4cc82015-04-15 17:16:28 -0700130 name, desc, sprites, labels, alpha,
131 pathstrokes = {},
Simon Huntfd8c7d72015-04-14 17:53:37 -0700132 defs = {};
133
134 if (!data) {
135 $log.warn(tssid + 'No sprite data loaded.')
136 return;
137 }
Simon Huntabf66d92015-04-15 12:57:31 -0700138 name = data.defn_name;
139 desc = data.defn_desc;
Simon Huntfd8c7d72015-04-14 17:53:37 -0700140
141 $log.debug("Loading sprites...[" + name + "]", desc);
142
Simon Huntb2c4cc82015-04-15 17:16:28 -0700143 if (data.paths) {
144 registerPathsAsGlyphs(data.paths);
145 data.paths.forEach(function (p) {
146 pathstrokes[p.tag] = p.stroke;
147 });
148 }
Simon Huntfd8c7d72015-04-14 17:53:37 -0700149
Simon Huntabf66d92015-04-15 12:57:31 -0700150 if (data.defn) {
151 data.defn.forEach(function (d) {
152 defs[d.id] = d;
153 });
154 }
155
156 // pull out the sprite and label items
157 if (data.load) {
158 sprites = data.load.sprites;
159 labels = data.load.labels;
Simon Huntb2c4cc82015-04-15 17:16:28 -0700160 alpha = data.load.alpha;
161 if (alpha) {
162 spriteLayer.style('opacity', alpha);
163 }
Simon Huntabf66d92015-04-15 12:57:31 -0700164 }
165
166 if (sprites) {
167 sprites.forEach(function (spr) {
Simon Huntb2c4cc82015-04-15 17:16:28 -0700168 var def = defs[spr.id],
169 pstrk = def.path && pathstrokes[def.path];
170 doSprite(spr, def, pstrk);
Simon Huntabf66d92015-04-15 12:57:31 -0700171 });
172 }
173
174 if (labels) {
175 labels.forEach(doLabel);
176 }
Simon Huntfd8c7d72015-04-14 17:53:37 -0700177 }
178
179
Simon Huntabf66d92015-04-15 12:57:31 -0700180 function loadSprites(layer, defsElem, defname) {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700181 var name = defname || 'sprites';
182 spriteLayer = layer;
Simon Huntabf66d92015-04-15 12:57:31 -0700183 defsElement = defsElem;
Simon Huntfd8c7d72015-04-14 17:53:37 -0700184
185 $log.info(tssid + 'Requesting sprite definition ['+name+']...');
186
187 wss.sendEvent('spriteListRequest');
188 wss.sendEvent('spriteDataRequest', {name: name});
189 }
Simon Hunt2052e5d2015-04-13 17:40:44 -0700190
191 // === -----------------------------------------------------
192 // === MODULE DEFINITION ===
193
194 angular.module('ovTopo')
195 .factory('TopoSpriteService',
Simon Huntabf66d92015-04-15 12:57:31 -0700196 ['$log', '$http', 'FnService', 'GlyphService',
197 'SvgUtilService', 'WebSocketService',
Simon Hunt2052e5d2015-04-13 17:40:44 -0700198
Simon Huntabf66d92015-04-15 12:57:31 -0700199 function (_$log_, _$http_, _fs_, _gs_, _sus_, _wss_) {
Simon Hunt2052e5d2015-04-13 17:40:44 -0700200 $log = _$log_;
201 $http = _$http_;
202 fs = _fs_;
Simon Huntabf66d92015-04-15 12:57:31 -0700203 gs = _gs_;
Simon Hunt2052e5d2015-04-13 17:40:44 -0700204 sus = _sus_;
Simon Huntfd8c7d72015-04-14 17:53:37 -0700205 wss = _wss_;
Simon Hunt2052e5d2015-04-13 17:40:44 -0700206
207 return {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700208 loadSprites: loadSprites,
209 spriteListResponse: inList,
210 spriteDataResponse: inData
Simon Hunt2052e5d2015-04-13 17:40:44 -0700211 };
212 }]);
213
214}());