blob: 1586a04bc1335a00ef997132926b99914006b972 [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.
19 Defines behavior for loading sprites.
20 */
21
22(function () {
23 'use strict';
24
25 // injected refs
Simon Huntfd8c7d72015-04-14 17:53:37 -070026 var $log, $http, fs, sus, wss;
27
28 var tssid = 'TopoSpriteService: ';
Simon Hunt2052e5d2015-04-13 17:40:44 -070029
30 // internal state
Simon Huntfd8c7d72015-04-14 17:53:37 -070031 var spriteLayer;
Simon Hunt2052e5d2015-04-13 17:40:44 -070032
33 function doSprite(def, item) {
34 var g;
35
36 function xfm(x, y, s) {
37 return sus.translate([x,y]) + sus.scale(s, s);
38 }
39
40 g = spriteLayer.append('g')
41 .classed(def['class'], true)
42 .attr('transform', xfm(item.x, item.y, def.scale));
43
44 if (item.label) {
45 g.append('text')
46 .text(item.label)
47 .attr({
48 x: def.width / 2,
49 y: def.height * def.textyoff
50 });
51 }
52
53 g.append('use').attr({
54 width: def.width,
55 height: def.height,
56 'xlink:href': '#' + def.use
57 });
58 }
59
Simon Huntfd8c7d72015-04-14 17:53:37 -070060 // ==========================
61 // event handlers
Simon Hunt2052e5d2015-04-13 17:40:44 -070062
Simon Huntfd8c7d72015-04-14 17:53:37 -070063 // Handles response from 'spriteListRequest' which lists all the
64 // registered sprite definitions on the server.
65 // (see onos-upload-sprites)
66 function inList(payload) {
67 $log.debug(tssid + 'Registered sprite definitions:', payload.names);
68 // Some day, we will make this list available to the user in
69 // a dropdown selection box...
Simon Hunt2052e5d2015-04-13 17:40:44 -070070 }
71
Simon Huntfd8c7d72015-04-14 17:53:37 -070072 // Handles response from 'spriteDataRequest' which provides the
73 // data for the requested sprite definition.
74 function inData(payload) {
75 var data = payload.data,
76 name = data && data.defn_name,
77 desc = data && data.defn_desc,
78 defs = {};
79
80 if (!data) {
81 $log.warn(tssid + 'No sprite data loaded.')
82 return;
83 }
84
85 $log.debug("Loading sprites...[" + name + "]", desc);
86
87 data.defn.forEach(function (d) {
88 defs[d.id] = d;
89 });
90
91 data.load.forEach(function (item) {
92 doSprite(defs[item.id], item);
93 });
94 }
95
96
97 function loadSprites(layer, defname) {
98 var name = defname || 'sprites';
99 spriteLayer = layer;
100
101 $log.info(tssid + 'Requesting sprite definition ['+name+']...');
102
103 wss.sendEvent('spriteListRequest');
104 wss.sendEvent('spriteDataRequest', {name: name});
105 }
Simon Hunt2052e5d2015-04-13 17:40:44 -0700106
107 // === -----------------------------------------------------
108 // === MODULE DEFINITION ===
109
110 angular.module('ovTopo')
111 .factory('TopoSpriteService',
Simon Huntfd8c7d72015-04-14 17:53:37 -0700112 ['$log', '$http', 'FnService', 'SvgUtilService', 'WebSocketService',
Simon Hunt2052e5d2015-04-13 17:40:44 -0700113
Simon Huntfd8c7d72015-04-14 17:53:37 -0700114 function (_$log_, _$http_, _fs_, _sus_, _wss_) {
Simon Hunt2052e5d2015-04-13 17:40:44 -0700115 $log = _$log_;
116 $http = _$http_;
117 fs = _fs_;
118 sus = _sus_;
Simon Huntfd8c7d72015-04-14 17:53:37 -0700119 wss = _wss_;
Simon Hunt2052e5d2015-04-13 17:40:44 -0700120
121 return {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700122 loadSprites: loadSprites,
123 spriteListResponse: inList,
124 spriteDataResponse: inData
Simon Hunt2052e5d2015-04-13 17:40:44 -0700125 };
126 }]);
127
128}());