blob: 411d13071662ea8bf916332562e476209efbd804 [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', [
34 'Topo2ViewController', 'SpriteService',
35
36 function (ViewController, ss) {
37
38 var SpriteLayer = ViewController.extend({
39 init: function(svg, zoomLayer) {
40 this.svg = svg;
41 this.createSpriteDefs();
42 this.container = zoomLayer.append('g').attr('id', 'topo-sprites');
43 },
44 loadLayout: function (id) {
45 this.container.selectAll("*").remove();
46 this.layout = ss.layout(id);
Steven Burrows3db5ddb2017-02-17 15:21:20 +000047
48 this.width = this.layout.data.w;
49 this.height = this.layout.data.h;
50
Steven Burrows3cc0c372017-02-10 15:15:24 +000051 this.renderLayout();
52 },
53 createSpriteDefs: function () {
54 this.defs = this.svg.append('defs')
55 .attr('id', 'sprites');
56 },
Steven Burrows3db5ddb2017-02-17 15:21:20 +000057 getWidth: function () {
58 return this.width;
59 },
60 getHeight: function () {
61 return this.height;
62 },
Steven Burrows3cc0c372017-02-10 15:15:24 +000063 renderSprite: function (spriteData) {
64
65 var id = spriteData.sprite.data.id,
66 definition = d3.select('#' + id);
67
68 if (definition.empty()) {
69
70 var data = spriteData.sprite.data,
71 spriteEl = this.defs.append('symbol')
72 .attr('viewBox', vbox(data.w, data.h))
73 .attr('id', id);
74
75 _.each(spriteData.sprite.paths, function (path) {
76 spriteEl.append('path')
77 .attr('d', path)
78 .style('fill', 'none')
79 .style('stroke', 'black');
80 });
81
82 _.each(spriteData.sprite.rects, function (rect) {
83 spriteEl.append('rect')
84 .attr('width', rect.w)
85 .attr('height', rect.h)
86 .attr('x', rect.x)
87 .attr('y', rect.y)
88 .style('fill', 'rgba(0,0,0,0.5)')
89 });
90 }
91
92 return spriteEl
93 },
94 renderLayout: function () {
95
96 var _this = this;
97
98 var layout = this.container.append('svg')
99 .attr('class', layout)
100 .attr('viewBox', vbox(this.layout.data.w, this.layout.data.h));
101
102 _.each(this.layout.sprites, function (spriteData) {
103 _this.renderSprite(spriteData);
104
105 layout
106 .append('g')
107 .append("use")
108 .attr('xlink:href', '#rack')
109 .attr('width', 20)
110 .attr('height', 25)
111 .attr('x', spriteData.x)
112 .attr('y', spriteData.y);
113 });
114 },
115
116 hide: function () {
117 if (this.visible) {
118 this.container
119 .style('opacity', 1)
120 .transition()
121 .duration(400)
122 .style('opacity', 0);
123 }
124 this.visible = false;
125 },
126 show: function () {
127 if (!this.visible) {
128 this.container
129 .style('opacity', 0)
130 .transition()
131 .duration(400)
132 .style('opacity', 1);
133 }
134 this.visible = true;
135 }
136 });
137
138 function getInstance() {
139 return instance || new SpriteLayer();
140 }
141
142 return getInstance();
143 }
144 ])
145 })();