2015-02-13 15:24:41 +00:00
|
|
|
define(
|
|
|
|
[
|
2015-10-02 19:52:29 +00:00
|
|
|
'knockout', 'jquery', 'leaflet', 'text!./map.html', 'stamen'
|
2015-02-13 15:24:41 +00:00
|
|
|
],
|
2015-09-27 18:39:47 +00:00
|
|
|
function(ko, jquery, leaflet, htmlString) {
|
|
|
|
|
|
|
|
if (!L.AircraftMarker) {
|
|
|
|
L.AircraftMarker = L.Marker.extend({
|
|
|
|
options : {
|
|
|
|
draggable : true,
|
|
|
|
clickable : true,
|
|
|
|
keyboard : false,
|
|
|
|
zIndexOffset : 10000,
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize : function(latlng, options) {
|
|
|
|
var extraIconClass = '';
|
|
|
|
if (options && options.className) {
|
|
|
|
extraIconClass = ' ' + options.className;
|
|
|
|
}
|
|
|
|
L.Marker.prototype.initialize(latlng, options);
|
|
|
|
L.Util.setOptions(this, options);
|
|
|
|
this.setIcon(L.divIcon({
|
|
|
|
iconSize : null,
|
|
|
|
className : 'aircraft-marker-icon' + extraIconClass,
|
|
|
|
html : '<div data-bind="component: { ' + 'name: \'AircraftMarker\', '
|
|
|
|
+ 'params: { rotate: heading, label: labelLines } ' + '}"></div>',
|
|
|
|
}));
|
|
|
|
|
|
|
|
this.isDragging = false;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
2015-02-13 15:24:41 +00:00
|
|
|
|
2018-08-31 06:06:42 +00:00
|
|
|
// Builds the marker for my aircraft
|
2015-07-30 19:03:06 +00:00
|
|
|
L.aircraftMarker = function(latlng, options) {
|
2018-08-31 06:06:42 +00:00
|
|
|
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) {
|
2015-07-30 19:03:06 +00:00
|
|
|
return new L.AircraftMarker(latlng, options);
|
2015-07-08 11:27:54 +00:00
|
|
|
}
|
2015-09-27 18:39:47 +00:00
|
|
|
}
|
2015-07-30 19:03:06 +00:00
|
|
|
|
|
|
|
function ViewModel(params, componentInfo) {
|
|
|
|
var self = this;
|
2015-07-08 11:27:54 +00:00
|
|
|
|
2015-02-13 15:24:41 +00:00
|
|
|
self.element = componentInfo.element;
|
|
|
|
self.followAircraft = ko.observable(true);
|
|
|
|
|
2015-02-25 20:42:46 +00:00
|
|
|
self.toggleFollowAircraft = function(a) {
|
|
|
|
self.followAircraft(!self.followAircraft());
|
2015-09-27 18:39:47 +00:00
|
|
|
if (self.followAircraft()) {
|
2015-08-07 16:53:12 +00:00
|
|
|
self.map.setView(self.mapCenter());
|
|
|
|
}
|
2015-02-25 20:42:46 +00:00
|
|
|
}
|
2015-09-27 18:39:47 +00:00
|
|
|
|
2015-02-13 15:24:41 +00:00
|
|
|
self.altitude = ko.observable(0).extend({
|
|
|
|
fgprop : 'altitude'
|
|
|
|
});
|
|
|
|
|
|
|
|
self.tas = ko.observable(0).extend({
|
2015-02-16 13:51:54 +00:00
|
|
|
fgprop : 'groundspeed'
|
2015-02-13 15:24:41 +00:00
|
|
|
});
|
|
|
|
|
2015-02-25 08:35:23 +00:00
|
|
|
if (params && params.css) {
|
2015-02-25 08:27:19 +00:00
|
|
|
for ( var p in params.css) {
|
2015-03-27 23:39:20 +00:00
|
|
|
jquery(self.element).css(p, params.css[p]);
|
2015-02-25 08:27:19 +00:00
|
|
|
}
|
2015-02-25 08:35:23 +00:00
|
|
|
}
|
2015-03-27 23:39:20 +00:00
|
|
|
if (jquery(self.element).height() < 1) {
|
|
|
|
jquery(self.element).css("min-height", jquery(self.element).width());
|
2015-02-25 08:35:23 +00:00
|
|
|
}
|
2015-02-13 15:24:41 +00:00
|
|
|
|
2015-02-25 15:44:47 +00:00
|
|
|
var MapOptions = {
|
2015-02-25 20:42:46 +00:00
|
|
|
attributionControl : false,
|
2015-09-27 18:39:47 +00:00
|
|
|
dragging : false,
|
2015-02-25 15:44:47 +00:00
|
|
|
};
|
2015-02-25 20:42:46 +00:00
|
|
|
|
|
|
|
if (params && params.map) {
|
|
|
|
for ( var p in params.map) {
|
2015-02-25 15:44:47 +00:00
|
|
|
MapOptions[p] = params.map[p];
|
|
|
|
}
|
|
|
|
MapOptions = params.map;
|
|
|
|
}
|
2015-02-13 15:24:41 +00:00
|
|
|
|
2015-02-25 20:42:46 +00:00
|
|
|
self.map = leaflet.map(self.element, MapOptions).setView([
|
2015-02-25 15:44:47 +00:00
|
|
|
53.5, 10.0
|
|
|
|
], MapOptions.zoom || 13);
|
2015-02-13 15:24:41 +00:00
|
|
|
|
2015-09-27 18:39:47 +00:00
|
|
|
if (params && params.on) {
|
|
|
|
for ( var p in params.on) {
|
2015-03-09 19:59:17 +00:00
|
|
|
var h = params.on[p];
|
2015-09-27 18:39:47 +00:00
|
|
|
if (typeof (h) === 'function')
|
|
|
|
self.map.on(p, h);
|
2015-03-09 19:59:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 15:44:47 +00:00
|
|
|
var baseLayers = {
|
2015-09-27 18:39:47 +00:00
|
|
|
"OpenStreetMaps" : new leaflet.TileLayer(
|
|
|
|
'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
|
|
|
|
{
|
|
|
|
maxZoom : 18,
|
|
|
|
attribution : 'Map data © <a target="_blank" href="http://openstreetmap.org">OpenStreetMap</a> contributors'
|
2015-10-02 19:52:29 +00:00
|
|
|
}),
|
|
|
|
"Stamen - toner" : new L.StamenTileLayer("toner"),
|
|
|
|
"Stamen - toner-background" : new L.StamenTileLayer("toner-background"),
|
|
|
|
"Stamen - toner-hybrid" : new L.StamenTileLayer("toner-hybrid"),
|
|
|
|
"Stamen - watercolor" : new L.StamenTileLayer("watercolor"),
|
|
|
|
"None" : new L.layerGroup(),
|
2015-02-25 15:44:47 +00:00
|
|
|
}
|
|
|
|
self.map.addLayer(baseLayers["OpenStreetMaps"]);
|
2015-02-25 20:42:46 +00:00
|
|
|
|
2015-09-27 18:39:47 +00:00
|
|
|
if (params && params.hasFollowAircraft) {
|
2015-02-25 20:42:46 +00:00
|
|
|
self.map.on('dragstart', function(e) {
|
|
|
|
self.followAircraft(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
var followAircraftControl = L.control();
|
|
|
|
|
|
|
|
followAircraftControl.onAdd = function(map) {
|
2022-07-31 15:06:56 +00:00
|
|
|
this._div = L.DomUtil.create('div', 'mapOption');
|
2022-07-31 14:35:15 +00:00
|
|
|
this._div.innerHTML = '<img src="images/followAircraft.svg" title="Center Map on Aircraft Position" data-bind="css: { mapOptionEnabled: followAircraft() }, click: toggleFollowAircraft"/>';
|
2015-02-25 20:42:46 +00:00
|
|
|
return this._div;
|
|
|
|
}
|
|
|
|
followAircraftControl.addTo(self.map);
|
|
|
|
}
|
|
|
|
|
2022-07-31 15:06:56 +00:00
|
|
|
if (params && params.hasToggleOwnAircraftDrag) {
|
|
|
|
var ownAircraftDragControl = L.control();
|
|
|
|
|
|
|
|
ownAircraftDragControl.onAdd = function(map) {
|
|
|
|
this._div = L.DomUtil.create('div', 'mapOption');
|
|
|
|
this._div.innerHTML = '<img src="images/dragAircraft.svg" title="Allow dragging own aircraft" data-bind="css: { mapOptionEnabled: dragOwnAircraft() }, click: toggleDragOwnAircraft"/>';
|
|
|
|
return this._div;
|
|
|
|
}
|
|
|
|
ownAircraftDragControl.addTo(self.map);
|
|
|
|
}
|
|
|
|
|
2015-02-25 20:42:46 +00:00
|
|
|
if (params && params.overlays) {
|
2015-02-25 15:44:47 +00:00
|
|
|
L.control.layers(baseLayers, params.overlays).addTo(self.map);
|
|
|
|
}
|
2015-02-25 20:42:46 +00:00
|
|
|
|
2015-09-27 18:39:47 +00:00
|
|
|
if (params && params.selectedOverlays && params.overlays) {
|
2015-03-10 20:48:11 +00:00
|
|
|
params.selectedOverlays.forEach(function(ovl) {
|
2018-08-31 06:06:42 +00:00
|
|
|
if(params.overlays[ovl] != undefined){
|
|
|
|
params.overlays[ovl].addTo(self.map);
|
|
|
|
}
|
|
|
|
|
2015-03-10 20:48:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-05 18:40:38 +00:00
|
|
|
if (params && params.scale) {
|
2015-09-27 18:39:47 +00:00
|
|
|
L.control.scale(params.scale).addTo(self.map);
|
2015-03-05 18:40:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 15:06:56 +00:00
|
|
|
self.aircraftMarker = L.aircraftMarker(self.map.getCenter(), {
|
2015-09-27 18:39:47 +00:00
|
|
|
className : 'you-aircraft-marker-icon'
|
|
|
|
});
|
2015-02-13 15:24:41 +00:00
|
|
|
|
2022-07-31 15:06:56 +00:00
|
|
|
self.aircraftMarker.addTo(self.map);
|
2015-02-13 15:24:41 +00:00
|
|
|
|
2015-02-13 15:50:29 +00:00
|
|
|
var aircraftTrack = L.polyline([], {
|
|
|
|
color : 'red'
|
|
|
|
}).addTo(self.map);
|
|
|
|
|
2015-02-13 15:24:41 +00:00
|
|
|
self.latitude = ko.observable(0).extend({
|
|
|
|
fgprop : 'latitude'
|
|
|
|
});
|
|
|
|
|
|
|
|
self.longitude = ko.observable(0).extend({
|
|
|
|
fgprop : 'longitude'
|
|
|
|
});
|
|
|
|
|
|
|
|
self.heading = ko.observable(0).extend({
|
2015-02-27 15:29:26 +00:00
|
|
|
fgprop : 'true-heading'
|
2015-02-13 15:24:41 +00:00
|
|
|
});
|
|
|
|
|
2015-03-03 12:30:13 +00:00
|
|
|
self.position = ko.pureComputed(function() {
|
2015-02-13 15:24:41 +00:00
|
|
|
return leaflet.latLng(self.latitude(), self.longitude());
|
|
|
|
}).extend({
|
|
|
|
rateLimit : 200
|
|
|
|
});
|
|
|
|
|
|
|
|
self.position.subscribe(function(newValue) {
|
2022-07-31 15:06:56 +00:00
|
|
|
if (!self.aircraftMarker.isDragging)
|
|
|
|
self.aircraftMarker.setLatLng(newValue);
|
2015-02-13 15:24:41 +00:00
|
|
|
});
|
|
|
|
|
2015-07-30 19:03:06 +00:00
|
|
|
self.labelLines = [
|
2015-09-27 18:39:47 +00:00
|
|
|
'You', ko.pureComputed(function() {
|
|
|
|
var h = Math.round(self.heading());
|
|
|
|
var t = Math.round(self.tas());
|
|
|
|
var a = Math.round(self.altitude());
|
|
|
|
return '' + h + "T " + t + "KTAS " + a + "ft";
|
|
|
|
}),
|
2015-07-30 19:03:06 +00:00
|
|
|
];
|
2015-02-13 15:24:41 +00:00
|
|
|
|
2015-03-03 12:30:13 +00:00
|
|
|
self.mapCenter = ko.pureComputed(function() {
|
2015-02-13 15:24:41 +00:00
|
|
|
return leaflet.latLng(self.latitude(), self.longitude());
|
|
|
|
}).extend({
|
|
|
|
rateLimit : 2000
|
|
|
|
});
|
|
|
|
|
2015-02-13 15:50:29 +00:00
|
|
|
self.aircraftTrailLength = 60;
|
|
|
|
|
2015-02-13 15:24:41 +00:00
|
|
|
self.mapCenter.subscribe(function(newValue) {
|
|
|
|
if (self.followAircraft()) {
|
|
|
|
self.map.setView(newValue);
|
|
|
|
}
|
2015-02-13 15:50:29 +00:00
|
|
|
|
|
|
|
var trail = aircraftTrack.getLatLngs();
|
|
|
|
while (trail.length > self.aircraftTrailLength)
|
|
|
|
trail.shift();
|
|
|
|
trail.push(newValue);
|
|
|
|
aircraftTrack.setLatLngs(trail);
|
2015-02-13 15:24:41 +00:00
|
|
|
});
|
2015-03-03 12:30:13 +00:00
|
|
|
|
|
|
|
var center = leaflet.latLng(self.latitude(), self.longitude());
|
2015-09-27 18:39:47 +00:00
|
|
|
self.map.setView(center);
|
2022-07-31 15:06:56 +00:00
|
|
|
self.aircraftMarker.setLatLng(center);
|
|
|
|
|
|
|
|
// Enable/disable own aircraft dragging
|
|
|
|
self.dragOwnAircraft = ko.observable(true);
|
|
|
|
|
|
|
|
self.toggleDragOwnAircraft = function(a) {
|
|
|
|
self.dragOwnAircraft(!self.dragOwnAircraft());
|
|
|
|
self.dragOwnAircraft() ? self.aircraftMarker.dragging.enable() : self.aircraftMarker.dragging.disable();
|
|
|
|
}
|
|
|
|
|
2015-01-25 12:02:20 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 20:48:11 +00:00
|
|
|
ViewModel.prototype.dispose = function() {
|
|
|
|
this.map.remove();
|
|
|
|
}
|
|
|
|
|
2015-02-13 15:24:41 +00:00
|
|
|
// Return component definition
|
|
|
|
return {
|
|
|
|
viewModel : {
|
|
|
|
createViewModel : function(params, componentInfo) {
|
|
|
|
return new ViewModel(params, componentInfo);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
template : htmlString,
|
|
|
|
};
|
2015-01-25 12:02:20 +00:00
|
|
|
});
|