blob: 7aee3ed45c752c939ee38ecc75ce4ad2b497686f [file] [log] [blame]
Steven Burrowsb43c1a92017-03-07 17:13:28 +00001/*
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 Burrowse9e45712017-03-24 12:39:14 +000028 function getZoom(z, useCfg) {
Simon Huntf0c6f542017-03-22 18:31:18 -070029 var u = z.usr,
30 c = z.cfg;
Steven Burrowse9e45712017-03-24 12:39:14 +000031 return (u && !u.useCfg && !useCfg) ? u : c;
Simon Huntf0c6f542017-03-22 18:31:18 -070032 }
33
34 // returns the pan (offset) values as an array [x, y]
Steven Burrowse9e45712017-03-24 12:39:14 +000035 function zoomPan(z, useCfg) {
36 var zoom = getZoom(z, useCfg);
Simon Huntf0c6f542017-03-22 18:31:18 -070037 return [zoom.offsetX, zoom.offsetY];
38 }
39
40 // returns the scale value
Steven Burrowse9e45712017-03-24 12:39:14 +000041 function zoomScale(z, useCfg) {
42 var zoom = getZoom(z, useCfg);
Simon Huntf0c6f542017-03-22 18:31:18 -070043 return zoom.scale;
44 }
45
Steven Burrowsb43c1a92017-03-07 17:13:28 +000046 angular.module('ovTopo2')
47 .factory('Topo2BackgroundService', [
48 '$log', 'Topo2ViewController', 'Topo2SpriteLayerService', 'Topo2MapService',
Steven Burrows3bbd9af2017-03-16 14:44:14 +000049 'Topo2MapConfigService', 'Topo2ZoomService',
50 function (_$log_, ViewController, t2sls, t2ms, t2mcs, t2zs) {
Steven Burrowsb43c1a92017-03-07 17:13:28 +000051
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 Burrowse8a455a2017-03-16 16:58:59 +000064 this.zoomer = t2zs.getZoomer();
Steven Burrowsb43c1a92017-03-07 17:13:28 +000065 },
66 addLayout: function (data) {
Steven Burrowsb43c1a92017-03-07 17:13:28 +000067
Simon Huntb3656d42017-04-07 18:15:35 -070068 var oldBgType = this.bgType,
69 oldBgId = this.bgId;
70
Steven Burrows68d6f952017-03-10 13:53:35 +000071 this.background = data;
72 this.bgType = data.bgType;
Simon Huntb3656d42017-04-07 18:15:35 -070073 this.bgId = data.bgId;
Simon Huntf0c6f542017-03-22 18:31:18 -070074 this.zoomData = data.bgZoom;
Steven Burrowse8a455a2017-03-16 16:58:59 +000075
76 var _this = this,
Simon Huntf7e7d4a2017-03-24 15:22:20 -070077 pan = zoomPan(this.zoomData),
78 scale = zoomScale(this.zoomData);
Steven Burrows68d6f952017-03-10 13:53:35 +000079
Simon Huntb3656d42017-04-07 18:15:35 -070080 // 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 Burrows68d6f952017-03-10 13:53:35 +000088 if (this.bgType === 'geo') {
Steven Burrowsb43c1a92017-03-07 17:13:28 +000089
90 // Hide Sprite Layer and show Map
91 t2sls.hide();
92 t2ms.show();
93
Steven Burrows7d1efef2017-03-25 02:07:43 +000094 t2ms.setUpMap(data.bgId, data.bgFilePath)
Steven Burrowsb43c1a92017-03-07 17:13:28 +000095 .then(function (proj) {
Steven Burrowsb43c1a92017-03-07 17:13:28 +000096 t2mcs.projection(proj);
Steven Burrowsb43c1a92017-03-07 17:13:28 +000097 $log.debug('** We installed the projection:', proj);
Steven Burrows68d6f952017-03-10 13:53:35 +000098 _this.region.loaded('bgRendered', true);
Simon Huntf7e7d4a2017-03-24 15:22:20 -070099 t2zs.panAndZoom(pan, scale, 1000);
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000100 });
Steven Burrowse8a455a2017-03-16 16:58:59 +0000101 } else if (this.bgType === 'grid') {
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000102
103 // Hide Sprite Layer and show Map
104 t2ms.hide();
105 t2sls.show();
106
Steven Burrows68d6f952017-03-10 13:53:35 +0000107 t2sls.loadLayout(data.bgId).then(function (spriteLayout) {
108 _this.background.layout = spriteLayout;
109 _this.region.loaded('bgRendered', true);
Simon Huntf7e7d4a2017-03-24 15:22:20 -0700110 t2zs.panAndZoom(pan, scale, 1000);
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000111 });
Steven Burrowse8a455a2017-03-16 16:58:59 +0000112 } else {
113 // No background type - Tell the region the background is ready for placing nodes
114 t2ms.hide();
115 t2sls.hide();
Simon Huntb3656d42017-04-07 18:15:35 -0700116
117 // TODO: don't just use previous layout's pan/zoom settings!
Steven Burrowse8a455a2017-03-16 16:58:59 +0000118 // _this.region.loaded('bgRendered', true);
119 // t2zs.panAndZoom(pan, _this.background.bgZoomScale, 1000);
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000120 }
Steven Burrows68d6f952017-03-10 13:53:35 +0000121 },
122 getBackgroundType: function () {
123 return this.bgType;
Steven Burrows3bbd9af2017-03-16 14:44:14 +0000124 },
125 resetZoom: function () {
Steven Burrowse9e45712017-03-24 12:39:14 +0000126 var pan = zoomPan(this.zoomData, true);
127 t2zs.panAndZoom(pan, zoomScale(this.zoomData, true), 1000);
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000128 }
129 });
130
Simon Huntf0c6f542017-03-22 18:31:18 -0700131 return instance || new BackgroundView();
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000132 }
133 ]);
134})();