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 | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 32 | var mapG; |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 33 | |
| 34 | function init(zoomLayer) { |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 35 | 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 Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 43 | loadMap = ms.loadMapInto, |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 44 | 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 Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 56 | if (mapFilePath === '*countries') { |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 57 | cfilter = countryFilters[mapId] || countryFilters.uk; |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 58 | loadMap = ms.loadMapRegionInto; |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Steven Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 61 | promise = loadMap(mapG, mapFilePath, mapId, { |
| 62 | countryFilters: cfilter, |
| 63 | adjustScale: mapScale, |
| 64 | shading: '' |
| 65 | }); |
| 66 | |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 67 | return promise; |
| 68 | } |
| 69 | |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 70 | 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 Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 83 | 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 Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 95 | // 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 Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 104 | var on = (x === 'keyev') ? !sus.visible(G) : Boolean(x); |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 105 | 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 Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 115 | 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 Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 127 | angular.module('ovTopo2') |
| 128 | .factory('Topo2MapService', |
Steven Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 129 | ['$location', 'PrefsService', 'MapService', |
| 130 | 'SvgUtilService', 'Topo2CountryFilters', 'Topo2MapDialog', |
| 131 | function (_$loc_, _ps_, _ms_, _sus_, _t2cf_, _t2md_) { |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 132 | |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 133 | $loc = _$loc_; |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 134 | ps = _ps_; |
| 135 | ms = _ms_; |
| 136 | sus = _sus_; |
Steven Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 137 | countryFilters = _t2cf_; |
| 138 | MapSelectionDialog = _t2md_; |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 139 | |
| 140 | return { |
| 141 | init: init, |
| 142 | openMapSelection: openMapSelection, |
Steven Burrows | d576f64 | 2016-09-26 10:40:51 -0700 | [diff] [blame] | 143 | toggle: toggle |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 144 | }; |
| 145 | } |
| 146 | ]); |
| 147 | |
| 148 | })(); |