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