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', [ |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 34 | 'FnService', 'Topo2ViewController', 'SpriteService', |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 35 | |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 36 | function (fs, ViewController, ss) { |
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); |
| 81 | |
| 82 | if (definition.empty()) { |
| 83 | |
| 84 | var data = spriteData.sprite.data, |
| 85 | spriteEl = this.defs.append('symbol') |
| 86 | .attr('viewBox', vbox(data.w, data.h)) |
| 87 | .attr('id', id); |
| 88 | |
| 89 | _.each(spriteData.sprite.paths, function (path) { |
| 90 | spriteEl.append('path') |
| 91 | .attr('d', path) |
| 92 | .style('fill', 'none') |
| 93 | .style('stroke', 'black'); |
| 94 | }); |
| 95 | |
| 96 | _.each(spriteData.sprite.rects, function (rect) { |
| 97 | spriteEl.append('rect') |
| 98 | .attr('width', rect.w) |
| 99 | .attr('height', rect.h) |
| 100 | .attr('x', rect.x) |
| 101 | .attr('y', rect.y) |
| 102 | .style('fill', 'rgba(0,0,0,0.5)') |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | return spriteEl |
| 107 | }, |
| 108 | renderLayout: function () { |
| 109 | |
| 110 | var _this = this; |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 111 | var layout = this.container.append('svg') |
| 112 | .attr('class', layout) |
| 113 | .attr('viewBox', vbox(this.layout.data.w, this.layout.data.h)); |
| 114 | |
| 115 | _.each(this.layout.sprites, function (spriteData) { |
| 116 | _this.renderSprite(spriteData); |
| 117 | |
| 118 | layout |
| 119 | .append('g') |
| 120 | .append("use") |
| 121 | .attr('xlink:href', '#rack') |
| 122 | .attr('width', 20) |
| 123 | .attr('height', 25) |
| 124 | .attr('x', spriteData.x) |
| 125 | .attr('y', spriteData.y); |
| 126 | }); |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 127 | }, |
| 128 | renderGrid: function () { |
| 129 | |
| 130 | |
| 131 | var layout = this.container.select('svg').append('g'); |
| 132 | |
| 133 | var gridSpacing = 5, |
| 134 | x = this.width / gridSpacing, |
| 135 | y = this.height / gridSpacing; |
| 136 | |
| 137 | for (var i = 0, l = x; i < l; i++) { |
| 138 | layout.append('rect') |
| 139 | .attr('width', 0.1) |
| 140 | .attr('height', this.height) |
| 141 | .attr('x', i * gridSpacing) |
| 142 | .attr('y', 0) |
| 143 | } |
| 144 | |
| 145 | for (var i = 0, l = y; i < l; i++) { |
| 146 | layout.append('rect') |
| 147 | .attr('height', 0.1) |
| 148 | .attr('width', this.width) |
| 149 | .attr('y', i * gridSpacing) |
| 150 | .attr('x', 0) |
| 151 | } |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 152 | } |
| 153 | }); |
| 154 | |
| 155 | function getInstance() { |
| 156 | return instance || new SpriteLayer(); |
| 157 | } |
| 158 | |
| 159 | return getInstance(); |
| 160 | } |
| 161 | ]) |
| 162 | })(); |