blob: 98e890b1da4c1c7c4c0667091b05ba41e1a179ae [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 Hunta6ae1212017-03-18 17:58:53 -070034 'FnService', 'Topo2ViewController', 'SpriteService', 'ThemeService',
Steven Burrows3cc0c372017-02-10 15:15:24 +000035
Simon Hunta6ae1212017-03-18 17:58:53 -070036 function (fs, ViewController, ss, ts) {
Steven Burrows3cc0c372017-02-10 15:15:24 +000037
38 var SpriteLayer = ViewController.extend({
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000039
Simon Hunt95f4b422017-03-03 13:49:05 -080040 id: 'topo2-sprites',
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000041 displayName: 'Sprite Layer',
42
Steven Burrowsb43c1a92017-03-07 17:13:28 +000043 init: function() {
44 this.svg = d3.select('#topo2');
Steven Burrows3cc0c372017-02-10 15:15:24 +000045 this.createSpriteDefs();
Steven Burrowsb43c1a92017-03-07 17:13:28 +000046 this.container = this.appendElement('#topo2-background', 'g');
Steven Burrows3cc0c372017-02-10 15:15:24 +000047 },
48 loadLayout: function (id) {
Steven Burrows68d6f952017-03-10 13:53:35 +000049 var _this = this;
Steven Burrows3cc0c372017-02-10 15:15:24 +000050 this.container.selectAll("*").remove();
51 this.layout = ss.layout(id);
Steven Burrows3db5ddb2017-02-17 15:21:20 +000052
53 this.width = this.layout.data.w;
54 this.height = this.layout.data.h;
55
Steven Burrows3cc0c372017-02-10 15:15:24 +000056 this.renderLayout();
Steven Burrows11162542017-02-27 21:52:56 +000057
58 if (fs.debugOn('sprite_grid')) {
59 this.renderGrid();
60 }
Steven Burrowsb43c1a92017-03-07 17:13:28 +000061
62 // Returns a promise for consistency with Topo2MapService
63 return new Promise(function(resolve) {
Steven Burrows68d6f952017-03-10 13:53:35 +000064 resolve(_this);
Steven Burrowsb43c1a92017-03-07 17:13:28 +000065 });
Steven Burrows3cc0c372017-02-10 15:15:24 +000066 },
67 createSpriteDefs: function () {
68 this.defs = this.svg.append('defs')
69 .attr('id', 'sprites');
70 },
Steven Burrows3db5ddb2017-02-17 15:21:20 +000071 getWidth: function () {
72 return this.width;
73 },
74 getHeight: function () {
75 return this.height;
76 },
Steven Burrows3cc0c372017-02-10 15:15:24 +000077 renderSprite: function (spriteData) {
78
79 var id = spriteData.sprite.data.id,
80 definition = d3.select('#' + id);
Simon Hunta6ae1212017-03-18 17:58:53 -070081 // is '#<id>' sufficient? namespace? '#t2spr-<id>' ?
Steven Burrows3cc0c372017-02-10 15:15:24 +000082
83 if (definition.empty()) {
84
85 var data = spriteData.sprite.data,
86 spriteEl = this.defs.append('symbol')
87 .attr('viewBox', vbox(data.w, data.h))
88 .attr('id', id);
89
Simon Hunta6ae1212017-03-18 17:58:53 -070090 // TODO: add elements to sprite in the order defined
91 // when the sprite was created (layered bottom up)
92 // e.g. (1) rect, (2) path, (3) rect...
93 // not in groups by type.
94 // For now, assume rectangles are behind paths...
Steven Burrows3cc0c372017-02-10 15:15:24 +000095
Simon Hunta6ae1212017-03-18 17:58:53 -070096 // draw rectangles before paths
Steven Burrows3cc0c372017-02-10 15:15:24 +000097 _.each(spriteData.sprite.rects, function (rect) {
98 spriteEl.append('rect')
99 .attr('width', rect.w)
100 .attr('height', rect.h)
101 .attr('x', rect.x)
102 .attr('y', rect.y)
Simon Hunta6ae1212017-03-18 17:58:53 -0700103 .style('fill', ts.spriteColor(rect.o.fill, 'fill'))
104 .style('stroke', ts.spriteColor(rect.o.stroke, 'stroke'));
105 });
106
107 // draw paths after rectangles
108 _.each(spriteData.sprite.paths, function (path) {
109 spriteEl.append('path')
110 .attr('d', path.d)
111 .style('fill', ts.spriteColor(path.o.fill, 'fill'))
112 .style('stroke', ts.spriteColor(path.o.stroke, 'stroke'));
Steven Burrows3cc0c372017-02-10 15:15:24 +0000113 });
114 }
115
Simon Hunta6ae1212017-03-18 17:58:53 -0700116 return spriteEl;
Steven Burrows3cc0c372017-02-10 15:15:24 +0000117 },
118 renderLayout: function () {
119
120 var _this = this;
Steven Burrows3cc0c372017-02-10 15:15:24 +0000121 var layout = this.container.append('svg')
122 .attr('class', layout)
123 .attr('viewBox', vbox(this.layout.data.w, this.layout.data.h));
124
125 _.each(this.layout.sprites, function (spriteData) {
126 _this.renderSprite(spriteData);
127
128 layout
129 .append('g')
130 .append("use")
131 .attr('xlink:href', '#rack')
132 .attr('width', 20)
133 .attr('height', 25)
134 .attr('x', spriteData.x)
135 .attr('y', spriteData.y);
136 });
Steven Burrows11162542017-02-27 21:52:56 +0000137 },
138 renderGrid: function () {
139
Steven Burrows11162542017-02-27 21:52:56 +0000140 var layout = this.container.select('svg').append('g');
141
142 var gridSpacing = 5,
143 x = this.width / gridSpacing,
Simon Hunta6ae1212017-03-18 17:58:53 -0700144 y = this.height / gridSpacing,
145 i, l;
Steven Burrows11162542017-02-27 21:52:56 +0000146
Simon Hunta6ae1212017-03-18 17:58:53 -0700147 for (i = 0, l = x; i < l; i++) {
Steven Burrows11162542017-02-27 21:52:56 +0000148 layout.append('rect')
149 .attr('width', 0.1)
150 .attr('height', this.height)
151 .attr('x', i * gridSpacing)
Simon Hunta6ae1212017-03-18 17:58:53 -0700152 .attr('y', 0);
Steven Burrows11162542017-02-27 21:52:56 +0000153 }
154
Simon Hunta6ae1212017-03-18 17:58:53 -0700155 for (i = 0, l = y; i < l; i++) {
Steven Burrows11162542017-02-27 21:52:56 +0000156 layout.append('rect')
157 .attr('height', 0.1)
158 .attr('width', this.width)
159 .attr('y', i * gridSpacing)
Simon Hunta6ae1212017-03-18 17:58:53 -0700160 .attr('x', 0);
Steven Burrows11162542017-02-27 21:52:56 +0000161 }
Steven Burrows3cc0c372017-02-10 15:15:24 +0000162 }
163 });
164
165 function getInstance() {
166 return instance || new SpriteLayer();
167 }
168
169 return getInstance();
170 }
171 ])
172 })();