blob: 15e51828f9133e1413396033deccfe151b0b8fb5 [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
26 var $log, $http, fs, sus;
27
28 // internal state
29 var spriteLayer,
30 cache = d3.map();
31
32 // constants
33 var urlPrefix = 'data/ext/';
34
35 function getUrl(id) {
36 return urlPrefix + id + '.json';
37 }
38
39 // =========================
40
41 function clearCache() {
42 cache = d3.map();
43 }
44
45
46 function loadSpriteData(id, cb) {
47 var url = getUrl(id),
48 promise = cache.get(id);
49
50 if (!promise) {
51 // need to fetch data and cache it
52 promise = $http.get(url);
53
54 promise.meta = {
55 id: id,
56 url: url,
57 wasCached: false
58 };
59
60 promise.then(function (response) {
61 // success
62 promise.spriteData = response.data;
63 cb(promise.spriteData);
64 }, function (response) {
65 // error
66 $log.warn('Failed to retrieve sprite data: ' + url,
67 response.status, response.data);
68 });
69
70 } else {
71 promise.meta.wasCached = true;
72 cb(promise.spriteData);
73 }
74 }
75
76 function doSprite(def, item) {
77 var g;
78
79 function xfm(x, y, s) {
80 return sus.translate([x,y]) + sus.scale(s, s);
81 }
82
83 g = spriteLayer.append('g')
84 .classed(def['class'], true)
85 .attr('transform', xfm(item.x, item.y, def.scale));
86
87 if (item.label) {
88 g.append('text')
89 .text(item.label)
90 .attr({
91 x: def.width / 2,
92 y: def.height * def.textyoff
93 });
94 }
95
96 g.append('use').attr({
97 width: def.width,
98 height: def.height,
99 'xlink:href': '#' + def.use
100 });
101 }
102
103 function loadSprites(layer) {
104 spriteLayer = layer;
105
106 loadSpriteData('sprites', function (data) {
107 var defs = {};
108
109 $log.debug("Loading sprites...", data.file_desc);
110
111 data.defn.forEach(function (d) {
112 defs[d.id] = d;
113 });
114
115 data.load.forEach(function (item) {
116 doSprite(defs[item.id], item);
117 });
118 });
119
120 }
121
122
123 // === -----------------------------------------------------
124 // === MODULE DEFINITION ===
125
126 angular.module('ovTopo')
127 .factory('TopoSpriteService',
128 ['$log', '$http', 'FnService', 'SvgUtilService',
129
130 function (_$log_, _$http_, _fs_, _sus_) {
131 $log = _$log_;
132 $http = _$http_;
133 fs = _fs_;
134 sus = _sus_;
135
136 return {
137 clearCache: clearCache,
138 loadSprites: loadSprites
139 };
140 }]);
141
142}());