Use History API to avoid page reload

This commit is contained in:
nemunaire 2018-06-06 20:51:10 +02:00
parent 64d578c3ba
commit 9efaf3d1ff

50
app.js
View File

@ -1,12 +1,5 @@
const BASE_API = "/api"; const BASE_API = "/api";
$("#lines").hide();
$("#line").attr("disabled", true);
$("#destinations").attr("disabled", true);
$("#stations").attr("disabled", true);
$("#traffic").hide();
$("#schedule").hide();
$("#loading").hide();
var nextSchedule = null; var nextSchedule = null;
var nextTraffic = null; var nextTraffic = null;
@ -38,7 +31,9 @@ $("#stations").change(function() {
$("#lines").submit(function(event) { $("#lines").submit(function(event) {
event.preventDefault(); event.preventDefault();
window.location.pathname = ["", $("#linesFilter").val(), $("#line").val(), $("#stations").val(), $("#destinations").val().length != 0?$("#destinations").val():""].join("/") var v = [$("#linesFilter").val(), $("#line").val(), $("#stations").val(), $("#destinations").val().length != 0?$("#destinations").val():""]
history.pushState(null, $("title").text(), "/" + v.join("/"));
main(v, false);
}); });
function findLines(type) { function findLines(type) {
@ -119,14 +114,37 @@ function getSchedule(type, code, station, way) {
}); });
} }
function main(uri) { function main(uri, fetchTrafic) {
$("#lines").hide();
$("#line").attr("disabled", true);
$("#destinations").attr("disabled", true);
$("#stations").attr("disabled", true);
$("#schedule").hide();
$("#loading").hide();
if (nextTraffic != null) {
clearTimeout(nextTraffic);
nextTraffic = null;
}
if (nextSchedule != null) {
clearTimeout(nextSchedule);
nextSchedule = null;
}
if (uri.length == 4 || uri.length == 3) { if (uri.length == 4 || uri.length == 3) {
if (uri[3] == "")
uri.pop();
if (uri.length == 3) if (uri.length == 3)
uri.push("A+R"); uri.push("A+R");
getSchedule(uri[0], uri[1], uri[2], uri[3]); getSchedule(uri[0], uri[1], uri[2], uri[3]);
getTraffic(uri[0], uri[1]); if (fetchTrafic)
getTraffic(uri[0], uri[1]);
} else if (uri.length == 1 || uri.length == 2) { } else if (uri.length == 1 || uri.length == 2) {
$("#traffic").hide();
$("#station").hide();
$("title").text("Application RATP");
switch(uri[0]) { switch(uri[0]) {
case "metros": case "metros":
case "rers": case "rers":
@ -138,13 +156,19 @@ function main(uri) {
$("#linesFilter").change(); $("#linesFilter").change();
break; break;
default: default:
window.location.pathname = window.location.pathname + "/.."; window.location.pathname = "/";
} }
} else if (uri.length == 0) { } else if (uri.length == 0) {
if ($("#linesFilter").val().length != 0) if ($("#linesFilter").val().length != 0)
main([ $("#linesFilter").val() ]); main([ $("#linesFilter").val() ], true);
else else
$("#lines").show(); $("#lines").show();
} }
} }
main(uri); main(uri, true);
window.onpopstate = function(event) {
main(window.location.pathname.split("/").filter(function(v) {
return v.length > 0;
}), true);
}