Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 1 | /* |
| 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', [ |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame^] | 34 | 'FnService', 'Topo2ViewController', 'SpriteService', 'ThemeService', |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 35 | |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame^] | 36 | function (fs, ViewController, ss, ts) { |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 37 | |
| 38 | var SpriteLayer = ViewController.extend({ |
Steven Burrows | ea1d1ec | 2017-02-23 15:39:25 +0000 | [diff] [blame] | 39 | |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 40 | id: 'topo2-sprites', |
Steven Burrows | ea1d1ec | 2017-02-23 15:39:25 +0000 | [diff] [blame] | 41 | displayName: 'Sprite Layer', |
| 42 | |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 43 | init: function() { |
| 44 | this.svg = d3.select('#topo2'); |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 45 | this.createSpriteDefs(); |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 46 | this.container = this.appendElement('#topo2-background', 'g'); |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 47 | }, |
| 48 | loadLayout: function (id) { |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 49 | var _this = this; |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 50 | this.container.selectAll("*").remove(); |
| 51 | this.layout = ss.layout(id); |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 52 | |
| 53 | this.width = this.layout.data.w; |
| 54 | this.height = this.layout.data.h; |
| 55 | |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 56 | this.renderLayout(); |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 57 | |
| 58 | if (fs.debugOn('sprite_grid')) { |
| 59 | this.renderGrid(); |
| 60 | } |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 61 | |
| 62 | // Returns a promise for consistency with Topo2MapService |
| 63 | return new Promise(function(resolve) { |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 64 | resolve(_this); |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 65 | }); |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 66 | }, |
| 67 | createSpriteDefs: function () { |
| 68 | this.defs = this.svg.append('defs') |
| 69 | .attr('id', 'sprites'); |
| 70 | }, |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 71 | getWidth: function () { |
| 72 | return this.width; |
| 73 | }, |
| 74 | getHeight: function () { |
| 75 | return this.height; |
| 76 | }, |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 77 | renderSprite: function (spriteData) { |
| 78 | |
| 79 | var id = spriteData.sprite.data.id, |
| 80 | definition = d3.select('#' + id); |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame^] | 81 | // is '#<id>' sufficient? namespace? '#t2spr-<id>' ? |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 82 | |
| 83 | if (definition.empty()) { |
| 84 | |
| 85 | var data = spriteData.sprite.data, |
| 86 | spriteEl = this.defs.append('symbol') |
| 87 | .attr('viewBox', vbox(data.w, data.h)) |
| 88 | .attr('id', id); |
| 89 | |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame^] | 90 | // TODO: add elements to sprite in the order defined |
| 91 | // when the sprite was created (layered bottom up) |
| 92 | // e.g. (1) rect, (2) path, (3) rect... |
| 93 | // not in groups by type. |
| 94 | // For now, assume rectangles are behind paths... |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 95 | |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame^] | 96 | // draw rectangles before paths |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 97 | _.each(spriteData.sprite.rects, function (rect) { |
| 98 | spriteEl.append('rect') |
| 99 | .attr('width', rect.w) |
| 100 | .attr('height', rect.h) |
| 101 | .attr('x', rect.x) |
| 102 | .attr('y', rect.y) |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame^] | 103 | .style('fill', ts.spriteColor(rect.o.fill, 'fill')) |
| 104 | .style('stroke', ts.spriteColor(rect.o.stroke, 'stroke')); |
| 105 | }); |
| 106 | |
| 107 | // draw paths after rectangles |
| 108 | _.each(spriteData.sprite.paths, function (path) { |
| 109 | spriteEl.append('path') |
| 110 | .attr('d', path.d) |
| 111 | .style('fill', ts.spriteColor(path.o.fill, 'fill')) |
| 112 | .style('stroke', ts.spriteColor(path.o.stroke, 'stroke')); |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 113 | }); |
| 114 | } |
| 115 | |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame^] | 116 | return spriteEl; |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 117 | }, |
| 118 | renderLayout: function () { |
| 119 | |
| 120 | var _this = this; |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 121 | var layout = this.container.append('svg') |
| 122 | .attr('class', layout) |
| 123 | .attr('viewBox', vbox(this.layout.data.w, this.layout.data.h)); |
| 124 | |
| 125 | _.each(this.layout.sprites, function (spriteData) { |
| 126 | _this.renderSprite(spriteData); |
| 127 | |
| 128 | layout |
| 129 | .append('g') |
| 130 | .append("use") |
| 131 | .attr('xlink:href', '#rack') |
| 132 | .attr('width', 20) |
| 133 | .attr('height', 25) |
| 134 | .attr('x', spriteData.x) |
| 135 | .attr('y', spriteData.y); |
| 136 | }); |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 137 | }, |
| 138 | renderGrid: function () { |
| 139 | |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 140 | var layout = this.container.select('svg').append('g'); |
| 141 | |
| 142 | var gridSpacing = 5, |
| 143 | x = this.width / gridSpacing, |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame^] | 144 | y = this.height / gridSpacing, |
| 145 | i, l; |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 146 | |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame^] | 147 | for (i = 0, l = x; i < l; i++) { |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 148 | layout.append('rect') |
| 149 | .attr('width', 0.1) |
| 150 | .attr('height', this.height) |
| 151 | .attr('x', i * gridSpacing) |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame^] | 152 | .attr('y', 0); |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame^] | 155 | for (i = 0, l = y; i < l; i++) { |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 156 | layout.append('rect') |
| 157 | .attr('height', 0.1) |
| 158 | .attr('width', this.width) |
| 159 | .attr('y', i * gridSpacing) |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame^] | 160 | .attr('x', 0); |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 161 | } |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 162 | } |
| 163 | }); |
| 164 | |
| 165 | function getInstance() { |
| 166 | return instance || new SpriteLayer(); |
| 167 | } |
| 168 | |
| 169 | return getInstance(); |
| 170 | } |
| 171 | ]) |
| 172 | })(); |