diff --git a/app.js b/app.js index 2afc20e..ae2abbb 100644 --- a/app.js +++ b/app.js @@ -1,12 +1,5 @@ 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 nextTraffic = null; @@ -38,7 +31,9 @@ $("#stations").change(function() { $("#lines").submit(function(event) { 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) { @@ -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[3] == "") + uri.pop(); if (uri.length == 3) uri.push("A+R"); 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) { + $("#traffic").hide(); + $("#station").hide(); + $("title").text("Application RATP"); + switch(uri[0]) { case "metros": case "rers": @@ -138,13 +156,19 @@ function main(uri) { $("#linesFilter").change(); break; default: - window.location.pathname = window.location.pathname + "/.."; + window.location.pathname = "/"; } } else if (uri.length == 0) { if ($("#linesFilter").val().length != 0) - main([ $("#linesFilter").val() ]); + main([ $("#linesFilter").val() ], true); else $("#lines").show(); } } -main(uri); +main(uri, true); + +window.onpopstate = function(event) { + main(window.location.pathname.split("/").filter(function(v) { + return v.length > 0; + }), true); +}