blob: b3452c2446810aab8c4c8e25fa7d15abdd84a9cb [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', [
Steven Burrows11162542017-02-27 21:52:56 +000034 'FnService', 'Topo2ViewController', 'SpriteService',
Steven Burrows3cc0c372017-02-10 15:15:24 +000035
Steven Burrows11162542017-02-27 21:52:56 +000036 function (fs, ViewController, ss) {
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);
81
82 if (definition.empty()) {
83
84 var data = spriteData.sprite.data,
85 spriteEl = this.defs.append('symbol')
86 .attr('viewBox', vbox(data.w, data.h))
87 .attr('id', id);
88
89 _.each(spriteData.sprite.paths, function (path) {
90 spriteEl.append('path')
91 .attr('d', path)
92 .style('fill', 'none')
93 .style('stroke', 'black');
94 });
95
96 _.each(spriteData.sprite.rects, function (rect) {
97 spriteEl.append('rect')
98 .attr('width', rect.w)
99 .attr('height', rect.h)
100 .attr('x', rect.x)
101 .attr('y', rect.y)
102 .style('fill', 'rgba(0,0,0,0.5)')
103 });
104 }
105
106 return spriteEl
107 },
108 renderLayout: function () {
109
110 var _this = this;
Steven Burrows3cc0c372017-02-10 15:15:24 +0000111 var layout = this.container.append('svg')
112 .attr('class', layout)
113 .attr('viewBox', vbox(this.layout.data.w, this.layout.data.h));
114
115 _.each(this.layout.sprites, function (spriteData) {
116 _this.renderSprite(spriteData);
117
118 layout
119 .append('g')
120 .append("use")
121 .attr('xlink:href', '#rack')
122 .attr('width', 20)
123 .attr('height', 25)
124 .attr('x', spriteData.x)
125 .attr('y', spriteData.y);
126 });
Steven Burrows11162542017-02-27 21:52:56 +0000127 },
128 renderGrid: function () {
129
130
131 var layout = this.container.select('svg').append('g');
132
133 var gridSpacing = 5,
134 x = this.width / gridSpacing,
135 y = this.height / gridSpacing;
136
137 for (var i = 0, l = x; i < l; i++) {
138 layout.append('rect')
139 .attr('width', 0.1)
140 .attr('height', this.height)
141 .attr('x', i * gridSpacing)
142 .attr('y', 0)
143 }
144
145 for (var i = 0, l = y; i < l; i++) {
146 layout.append('rect')
147 .attr('height', 0.1)
148 .attr('width', this.width)
149 .attr('y', i * gridSpacing)
150 .attr('x', 0)
151 }
Steven Burrows3cc0c372017-02-10 15:15:24 +0000152 }
153 });
154
155 function getInstance() {
156 return instance || new SpriteLayer();
157 }
158
159 return getInstance();
160 }
161 ])
162 })();