blob: 6de99fe313b3328259f56934af90b40cf9020dc8 [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) {
49 this.container.selectAll("*").remove();
50 this.layout = ss.layout(id);
Steven Burrows3db5ddb2017-02-17 15:21:20 +000051
52 this.width = this.layout.data.w;
53 this.height = this.layout.data.h;
54
Steven Burrows3cc0c372017-02-10 15:15:24 +000055 this.renderLayout();
Steven Burrows11162542017-02-27 21:52:56 +000056
57 if (fs.debugOn('sprite_grid')) {
58 this.renderGrid();
59 }
Steven Burrowsb43c1a92017-03-07 17:13:28 +000060
61 // Returns a promise for consistency with Topo2MapService
62 return new Promise(function(resolve) {
63 resolve();
64 });
Steven Burrows3cc0c372017-02-10 15:15:24 +000065 },
66 createSpriteDefs: function () {
67 this.defs = this.svg.append('defs')
68 .attr('id', 'sprites');
69 },
Steven Burrows3db5ddb2017-02-17 15:21:20 +000070 getWidth: function () {
71 return this.width;
72 },
73 getHeight: function () {
74 return this.height;
75 },
Steven Burrows3cc0c372017-02-10 15:15:24 +000076 renderSprite: function (spriteData) {
77
78 var id = spriteData.sprite.data.id,
79 definition = d3.select('#' + id);
80
81 if (definition.empty()) {
82
83 var data = spriteData.sprite.data,
84 spriteEl = this.defs.append('symbol')
85 .attr('viewBox', vbox(data.w, data.h))
86 .attr('id', id);
87
88 _.each(spriteData.sprite.paths, function (path) {
89 spriteEl.append('path')
90 .attr('d', path)
91 .style('fill', 'none')
92 .style('stroke', 'black');
93 });
94
95 _.each(spriteData.sprite.rects, function (rect) {
96 spriteEl.append('rect')
97 .attr('width', rect.w)
98 .attr('height', rect.h)
99 .attr('x', rect.x)
100 .attr('y', rect.y)
101 .style('fill', 'rgba(0,0,0,0.5)')
102 });
103 }
104
105 return spriteEl
106 },
107 renderLayout: function () {
108
109 var _this = this;
Steven Burrows3cc0c372017-02-10 15:15:24 +0000110 var layout = this.container.append('svg')
111 .attr('class', layout)
112 .attr('viewBox', vbox(this.layout.data.w, this.layout.data.h));
113
114 _.each(this.layout.sprites, function (spriteData) {
115 _this.renderSprite(spriteData);
116
117 layout
118 .append('g')
119 .append("use")
120 .attr('xlink:href', '#rack')
121 .attr('width', 20)
122 .attr('height', 25)
123 .attr('x', spriteData.x)
124 .attr('y', spriteData.y);
125 });
Steven Burrows11162542017-02-27 21:52:56 +0000126 },
127 renderGrid: function () {
128
129
130 var layout = this.container.select('svg').append('g');
131
132 var gridSpacing = 5,
133 x = this.width / gridSpacing,
134 y = this.height / gridSpacing;
135
136 for (var i = 0, l = x; i < l; i++) {
137 layout.append('rect')
138 .attr('width', 0.1)
139 .attr('height', this.height)
140 .attr('x', i * gridSpacing)
141 .attr('y', 0)
142 }
143
144 for (var i = 0, l = y; i < l; i++) {
145 layout.append('rect')
146 .attr('height', 0.1)
147 .attr('width', this.width)
148 .attr('y', i * gridSpacing)
149 .attr('x', 0)
150 }
Steven Burrows3cc0c372017-02-10 15:15:24 +0000151 }
152 });
153
154 function getInstance() {
155 return instance || new SpriteLayer();
156 }
157
158 return getInstance();
159 }
160 ])
161 })();