factorisation du code client
This commit is contained in:
parent
9c185c241f
commit
b0ccf9284d
4 changed files with 162 additions and 338 deletions
|
@ -19,6 +19,8 @@
|
|||
<style type="text/css">
|
||||
#map { height: 500px; }
|
||||
</style>
|
||||
|
||||
<script type="text/javascript" src="utils.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="map"></div><br/>
|
||||
|
@ -26,58 +28,10 @@
|
|||
<input id="popup"/><button id="sendPopup">Send popup to all clients</button><br/>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
var protocol = location.protocol;
|
||||
var server = location.hostname;
|
||||
var port = location.port;
|
||||
var socket = io.connect({rejectUnauthorized: false},
|
||||
protocol+"://"+server+":"+port);
|
||||
|
||||
var id = -1;
|
||||
var names = [];
|
||||
var colors = [];
|
||||
var markers = [];
|
||||
var icons = [
|
||||
L.icon({
|
||||
iconUrl: 'def.png',
|
||||
iconSize: [10, 10],
|
||||
iconAnchor: [5, 5],
|
||||
popupAnchor: [5, 5],
|
||||
//shadowUrl: 'my-icon-shadow.png',
|
||||
//shadowSize: [68, 95],
|
||||
//shadowAnchor: [22, 94]
|
||||
}),
|
||||
L.icon({
|
||||
iconUrl: 'track.png',
|
||||
iconSize: [10, 10],
|
||||
iconAnchor: [5, 5],
|
||||
popupAnchor: [5, 5],
|
||||
//shadowUrl: 'my-icon-shadow.png',
|
||||
//shadowSize: [68, 95],
|
||||
//shadowAnchor: [22, 94]
|
||||
})
|
||||
];
|
||||
var self_icons = icons;
|
||||
var icon_invisible =
|
||||
L.icon({
|
||||
iconUrl: 'invi.png',
|
||||
iconSize: [10, 10],
|
||||
iconAnchor: [5, 5],
|
||||
popupAnchor: [5, 5],
|
||||
//shadowUrl: 'my-icon-shadow.png',
|
||||
//shadowSize: [68, 95],
|
||||
//shadowAnchor: [22, 94]
|
||||
});
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// INIT MAP
|
||||
// SETUP MAP
|
||||
|
||||
var map = L.map('map').setView([48.8448, 2.3550], 13);
|
||||
|
||||
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||||
maxZoom: 19,
|
||||
attribution: '© OpenStreetMap'
|
||||
}).addTo(map);
|
||||
setup_map();
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// UPDATE MAP
|
||||
|
@ -93,13 +47,6 @@
|
|||
socket.emit("admin");
|
||||
});
|
||||
|
||||
socket.on("setName", function(data){
|
||||
console.log("setName", data);
|
||||
names[data.id] = data.name;
|
||||
if(data.id in markers)
|
||||
markers[data.id].bindPopup(data.name);
|
||||
});
|
||||
|
||||
socket.on("changeColor", function(data){
|
||||
console.log("changeColor", data);
|
||||
if(data.id in colors && colors[data.id] == -1 && data.id in markers)
|
||||
|
@ -116,25 +63,7 @@
|
|||
}
|
||||
});
|
||||
|
||||
socket.on("moving", function(data){
|
||||
console.log("moving", data);
|
||||
if(!(data.id in markers)){
|
||||
if(!(data.id in colors))
|
||||
colors[data.id] = 0;
|
||||
var icon;
|
||||
if(data.id == id)
|
||||
icon = self_icons[colors[data.id]];
|
||||
else
|
||||
icon = icons[colors[data.id]];
|
||||
markers[data.id] = L.marker(data.position, {"icon": icon}).addTo(map);
|
||||
if(data.id == id)
|
||||
markers[data.id].setOpacity(0);
|
||||
if(data.id in names)
|
||||
markers[data.id].bindPopup(names[data.id]);
|
||||
} else{
|
||||
markers[data.id].setLatLng(data.position);
|
||||
}
|
||||
});
|
||||
setup_socket_common();
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// INTERACTION
|
||||
|
@ -145,24 +74,6 @@
|
|||
socket.emit("popup", {"content": input.value});
|
||||
});
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// GEOLOCALISATION
|
||||
|
||||
function geoLoc_success(pos) {
|
||||
socket.emit("geoLoc", {"position": [pos.coords.latitude, pos.coords.longitude]});
|
||||
}
|
||||
|
||||
function geoLoc_error(err) {
|
||||
console.error(`ERROR(${err.code}): ${err.message}`);
|
||||
}
|
||||
|
||||
var options = {
|
||||
enableHighAccuracy: false,
|
||||
timeout: 5000,
|
||||
maximumAge: 0
|
||||
};
|
||||
|
||||
id = navigator.geolocation.watchPosition(geoLoc_success, geoLoc_error, options);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
<style type="text/css">
|
||||
#map { height: 500px; }
|
||||
</style>
|
||||
|
||||
<script type="text/javascript" src="utils.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="map"></div><br/>
|
||||
|
@ -27,120 +29,17 @@
|
|||
<input id="message"/><button id="sendMessage">Send message to base</button><br/>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
var protocol = location.protocol;
|
||||
var server = location.hostname;
|
||||
var port = location.port;
|
||||
var socket = io.connect({rejectUnauthorized: false},
|
||||
protocol+"://"+server+":"+port);
|
||||
|
||||
var id = -1;
|
||||
var names = [];
|
||||
var colors = [];
|
||||
var markers = [];
|
||||
var icons = [
|
||||
L.icon({
|
||||
iconUrl: 'def.png',
|
||||
iconSize: [10, 10],
|
||||
iconAnchor: [5, 5],
|
||||
popupAnchor: [5, 5],
|
||||
//shadowUrl: 'my-icon-shadow.png',
|
||||
//shadowSize: [68, 95],
|
||||
//shadowAnchor: [22, 94]
|
||||
}),
|
||||
L.icon({
|
||||
iconUrl: 'track.png',
|
||||
iconSize: [10, 10],
|
||||
iconAnchor: [5, 5],
|
||||
popupAnchor: [5, 5],
|
||||
//shadowUrl: 'my-icon-shadow.png',
|
||||
//shadowSize: [68, 95],
|
||||
//shadowAnchor: [22, 94]
|
||||
})
|
||||
];
|
||||
var self_icons = icons;
|
||||
var self_invisible =
|
||||
L.icon({
|
||||
iconUrl: 'invi.png',
|
||||
iconSize: [10, 10],
|
||||
iconAnchor: [5, 5],
|
||||
popupAnchor: [5, 5],
|
||||
//shadowUrl: 'my-icon-shadow.png',
|
||||
//shadowSize: [68, 95],
|
||||
//shadowAnchor: [22, 94]
|
||||
});
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// INIT MAP
|
||||
// SETUP MAP
|
||||
|
||||
var map = L.map('map').setView([48.8448, 2.3550], 13);
|
||||
|
||||
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||||
maxZoom: 19,
|
||||
attribution: '© OpenStreetMap'
|
||||
}).addTo(map);
|
||||
setup_map();
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// UPDATE MAP
|
||||
|
||||
socket.on("yourId", function(data){
|
||||
console.log("yourId", data);
|
||||
id = data.id;
|
||||
if(!(id in colors))
|
||||
colors[id] = 0;
|
||||
if(id in markers)
|
||||
markers[id].setIcon(self_icons[colors[id]]);
|
||||
});
|
||||
setup_socket_not_admin();
|
||||
setup_socket_common();
|
||||
|
||||
socket.on("setName", function(data){
|
||||
console.log("setName", data);
|
||||
names[data.id] = data.name;
|
||||
if(data.id in markers)
|
||||
markers[data.id].bindPopup(data.name);
|
||||
});
|
||||
|
||||
socket.on("changeColor", function(data){
|
||||
console.log("changeColor", data);
|
||||
if(data.id in colors && colors[data.id] == -1 && data.id in markers)
|
||||
markers[data.id].addTo(map);
|
||||
colors[data.id] = data.color;
|
||||
if(data.id in markers){
|
||||
if(data.id == id)
|
||||
if(data.color == -1)
|
||||
markers[data.id].setIcon(self_invisible);
|
||||
else
|
||||
markers[data.id].setIcon(self_icons[data.color]);
|
||||
else
|
||||
if(data.color == -1)
|
||||
markers[data.id].setLatLng([0,0]);
|
||||
else
|
||||
markers[data.id].setIcon(icons[data.color]);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("moving", function(data){
|
||||
console.log("moving", data);
|
||||
if(!(data.id in markers)){
|
||||
if(!(data.id in colors))
|
||||
colors[data.id] = 0;
|
||||
var icon;
|
||||
if(data.id == id)
|
||||
icon = self_icons[colors[data.id]];
|
||||
else
|
||||
icon = icons[colors[data.id]];
|
||||
markers[data.id] = L.marker(data.position, {"icon": icon}).addTo(map);
|
||||
if(data.id == id)
|
||||
markers[data.id].setZIndexOffset(10000);
|
||||
if(data.id in names)
|
||||
markers[data.id].bindPopup(names[data.id]);
|
||||
} else{
|
||||
markers[data.id].setLatLng(data.position);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("popup", function(data){
|
||||
alert(data.content);
|
||||
});
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// SETTINGS -- NAME AND COLOR
|
||||
|
@ -160,21 +59,7 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
// GEOLOCALISATION
|
||||
|
||||
function geoLoc_success(pos) {
|
||||
socket.emit("geoLoc", {"position": [pos.coords.latitude, pos.coords.longitude]});
|
||||
}
|
||||
|
||||
function geoLoc_error(err) {
|
||||
console.error(`ERROR(${err.code}): ${err.message}`);
|
||||
}
|
||||
|
||||
var options = {
|
||||
enableHighAccuracy: false,
|
||||
timeout: 5000,
|
||||
maximumAge: 0
|
||||
};
|
||||
|
||||
id = navigator.geolocation.watchPosition(geoLoc_success, geoLoc_error, options);
|
||||
setup_geoLoc();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
142
static/utils.js
Normal file
142
static/utils.js
Normal file
|
@ -0,0 +1,142 @@
|
|||
var protocol = location.protocol;
|
||||
var server = location.hostname;
|
||||
var port = location.port;
|
||||
var socket = io.connect({rejectUnauthorized: false},
|
||||
protocol+"://"+server+":"+port);
|
||||
|
||||
var id = -1;
|
||||
var names = [];
|
||||
var colors = [];
|
||||
var markers = [];
|
||||
var icons = [
|
||||
L.icon({
|
||||
iconUrl: 'def.png',
|
||||
iconSize: [10, 10],
|
||||
iconAnchor: [5, 5],
|
||||
popupAnchor: [5, 5],
|
||||
//shadowUrl: 'my-icon-shadow.png',
|
||||
//shadowSize: [68, 95],
|
||||
//shadowAnchor: [22, 94]
|
||||
}),
|
||||
L.icon({
|
||||
iconUrl: 'track.png',
|
||||
iconSize: [10, 10],
|
||||
iconAnchor: [5, 5],
|
||||
popupAnchor: [5, 5],
|
||||
//shadowUrl: 'my-icon-shadow.png',
|
||||
//shadowSize: [68, 95],
|
||||
//shadowAnchor: [22, 94]
|
||||
})
|
||||
];
|
||||
var self_icons = icons;
|
||||
var self_invisible =
|
||||
L.icon({
|
||||
iconUrl: 'invi.png',
|
||||
iconSize: [10, 10],
|
||||
iconAnchor: [5, 5],
|
||||
popupAnchor: [5, 5],
|
||||
//shadowUrl: 'my-icon-shadow.png',
|
||||
//shadowSize: [68, 95],
|
||||
//shadowAnchor: [22, 94]
|
||||
});
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// INIT MAP
|
||||
|
||||
var map;
|
||||
|
||||
function setup_map(){
|
||||
map = L.map('map').setView([48.8448, 2.3550], 13);
|
||||
|
||||
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||||
maxZoom: 19,
|
||||
attribution: '© OpenStreetMap'
|
||||
}).addTo(map);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// UPDATE MAP
|
||||
|
||||
function setup_socket_common(){
|
||||
socket.on("moving", function(data){
|
||||
console.log("moving", data);
|
||||
if(!(data.id in markers)){
|
||||
if(!(data.id in colors))
|
||||
colors[data.id] = 0;
|
||||
var icon;
|
||||
if(data.id == id)
|
||||
icon = self_icons[colors[data.id]];
|
||||
else
|
||||
icon = icons[colors[data.id]];
|
||||
markers[data.id] = L.marker(data.position, {"icon": icon}).addTo(map);
|
||||
if(data.id == id)
|
||||
markers[data.id].setZIndexOffset(10000);
|
||||
if(data.id in names)
|
||||
markers[data.id].bindPopup(names[data.id]);
|
||||
} else{
|
||||
markers[data.id].setLatLng(data.position);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("popup", function(data){
|
||||
alert(data.content);
|
||||
});
|
||||
|
||||
socket.on("setName", function(data){
|
||||
console.log("setName", data);
|
||||
names[data.id] = data.name;
|
||||
if(data.id in markers)
|
||||
markers[data.id].bindPopup(data.name);
|
||||
});
|
||||
}
|
||||
|
||||
function setup_socket_not_admin(){
|
||||
socket.on("yourId", function(data){
|
||||
console.log("yourId", data);
|
||||
id = data.id;
|
||||
if(!(id in colors))
|
||||
colors[id] = 0;
|
||||
if(id in markers)
|
||||
markers[id].setIcon(self_icons[colors[id]]);
|
||||
});
|
||||
|
||||
socket.on("changeColor", function(data){
|
||||
console.log("changeColor", data);
|
||||
if(data.id in colors && colors[data.id] == -1 && data.id in markers)
|
||||
markers[data.id].addTo(map);
|
||||
colors[data.id] = data.color;
|
||||
if(data.id in markers){
|
||||
if(data.id == id)
|
||||
if(data.color == -1)
|
||||
markers[data.id].setIcon(self_invisible);
|
||||
else
|
||||
markers[data.id].setIcon(self_icons[data.color]);
|
||||
else
|
||||
if(data.color == -1)
|
||||
markers[data.id].setLatLng([0,0]);
|
||||
else
|
||||
markers[data.id].setIcon(icons[data.color]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// GEOLOCALISATION
|
||||
|
||||
function setup_geoLoc(){
|
||||
function geoLoc_success(pos) {
|
||||
socket.emit("geoLoc", {"position": [pos.coords.latitude, pos.coords.longitude]});
|
||||
}
|
||||
|
||||
function geoLoc_error(err) {
|
||||
console.error(`ERROR(${err.code}): ${err.message}`);
|
||||
}
|
||||
|
||||
var options = {
|
||||
enableHighAccuracy: false,
|
||||
timeout: 5000,
|
||||
maximumAge: 0
|
||||
};
|
||||
|
||||
navigator.geolocation.watchPosition(geoLoc_success, geoLoc_error, options);
|
||||
}
|
|
@ -19,6 +19,8 @@
|
|||
<style type="text/css">
|
||||
#map { height: 500px; }
|
||||
</style>
|
||||
|
||||
<script type="text/javascript" src="utils.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="map"></div><br/>
|
||||
|
@ -27,118 +29,16 @@
|
|||
<input type="number" id="color" min="-1" value="0"/><button id="setColor">Set color</button><br/>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
var protocol = location.protocol;
|
||||
var server = location.hostname;
|
||||
var port = location.port;
|
||||
var socket = io.connect({rejectUnauthorized: false},
|
||||
protocol+"://"+server+":"+port);
|
||||
|
||||
var id = -1;
|
||||
var names = [];
|
||||
var colors = [];
|
||||
var markers = [];
|
||||
var icons = [
|
||||
L.icon({
|
||||
iconUrl: 'def.png',
|
||||
iconSize: [10, 10],
|
||||
iconAnchor: [5, 5],
|
||||
popupAnchor: [5, 5],
|
||||
//shadowUrl: 'my-icon-shadow.png',
|
||||
//shadowSize: [68, 95],
|
||||
//shadowAnchor: [22, 94]
|
||||
}),
|
||||
L.icon({
|
||||
iconUrl: 'track.png',
|
||||
iconSize: [10, 10],
|
||||
iconAnchor: [5, 5],
|
||||
popupAnchor: [5, 5],
|
||||
//shadowUrl: 'my-icon-shadow.png',
|
||||
//shadowSize: [68, 95],
|
||||
//shadowAnchor: [22, 94]
|
||||
})
|
||||
];
|
||||
var self_icons = icons;
|
||||
var self_invisible =
|
||||
L.icon({
|
||||
iconUrl: 'invi.png',
|
||||
iconSize: [10, 10],
|
||||
iconAnchor: [5, 5],
|
||||
popupAnchor: [5, 5],
|
||||
//shadowUrl: 'my-icon-shadow.png',
|
||||
//shadowSize: [68, 95],
|
||||
//shadowAnchor: [22, 94]
|
||||
});
|
||||
|
||||
document.getElementById('color').max = icons.length - 1;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// INIT MAP
|
||||
|
||||
var map = L.map('map').setView([48.8448, 2.3550], 13);
|
||||
|
||||
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||||
maxZoom: 19,
|
||||
attribution: '© OpenStreetMap'
|
||||
}).addTo(map);
|
||||
// SETUP MAP
|
||||
|
||||
setup_map();
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// UPDATE MAP
|
||||
|
||||
socket.on("yourId", function(data){
|
||||
console.log("yourId", data);
|
||||
id = data.id;
|
||||
if(!(id in colors))
|
||||
colors[id] = 0;
|
||||
if(id in markers)
|
||||
markers[id].setIcon(self_icons[colors[id]]);
|
||||
});
|
||||
|
||||
socket.on("setName", function(data){
|
||||
console.log("setName", data);
|
||||
names[data.id] = data.name;
|
||||
if(data.id in markers)
|
||||
markers[data.id].bindPopup(data.name);
|
||||
});
|
||||
|
||||
socket.on("changeColor", function(data){
|
||||
console.log("changeColor", data);
|
||||
if(data.id in colors && colors[data.id] == -1 && data.id in markers)
|
||||
markers[data.id].addTo(map);
|
||||
colors[data.id] = data.color;
|
||||
if(data.id in markers){
|
||||
if(data.id == id)
|
||||
if(data.color == -1)
|
||||
markers[data.id].setIcon(self_invisible);
|
||||
else
|
||||
markers[data.id].setIcon(self_icons[data.color]);
|
||||
else
|
||||
if(data.color == -1)
|
||||
markers[data.id].setLatLng([0,0]);
|
||||
else
|
||||
markers[data.id].setIcon(icons[data.color]);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("moving", function(data){
|
||||
console.log("moving", data);
|
||||
if(!(data.id in markers)){
|
||||
if(!(data.id in colors))
|
||||
colors[data.id] = 0;
|
||||
var icon;
|
||||
if(data.id == id)
|
||||
icon = self_icons[colors[data.id]];
|
||||
else
|
||||
icon = icons[colors[data.id]];
|
||||
markers[data.id] = L.marker(data.position, {"icon": icon}).addTo(map);
|
||||
if(data.id == id)
|
||||
markers[data.id].setZIndexOffset(10000);
|
||||
if(data.id in names)
|
||||
markers[data.id].bindPopup(names[data.id]);
|
||||
} else{
|
||||
markers[data.id].setLatLng(data.position);
|
||||
}
|
||||
});
|
||||
setup_socket_not_admin();
|
||||
setup_socket_common();
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// SETTINGS -- NAME AND COLOR
|
||||
|
@ -158,21 +58,7 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
// GEOLOCALISATION
|
||||
|
||||
function geoLoc_success(pos) {
|
||||
socket.emit("geoLoc", {"position": [pos.coords.latitude, pos.coords.longitude]});
|
||||
}
|
||||
|
||||
function geoLoc_error(err) {
|
||||
console.error(`ERROR(${err.code}): ${err.message}`);
|
||||
}
|
||||
|
||||
var options = {
|
||||
enableHighAccuracy: false,
|
||||
timeout: 5000,
|
||||
maximumAge: 0
|
||||
};
|
||||
|
||||
id = navigator.geolocation.watchPosition(geoLoc_success, geoLoc_error, options);
|
||||
setup_geoLoc();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in a new issue