Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 1 | /* |
| 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 Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 25 | // Injected Services |
Steven Burrows | c714661 | 2016-12-27 13:50:03 +0000 | [diff] [blame] | 26 | var $loc, ps, ms, flash, sus, countryFilters; |
Steven Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 27 | |
| 28 | // Injected Classes |
| 29 | var MapSelectionDialog; |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 30 | |
| 31 | // internal state |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 32 | var mapG, zoomLayer, zoomer; |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 33 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 34 | function init(_zoomLayer_, _zoomer_) { |
| 35 | zoomLayer = _zoomLayer_; |
| 36 | zoomer = _zoomer_; |
| 37 | return setUpMap(); |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 40 | function setUpMap() { |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 41 | var prefs = currentMap(), |
| 42 | mapId = prefs.mapid, |
| 43 | mapFilePath = prefs.mapfilepath, |
| 44 | mapScale = prefs.mapscale, |
Steven Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 45 | loadMap = ms.loadMapInto, |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 46 | 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 Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 58 | if (mapFilePath === '*countries') { |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 59 | cfilter = countryFilters[mapId] || countryFilters.uk; |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 60 | loadMap = ms.loadMapRegionInto; |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Steven Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 63 | promise = loadMap(mapG, mapFilePath, mapId, { |
| 64 | countryFilters: cfilter, |
| 65 | adjustScale: mapScale, |
| 66 | shading: '' |
| 67 | }); |
| 68 | |
Steven Burrows | c714661 | 2016-12-27 13:50:03 +0000 | [diff] [blame] | 69 | if (!ps.getPrefs('topo_prefs').bg) { |
| 70 | toggle(false); |
| 71 | } |
| 72 | |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 73 | return promise; |
| 74 | } |
| 75 | |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 76 | 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 Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 89 | 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 Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 101 | // 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 Burrows | c714661 | 2016-12-27 13:50:03 +0000 | [diff] [blame] | 110 | var on = (x === 'keyev') ? !sus.visible(G) : Boolean(x), |
| 111 | verb = on ? 'Show' : 'Hide'; |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 112 | sus.visible(G, on); |
| 113 | updatePrefsState(tag, on); |
Steven Burrows | c714661 | 2016-12-27 13:50:03 +0000 | [diff] [blame] | 114 | |
| 115 | if (x === 'keyev') { |
| 116 | flash.flash(verb + ' ' + what); |
| 117 | } |
| 118 | |
| 119 | return on; |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 120 | } |
| 121 | // TODO: -- END -- Move to dedicated module |
| 122 | |
| 123 | function toggle(x) { |
Steven Burrows | c714661 | 2016-12-27 13:50:03 +0000 | [diff] [blame] | 124 | return _togSvgLayer(x, mapG, 'bg', 'background map'); |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Steven Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 127 | 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 Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 139 | function resetZoom() { |
| 140 | zoomer.reset(); |
| 141 | } |
| 142 | |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 143 | angular.module('ovTopo2') |
| 144 | .factory('Topo2MapService', |
Steven Burrows | c714661 | 2016-12-27 13:50:03 +0000 | [diff] [blame] | 145 | ['$location', 'PrefsService', 'MapService', 'FlashService', |
Steven Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 146 | 'SvgUtilService', 'Topo2CountryFilters', 'Topo2MapDialog', |
Steven Burrows | c714661 | 2016-12-27 13:50:03 +0000 | [diff] [blame] | 147 | function (_$loc_, _ps_, _ms_, _flash_, _sus_, _t2cf_, _t2md_) { |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 148 | |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 149 | $loc = _$loc_; |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 150 | ps = _ps_; |
| 151 | ms = _ms_; |
Steven Burrows | c714661 | 2016-12-27 13:50:03 +0000 | [diff] [blame] | 152 | flash = _flash_; |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 153 | sus = _sus_; |
Steven Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 154 | countryFilters = _t2cf_; |
| 155 | MapSelectionDialog = _t2md_; |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 156 | |
| 157 | return { |
| 158 | init: init, |
| 159 | openMapSelection: openMapSelection, |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 160 | toggle: toggle, |
| 161 | |
| 162 | resetZoom: resetZoom |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 163 | }; |
| 164 | } |
| 165 | ]); |
| 166 | |
| 167 | })(); |