@@ -34,6 +34,7 @@ var launchPredictions = {};
3434
3535var stationHistoricalData = { } ;
3636var historicalPlots = { } ;
37+ var historicalAjax = [ ] ;
3738
3839var sites = null ;
3940var launches = new L . LayerGroup ( ) ;
@@ -579,6 +580,28 @@ function load() {
579580
580581 L . control . periodcontrol ( { position : 'topleft' } ) . addTo ( map ) ;
581582
583+ L . Control . HistoricalControl = L . Control . extend ( {
584+ onAdd : function ( map ) {
585+ var div = L . DomUtil . create ( 'div' ) ;
586+
587+ div . innerHTML = '<button onclick="deleteHistoricalButton()">Delete Historical</button>' ;
588+ div . id = "historicalControlButton" ;
589+ div . style . display = "none" ;
590+
591+ return div ;
592+ } ,
593+
594+ onRemove : function ( map ) {
595+ // Nothing to do here
596+ }
597+ } ) ;
598+
599+ L . control . historicalontrol = function ( opts ) {
600+ return new L . Control . HistoricalControl ( opts ) ;
601+ }
602+
603+ L . control . historicalontrol ( { position : 'topleft' } ) . addTo ( map ) ;
604+
582605 // update current position if we geolocation is available
583606 if ( currentPosition ) updateCurrentPosition ( currentPosition . lat , currentPosition . lon ) ;
584607
@@ -805,7 +828,7 @@ function getSelectedNumber (station) {
805828// Download summary data from AWS S3
806829function downloadHistorical ( suffix ) {
807830 var url = "https://sondehub-history.s3.amazonaws.com/" + suffix ;
808- return $ . ajax ( {
831+ var ajaxReq = $ . ajax ( {
809832 type : "GET" ,
810833 url : url ,
811834 dataType : "json" ,
@@ -823,6 +846,8 @@ function downloadHistorical (suffix) {
823846 }
824847 }
825848 } ) ;
849+ historicalAjax . push ( ajaxReq ) ;
850+ return ajaxReq ;
826851}
827852
828853// Draw historic summaries to map
@@ -832,21 +857,9 @@ function drawHistorical (data, station) {
832857 var time = landing . datetime ;
833858
834859 if ( ! historicalPlots [ station ] . sondes . hasOwnProperty ( serial ) ) {
835- // Using age to detmine colour
836- /*
837- const date = new Date(time);
838- var minTime = historicalPlots[station].data.minTime;
839- var actualTime = date.getTime();
840- var maxTime = historicalPlots[station].data.maxTime;
841860
842861 historicalPlots [ station ] . sondes [ serial ] = { } ;
843862
844- historicalPlots[station].sondes[serial].time = actualTime
845-
846- var normalisedTime = ((actualTime-minTime)/(maxTime-minTime));
847- var iconColour = ConvertRGBtoHex(evaluate_cmap(normalisedTime, 'turbo'));
848- */
849-
850863 // Using last known alt to detmine colour
851864 var minAlt = 0 ;
852865 var actualAlt = landing . alt ;
@@ -912,9 +925,9 @@ function drawHistorical (data, station) {
912925 popup . setContent ( html ) ;
913926
914927 if ( ! recovered ) {
915- var marker = L . circleMarker ( [ landing . lat , landing . lon ] , { fillColor : "white" , color : iconColour , weight : 2 , radius : 5 , fillOpacity :1 } ) ;
928+ var marker = L . circleMarker ( [ landing . lat , landing . lon ] , { fillColor : "white" , color : iconColour , weight : 3 , radius : 5 , fillOpacity :1 } ) ;
916929 } else {
917- var marker = L . circleMarker ( [ landing . lat , landing . lon ] , { fillColor : "grey" , color : iconColour , weight : 2 , radius : 5 , fillOpacity :1 } ) ;
930+ var marker = L . circleMarker ( [ landing . lat , landing . lon ] , { fillColor : "grey" , color : iconColour , weight : 3 , radius : 5 , fillOpacity :1 } ) ;
918931 }
919932
920933 marker . bindPopup ( popup ) ;
@@ -929,6 +942,7 @@ function drawHistorical (data, station) {
929942function deleteHistorical ( station ) {
930943 var popup = $ ( "#popup" + station ) ;
931944 var deleteHistorical = popup . find ( "#deleteHistorical" ) ;
945+ var historicalDelete = $ ( "#historicalControlButton" ) ;
932946
933947 deleteHistorical . hide ( ) ;
934948
@@ -939,6 +953,45 @@ function deleteHistorical (station) {
939953 }
940954
941955 delete historicalPlots [ station ] ;
956+
957+ var otherSondes = false ;
958+
959+ for ( station in historicalPlots ) {
960+ if ( historicalPlots . hasOwnProperty ( station ) ) {
961+ if ( Object . keys ( historicalPlots [ station ] . sondes ) . length > 1 ) {
962+ otherSondes = true ;
963+ }
964+ }
965+ }
966+
967+ if ( ! otherSondes ) historicalDelete . hide ( ) ;
968+ }
969+
970+ function deleteHistoricalButton ( ) {
971+ var historicalDelete = $ ( "#historicalControlButton" ) ;
972+
973+ for ( station in historicalPlots ) {
974+ if ( historicalPlots . hasOwnProperty ( station ) ) {
975+ historicalPlots [ station ] . data . drawing = false ;
976+ for ( let serial in historicalPlots [ station ] . sondes ) {
977+ map . removeLayer ( historicalPlots [ station ] . sondes [ serial ] . marker ) ;
978+ }
979+ var popup = $ ( "#popup" + station ) ;
980+ var deleteHistorical = popup . find ( "#deleteHistorical" ) ;
981+ deleteHistorical . hide ( ) ;
982+ }
983+ }
984+
985+ for ( i = 0 ; i < historicalAjax . length ; i ++ ) {
986+ historicalAjax [ i ] . abort ( ) ;
987+ }
988+
989+ historicalAjax = [ ] ;
990+
991+ historicalPlots = { } ;
992+
993+ historicalDelete . hide ( ) ;
994+
942995}
943996
944997// Master function to display historic summaries
@@ -982,8 +1035,6 @@ function showHistorical (station, marker) {
9821035 historicalPlots [ station ] = { } ;
9831036 historicalPlots [ station ] . sondes = { } ;
9841037 historicalPlots [ station ] . data = { } ;
985- historicalPlots [ station ] . data . minTime = 1511960400000 ;
986- historicalPlots [ station ] . data . maxTime = dateNow . getTime ( ) ;
9871038 }
9881039
9891040 // Get station location to fetch recoveries
@@ -1008,26 +1059,36 @@ function showHistorical (station, marker) {
10081059 processHistorical ( ) ;
10091060 }
10101061 } ) ;
1062+ } else {
1063+ processHistorical ( ) ;
10111064 }
10121065
10131066 function processHistorical ( ) {
1067+ var historicalDelete = $ ( "#historicalControlButton" ) ;
1068+ historicalDelete . show ( ) ;
1069+
1070+ historicalPlots [ station ] . data . drawing = true ;
1071+
10141072 for ( let i = 0 ; i < sondes . length ; i ++ ) {
1015- downloadHistorical ( sondes [ i ] ) . done ( handleData ) . fail ( handleError ) ; ;
1073+ downloadHistorical ( sondes [ i ] ) . done ( handleData ) . fail ( handleError ) ;
10161074 }
10171075
10181076 var completed = 0 ;
10191077
10201078 function handleData ( data ) {
10211079 completed += 1 ;
1022- drawHistorical ( data , station ) ;
1080+ try {
1081+ drawHistorical ( data , station ) ;
1082+ } catch ( e ) { } ;
10231083 if ( completed == sondes . length ) {
10241084 submit . show ( ) ;
10251085 submitLoading . hide ( ) ;
1026- deleteHistorical . show ( ) ;
1086+ if ( historicalPlots [ station ] . data . drawing ) deleteHistorical . show ( ) ;
10271087 // If modal is closed the contents needs to be forced updated
10281088 if ( ! realpopup . isOpen ( ) ) {
10291089 realpopup . setContent ( "<div id='popup" + station + "'>" + popup . html ( ) + "</div>" ) ;
10301090 }
1091+ historicalPlots [ station ] . data . drawing = false ;
10311092 }
10321093 }
10331094
@@ -1036,11 +1097,12 @@ function showHistorical (station, marker) {
10361097 if ( completed == sondes . length ) {
10371098 submit . show ( ) ;
10381099 submitLoading . hide ( ) ;
1039- deleteHistorical . show ( ) ;
1040- }
1041- // If modal is closed the contents needs to be forced updated
1042- if ( ! realpopup . isOpen ( ) ) {
1043- realpopup . setContent ( "<div id='popup" + station + "'>" + popup . html ( ) + "</div>" ) ;
1100+ if ( historicalPlots [ station ] . data . drawing ) deleteHistorical . show ( ) ;
1101+ // If modal is closed the contents needs to be forced updated
1102+ if ( ! realpopup . isOpen ( ) ) {
1103+ realpopup . setContent ( "<div id='popup" + station + "'>" + popup . html ( ) + "</div>" ) ;
1104+ }
1105+ historicalPlots [ station ] . data . drawing = false ;
10441106 }
10451107 }
10461108 }
0 commit comments