blob: a14a0383613aaa58445d439f6f78c24362b520c9 [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
Simon Hunt1e8eff42015-01-08 17:19:02 -080045 var $log, $http, $q, fs;
Simon Huntf044d8a2015-01-07 17:53:04 -080046
47 // internal state
48 var maps = d3.map(),
49 msgMs = 'MapService.',
Simon Hunt1e8eff42015-01-08 17:19:02 -080050 bundledUrlPrefix = '../data/map/';
Simon Huntf044d8a2015-01-07 17:53:04 -080051
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 Hunt1e8eff42015-01-08 17:19:02 -080060 .factory('MapService', ['$log', '$http', '$q', 'FnService',
Simon Huntf044d8a2015-01-07 17:53:04 -080061
Simon Hunt1e8eff42015-01-08 17:19:02 -080062 function (_$log_, _$http_, _$q_, _fs_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -080063 $log = _$log_;
Simon Hunt1e8eff42015-01-08 17:19:02 -080064 $http = _$http_;
65 $q = _$q_;
Simon Huntf044d8a2015-01-07 17:53:04 -080066 fs = _fs_;
67
68 function clearCache() {
69 maps = d3.map();
70 }
71
Simon Hunt1e8eff42015-01-08 17:19:02 -080072 // 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 Huntf044d8a2015-01-07 17:53:04 -080080 function fetchGeoMap(id) {
81 if (!fs.isS(id)) {
82 return null;
83 }
Simon Hunt1e8eff42015-01-08 17:19:02 -080084 var url = getUrl(id);
Simon Huntf044d8a2015-01-07 17:53:04 -080085
Simon Hunt1e8eff42015-01-08 17:19:02 -080086 var promise = maps.get(id);
Simon Huntf044d8a2015-01-07 17:53:04 -080087
Simon Hunt1e8eff42015-01-08 17:19:02 -080088 if (!promise) {
Simon Huntf044d8a2015-01-07 17:53:04 -080089 // need to fetch the data and build the object...
Simon Hunt1e8eff42015-01-08 17:19:02 -080090 var deferred = $q.defer();
91 promise = deferred.promise;
Simon Huntf044d8a2015-01-07 17:53:04 -080092
Simon Hunt1e8eff42015-01-08 17:19:02 -080093 $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 Huntf044d8a2015-01-07 17:53:04 -0800110 } else {
Simon Hunt1e8eff42015-01-08 17:19:02 -0800111 promise.meta.wasCached = true;
Simon Huntf044d8a2015-01-07 17:53:04 -0800112 }
113
Simon Hunt1e8eff42015-01-08 17:19:02 -0800114 return promise;
Simon Huntf044d8a2015-01-07 17:53:04 -0800115 }
Simon Hunt7ac7be92015-01-06 10:47:56 -0800116
117 return {
Simon Huntf044d8a2015-01-07 17:53:04 -0800118 clearCache: clearCache,
119 fetchGeoMap: fetchGeoMap
Simon Hunt7ac7be92015-01-06 10:47:56 -0800120 };
121 }]);
122
123}());