blob: 8820a3084b4e3b3c4bbb1656b205180099da7a27 [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
40 id: 'topo-sprites',
41 displayName: 'Sprite Layer',
42
Steven Burrows3cc0c372017-02-10 15:15:24 +000043 init: function(svg, zoomLayer) {
44 this.svg = svg;
45 this.createSpriteDefs();
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000046 this.container = zoomLayer.append('g').attr('id', this.id);
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 Burrows3cc0c372017-02-10 15:15:24 +000060 },
61 createSpriteDefs: function () {
62 this.defs = this.svg.append('defs')
63 .attr('id', 'sprites');
64 },
Steven Burrows3db5ddb2017-02-17 15:21:20 +000065 getWidth: function () {
66 return this.width;
67 },
68 getHeight: function () {
69 return this.height;
70 },
Steven Burrows3cc0c372017-02-10 15:15:24 +000071 renderSprite: function (spriteData) {
72
73 var id = spriteData.sprite.data.id,
74 definition = d3.select('#' + id);
75
76 if (definition.empty()) {
77
78 var data = spriteData.sprite.data,
79 spriteEl = this.defs.append('symbol')
80 .attr('viewBox', vbox(data.w, data.h))
81 .attr('id', id);
82
83 _.each(spriteData.sprite.paths, function (path) {
84 spriteEl.append('path')
85 .attr('d', path)
86 .style('fill', 'none')
87 .style('stroke', 'black');
88 });
89
90 _.each(spriteData.sprite.rects, function (rect) {
91 spriteEl.append('rect')
92 .attr('width', rect.w)
93 .attr('height', rect.h)
94 .attr('x', rect.x)
95 .attr('y', rect.y)
96 .style('fill', 'rgba(0,0,0,0.5)')
97 });
98 }
99
100 return spriteEl
101 },
102 renderLayout: function () {
103
104 var _this = this;
Steven Burrows3cc0c372017-02-10 15:15:24 +0000105 var layout = this.container.append('svg')
106 .attr('class', layout)
107 .attr('viewBox', vbox(this.layout.data.w, this.layout.data.h));
108
109 _.each(this.layout.sprites, function (spriteData) {
110 _this.renderSprite(spriteData);
111
112 layout
113 .append('g')
114 .append("use")
115 .attr('xlink:href', '#rack')
116 .attr('width', 20)
117 .attr('height', 25)
118 .attr('x', spriteData.x)
119 .attr('y', spriteData.y);
120 });
Steven Burrows11162542017-02-27 21:52:56 +0000121 },
122 renderGrid: function () {
123
124
125 var layout = this.container.select('svg').append('g');
126
127 var gridSpacing = 5,
128 x = this.width / gridSpacing,
129 y = this.height / gridSpacing;
130
131 for (var i = 0, l = x; i < l; i++) {
132 layout.append('rect')
133 .attr('width', 0.1)
134 .attr('height', this.height)
135 .attr('x', i * gridSpacing)
136 .attr('y', 0)
137 }
138
139 for (var i = 0, l = y; i < l; i++) {
140 layout.append('rect')
141 .attr('height', 0.1)
142 .attr('width', this.width)
143 .attr('y', i * gridSpacing)
144 .attr('x', 0)
145 }
Steven Burrows3cc0c372017-02-10 15:15:24 +0000146 }
147 });
148
149 function getInstance() {
150 return instance || new SpriteLayer();
151 }
152
153 return getInstance();
154 }
155 ])
156 })();