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 |
| 26 | var $loc, ps, ms, sus, countryFilters; |
| 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 | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 69 | return promise; |
| 70 | } |
| 71 | |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 72 | 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 Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 85 | 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 Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 97 | // 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 Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 106 | var on = (x === 'keyev') ? !sus.visible(G) : Boolean(x); |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 107 | 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 Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 117 | 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 Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 129 | function resetZoom() { |
| 130 | zoomer.reset(); |
| 131 | } |
| 132 | |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 133 | angular.module('ovTopo2') |
| 134 | .factory('Topo2MapService', |
Steven Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 135 | ['$location', 'PrefsService', 'MapService', |
| 136 | 'SvgUtilService', 'Topo2CountryFilters', 'Topo2MapDialog', |
| 137 | function (_$loc_, _ps_, _ms_, _sus_, _t2cf_, _t2md_) { |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 138 | |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 139 | $loc = _$loc_; |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 140 | ps = _ps_; |
| 141 | ms = _ms_; |
| 142 | sus = _sus_; |
Steven Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 143 | countryFilters = _t2cf_; |
| 144 | MapSelectionDialog = _t2md_; |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 145 | |
| 146 | return { |
| 147 | init: init, |
| 148 | openMapSelection: openMapSelection, |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 149 | toggle: toggle, |
| 150 | |
| 151 | resetZoom: resetZoom |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 152 | }; |
| 153 | } |
| 154 | ]); |
| 155 | |
| 156 | })(); |