blob: ce7e956c894f7bf7b82d613d780e2460bc5e4850 [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 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 Burrowse3a18842016-09-22 15:33:33 +010069 return promise;
70 }
71
Steven Burrowse3a18842016-09-22 15:33:33 +010072 function currentMap() {
73 return ps.getPrefs(
74 'topo_mapid',
75 {
76 mapid: 'usa',
77 mapscale: 1,
78 mapfilepath: '*continental_us',
79 tint: 'off'
80 },
81 $loc.search()
82 );
83 }
84
Steven Burrowse3a18842016-09-22 15:33:33 +010085 function opacifyMap(b) {
86 mapG.transition()
87 .duration(1000)
88 .attr('opacity', b ? 1 : 0);
89 }
90
91 function setMap(map) {
92 ps.setPrefs('topo_mapid', map);
93 setUpMap();
94 opacifyMap(true);
95 }
96
Steven Burrowse3a18842016-09-22 15:33:33 +010097 // TODO: -- START -- Move to dedicated module
98 var prefsState = {};
99
100 function updatePrefsState(what, b) {
101 prefsState[what] = b ? 1 : 0;
102 ps.setPrefs('topo_prefs', prefsState);
103 }
104
105 function _togSvgLayer(x, G, tag, what) {
Steven Burrowsd576f642016-09-26 10:40:51 -0700106 var on = (x === 'keyev') ? !sus.visible(G) : Boolean(x);
Steven Burrowse3a18842016-09-22 15:33:33 +0100107 sus.visible(G, on);
108 updatePrefsState(tag, on);
109 // flash.flash(verb + ' ' + what);
110 }
111 // TODO: -- END -- Move to dedicated module
112
113 function toggle(x) {
114 _togSvgLayer(x, mapG, 'bg', 'background map');
115 }
116
Steven Burrowsd576f642016-09-26 10:40:51 -0700117 function openMapSelection() {
118
119 // TODO: Create a view class with extend method
120 MapSelectionDialog.prototype.currentMap = currentMap;
121
122 new MapSelectionDialog({
123 okHandler: function (preferences) {
124 setMap(preferences);
125 }
126 }).open();
127 }
128
Steven Burrows1c5c8612016-10-05 13:45:13 -0500129 function resetZoom() {
130 zoomer.reset();
131 }
132
Steven Burrowse3a18842016-09-22 15:33:33 +0100133 angular.module('ovTopo2')
134 .factory('Topo2MapService',
Steven Burrowsd576f642016-09-26 10:40:51 -0700135 ['$location', 'PrefsService', 'MapService',
136 'SvgUtilService', 'Topo2CountryFilters', 'Topo2MapDialog',
137 function (_$loc_, _ps_, _ms_, _sus_, _t2cf_, _t2md_) {
Steven Burrowse3a18842016-09-22 15:33:33 +0100138
Steven Burrowse3a18842016-09-22 15:33:33 +0100139 $loc = _$loc_;
Steven Burrowse3a18842016-09-22 15:33:33 +0100140 ps = _ps_;
141 ms = _ms_;
142 sus = _sus_;
Steven Burrowsd576f642016-09-26 10:40:51 -0700143 countryFilters = _t2cf_;
144 MapSelectionDialog = _t2md_;
Steven Burrowse3a18842016-09-22 15:33:33 +0100145
146 return {
147 init: init,
148 openMapSelection: openMapSelection,
Steven Burrows1c5c8612016-10-05 13:45:13 -0500149 toggle: toggle,
150
151 resetZoom: resetZoom
Steven Burrowse3a18842016-09-22 15:33:33 +0100152 };
153 }
154 ]);
155
156})();