blob: f53ccc68787584fedab2a43ce77db9ed3f4ec130 [file] [log] [blame]
Steven Burrows3cc0c372017-02-10 15:15:24 +00001/*
2 * Copyright 2016-present 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 into the sprite layer.
20 */
21
22 (function() {
23 'use strict';
24
25 var instance,
26 renderer;
27
28 function vbox(w, h) {
29 return '0 0 ' + w + ' ' + h;
30 }
31
32 angular.module('ovTopo2')
33 .factory('Topo2SpriteLayerService', [
Simon Hunt0ee20bf2017-05-10 19:59:17 -070034 '$log', 'FnService',
35 'Topo2ViewController', 'SpriteService', 'ThemeService',
Steven Burrows3cc0c372017-02-10 15:15:24 +000036
Simon Hunt0ee20bf2017-05-10 19:59:17 -070037 function ($log, fs, ViewController, ss, ts) {
Steven Burrows3cc0c372017-02-10 15:15:24 +000038
39 var SpriteLayer = ViewController.extend({
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000040
Simon Hunt95f4b422017-03-03 13:49:05 -080041 id: 'topo2-sprites',
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000042 displayName: 'Sprite Layer',
43
Steven Burrowsb43c1a92017-03-07 17:13:28 +000044 init: function() {
45 this.svg = d3.select('#topo2');
Steven Burrows3cc0c372017-02-10 15:15:24 +000046 this.createSpriteDefs();
Steven Burrowsb43c1a92017-03-07 17:13:28 +000047 this.container = this.appendElement('#topo2-background', 'g');
Steven Burrows3cc0c372017-02-10 15:15:24 +000048 },
49 loadLayout: function (id) {
Steven Burrows68d6f952017-03-10 13:53:35 +000050 var _this = this;
Steven Burrows3cc0c372017-02-10 15:15:24 +000051 this.container.selectAll("*").remove();
52 this.layout = ss.layout(id);
Steven Burrows3db5ddb2017-02-17 15:21:20 +000053
Simon Hunt0ee20bf2017-05-10 19:59:17 -070054 if (this.layout) {
55 this.width = this.layout.data.w;
56 this.height = this.layout.data.h;
Steven Burrows3db5ddb2017-02-17 15:21:20 +000057
Simon Hunt0ee20bf2017-05-10 19:59:17 -070058 this.renderLayout();
Steven Burrows11162542017-02-27 21:52:56 +000059
Simon Hunt0ee20bf2017-05-10 19:59:17 -070060 if (fs.debugOn('sprite_grid')) {
61 this.renderGrid();
62 }
63 } else {
64 $log.warn('no sprite layout registered:', id);
Steven Burrows11162542017-02-27 21:52:56 +000065 }
Steven Burrowsb43c1a92017-03-07 17:13:28 +000066
67 // Returns a promise for consistency with Topo2MapService
68 return new Promise(function(resolve) {
Steven Burrows68d6f952017-03-10 13:53:35 +000069 resolve(_this);
Steven Burrowsb43c1a92017-03-07 17:13:28 +000070 });
Steven Burrows3cc0c372017-02-10 15:15:24 +000071 },
72 createSpriteDefs: function () {
73 this.defs = this.svg.append('defs')
74 .attr('id', 'sprites');
75 },
Steven Burrows3db5ddb2017-02-17 15:21:20 +000076 getWidth: function () {
77 return this.width;
78 },
79 getHeight: function () {
80 return this.height;
81 },
Steven Burrows3cc0c372017-02-10 15:15:24 +000082 renderSprite: function (spriteData) {
83
84 var id = spriteData.sprite.data.id,
85 definition = d3.select('#' + id);
Simon Hunta6ae1212017-03-18 17:58:53 -070086 // is '#<id>' sufficient? namespace? '#t2spr-<id>' ?
Steven Burrows3cc0c372017-02-10 15:15:24 +000087
88 if (definition.empty()) {
89
90 var data = spriteData.sprite.data,
91 spriteEl = this.defs.append('symbol')
92 .attr('viewBox', vbox(data.w, data.h))
93 .attr('id', id);
94
Simon Hunta6ae1212017-03-18 17:58:53 -070095 // TODO: add elements to sprite in the order defined
96 // when the sprite was created (layered bottom up)
97 // e.g. (1) rect, (2) path, (3) rect...
98 // not in groups by type.
99 // For now, assume rectangles are behind paths...
Steven Burrows3cc0c372017-02-10 15:15:24 +0000100
Simon Hunta6ae1212017-03-18 17:58:53 -0700101 // draw rectangles before paths
Steven Burrows3cc0c372017-02-10 15:15:24 +0000102 _.each(spriteData.sprite.rects, function (rect) {
103 spriteEl.append('rect')
104 .attr('width', rect.w)
105 .attr('height', rect.h)
106 .attr('x', rect.x)
107 .attr('y', rect.y)
Simon Hunta6ae1212017-03-18 17:58:53 -0700108 .style('fill', ts.spriteColor(rect.o.fill, 'fill'))
109 .style('stroke', ts.spriteColor(rect.o.stroke, 'stroke'));
110 });
111
112 // draw paths after rectangles
113 _.each(spriteData.sprite.paths, function (path) {
114 spriteEl.append('path')
115 .attr('d', path.d)
116 .style('fill', ts.spriteColor(path.o.fill, 'fill'))
117 .style('stroke', ts.spriteColor(path.o.stroke, 'stroke'));
Steven Burrows3cc0c372017-02-10 15:15:24 +0000118 });
119 }
120
Simon Hunta6ae1212017-03-18 17:58:53 -0700121 return spriteEl;
Steven Burrows3cc0c372017-02-10 15:15:24 +0000122 },
123 renderLayout: function () {
124
125 var _this = this;
Steven Burrows3cc0c372017-02-10 15:15:24 +0000126 var layout = this.container.append('svg')
127 .attr('class', layout)
128 .attr('viewBox', vbox(this.layout.data.w, this.layout.data.h));
129
130 _.each(this.layout.sprites, function (spriteData) {
131 _this.renderSprite(spriteData);
132
133 layout
134 .append('g')
135 .append("use")
136 .attr('xlink:href', '#rack')
137 .attr('width', 20)
138 .attr('height', 25)
139 .attr('x', spriteData.x)
140 .attr('y', spriteData.y);
141 });
Steven Burrows11162542017-02-27 21:52:56 +0000142 },
143 renderGrid: function () {
144
Steven Burrows11162542017-02-27 21:52:56 +0000145 var layout = this.container.select('svg').append('g');
146
147 var gridSpacing = 5,
148 x = this.width / gridSpacing,
Simon Hunta6ae1212017-03-18 17:58:53 -0700149 y = this.height / gridSpacing,
150 i, l;
Steven Burrows11162542017-02-27 21:52:56 +0000151
Simon Hunta6ae1212017-03-18 17:58:53 -0700152 for (i = 0, l = x; i < l; i++) {
Steven Burrows11162542017-02-27 21:52:56 +0000153 layout.append('rect')
154 .attr('width', 0.1)
155 .attr('height', this.height)
156 .attr('x', i * gridSpacing)
Simon Hunta6ae1212017-03-18 17:58:53 -0700157 .attr('y', 0);
Steven Burrows11162542017-02-27 21:52:56 +0000158 }
159
Simon Hunta6ae1212017-03-18 17:58:53 -0700160 for (i = 0, l = y; i < l; i++) {
Steven Burrows11162542017-02-27 21:52:56 +0000161 layout.append('rect')
162 .attr('height', 0.1)
163 .attr('width', this.width)
164 .attr('y', i * gridSpacing)
Simon Hunta6ae1212017-03-18 17:58:53 -0700165 .attr('x', 0);
Steven Burrows11162542017-02-27 21:52:56 +0000166 }
Steven Burrows3cc0c372017-02-10 15:15:24 +0000167 }
168 });
169
170 function getInstance() {
171 return instance || new SpriteLayer();
172 }
173
174 return getInstance();
175 }
176 ])
177 })();