forked from CartoDB/cdb-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
111 lines (98 loc) · 3.47 KB
/
map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
cdbmanager.directive('draggable', function () {
return {
restrict: 'A',
link: function (scope, element) {
element = element[0];
element.addEventListener('dragstart', function (e) {
element.style.opacity = '0.4';
e.dataTransfer.setData('text', element.innerText);
e.dataTransfer.effectAllowed = 'copy';
return false;
}, false);
element.addEventListener('dragend', function (e) {
element.style.opacity = '1';
return false;
}, false);
}
}
});
cdbmanager.directive('droppable', function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
element[0].addEventListener('drop', scope.handleDrop, false);
element[0].addEventListener('dragover', function (e) {
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
return false;
}, false);
}
}
});
cdbmanager.service("map", ["$timeout", function ($timeout) {
let self = this;
this.reloading = true;
this.reset = function (force) {
if (this.reloading || force) {
self.map = new mapboxgl.Map({
container: 'map',
style: 'https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json'
});
}
};
this.reset(true);
this.setAuth = function (user, apiKey) {
carto.setDefaultAuth({
user: user,
apiKey: apiKey
});
};
this.update = function (query) {
if (self.map._container.offsetParent) {
self.reset();
let source = new carto.source.SQL(query);
let viz = new carto.Viz();
let layer = new carto.Layer("layer_" + Math.floor((Math.random() * 1000000000) + 1), source, viz);
layer.addTo(self.map);
}
};
this.showGeometry = function (geometry) {
if (self.map._container.offsetParent) {
self.reset();
let source = null;
if (geometry.slice(10, 14) == "10E6" || geometry.slice(10, 14) == "E610") {
source = new carto.source.SQL("select 1 as cartodb_id, the_geom, st_transform(the_geom, 3857) as the_geom_webmercator from (select '" + geometry + "'::geometry AS the_geom) c");
} else if (geometry.slice(10, 14) == "0F11" || geometry.slice(10, 14) == "110F") {
source = new carto.source.SQL("select 1 as cartodb_id, the_geom_webmercator, st_transform(the_geom_webmercator, 4326) as the_geom from (select '" + geometry + "'::geometry AS the_geom_webmercator) c");
} else {
source = new carto.source.SQL('SELECT cartodb_id, the_geom, the_geom_webmercator from "' + geometry + '"'); // "geometry" is most likely a table name ;-)
}
let viz = new carto.Viz();
let layer = new carto.Layer("layer_" + Math.floor((Math.random() * 1000000000) + 1), source, viz);
layer.addTo(self.map);
}
}
}]);
cdbmanager.controller('cartovlCtrl', ["$scope", "map", "endpoints", function ($scope, map, endpoints) {
$scope.reloading = map.reloading;
$scope.toggleReloading = function () {
map.reloading = !(map.reloading);
};
$scope.$watch(function () {
return map.reloading;
}, function (currentTable) {
$scope.reloading = map.reloading;
});
// keep the endpoint list in the scope always updated
$scope.$watch(function () {
return endpoints.current;
}, function (currentEndpoint) {
map.setAuth(currentEndpoint.account, currentEndpoint.apiKey)
});
$scope.handleDrop = function (e) {
e.preventDefault();
e.stopPropagation();
let dataText = e.dataTransfer.getData('text');
map.showGeometry(dataText.trim());
};
}]);