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 | |
| 25 | var $log, $loc, wss, ps, ms, t2ds, sus; |
| 26 | |
| 27 | // internal state |
| 28 | var mapG, order, maps, map, mapItems, tintCheck, messageHandlers; |
| 29 | |
| 30 | // constants |
| 31 | var mapRequest = 'mapSelectorRequest'; |
| 32 | |
| 33 | var countryFilters = { |
| 34 | s_america: function (c) { |
| 35 | return c.properties.continent === 'South America'; |
| 36 | }, |
| 37 | |
| 38 | ns_america: function (c) { |
| 39 | return c.properties.custom === 'US-cont' || |
| 40 | c.properties.subregion === 'Central America' || |
| 41 | c.properties.continent === 'South America'; |
| 42 | }, |
| 43 | |
| 44 | japan: function (c) { |
| 45 | return c.properties.geounit === 'Japan'; |
| 46 | }, |
| 47 | |
| 48 | europe: function (c) { |
| 49 | return c.properties.continent === 'Europe'; |
| 50 | }, |
| 51 | |
| 52 | italy: function (c) { |
| 53 | return c.properties.geounit === 'Italy'; |
| 54 | }, |
| 55 | |
| 56 | uk: function (c) { |
| 57 | // technically, Ireland is not part of the United Kingdom, |
| 58 | // but the map looks weird without it showing. |
| 59 | return c.properties.adm0_a3 === 'GBR' || |
| 60 | c.properties.adm0_a3 === 'IRL'; |
| 61 | }, |
| 62 | |
| 63 | s_korea: function (c) { |
| 64 | return c.properties.adm0_a3 === 'KOR'; |
| 65 | }, |
| 66 | |
| 67 | australia: function (c) { |
| 68 | return c.properties.adm0_a3 === 'AUS'; |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | function init(zoomLayer) { |
| 73 | |
| 74 | start(); |
| 75 | return setUpMap(zoomLayer); |
| 76 | } |
| 77 | |
| 78 | function setUpMap(zoomLayer) { |
| 79 | var prefs = currentMap(), |
| 80 | mapId = prefs.mapid, |
| 81 | mapFilePath = prefs.mapfilepath, |
| 82 | mapScale = prefs.mapscale, |
| 83 | tint = prefs.tint, |
| 84 | promise, cfilter; |
| 85 | |
| 86 | mapG = d3.select('#topo-map'); |
| 87 | |
| 88 | if (mapG.empty()) { |
| 89 | mapG = zoomLayer.append('g').attr('id', 'topo-map'); |
| 90 | } else { |
| 91 | mapG.each(function (d, i) { |
| 92 | d3.selectAll(this.childNodes).remove(); |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | cfilter = countryFilters[mapId] || countryFilters.uk; |
| 97 | |
| 98 | if (mapFilePath === '*countries') { |
| 99 | |
| 100 | cfilter = countryFilters[mapId] || countryFilters.uk; |
| 101 | |
| 102 | promise = ms.loadMapRegionInto(mapG, { |
| 103 | countryFilter: cfilter, |
| 104 | adjustScale: mapScale, |
| 105 | shading: '' |
| 106 | }); |
| 107 | } else { |
| 108 | |
| 109 | promise = ms.loadMapInto(mapG, mapFilePath, mapId, { |
| 110 | adjustScale: mapScale, |
| 111 | shading: '' |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | return promise; |
| 116 | } |
| 117 | |
| 118 | function start() { |
| 119 | wss.bindHandlers(messageHandlers); |
| 120 | } |
| 121 | |
| 122 | function stop() { |
| 123 | wss.unbindHandlers(messageHandlers); |
| 124 | } |
| 125 | |
| 126 | function currentMap() { |
| 127 | return ps.getPrefs( |
| 128 | 'topo_mapid', |
| 129 | { |
| 130 | mapid: 'usa', |
| 131 | mapscale: 1, |
| 132 | mapfilepath: '*continental_us', |
| 133 | tint: 'off' |
| 134 | }, |
| 135 | $loc.search() |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | function openMapSelection() { |
| 140 | wss.sendEvent(mapRequest); |
| 141 | } |
| 142 | |
| 143 | function opacifyMap(b) { |
| 144 | mapG.transition() |
| 145 | .duration(1000) |
| 146 | .attr('opacity', b ? 1 : 0); |
| 147 | } |
| 148 | |
| 149 | function setMap(map) { |
| 150 | ps.setPrefs('topo_mapid', map); |
| 151 | setUpMap(); |
| 152 | opacifyMap(true); |
| 153 | } |
| 154 | |
| 155 | function dOk() { |
| 156 | var p = { |
| 157 | mapid: map.id, |
| 158 | mapscale: map.scale, |
| 159 | mapfilepath: map.filePath, |
| 160 | tint: 'off' |
| 161 | // tint: tintCheck.property('checked') ? 'on' : 'off' |
| 162 | }; |
| 163 | setMap(p); |
| 164 | $log.debug('Dialog OK button clicked'); |
| 165 | } |
| 166 | |
| 167 | function dClose() { |
| 168 | $log.debug('Dialog Close button clicked (or Esc pressed)'); |
| 169 | } |
| 170 | |
| 171 | function selectMap() { |
| 172 | map = maps[this.options[this.selectedIndex].value]; |
| 173 | $log.info('Selected map', map); |
| 174 | } |
| 175 | |
| 176 | function createListContent() { |
| 177 | var content = t2ds.createDiv('map-list'), |
| 178 | form = content.append('form'), |
| 179 | current = currentMap(); |
| 180 | map = maps[current.mapid]; |
| 181 | mapItems = form.append('select').on('change', selectMap); |
| 182 | order.forEach(function (id) { |
| 183 | var m = maps[id]; |
| 184 | mapItems.append('option') |
| 185 | .attr('value', m.id) |
| 186 | .attr('selected', m.id === current.mapid ? true : null) |
| 187 | .text(m.description); |
| 188 | }); |
| 189 | |
| 190 | return content; |
| 191 | } |
| 192 | |
| 193 | function handleMapResponse(data) { |
| 194 | $log.info('Got response', data); |
| 195 | order = data.order; |
| 196 | maps = data.maps; |
| 197 | t2ds.openDialog() |
| 198 | .setTitle('Select Map') |
| 199 | .addContent(createListContent()) |
| 200 | .addOk(dOk, 'OK') |
| 201 | .addCancel(dClose, 'Close') |
| 202 | .bindKeys(); |
| 203 | } |
| 204 | |
| 205 | // TODO: -- START -- Move to dedicated module |
| 206 | var prefsState = {}; |
| 207 | |
| 208 | function updatePrefsState(what, b) { |
| 209 | prefsState[what] = b ? 1 : 0; |
| 210 | ps.setPrefs('topo_prefs', prefsState); |
| 211 | } |
| 212 | |
| 213 | function _togSvgLayer(x, G, tag, what) { |
| 214 | var on = (x === 'keyev') ? !sus.visible(G) : !!x, |
| 215 | verb = on ? 'Show' : 'Hide'; |
| 216 | sus.visible(G, on); |
| 217 | updatePrefsState(tag, on); |
| 218 | // flash.flash(verb + ' ' + what); |
| 219 | } |
| 220 | // TODO: -- END -- Move to dedicated module |
| 221 | |
| 222 | function toggle(x) { |
| 223 | _togSvgLayer(x, mapG, 'bg', 'background map'); |
| 224 | } |
| 225 | |
| 226 | angular.module('ovTopo2') |
| 227 | .factory('Topo2MapService', |
| 228 | ['$log', '$location', 'WebSocketService', 'PrefsService', 'MapService', |
| 229 | 'SvgUtilService', 'Topo2DialogService', |
| 230 | function (_$log_, _$loc_, _wss_, _ps_, _ms_, _sus_, _t2ds_) { |
| 231 | |
| 232 | $log = _$log_; |
| 233 | $loc = _$loc_; |
| 234 | wss = _wss_; |
| 235 | ps = _ps_; |
| 236 | ms = _ms_; |
| 237 | sus = _sus_; |
| 238 | t2ds = _t2ds_; |
| 239 | |
| 240 | messageHandlers = { |
| 241 | mapSelectorResponse: handleMapResponse |
| 242 | }; |
| 243 | |
| 244 | return { |
| 245 | init: init, |
| 246 | openMapSelection: openMapSelection, |
| 247 | toggle: toggle, |
| 248 | stop: stop |
| 249 | }; |
| 250 | } |
| 251 | ]); |
| 252 | |
| 253 | })(); |