blob: 0ab744b2c847355359afe781d6203eccba071f0d [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,
44 mapScale = prefs.mapscale,
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);
97 setUpMap();
98 opacifyMap(true);
99 }
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
123 function toggle(x) {
Steven Burrowsc7146612016-12-27 13:50:03 +0000124 return _togSvgLayer(x, mapG, 'bg', 'background map');
Steven Burrowse3a18842016-09-22 15:33:33 +0100125 }
126
Steven Burrowsd576f642016-09-26 10:40:51 -0700127 function openMapSelection() {
128
129 // TODO: Create a view class with extend method
130 MapSelectionDialog.prototype.currentMap = currentMap;
131
132 new MapSelectionDialog({
133 okHandler: function (preferences) {
134 setMap(preferences);
135 }
136 }).open();
137 }
138
Steven Burrows1c5c8612016-10-05 13:45:13 -0500139 function resetZoom() {
140 zoomer.reset();
141 }
142
Steven Burrowse3a18842016-09-22 15:33:33 +0100143 angular.module('ovTopo2')
144 .factory('Topo2MapService',
Steven Burrowsc7146612016-12-27 13:50:03 +0000145 ['$location', 'PrefsService', 'MapService', 'FlashService',
Steven Burrowsd576f642016-09-26 10:40:51 -0700146 'SvgUtilService', 'Topo2CountryFilters', 'Topo2MapDialog',
Steven Burrowsc7146612016-12-27 13:50:03 +0000147 function (_$loc_, _ps_, _ms_, _flash_, _sus_, _t2cf_, _t2md_) {
Steven Burrowse3a18842016-09-22 15:33:33 +0100148
Steven Burrowse3a18842016-09-22 15:33:33 +0100149 $loc = _$loc_;
Steven Burrowse3a18842016-09-22 15:33:33 +0100150 ps = _ps_;
151 ms = _ms_;
Steven Burrowsc7146612016-12-27 13:50:03 +0000152 flash = _flash_;
Steven Burrowse3a18842016-09-22 15:33:33 +0100153 sus = _sus_;
Steven Burrowsd576f642016-09-26 10:40:51 -0700154 countryFilters = _t2cf_;
155 MapSelectionDialog = _t2md_;
Steven Burrowse3a18842016-09-22 15:33:33 +0100156
157 return {
158 init: init,
159 openMapSelection: openMapSelection,
Steven Burrows1c5c8612016-10-05 13:45:13 -0500160 toggle: toggle,
161
162 resetZoom: resetZoom
Steven Burrowse3a18842016-09-22 15:33:33 +0100163 };
164 }
165 ]);
166
167})();