blob: c736149df8a53b837b80ec931d8817f7d96f3772 [file] [log] [blame]
Simon Hunt7ac7be92015-01-06 10:47:56 -08001/*
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 Huntf044d8a2015-01-07 17:53:04 -080022
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 Hunt7ac7be92015-01-06 10:47:56 -080041(function () {
42 'use strict';
43
Simon Huntf044d8a2015-01-07 17:53:04 -080044 // injected references
45 var $log, fs;
46
47 // internal state
48 var maps = d3.map(),
49 msgMs = 'MapService.',
50 bundledUrlPrefix = 'data/map/';
51
52 function getUrl(id) {
53 if (id[0] === '*') {
54 return bundledUrlPrefix + id.slice(1) + '.json';
55 }
56 return id + '.json';
57 }
Simon Hunt7ac7be92015-01-06 10:47:56 -080058
59 angular.module('onosSvg')
Simon Huntf044d8a2015-01-07 17:53:04 -080060 .factory('MapService', ['$log', 'FnService',
61
62 function (_$log_, _fs_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -080063 $log = _$log_;
Simon Huntf044d8a2015-01-07 17:53:04 -080064 fs = _fs_;
65
66 function clearCache() {
67 maps = d3.map();
68 }
69
70 function fetchGeoMap(id) {
71 if (!fs.isS(id)) {
72 return null;
73 }
74
75 var geomap = maps.get(id);
76
77 if (!geomap) {
78 // need to fetch the data and build the object...
79 geomap = {
80 id: id,
81 url: getUrl(id),
82 wasCached: false
83 };
84 // TODO: use $http service to load map data asynchronously
85
86 maps.set(id, geomap);
87 } else {
88 geomap.wasCached = true;
89 }
90
91 return geomap;
92 }
Simon Hunt7ac7be92015-01-06 10:47:56 -080093
94 return {
Simon Huntf044d8a2015-01-07 17:53:04 -080095 clearCache: clearCache,
96 fetchGeoMap: fetchGeoMap
Simon Hunt7ac7be92015-01-06 10:47:56 -080097 };
98 }]);
99
100}());