blob: 1701f6b19e40d297d0533edfbeec893a4fee6e15 [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({
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();
56 },
57 createSpriteDefs: function () {
58 this.defs = this.svg.append('defs')
59 .attr('id', 'sprites');
60 },
Steven Burrows3db5ddb2017-02-17 15:21:20 +000061 getWidth: function () {
62 return this.width;
63 },
64 getHeight: function () {
65 return this.height;
66 },
Steven Burrows3cc0c372017-02-10 15:15:24 +000067 renderSprite: function (spriteData) {
68
69 var id = spriteData.sprite.data.id,
70 definition = d3.select('#' + id);
71
72 if (definition.empty()) {
73
74 var data = spriteData.sprite.data,
75 spriteEl = this.defs.append('symbol')
76 .attr('viewBox', vbox(data.w, data.h))
77 .attr('id', id);
78
79 _.each(spriteData.sprite.paths, function (path) {
80 spriteEl.append('path')
81 .attr('d', path)
82 .style('fill', 'none')
83 .style('stroke', 'black');
84 });
85
86 _.each(spriteData.sprite.rects, function (rect) {
87 spriteEl.append('rect')
88 .attr('width', rect.w)
89 .attr('height', rect.h)
90 .attr('x', rect.x)
91 .attr('y', rect.y)
92 .style('fill', 'rgba(0,0,0,0.5)')
93 });
94 }
95
96 return spriteEl
97 },
98 renderLayout: function () {
99
100 var _this = this;
101
102 var layout = this.container.append('svg')
103 .attr('class', layout)
104 .attr('viewBox', vbox(this.layout.data.w, this.layout.data.h));
105
106 _.each(this.layout.sprites, function (spriteData) {
107 _this.renderSprite(spriteData);
108
109 layout
110 .append('g')
111 .append("use")
112 .attr('xlink:href', '#rack')
113 .attr('width', 20)
114 .attr('height', 25)
115 .attr('x', spriteData.x)
116 .attr('y', spriteData.y);
117 });
Steven Burrows3cc0c372017-02-10 15:15:24 +0000118 }
119 });
120
121 function getInstance() {
122 return instance || new SpriteLayer();
123 }
124
125 return getInstance();
126 }
127 ])
128 })();