Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -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 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 22 | (function () { |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 23 | 'use strict'; |
| 24 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 25 | var instance; |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 26 | |
| 27 | function vbox(w, h) { |
| 28 | return '0 0 ' + w + ' ' + h; |
| 29 | } |
| 30 | |
| 31 | angular.module('ovTopo2') |
| 32 | .factory('Topo2SpriteLayerService', [ |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 33 | '$log', 'FnService', |
| 34 | 'Topo2ViewController', 'SpriteService', 'ThemeService', |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 35 | |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 36 | function ($log, 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 | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 43 | init: function () { |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 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 | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 50 | this.container.selectAll('*').remove(); |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 51 | this.layout = ss.layout(id); |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 52 | |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 53 | if (this.layout) { |
| 54 | this.width = this.layout.data.w; |
| 55 | this.height = this.layout.data.h; |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 56 | |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 57 | this.renderLayout(); |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 58 | |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 59 | if (fs.debugOn('sprite_grid')) { |
| 60 | this.renderGrid(); |
| 61 | } |
| 62 | } else { |
| 63 | $log.warn('no sprite layout registered:', id); |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 64 | } |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 65 | |
| 66 | // Returns a promise for consistency with Topo2MapService |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 67 | return new Promise(function (resolve) { |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 68 | resolve(_this); |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 69 | }); |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 70 | }, |
| 71 | createSpriteDefs: function () { |
| 72 | this.defs = this.svg.append('defs') |
| 73 | .attr('id', 'sprites'); |
| 74 | }, |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 75 | getWidth: function () { |
| 76 | return this.width; |
| 77 | }, |
| 78 | getHeight: function () { |
| 79 | return this.height; |
| 80 | }, |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 81 | renderSprite: function (spriteData) { |
| 82 | |
| 83 | var id = spriteData.sprite.data.id, |
| 84 | definition = d3.select('#' + id); |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame] | 85 | // is '#<id>' sufficient? namespace? '#t2spr-<id>' ? |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 86 | |
| 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 Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame] | 94 | // 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 Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 99 | |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame] | 100 | // draw rectangles before paths |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 101 | _.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 Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame] | 107 | .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 Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 117 | }); |
| 118 | } |
| 119 | |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame] | 120 | return spriteEl; |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 121 | }, |
| 122 | renderLayout: function () { |
| 123 | |
| 124 | var _this = this; |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 125 | 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 Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 134 | .append('use') |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 135 | .attr('xlink:href', '#rack') |
| 136 | .attr('width', 20) |
| 137 | .attr('height', 25) |
| 138 | .attr('x', spriteData.x) |
| 139 | .attr('y', spriteData.y); |
| 140 | }); |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 141 | }, |
| 142 | renderGrid: function () { |
| 143 | |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 144 | var layout = this.container.select('svg').append('g'); |
| 145 | |
| 146 | var gridSpacing = 5, |
| 147 | x = this.width / gridSpacing, |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame] | 148 | y = this.height / gridSpacing, |
| 149 | i, l; |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 150 | |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame] | 151 | for (i = 0, l = x; i < l; i++) { |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 152 | layout.append('rect') |
| 153 | .attr('width', 0.1) |
| 154 | .attr('height', this.height) |
| 155 | .attr('x', i * gridSpacing) |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame] | 156 | .attr('y', 0); |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame] | 159 | for (i = 0, l = y; i < l; i++) { |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 160 | layout.append('rect') |
| 161 | .attr('height', 0.1) |
| 162 | .attr('width', this.width) |
| 163 | .attr('y', i * gridSpacing) |
Simon Hunt | a6ae121 | 2017-03-18 17:58:53 -0700 | [diff] [blame] | 164 | .attr('x', 0); |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 165 | } |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 166 | }, |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 167 | }); |
| 168 | |
| 169 | function getInstance() { |
| 170 | return instance || new SpriteLayer(); |
| 171 | } |
| 172 | |
| 173 | return getInstance(); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 174 | }, |
| 175 | ]); |
| 176 | })(); |