Skip to content

Commit 5921a8e

Browse files
committed
Delete predictions button
1 parent 573dc84 commit 5921a8e

File tree

1 file changed

+84
-3
lines changed

1 file changed

+84
-3
lines changed

js/tracker.js

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ var recovery_names = [];
3131
var recoveries = [];
3232

3333
var launchPredictions = {};
34+
var stationMarkerLookup = {};
35+
var markerStationLookup = {};
36+
var predictionAjax = [];
3437

3538
var stationHistoricalData = {};
3639
var historicalPlots = {};
@@ -670,6 +673,28 @@ function load() {
670673

671674
L.control.historicalontrol({ position: 'topleft' }).addTo(map);
672675

676+
L.Control.PredictionControl = L.Control.extend({
677+
onAdd: function(map) {
678+
var div = L.DomUtil.create('div');
679+
680+
div.innerHTML = '<button onclick="deletePredictionButton()">Delete Predictions</button>';
681+
div.id = "predictionControlButton";
682+
div.style.display = "none";
683+
684+
return div;
685+
},
686+
687+
onRemove: function(map) {
688+
// Nothing to do here
689+
}
690+
});
691+
692+
L.control.predictionontrol = function(opts) {
693+
return new L.Control.PredictionControl(opts);
694+
}
695+
696+
L.control.predictionontrol({ position: 'topleft' }).addTo(map);
697+
673698
// update current position if we geolocation is available
674699
if(currentPosition) updateCurrentPosition(currentPosition.lat, currentPosition.lon);
675700

@@ -1043,9 +1068,15 @@ function deleteHistoricalButton() {
10431068
for (let serial in historicalPlots[station].sondes) {
10441069
map.removeLayer(historicalPlots[station].sondes[serial].marker);
10451070
}
1071+
var realpopup = launches.getLayer(stationMarkerLookup[station]).getPopup();
10461072
var popup = $("#popup" + station);
10471073
var deleteHistorical = popup.find("#deleteHistorical");
10481074
deleteHistorical.hide();
1075+
if (!realpopup.isOpen()) {
1076+
var tempContent = $(realpopup.getContent());
1077+
tempContent.find("#deleteHistorical").hide();
1078+
realpopup.setContent("<div id='popup" + station + "'>" + tempContent.html() + "</div>");
1079+
}
10491080
}
10501081
}
10511082

@@ -1061,6 +1092,44 @@ function deleteHistoricalButton() {
10611092

10621093
}
10631094

1095+
function deletePredictionButton() {
1096+
var predictionDelete = $("#predictionControlButton");
1097+
1098+
for (var marker in launchPredictions) {
1099+
if (launchPredictions.hasOwnProperty(marker)) {
1100+
for (var prediction in launchPredictions[marker]) {
1101+
if (launchPredictions[marker].hasOwnProperty(prediction)) {
1102+
for (var object in launchPredictions[marker][prediction]) {
1103+
if (launchPredictions[marker][prediction].hasOwnProperty(object)) {
1104+
map.removeLayer(launchPredictions[marker][prediction][object]);
1105+
}
1106+
}
1107+
}
1108+
}
1109+
var realpopup = launches.getLayer(marker).getPopup();
1110+
var popup = $("#popup" + markerStationLookup[marker]);
1111+
var deletePrediction = popup.find("#predictionDeleteButton");
1112+
deletePrediction.hide();
1113+
if (!realpopup.isOpen()) {
1114+
var tempContent = $(realpopup.getContent());
1115+
tempContent.find("#predictionDeleteButton").hide();
1116+
realpopup.setContent("<div id='popup" + markerStationLookup[marker] + "'>" + tempContent.html() + "</div>");
1117+
}
1118+
}
1119+
}
1120+
1121+
launchPredictions = {};
1122+
1123+
for (i=0; i < predictionAjax.length; i++) {
1124+
predictionAjax[i].abort();
1125+
}
1126+
1127+
predictionAjax = [];
1128+
1129+
predictionDelete.hide();
1130+
1131+
}
1132+
10641133
// Master function to display historic summaries
10651134
function showHistorical (station, marker) {
10661135
var popup = $("#popup" + station);
@@ -1368,6 +1437,8 @@ function launchSitePredictions(times, station, properties, marker, id) {
13681437
dates.push(date.toISOString().split('.')[0]+"Z");
13691438
}
13701439
var completed = 0;
1440+
var predictionDelete = $("#predictionControlButton");
1441+
predictionDelete.show();
13711442
for (var i = 0; i < dates.length; i++) {
13721443
var lon = ((360 + (position[1] % 360)) % 360);
13731444
//var url = "https://predict.cusf.co.uk/api/v1/?launch_latitude=" + position[0] + "&launch_longitude=" + lon + "&launch_datetime=" + dates[i] + "&ascent_rate=" + properties[0] + "&burst_altitude=" + properties[2] + "&descent_rate=" + properties[1];
@@ -1378,7 +1449,7 @@ function launchSitePredictions(times, station, properties, marker, id) {
13781449
completed += 1;
13791450
plotPrediction(data, dates, marker, properties);
13801451
if (completed == dates.length) {
1381-
predictionDeleteButton.show();
1452+
if (Object.keys(launchPredictions).length != 0) predictionDeleteButton.show();
13821453
predictionButton.show();
13831454
predictionButtonLoading.hide();
13841455
if (!realpopup.isOpen()) {
@@ -1389,7 +1460,7 @@ function launchSitePredictions(times, station, properties, marker, id) {
13891460
function handleError(error) {
13901461
completed += 1;
13911462
if (completed == dates.length) {
1392-
predictionDeleteButton.show();
1463+
if (Object.keys(launchPredictions).length != 0) predictionDeleteButton.show();
13931464
predictionButton.show();
13941465
predictionButtonLoading.hide();
13951466
if (!realpopup.isOpen()) {
@@ -1474,14 +1545,17 @@ function plotPrediction (data, dates, marker, properties) {
14741545
}
14751546

14761547
function showPrediction(url) {
1477-
return $.ajax({
1548+
var ajaxReq = $.ajax({
14781549
type: "GET",
14791550
url: url,
14801551
dataType: "json",
14811552
});
1553+
predictionAjax.push(ajaxReq);
1554+
return ajaxReq;
14821555
}
14831556

14841557
function deletePredictions(marker, station) {
1558+
var predictionDelete = $("#predictionControlButton");
14851559
if (launchPredictions.hasOwnProperty(marker)) {
14861560
for (var prediction in launchPredictions[marker]) {
14871561
if (launchPredictions[marker].hasOwnProperty(prediction)) {
@@ -1492,12 +1566,14 @@ function deletePredictions(marker, station) {
14921566
}
14931567
}
14941568
}
1569+
delete launchPredictions[marker];
14951570
}
14961571
var popup = $("#popup" + station);
14971572
var predictionDeleteButton = popup.find("#predictionDeleteButton");
14981573
if (predictionDeleteButton.is(':visible')) {
14991574
predictionDeleteButton.hide();
15001575
}
1576+
if (Object.keys(launchPredictions).length == 0) predictionDelete.hide();
15011577
}
15021578

15031579
function getLaunchSites() {
@@ -1652,6 +1728,11 @@ function generateLaunchSites() {
16521728
div.innerHTML = popupContent;
16531729

16541730
popup.setContent(div.innerHTML);
1731+
1732+
var leafletID = launches.getLayerId(marker);
1733+
1734+
stationMarkerLookup[key] = leafletID;
1735+
markerStationLookup[leafletID] = key;
16551736
}
16561737
}
16571738
if (focusID != 0) {

0 commit comments

Comments
 (0)