Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 -- SVG -- Map Service |
| 19 | |
| 20 | @author Simon Hunt |
| 21 | */ |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | The Map Service caches GeoJSON maps, which can be loaded into the map |
| 25 | layer of the Topology View. |
| 26 | |
| 27 | A GeoMap object can be fetched by ID. IDs that start with an asterisk |
| 28 | identify maps bundled with the GUI. IDs that do not start with an |
| 29 | asterisk are assumed to be URLs to externally provided data (exact |
| 30 | format to be decided). |
| 31 | |
| 32 | e.g. var geomap = MapService.fetchGeoMap('*continental-us'); |
| 33 | |
| 34 | The GeoMap object encapsulates topology data (features), and the |
| 35 | D3 projection object. |
| 36 | |
| 37 | Note that, since the GeoMap instance is cached / shared, it should |
| 38 | contain no state. |
| 39 | */ |
| 40 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 41 | (function () { |
| 42 | 'use strict'; |
| 43 | |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 44 | // injected references |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame^] | 45 | var $log, $http, $q, fs; |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 46 | |
| 47 | // internal state |
| 48 | var maps = d3.map(), |
| 49 | msgMs = 'MapService.', |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame^] | 50 | bundledUrlPrefix = '../data/map/'; |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 51 | |
| 52 | function getUrl(id) { |
| 53 | if (id[0] === '*') { |
| 54 | return bundledUrlPrefix + id.slice(1) + '.json'; |
| 55 | } |
| 56 | return id + '.json'; |
| 57 | } |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 58 | |
| 59 | angular.module('onosSvg') |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame^] | 60 | .factory('MapService', ['$log', '$http', '$q', 'FnService', |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 61 | |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame^] | 62 | function (_$log_, _$http_, _$q_, _fs_) { |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 63 | $log = _$log_; |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame^] | 64 | $http = _$http_; |
| 65 | $q = _$q_; |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 66 | fs = _fs_; |
| 67 | |
| 68 | function clearCache() { |
| 69 | maps = d3.map(); |
| 70 | } |
| 71 | |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame^] | 72 | // NOTE: It is expected that mapLayer is a D3 selection of the |
| 73 | // <g> element (a child of zoomLayer) into which the map |
| 74 | // path data will be rendered. |
| 75 | function renderMap(mapLayer) { |
| 76 | // TODO --- |
| 77 | $log.log('Hey, let\'s render the map...'); |
| 78 | } |
| 79 | |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 80 | function fetchGeoMap(id) { |
| 81 | if (!fs.isS(id)) { |
| 82 | return null; |
| 83 | } |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame^] | 84 | var url = getUrl(id); |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 85 | |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame^] | 86 | var promise = maps.get(id); |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 87 | |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame^] | 88 | if (!promise) { |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 89 | // need to fetch the data and build the object... |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame^] | 90 | var deferred = $q.defer(); |
| 91 | promise = deferred.promise; |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 92 | |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame^] | 93 | $http.get(url) |
| 94 | .success(function (data) { |
| 95 | deferred.resolve(data); |
| 96 | }) |
| 97 | .error(function (msg, code) { |
| 98 | deferred.reject(msg); |
| 99 | $log.warn(msg, code); |
| 100 | }); |
| 101 | |
| 102 | promise.meta = { |
| 103 | id: id, |
| 104 | url: url, |
| 105 | wasCached: false, |
| 106 | render: renderMap |
| 107 | }; |
| 108 | |
| 109 | maps.set(id, promise); |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 110 | } else { |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame^] | 111 | promise.meta.wasCached = true; |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame^] | 114 | return promise; |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 115 | } |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 116 | |
| 117 | return { |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 118 | clearCache: clearCache, |
| 119 | fetchGeoMap: fetchGeoMap |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 120 | }; |
| 121 | }]); |
| 122 | |
| 123 | }()); |