es5 with prototypes

This commit is contained in:
NatureFreshMilk 2019-02-01 12:58:26 +01:00
parent 39be159cda
commit 68f757b7d4
6 changed files with 107 additions and 116 deletions

View File

@ -1,4 +1,3 @@
var CoordinatesDisplay = (function(){
'use strict'; 'use strict';
// coord display // coord display
@ -20,11 +19,3 @@ var CoordinatesDisplay = (function(){
onRemove: function(map) { onRemove: function(map) {
} }
}); });
return {
create: function(opts){
return new CoordinatesDisplay(opts);
}
}
}())

View File

@ -1,11 +1,10 @@
var LayerManager = function(layers){ 'use strict';
this.addListener = function(listener){
};
this.getCurrentLayer = function(){
};
function LayerManager(layers){
}
LayerManager.prototype.addListener = function(listener){
}; };
LayerManager.prototype.getCurrentLayer = function(){
}; };

View File

@ -1,5 +1,6 @@
var RealtimeTileLayer = function(wsChannel){
'use strict'; 'use strict';
function RealtimeTileLayer(wsChannel){
var self = this; var self = this;
wsChannel.addListener("rendered-tile", function(tc){ wsChannel.addListener("rendered-tile", function(tc){
@ -11,17 +12,19 @@ var RealtimeTileLayer = function(wsChannel){
el.src = self.getTileSource(tc.layerid, tc.x, tc.y, tc.zoom, true); el.src = self.getTileSource(tc.layerid, tc.x, tc.y, tc.zoom, true);
} }
}); });
}
RealtimeTileLayer.prototype.getTileSource = function(layerId, x,y,zoom,cacheBust){
this.getTileSource = function(layerId, x,y,zoom,cacheBust){
return "api/tile/" + layerId + "/" + x + "/" + y + "/" + zoom + "?_=" + Date.now(); return "api/tile/" + layerId + "/" + x + "/" + y + "/" + zoom + "?_=" + Date.now();
} };
this.getImageId = function(layerId, x, y, zoom){ RealtimeTileLayer.prototype.getImageId = function(layerId, x, y, zoom){
return "tile-" + layerId + "/" + x + "/" + y + "/" + zoom; return "tile-" + layerId + "/" + x + "/" + y + "/" + zoom;
} };
RealtimeTileLayer.prototype.createLayer = function(layerId){
var self = this;
this.createLayer = function(layerId){
return L.TileLayer.extend({ return L.TileLayer.extend({
createTile: function(coords){ createTile: function(coords){
var tile = document.createElement('img'); var tile = document.createElement('img');
@ -31,6 +34,3 @@ var RealtimeTileLayer = function(wsChannel){
} }
}); });
}; };
};

View File

@ -1,29 +1,31 @@
var WebSocketChannel = function(){
'use strict'; 'use strict';
var wsUrl = location.protocol.replace("http", "ws") + "//" + location.host + location.pathname.substring(0, location.pathname.lastIndexOf("/")) + "/api/ws";
var listenerMap = {/* type -> [listeners] */}; function WebSocketChannel(){
this.wsUrl = location.protocol.replace("http", "ws") + "//" + location.host + location.pathname.substring(0, location.pathname.lastIndexOf("/")) + "/api/ws";
this.listenerMap = {/* type -> [listeners] */};
}
this.addListener = function(type, listener){ WebSocketChannel.prototype.addListener = function(type, listener){
var list = listenerMap[type]; var list = this.listenerMap[type];
if (!list){ if (!list){
list = []; list = [];
listenerMap[type] = list; this.listenerMap[type] = list;
} }
list.push(listener); list.push(listener);
}; };
this.connect = function(){ WebSocketChannel.prototype.connect = function(){
var ws = new WebSocket(wsUrl); var ws = new WebSocket(this.wsUrl);
var self = this;
ws.onmessage = function(e){ ws.onmessage = function(e){
var event = JSON.parse(e.data); var event = JSON.parse(e.data);
//rendered-tile, mapobject-created, mapobjects-cleared //rendered-tile, mapobject-created, mapobjects-cleared
var listeners = listenerMap[event.type]; var listeners = self.listenerMap[event.type];
if (listeners){ if (listeners){
listeners.forEach(function(listener){ self.listeners.forEach(function(listener){
listener(event.data); listener(event.data);
}); });
} }
@ -34,5 +36,3 @@ var WebSocketChannel = function(){
setTimeout(connect, 1000); setTimeout(connect, 1000);
} }
}; };
};

View File

@ -41,7 +41,7 @@ api.getConfig().then(function(cfg){
L.control.layers(layers, overlays).addTo(map); L.control.layers(layers, overlays).addTo(map);
var el = CoordinatesDisplay.create({ position: 'bottomleft' }); var el = new CoordinatesDisplay({ position: 'bottomleft' });
el.addTo(map); el.addTo(map);
}); });

View File

@ -1,4 +1,3 @@
var TravelnetOverlay = (function(){
'use strict'; 'use strict';
var TravelnetIcon = L.icon({ var TravelnetIcon = L.icon({
@ -9,7 +8,11 @@ var TravelnetOverlay = (function(){
popupAnchor: [0, -32] popupAnchor: [0, -32]
}); });
return L.LayerGroup.extend({ var TravelnetOverlay = L.LayerGroup.extend({
initialize: function() {
},
onAdd: function(map) { onAdd: function(map) {
console.log("TravelnetOverlay.onAdd", map); console.log("TravelnetOverlay.onAdd", map);
@ -27,5 +30,3 @@ var TravelnetOverlay = (function(){
console.log("TravelnetOverlay.onRemove"); console.log("TravelnetOverlay.onRemove");
} }
}); });
}());