-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
125 lines (117 loc) · 4.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var map;
var lastProposedCities = [];
checkUrlParams();
initSearchByCityInput();
function copyLink() {
navigator.clipboard.writeText(window.location.href);
document.getElementById('copied').innerText = "Lien copié dans le presse-papier !";
}
function initSearchByCityInput() {
const input = document.getElementById("station-search-container-by-city-input");
input.addEventListener("input", function (e) {
function onAPIResultComplete(proposals) {
const cities = document.getElementById('filtered-cities');
cities.innerHTML = "";
lastProposedCities = proposals;
proposals.forEach(
function (city) {
var option = document.createElement('option');
option.value = city.label;
cities.appendChild(option);
});
}
document.getElementById("error-city").innerHTML = '';
createProposals(input.value, onAPIResultComplete);
});
}
function checkUrlParams() {
var params = new URLSearchParams(window.location.search);
if (params.has('lat') && params.has('long')) {
const lat = parseFloat(params.get('lat'));
const long = parseFloat(params.get('long'));
searchByCoords(lat, long);
}
}
function hideSearchFields() {
document.getElementById("search-btn-container").className += 'active-button';
document.getElementById("train-station").className += 'loader';
document.getElementById("stations").className += "station-element";
document.getElementById("station-search-container-by-city").className += 'active-button';
}
function searchByGeolocation() {
hideSearchFields();
var warning = document.createElement("p");
warning.innerText = 'Si rien ne se passe, c\'est peut-être que vous n\'avez pas accepté la géolocalisation.';
setTimeout(function () {
document.getElementById("trainLoader-buttonSearch-container").appendChild(warning);
}, 6000);
var geoSuccess = function (p) {
const userLat = p.coords.latitude;
const userLong = p.coords.longitude;
search(userLat, userLong);
};
navigator.geolocation.getCurrentPosition(geoSuccess);
}
function searchByCity() {
console.log("searchByCity")
const cityInput = document.getElementById("station-search-container-by-city-input").value;
console.log(cityInput);
const selectedCity = lastProposedCities.filter(u => u.label == cityInput.trim())[0];
console.log(selectedCity);
if (selectedCity) {
hideSearchFields();
search(selectedCity.lat, selectedCity.lon, selectedCity.label);
}
}
function searchByCoords(userLat, userLong) {
hideSearchFields();
search(userLat, userLong);
}
function search(userLat, userLong, positionLabel = 'Votre position') {
if (window.location.search === '') {
history.replaceState(null, '', `/?lat=${userLat}&long=${userLong}`);
}
var mapContainerElement = document.getElementById('mapcontainer');
mapContainerElement.innerHTML = "<div id='mapid'></div>";
mapContainerElement.className += "map-container-element";
map = L.map('mapid').setView([userLat, userLong], 11);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);
const foundStations = findNearestStations(stations, userLat, userLong);
var trainIcon = new L.Icon({
iconUrl: './img/tchou.png',
iconSize: [40, 40]
});
var homeIcon = new L.Icon({
iconUrl: './img/yourPosition.png',
iconSize: [40, 51]
});
foundStations.forEach(function (e) {
L.marker([latitude(e), longitude(e)], {
icon: trainIcon
}).addTo(map).bindPopup("<p style='font-size:150%'>" +
"<a target='_blank' style='color: #D35400; text-decoration: none;' href='https://www.openstreetmap.org/?mlat=" + latitude(e) + "&mlon=" + longitude(e) + "'>" + stationName(e) + "</a>" +
"<br><a href='https://www.trainline.fr/search/" + encodeURI(stationName(e).replace(/'/g, "%27")) + "/' target='_blank' style='color: #D35400; font-weight: 700; text-decoration: none;'>Partir avec trainline</a>" +
"<br>" +
"</p>"
);
});
L.marker([userLat, userLong], {
icon: homeIcon
}).addTo(map).bindPopup('<p style="font-size:150%">' + positionLabel + '</p>');
var showedStations;
var others = ""
if (foundStations.length >= 24) {
showedStations = foundStations.slice(0, 23);
others = "<li><b>et d'autres encore...<b></li>"
} else {
showedStations = foundStations;
}
stationsHtml = document.getElementById('stations');
stationsHtml.innerHTML = '' +
'<h2>Voici les gares trouvées :</h2><ul>' +
showedStations.map(gare => '<li>' + stationName(gare) + '</li>').join('') +
others + '</ul>';
document.getElementById("share-link").style.visibility = 'visible';
var mobileHeaderTitle = document.getElementById("header");
mobileHeaderTitle.className += "mobile";
}