blob: 4ff21628f7a8b15c2016f8e8d6ac7729345d938e [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
26 var $loc, ps, ms, sus, countryFilters;
27
28 // Injected Classes
29 var MapSelectionDialog;
Steven Burrowse3a18842016-09-22 15:33:33 +010030
31 // internal state
Steven Burrowsd576f642016-09-26 10:40:51 -070032 var mapG;
Steven Burrowse3a18842016-09-22 15:33:33 +010033
34 function init(zoomLayer) {
Steven Burrowse3a18842016-09-22 15:33:33 +010035 return setUpMap(zoomLayer);
36 }
37
38 function setUpMap(zoomLayer) {
39 var prefs = currentMap(),
40 mapId = prefs.mapid,
41 mapFilePath = prefs.mapfilepath,
42 mapScale = prefs.mapscale,
Steven Burrowsd576f642016-09-26 10:40:51 -070043 loadMap = ms.loadMapInto,
Steven Burrowse3a18842016-09-22 15:33:33 +010044 promise, cfilter;
45
46 mapG = d3.select('#topo-map');
47
48 if (mapG.empty()) {
49 mapG = zoomLayer.append('g').attr('id', 'topo-map');
50 } else {
51 mapG.each(function (d, i) {
52 d3.selectAll(this.childNodes).remove();
53 });
54 }
55
Steven Burrowse3a18842016-09-22 15:33:33 +010056 if (mapFilePath === '*countries') {
Steven Burrowse3a18842016-09-22 15:33:33 +010057 cfilter = countryFilters[mapId] || countryFilters.uk;
Steven Burrowse7cc3082016-09-27 11:24:58 -070058 loadMap = ms.loadMapRegionInto;
Steven Burrowse3a18842016-09-22 15:33:33 +010059 }
60
Steven Burrowsd576f642016-09-26 10:40:51 -070061 promise = loadMap(mapG, mapFilePath, mapId, {
62 countryFilters: cfilter,
63 adjustScale: mapScale,
64 shading: ''
65 });
66
Steven Burrowse3a18842016-09-22 15:33:33 +010067 return promise;
68 }
69
Steven Burrowse3a18842016-09-22 15:33:33 +010070 function currentMap() {
71 return ps.getPrefs(
72 'topo_mapid',
73 {
74 mapid: 'usa',
75 mapscale: 1,
76 mapfilepath: '*continental_us',
77 tint: 'off'
78 },
79 $loc.search()
80 );
81 }
82
Steven Burrowse3a18842016-09-22 15:33:33 +010083 function opacifyMap(b) {
84 mapG.transition()
85 .duration(1000)
86 .attr('opacity', b ? 1 : 0);
87 }
88
89 function setMap(map) {
90 ps.setPrefs('topo_mapid', map);
91 setUpMap();
92 opacifyMap(true);
93 }
94
Steven Burrowse3a18842016-09-22 15:33:33 +010095 // TODO: -- START -- Move to dedicated module
96 var prefsState = {};
97
98 function updatePrefsState(what, b) {
99 prefsState[what] = b ? 1 : 0;
100 ps.setPrefs('topo_prefs', prefsState);
101 }
102
103 function _togSvgLayer(x, G, tag, what) {
Steven Burrowsd576f642016-09-26 10:40:51 -0700104 var on = (x === 'keyev') ? !sus.visible(G) : Boolean(x);
Steven Burrowse3a18842016-09-22 15:33:33 +0100105 sus.visible(G, on);
106 updatePrefsState(tag, on);
107 // flash.flash(verb + ' ' + what);
108 }
109 // TODO: -- END -- Move to dedicated module
110
111 function toggle(x) {
112 _togSvgLayer(x, mapG, 'bg', 'background map');
113 }
114
Steven Burrowsd576f642016-09-26 10:40:51 -0700115 function openMapSelection() {
116
117 // TODO: Create a view class with extend method
118 MapSelectionDialog.prototype.currentMap = currentMap;
119
120 new MapSelectionDialog({
121 okHandler: function (preferences) {
122 setMap(preferences);
123 }
124 }).open();
125 }
126
Steven Burrowse3a18842016-09-22 15:33:33 +0100127 angular.module('ovTopo2')
128 .factory('Topo2MapService',
Steven Burrowsd576f642016-09-26 10:40:51 -0700129 ['$location', 'PrefsService', 'MapService',
130 'SvgUtilService', 'Topo2CountryFilters', 'Topo2MapDialog',
131 function (_$loc_, _ps_, _ms_, _sus_, _t2cf_, _t2md_) {
Steven Burrowse3a18842016-09-22 15:33:33 +0100132
Steven Burrowse3a18842016-09-22 15:33:33 +0100133 $loc = _$loc_;
Steven Burrowse3a18842016-09-22 15:33:33 +0100134 ps = _ps_;
135 ms = _ms_;
136 sus = _sus_;
Steven Burrowsd576f642016-09-26 10:40:51 -0700137 countryFilters = _t2cf_;
138 MapSelectionDialog = _t2md_;
Steven Burrowse3a18842016-09-22 15:33:33 +0100139
140 return {
141 init: init,
142 openMapSelection: openMapSelection,
Steven Burrowsd576f642016-09-26 10:40:51 -0700143 toggle: toggle
Steven Burrowse3a18842016-09-22 15:33:33 +0100144 };
145 }
146 ]);
147
148})();