blob: 539f80c631980eca3874eed9bb872f0e0e712561 [file] [log] [blame]
Steven Burrows3cc0c372017-02-10 15:15:24 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Steven Burrows3cc0c372017-02-10 15:15:24 +00003 *
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
Steven Burrows1c2a9682017-07-14 16:52:46 +010022 (function () {
Steven Burrows3cc0c372017-02-10 15:15:24 +000023 'use strict';
24
Steven Burrows1c2a9682017-07-14 16:52:46 +010025 var instance;
Steven Burrows3cc0c372017-02-10 15:15:24 +000026
27 function vbox(w, h) {
28 return '0 0 ' + w + ' ' + h;
29 }
30
31 angular.module('ovTopo2')
32 .factory('Topo2SpriteLayerService', [
Simon Hunt0ee20bf2017-05-10 19:59:17 -070033 '$log', 'FnService',
34 'Topo2ViewController', 'SpriteService', 'ThemeService',
Steven Burrows3cc0c372017-02-10 15:15:24 +000035
Simon Hunt0ee20bf2017-05-10 19:59:17 -070036 function ($log, fs, ViewController, ss, ts) {
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 Burrows1c2a9682017-07-14 16:52:46 +010043 init: function () {
Steven Burrowsb43c1a92017-03-07 17:13:28 +000044 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 Burrows1c2a9682017-07-14 16:52:46 +010050 this.container.selectAll('*').remove();
Steven Burrows3cc0c372017-02-10 15:15:24 +000051 this.layout = ss.layout(id);
Steven Burrows3db5ddb2017-02-17 15:21:20 +000052
Simon Hunt0ee20bf2017-05-10 19:59:17 -070053 if (this.layout) {
54 this.width = this.layout.data.w;
55 this.height = this.layout.data.h;
Steven Burrows3db5ddb2017-02-17 15:21:20 +000056
Simon Hunt0ee20bf2017-05-10 19:59:17 -070057 this.renderLayout();
Steven Burrows11162542017-02-27 21:52:56 +000058
Simon Hunt0ee20bf2017-05-10 19:59:17 -070059 if (fs.debugOn('sprite_grid')) {
60 this.renderGrid();
61 }
62 } else {
63 $log.warn('no sprite layout registered:', id);
Steven Burrows11162542017-02-27 21:52:56 +000064 }
Steven Burrowsb43c1a92017-03-07 17:13:28 +000065
66 // Returns a promise for consistency with Topo2MapService
Steven Burrows1c2a9682017-07-14 16:52:46 +010067 return new Promise(function (resolve) {
Steven Burrows68d6f952017-03-10 13:53:35 +000068 resolve(_this);
Steven Burrowsb43c1a92017-03-07 17:13:28 +000069 });
Steven Burrows3cc0c372017-02-10 15:15:24 +000070 },
71 createSpriteDefs: function () {
72 this.defs = this.svg.append('defs')
73 .attr('id', 'sprites');
74 },
Steven Burrows3db5ddb2017-02-17 15:21:20 +000075 getWidth: function () {
76 return this.width;
77 },
78 getHeight: function () {
79 return this.height;
80 },
Steven Burrows3cc0c372017-02-10 15:15:24 +000081 renderSprite: function (spriteData) {
82
83 var id = spriteData.sprite.data.id,
84 definition = d3.select('#' + id);
Simon Hunta6ae1212017-03-18 17:58:53 -070085 // is '#<id>' sufficient? namespace? '#t2spr-<id>' ?
Steven Burrows3cc0c372017-02-10 15:15:24 +000086
87 if (definition.empty()) {
88
89 var data = spriteData.sprite.data,
90 spriteEl = this.defs.append('symbol')
91 .attr('viewBox', vbox(data.w, data.h))
92 .attr('id', id);
93
Simon Hunta6ae1212017-03-18 17:58:53 -070094 // TODO: add elements to sprite in the order defined
95 // when the sprite was created (layered bottom up)
96 // e.g. (1) rect, (2) path, (3) rect...
97 // not in groups by type.
98 // For now, assume rectangles are behind paths...
Steven Burrows3cc0c372017-02-10 15:15:24 +000099
Simon Hunta6ae1212017-03-18 17:58:53 -0700100 // draw rectangles before paths
Steven Burrows3cc0c372017-02-10 15:15:24 +0000101 _.each(spriteData.sprite.rects, function (rect) {
102 spriteEl.append('rect')
103 .attr('width', rect.w)
104 .attr('height', rect.h)
105 .attr('x', rect.x)
106 .attr('y', rect.y)
Simon Hunta6ae1212017-03-18 17:58:53 -0700107 .style('fill', ts.spriteColor(rect.o.fill, 'fill'))
108 .style('stroke', ts.spriteColor(rect.o.stroke, 'stroke'));
109 });
110
111 // draw paths after rectangles
112 _.each(spriteData.sprite.paths, function (path) {
113 spriteEl.append('path')
114 .attr('d', path.d)
115 .style('fill', ts.spriteColor(path.o.fill, 'fill'))
116 .style('stroke', ts.spriteColor(path.o.stroke, 'stroke'));
Steven Burrows3cc0c372017-02-10 15:15:24 +0000117 });
118 }
119
Simon Hunta6ae1212017-03-18 17:58:53 -0700120 return spriteEl;
Steven Burrows3cc0c372017-02-10 15:15:24 +0000121 },
122 renderLayout: function () {
123
124 var _this = this;
Steven Burrows3cc0c372017-02-10 15:15:24 +0000125 var layout = this.container.append('svg')
126 .attr('class', layout)
127 .attr('viewBox', vbox(this.layout.data.w, this.layout.data.h));
128
129 _.each(this.layout.sprites, function (spriteData) {
130 _this.renderSprite(spriteData);
131
132 layout
133 .append('g')
Steven Burrows1c2a9682017-07-14 16:52:46 +0100134 .append('use')
Steven Burrows3cc0c372017-02-10 15:15:24 +0000135 .attr('xlink:href', '#rack')
136 .attr('width', 20)
137 .attr('height', 25)
138 .attr('x', spriteData.x)
139 .attr('y', spriteData.y);
140 });
Steven Burrows11162542017-02-27 21:52:56 +0000141 },
142 renderGrid: function () {
143
Steven Burrows11162542017-02-27 21:52:56 +0000144 var layout = this.container.select('svg').append('g');
145
146 var gridSpacing = 5,
147 x = this.width / gridSpacing,
Simon Hunta6ae1212017-03-18 17:58:53 -0700148 y = this.height / gridSpacing,
149 i, l;
Steven Burrows11162542017-02-27 21:52:56 +0000150
Simon Hunta6ae1212017-03-18 17:58:53 -0700151 for (i = 0, l = x; i < l; i++) {
Steven Burrows11162542017-02-27 21:52:56 +0000152 layout.append('rect')
153 .attr('width', 0.1)
154 .attr('height', this.height)
155 .attr('x', i * gridSpacing)
Simon Hunta6ae1212017-03-18 17:58:53 -0700156 .attr('y', 0);
Steven Burrows11162542017-02-27 21:52:56 +0000157 }
158
Simon Hunta6ae1212017-03-18 17:58:53 -0700159 for (i = 0, l = y; i < l; i++) {
Steven Burrows11162542017-02-27 21:52:56 +0000160 layout.append('rect')
161 .attr('height', 0.1)
162 .attr('width', this.width)
163 .attr('y', i * gridSpacing)
Simon Hunta6ae1212017-03-18 17:58:53 -0700164 .attr('x', 0);
Steven Burrows11162542017-02-27 21:52:56 +0000165 }
Steven Burrows1c2a9682017-07-14 16:52:46 +0100166 },
Steven Burrows3cc0c372017-02-10 15:15:24 +0000167 });
168
169 function getInstance() {
170 return instance || new SpriteLayer();
171 }
172
173 return getInstance();
Steven Burrows1c2a9682017-07-14 16:52:46 +0100174 },
175 ]);
176 })();