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', [ |
| 34 | 'Topo2ViewController', 'SpriteService', |
| 35 | |
| 36 | function (ViewController, ss) { |
| 37 | |
| 38 | var SpriteLayer = ViewController.extend({ |
| 39 | init: function(svg, zoomLayer) { |
| 40 | this.svg = svg; |
| 41 | this.createSpriteDefs(); |
| 42 | this.container = zoomLayer.append('g').attr('id', 'topo-sprites'); |
| 43 | }, |
| 44 | loadLayout: function (id) { |
| 45 | this.container.selectAll("*").remove(); |
| 46 | this.layout = ss.layout(id); |
| 47 | this.renderLayout(); |
| 48 | }, |
| 49 | createSpriteDefs: function () { |
| 50 | this.defs = this.svg.append('defs') |
| 51 | .attr('id', 'sprites'); |
| 52 | }, |
| 53 | |
| 54 | renderSprite: function (spriteData) { |
| 55 | |
| 56 | var id = spriteData.sprite.data.id, |
| 57 | definition = d3.select('#' + id); |
| 58 | |
| 59 | if (definition.empty()) { |
| 60 | |
| 61 | var data = spriteData.sprite.data, |
| 62 | spriteEl = this.defs.append('symbol') |
| 63 | .attr('viewBox', vbox(data.w, data.h)) |
| 64 | .attr('id', id); |
| 65 | |
| 66 | _.each(spriteData.sprite.paths, function (path) { |
| 67 | spriteEl.append('path') |
| 68 | .attr('d', path) |
| 69 | .style('fill', 'none') |
| 70 | .style('stroke', 'black'); |
| 71 | }); |
| 72 | |
| 73 | _.each(spriteData.sprite.rects, function (rect) { |
| 74 | spriteEl.append('rect') |
| 75 | .attr('width', rect.w) |
| 76 | .attr('height', rect.h) |
| 77 | .attr('x', rect.x) |
| 78 | .attr('y', rect.y) |
| 79 | .style('fill', 'rgba(0,0,0,0.5)') |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | return spriteEl |
| 84 | }, |
| 85 | renderLayout: function () { |
| 86 | |
| 87 | var _this = this; |
| 88 | |
| 89 | var layout = this.container.append('svg') |
| 90 | .attr('class', layout) |
| 91 | .attr('viewBox', vbox(this.layout.data.w, this.layout.data.h)); |
| 92 | |
| 93 | _.each(this.layout.sprites, function (spriteData) { |
| 94 | _this.renderSprite(spriteData); |
| 95 | |
| 96 | layout |
| 97 | .append('g') |
| 98 | .append("use") |
| 99 | .attr('xlink:href', '#rack') |
| 100 | .attr('width', 20) |
| 101 | .attr('height', 25) |
| 102 | .attr('x', spriteData.x) |
| 103 | .attr('y', spriteData.y); |
| 104 | }); |
| 105 | }, |
| 106 | |
| 107 | hide: function () { |
| 108 | if (this.visible) { |
| 109 | this.container |
| 110 | .style('opacity', 1) |
| 111 | .transition() |
| 112 | .duration(400) |
| 113 | .style('opacity', 0); |
| 114 | } |
| 115 | this.visible = false; |
| 116 | }, |
| 117 | show: function () { |
| 118 | if (!this.visible) { |
| 119 | this.container |
| 120 | .style('opacity', 0) |
| 121 | .transition() |
| 122 | .duration(400) |
| 123 | .style('opacity', 1); |
| 124 | } |
| 125 | this.visible = true; |
| 126 | } |
| 127 | }); |
| 128 | |
| 129 | function getInstance() { |
| 130 | return instance || new SpriteLayer(); |
| 131 | } |
| 132 | |
| 133 | return getInstance(); |
| 134 | } |
| 135 | ]) |
| 136 | })(); |