-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgateway-map.js
More file actions
199 lines (174 loc) · 6.4 KB
/
Copy pathgateway-map.js
File metadata and controls
199 lines (174 loc) · 6.4 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
class Map {
constructor() {
this.gateways = [];
this.gatewaysCluster = this.initMarkerClusterGroup();
this.theThingsNetworkCluster = this.initMarkerClusterGroup();
this.privateNetworksCluster = this.initMarkerClusterGroup();
this.showThingsStack = true;
this.showPrivateNetworks = true;
this.map = null;
this.map = this.initMap();
this.map.scrollWheelZoom.disable();
this.onlineIcon = this.initIcon();
}
initIcon() {
return L.icon({
iconUrl: document.currentScript.getAttribute("data-path-icon-gateway"),
iconSize: [10, 20],
});
}
initMap() {
if (this.map) {
return this.map;
}
else {
return L.map("map-wrap", {
attributionControl: true,
zoomControl: true,
worldCopyJump: true,
preferCanvas: true,
}).setView(L.latLng(25, 0), 2);
}
}
initMarkerClusterGroup() {
return L.markerClusterGroup({
maxClusterRadius: 30,
chunkedLoading: true,
iconCreateFunction: function (cluster) {
return L.divIcon({
className: "gateways-cluster-icon",
html: cluster.getChildCount(),
});
},
});
}
labelDict(arg) {
return {
"map.name": "Gateway name:",
"map.id": "Gateway ID:",
"map.unknown": "Unkown"
}[arg];
}
popupContent(gateway) {
let content = {};
// Commented out till packet broker api gives the name as well
// content[this.labelDict("map.name")] = gateway.name;
content[this.labelDict("map.id")] = gateway.id;
let result = "";
for (let key in content) {
result +=
"<b>" +
key +
"</b> " +
(content[key] === undefined ? this.labelDict("map.unknown") : content[key]) +
"<br />";
}
return result;
}
createMapLayer(gatewaysList, gatewaysClusterList) {
for (let key in gatewaysList) {
let gateway = gatewaysList[key];
if (gateway.hasOwnProperty("location") && gateway.online) {
let marker = L.marker(
[gateway.location.latitude, gateway.location.longitude],
{ icon: this.onlineIcon }
);
marker.bindPopup(this.popupContent(gateway));
gatewaysClusterList.addLayer(marker);
}
}
return gatewaysClusterList;
}
initMapLayers() {
let theThingsNetwork = this.gateways.filter(gateway => (gateway.tenantID === "ttn" || gateway.tenantID === "ttnv2"));
let privateNetworks = this.gateways.filter(gateway => (gateway.tenantID !== "ttn" && gateway.tenantID !== "ttnv2"));
this.gatewaysCluster = this.createMapLayer(this.gateways, this.gatewaysCluster);
this.theThingsNetworkCluster = this.createMapLayer(theThingsNetwork, this.theThingsNetworkCluster);
this.privateNetworksCluster = this.createMapLayer(privateNetworks, this.privateNetworksCluster);
this.map.addLayer(this.gatewaysCluster);
}
createPacketBrokerAnchorTag() {
let packetBrokerTag = document.createElement('a');
packetBrokerTag.setAttribute('href', 'https://packetbroker.net/');
packetBrokerTag.setAttribute('class', 'packet-broker-link leaflet-control');
packetBrokerTag.setAttribute('target', '_blank');
packetBrokerTag.innerText = this.gateways.length + " gateways are connected via Packet Broker";
return packetBrokerTag;
}
addPacketBrokerText() {
let packetBrokerTag = this.createPacketBrokerAnchorTag();
let packetBrokerTag2 = this.createPacketBrokerAnchorTag();
packetBrokerTag2.setAttribute('class', 'packet-broker-mobile');
document.getElementById("packet-broker-id-desktop").appendChild(packetBrokerTag);
document.getElementById("packet-broker-id-mobile").appendChild(packetBrokerTag2);
}
changeMapLayers() {
this.map.removeLayer(this.gatewaysCluster);
if (this.showThingsStack && this.showPrivateNetworks) {
this.map.addLayer(this.gatewaysCluster);
return;
}
(this.showPrivateNetworks)
? this.map.addLayer(this.privateNetworksCluster)
: this.map.removeLayer(this.privateNetworksCluster);
(this.showThingsStack)
? this.map.addLayer(this.theThingsNetworkCluster)
: this.map.removeLayer(this.theThingsNetworkCluster);
}
addReferences() {
L.tileLayer("https://{s}.tile.osm.org/{z}/{x}/{y}.png", {
detectRetina: true,
maxNativeZoom: 17,
attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors',
}).addTo(this.map);
let attributions = document.getElementsByClassName('leaflet-control-attribution')
for (let index = 0; index < attributions.length; ++index) {
attributions[index].innerHTML = '<a href="https://leafletjs.com" title="A JS library for interactive maps" target="_blank">Leaflet</a> | © <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors';
}
}
addSearch() {
const provider = new window.GeoSearch.OpenStreetMapProvider();
const search = new GeoSearch.GeoSearchControl({
provider: provider,
style: 'bar',
updateMap: true,
autoClose: true,
});
search.addTo(this.map);
}
}
(function(){
let mapClass = new Map();
mapClass.addReferences();
mapClass.addSearch();
fetch("https://mapper.packetbroker.net/api/v2/gateways")
.then(function (response) {
return response.json();
})
.then(function (gateways) {
document.getElementById('loading').remove();
if (gateways === null) {
return;
}
mapClass.gateways = gateways;
mapClass.addPacketBrokerText();
mapClass.initMapLayers();
});
const thingsStackBtnClick = () => {
mapClass.showThingsStack = !mapClass.showThingsStack;
mapClass.changeMapLayers();
}
const privateNetBtnClick = () => {
mapClass.showPrivateNetworks = !mapClass.showPrivateNetworks;
mapClass.changeMapLayers();
}
document.getElementById("ThingsStackBtnDropdown").addEventListener("click", thingsStackBtnClick);
document.getElementById("PrivateNetBtnDropdown").addEventListener("click", privateNetBtnClick);
document.getElementById("ThingsStackBtn").addEventListener("click", thingsStackBtnClick);
document.getElementById("PrivateNetBtn").addEventListener("click", privateNetBtnClick);
})();
document.getElementById("dropdownBtn").addEventListener("click", function() {
document.getElementById("dropdownId").classList.toggle("show");
document.getElementById("filter-icon").classList.toggle("hide");
document.getElementById("close-icon").classList.toggle("hide");
});