From ca2f13baed8de8b774a0051e55c77f251a1573e9 Mon Sep 17 00:00:00 2001
From: portree_kid <keith.paterson@gmx.de>
Date: Fri, 31 Aug 2018 08:06:42 +0200
Subject: [PATCH] Make AI aircraft draggable in Phi

---
 Phi/topics/Map/AILayer.js | 46 ++++++++++++++++++++++++++---
 Phi/widgets/map.html      |  1 +
 Phi/widgets/map.js        | 61 ++++++++++++++++++++++++---------------
 3 files changed, 81 insertions(+), 27 deletions(-)

diff --git a/Phi/topics/Map/AILayer.js b/Phi/topics/Map/AILayer.js
index 1c9240e9b..d02d7715f 100644
--- a/Phi/topics/Map/AILayer.js
+++ b/Phi/topics/Map/AILayer.js
@@ -33,19 +33,53 @@
                     title : feature.properties.callsign,
                     alt : feature.properties.callsign,
                     riseOnHover : true,
+                    draggable : true,
                 };
 
+                var aiMarker = null;
                 if (feature.properties.type == "aircraft" || feature.properties.type == "multiplayer") {
                       var l1 = feature.properties.callsign,
                           l2 = feature.properties.heading + 'T ' + feature.properties.speed + 'KTAS ' + 
                                formatFL(feature.geometry.coordinates[2]);
-                      var m = L.aircraftMarker(latlng, { className: AITypeToCssClassMap[feature.properties.type] } );
-                      m.on('add', function(e) {
+                      
+                      aiMarker = L.aiAircraftMarker(latlng, { className: AITypeToCssClassMap[feature.properties.type] } );
+                      aiMarker.on('add', function(e) {
                           ko.applyBindings( new ViewModel(feature.properties.heading,l1,l2), e.target._icon);
                       });
-                      return m;
+                      aiMarker.options.draggable = true;
+                      //We can't drag multiplayer 
+                      if(feature.properties.type == "aircraft") {
+	                    	  aiMarker.on('dragstart', function(evt) {
+	                          evt.target.isDragging = true;
+	                      });
+	
+	                      aiMarker.on('dragend', function(evt) {
+	                          if( evt.target !== this)
+	                          	return;
+	                          var pos = evt.target.getLatLng();
+	
+	                          var props = {
+	                              name : "position",
+	                              children : [
+	                                      {
+	                                          name : "latitude-deg",
+	                                          value : pos.lat,
+	                                      }, {
+	                                          name : "longitude-deg",
+	                                          value : pos.lng,
+	                                      },
+	                              ],
+	                          };
+	                          $.post("json" + feature.properties.path, JSON.stringify(props));
+	                          evt.target.isDragging = false;
+	                      });
+                      }
+                      return aiMarker;
+                }
+                else if(feature.properties.type == "carrier"){
+                  aiMarker = new leaflet.Marker(latlng, options);
+                  return aiMarker;
                 }
-                return new leaflet.Marker(latlng, options);
             },
 
 //            onEachFeature : function(feature, layer) {
@@ -66,6 +100,7 @@
             this.updateId++;
         },
 
+        // Refresh method called every 10s to reload other aircraft
         updateId : 0,
         update : function(id) {
             var self = this;
@@ -92,6 +127,7 @@
             }
         },
 
+        // Builds the GeoJSON representation of AI, Multiplayer and Carriers
         aiPropsToGeoJson : function(props, types, bounds ) {
             var geoJSON = {
                 type : "FeatureCollection",
@@ -105,6 +141,7 @@
                     if (!child.getNode("valid").getValue())
                         return;
 
+                	var path = child.getPath();
                     var position = child.getNode("position");
                     var orientation = child.getNode("orientation");
                     var velocities = child.getNode("velocities");
@@ -141,6 +178,7 @@
                         },
                         "id" : id,
                         "properties" : {
+                        	"path" : path,
                             "type" : type,
                             "heading" : heading.toFixed(0),
                             "speed" : speed.toFixed(0),
diff --git a/Phi/widgets/map.html b/Phi/widgets/map.html
index 8d9a2f4a3..4a0801da8 100644
--- a/Phi/widgets/map.html
+++ b/Phi/widgets/map.html
@@ -20,6 +20,7 @@
     height: 20px;
     margin-left: -10px;
     margin-top: -10px;
+    cursor: move;
 }
 
 .ai-aircraft-marker-icon path {
diff --git a/Phi/widgets/map.js b/Phi/widgets/map.js
index bf1e22793..c4fe87433 100644
--- a/Phi/widgets/map.js
+++ b/Phi/widgets/map.js
@@ -29,33 +29,45 @@ define(
 
                         this.isDragging = false;
 
-                        this.on('dragstart', function(evt) {
-                            evt.target.isDragging = true;
-                        });
-
-                        this.on('dragend', function(evt) {
-                            var pos = evt.target.getLatLng();
-
-                            var props = {
-                                name : "position",
-                                children : [
-                                        {
-                                            name : "latitude-deg",
-                                            value : pos.lat,
-                                        }, {
-                                            name : "longitude-deg",
-                                            value : pos.lng,
-                                        },
-                                ],
-                            };
-                            $.post("/json/", JSON.stringify(props));
-                            evt.target.isDragging = false;
-                        });
                     },
 
                 });
 
+                // Builds the marker for my aircraft
                 L.aircraftMarker = function(latlng, options) {
+                	var m = new L.AircraftMarker(latlng, options);
+                    m.on('dragstart', function(evt) {
+                        if( evt.target !== this)
+                        	return;
+                        evt.target.isDragging = true;
+                    });
+
+                    m.on('dragend', function(evt) {
+                        if( evt.target !== this)
+                        	return;
+                        
+                        var pos = evt.target.getLatLng();
+
+                        var props = {
+                            name : "position",
+                            children : [
+                                    {
+                                        name : "latitude-deg",
+                                        value : pos.lat,
+                                    }, {
+                                        name : "longitude-deg",
+                                        value : pos.lng,
+                                    },
+                            ],
+                        };
+                        $.post("/json/", JSON.stringify(props));
+                        evt.target.isDragging = false;
+                    });
+                    return m;
+                }
+
+                //Builds a marker for a ai or multiplayer aircraft
+                L.aiAircraftMarker = function(latlng, options) {
                     return new L.AircraftMarker(latlng, options);
                 }
             }
@@ -150,7 +162,10 @@ define(
 
                 if (params && params.selectedOverlays && params.overlays) {
                     params.selectedOverlays.forEach(function(ovl) {
-                        params.overlays[ovl].addTo(self.map);
+                    	if(params.overlays[ovl] != undefined){
+                    		params.overlays[ovl].addTo(self.map);
+                    	}
+                        
                     });
                 }