blob: ff595e77d8cc9df4662fd2c0be483fd941b6a8b7 [file] [log] [blame]
Steven Burrowse3a18842016-09-22 15:33:33 +01001/*
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 Map Module.
19 Defines behavior for loading geographical maps into the map layer.
20 */
21
22(function () {
23 'use strict';
24
Steven Burrowsd576f642016-09-26 10:40:51 -070025 // Injected Services
Steven Burrowsc7146612016-12-27 13:50:03 +000026 var $loc, ps, ms, flash, sus, countryFilters;
Steven Burrowsd576f642016-09-26 10:40:51 -070027
28 // Injected Classes
29 var MapSelectionDialog;
Steven Burrowse3a18842016-09-22 15:33:33 +010030
31 // internal state
Steven Burrows1c5c8612016-10-05 13:45:13 -050032 var mapG, zoomLayer, zoomer;
Steven Burrowse3a18842016-09-22 15:33:33 +010033
Steven Burrows1c5c8612016-10-05 13:45:13 -050034 function init(_zoomLayer_, _zoomer_) {
35 zoomLayer = _zoomLayer_;
36 zoomer = _zoomer_;
37 return setUpMap();
Steven Burrowse3a18842016-09-22 15:33:33 +010038 }
39
Steven Burrows1c5c8612016-10-05 13:45:13 -050040 function setUpMap() {
Steven Burrowse3a18842016-09-22 15:33:33 +010041 var prefs = currentMap(),
42 mapId = prefs.mapid,
43 mapFilePath = prefs.mapfilepath,
Steven Burrows86b74fc2017-02-22 00:15:16 +000044 mapScale = prefs.mapscale || 1,
Steven Burrowsd576f642016-09-26 10:40:51 -070045 loadMap = ms.loadMapInto,
Steven Burrowse3a18842016-09-22 15:33:33 +010046 promise, cfilter;
47
48 mapG = d3.select('#topo-map');
49
50 if (mapG.empty()) {
51 mapG = zoomLayer.append('g').attr('id', 'topo-map');
52 } else {
53 mapG.each(function (d, i) {
54 d3.selectAll(this.childNodes).remove();
55 });
56 }
57
Steven Burrowse3a18842016-09-22 15:33:33 +010058 if (mapFilePath === '*countries') {
Steven Burrowse3a18842016-09-22 15:33:33 +010059 cfilter = countryFilters[mapId] || countryFilters.uk;
Steven Burrowse7cc3082016-09-27 11:24:58 -070060 loadMap = ms.loadMapRegionInto;
Steven Burrowse3a18842016-09-22 15:33:33 +010061 }
62
Steven Burrowsd576f642016-09-26 10:40:51 -070063 promise = loadMap(mapG, mapFilePath, mapId, {
64 countryFilters: cfilter,
65 adjustScale: mapScale,
66 shading: ''
67 });
68
Steven Burrowsc7146612016-12-27 13:50:03 +000069 if (!ps.getPrefs('topo_prefs').bg) {
70 toggle(false);
71 }
72
Steven Burrowse3a18842016-09-22 15:33:33 +010073 return promise;
74 }
75
Steven Burrowse3a18842016-09-22 15:33:33 +010076 function currentMap() {
77 return ps.getPrefs(
78 'topo_mapid',
79 {
80 mapid: 'usa',
81 mapscale: 1,
82 mapfilepath: '*continental_us',
83 tint: 'off'
84 },
85 $loc.search()
86 );
87 }
88
Steven Burrowse3a18842016-09-22 15:33:33 +010089 function opacifyMap(b) {
90 mapG.transition()
91 .duration(1000)
92 .attr('opacity', b ? 1 : 0);
93 }
94
95 function setMap(map) {
96 ps.setPrefs('topo_mapid', map);
Steven Burrowse3a18842016-09-22 15:33:33 +010097 opacifyMap(true);
Steven Burrows86b74fc2017-02-22 00:15:16 +000098 return setUpMap();
Steven Burrowse3a18842016-09-22 15:33:33 +010099 }
100
Steven Burrowse3a18842016-09-22 15:33:33 +0100101 // TODO: -- START -- Move to dedicated module
102 var prefsState = {};
103
104 function updatePrefsState(what, b) {
105 prefsState[what] = b ? 1 : 0;
106 ps.setPrefs('topo_prefs', prefsState);
107 }
108
109 function _togSvgLayer(x, G, tag, what) {
Steven Burrowsc7146612016-12-27 13:50:03 +0000110 var on = (x === 'keyev') ? !sus.visible(G) : Boolean(x),
111 verb = on ? 'Show' : 'Hide';
Steven Burrowse3a18842016-09-22 15:33:33 +0100112 sus.visible(G, on);
113 updatePrefsState(tag, on);
Steven Burrowsc7146612016-12-27 13:50:03 +0000114
115 if (x === 'keyev') {
116 flash.flash(verb + ' ' + what);
117 }
118
119 return on;
Steven Burrowse3a18842016-09-22 15:33:33 +0100120 }
121 // TODO: -- END -- Move to dedicated module
122
Steven Burrows86b74fc2017-02-22 00:15:16 +0000123 function show() {
124 sus.visible(mapG, true);
125 }
126
127 function hide() {
128 sus.visible(mapG, false);
129 }
130
Steven Burrowse3a18842016-09-22 15:33:33 +0100131 function toggle(x) {
Steven Burrowsc7146612016-12-27 13:50:03 +0000132 return _togSvgLayer(x, mapG, 'bg', 'background map');
Steven Burrowse3a18842016-09-22 15:33:33 +0100133 }
134
Steven Burrowsd576f642016-09-26 10:40:51 -0700135 function openMapSelection() {
136
137 // TODO: Create a view class with extend method
138 MapSelectionDialog.prototype.currentMap = currentMap;
139
140 new MapSelectionDialog({
141 okHandler: function (preferences) {
142 setMap(preferences);
143 }
144 }).open();
145 }
146
Steven Burrows1c5c8612016-10-05 13:45:13 -0500147 function resetZoom() {
148 zoomer.reset();
149 }
150
Steven Burrowse3a18842016-09-22 15:33:33 +0100151 angular.module('ovTopo2')
152 .factory('Topo2MapService',
Steven Burrowsc7146612016-12-27 13:50:03 +0000153 ['$location', 'PrefsService', 'MapService', 'FlashService',
Steven Burrowsd576f642016-09-26 10:40:51 -0700154 'SvgUtilService', 'Topo2CountryFilters', 'Topo2MapDialog',
Steven Burrowsc7146612016-12-27 13:50:03 +0000155 function (_$loc_, _ps_, _ms_, _flash_, _sus_, _t2cf_, _t2md_) {
Steven Burrowse3a18842016-09-22 15:33:33 +0100156
Steven Burrowse3a18842016-09-22 15:33:33 +0100157 $loc = _$loc_;
Steven Burrowse3a18842016-09-22 15:33:33 +0100158 ps = _ps_;
159 ms = _ms_;
Steven Burrowsc7146612016-12-27 13:50:03 +0000160 flash = _flash_;
Steven Burrowse3a18842016-09-22 15:33:33 +0100161 sus = _sus_;
Steven Burrowsd576f642016-09-26 10:40:51 -0700162 countryFilters = _t2cf_;
163 MapSelectionDialog = _t2md_;
Steven Burrowse3a18842016-09-22 15:33:33 +0100164
165 return {
166 init: init,
Steven Burrows86b74fc2017-02-22 00:15:16 +0000167 setMap: setMap,
Steven Burrowse3a18842016-09-22 15:33:33 +0100168 openMapSelection: openMapSelection,
Steven Burrows86b74fc2017-02-22 00:15:16 +0000169
170 show: show,
171 hide: hide,
Steven Burrows1c5c8612016-10-05 13:45:13 -0500172 toggle: toggle,
173
174 resetZoom: resetZoom
Steven Burrowse3a18842016-09-22 15:33:33 +0100175 };
176 }
177 ]);
178
179})();