Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 Background Module. |
| 19 | Module that maintains the map and sprite layers |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | |
| 24 | var $log; |
| 25 | |
| 26 | var instance; |
| 27 | |
Steven Burrows | e9e4571 | 2017-03-24 12:39:14 +0000 | [diff] [blame] | 28 | function getZoom(z, useCfg) { |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 29 | var u = z.usr, |
| 30 | c = z.cfg; |
Steven Burrows | e9e4571 | 2017-03-24 12:39:14 +0000 | [diff] [blame] | 31 | return (u && !u.useCfg && !useCfg) ? u : c; |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // returns the pan (offset) values as an array [x, y] |
Steven Burrows | e9e4571 | 2017-03-24 12:39:14 +0000 | [diff] [blame] | 35 | function zoomPan(z, useCfg) { |
| 36 | var zoom = getZoom(z, useCfg); |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 37 | return [zoom.offsetX, zoom.offsetY]; |
| 38 | } |
| 39 | |
| 40 | // returns the scale value |
Steven Burrows | e9e4571 | 2017-03-24 12:39:14 +0000 | [diff] [blame] | 41 | function zoomScale(z, useCfg) { |
| 42 | var zoom = getZoom(z, useCfg); |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 43 | return zoom.scale; |
| 44 | } |
| 45 | |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 46 | angular.module('ovTopo2') |
| 47 | .factory('Topo2BackgroundService', [ |
| 48 | '$log', 'Topo2ViewController', 'Topo2SpriteLayerService', 'Topo2MapService', |
Steven Burrows | 3bbd9af | 2017-03-16 14:44:14 +0000 | [diff] [blame] | 49 | 'Topo2MapConfigService', 'Topo2ZoomService', |
| 50 | function (_$log_, ViewController, t2sls, t2ms, t2mcs, t2zs) { |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 51 | |
| 52 | $log = _$log_; |
| 53 | |
| 54 | var BackgroundView = ViewController.extend({ |
| 55 | |
| 56 | id: 'topo2-background', |
| 57 | displayName: 'Background', |
| 58 | |
| 59 | init: function () { |
| 60 | instance = this; |
| 61 | this.appendElement('#topo2-zoomlayer', 'g'); |
| 62 | t2sls.init(); |
| 63 | t2ms.init(); |
Steven Burrows | e8a455a | 2017-03-16 16:58:59 +0000 | [diff] [blame] | 64 | this.zoomer = t2zs.getZoomer(); |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 65 | }, |
| 66 | addLayout: function (data) { |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 67 | |
Simon Hunt | b3656d4 | 2017-04-07 18:15:35 -0700 | [diff] [blame^] | 68 | var oldBgType = this.bgType, |
| 69 | oldBgId = this.bgId; |
| 70 | |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 71 | this.background = data; |
| 72 | this.bgType = data.bgType; |
Simon Hunt | b3656d4 | 2017-04-07 18:15:35 -0700 | [diff] [blame^] | 73 | this.bgId = data.bgId; |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 74 | this.zoomData = data.bgZoom; |
Steven Burrows | e8a455a | 2017-03-16 16:58:59 +0000 | [diff] [blame] | 75 | |
| 76 | var _this = this, |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 77 | pan = zoomPan(this.zoomData), |
| 78 | scale = zoomScale(this.zoomData); |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 79 | |
Simon Hunt | b3656d4 | 2017-04-07 18:15:35 -0700 | [diff] [blame^] | 80 | // TODO: test for same background... avoid reload on same |
| 81 | |
| 82 | // Avoid re-loading the background / sprite layer |
| 83 | // if the type and ID are same as last time. |
| 84 | |
| 85 | // For example, europe map can be panned/zoomed to |
| 86 | // focus on different countries |
| 87 | |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 88 | if (this.bgType === 'geo') { |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 89 | |
| 90 | // Hide Sprite Layer and show Map |
| 91 | t2sls.hide(); |
| 92 | t2ms.show(); |
| 93 | |
Steven Burrows | 7d1efef | 2017-03-25 02:07:43 +0000 | [diff] [blame] | 94 | t2ms.setUpMap(data.bgId, data.bgFilePath) |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 95 | .then(function (proj) { |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 96 | t2mcs.projection(proj); |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 97 | $log.debug('** We installed the projection:', proj); |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 98 | _this.region.loaded('bgRendered', true); |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 99 | t2zs.panAndZoom(pan, scale, 1000); |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 100 | }); |
Steven Burrows | e8a455a | 2017-03-16 16:58:59 +0000 | [diff] [blame] | 101 | } else if (this.bgType === 'grid') { |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 102 | |
| 103 | // Hide Sprite Layer and show Map |
| 104 | t2ms.hide(); |
| 105 | t2sls.show(); |
| 106 | |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 107 | t2sls.loadLayout(data.bgId).then(function (spriteLayout) { |
| 108 | _this.background.layout = spriteLayout; |
| 109 | _this.region.loaded('bgRendered', true); |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 110 | t2zs.panAndZoom(pan, scale, 1000); |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 111 | }); |
Steven Burrows | e8a455a | 2017-03-16 16:58:59 +0000 | [diff] [blame] | 112 | } else { |
| 113 | // No background type - Tell the region the background is ready for placing nodes |
| 114 | t2ms.hide(); |
| 115 | t2sls.hide(); |
Simon Hunt | b3656d4 | 2017-04-07 18:15:35 -0700 | [diff] [blame^] | 116 | |
| 117 | // TODO: don't just use previous layout's pan/zoom settings! |
Steven Burrows | e8a455a | 2017-03-16 16:58:59 +0000 | [diff] [blame] | 118 | // _this.region.loaded('bgRendered', true); |
| 119 | // t2zs.panAndZoom(pan, _this.background.bgZoomScale, 1000); |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 120 | } |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 121 | }, |
| 122 | getBackgroundType: function () { |
| 123 | return this.bgType; |
Steven Burrows | 3bbd9af | 2017-03-16 14:44:14 +0000 | [diff] [blame] | 124 | }, |
| 125 | resetZoom: function () { |
Steven Burrows | e9e4571 | 2017-03-24 12:39:14 +0000 | [diff] [blame] | 126 | var pan = zoomPan(this.zoomData, true); |
| 127 | t2zs.panAndZoom(pan, zoomScale(this.zoomData, true), 1000); |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 128 | } |
| 129 | }); |
| 130 | |
Simon Hunt | f0c6f54 | 2017-03-22 18:31:18 -0700 | [diff] [blame] | 131 | return instance || new BackgroundView(); |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 132 | } |
| 133 | ]); |
| 134 | })(); |