2018-06-06 17:14:27 +00:00
|
|
|
const BASE_API = "/api";
|
|
|
|
|
|
|
|
var nextSchedule = null;
|
2018-06-06 17:27:14 +00:00
|
|
|
var nextTraffic = null;
|
2018-06-06 17:14:27 +00:00
|
|
|
|
|
|
|
var uri = window.location.pathname.split("/").filter(function(v) {
|
|
|
|
return v.length > 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#linesFilter").change(function() {
|
|
|
|
if ($("#linesFilter").val().length != 0) {
|
|
|
|
findLines($("#linesFilter").val());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#line").change(function() {
|
|
|
|
if ($("#line").val().length != 0) {
|
|
|
|
findDestinations($("#linesFilter").val(), $("#line").val());
|
|
|
|
findStations($("#linesFilter").val(), $("#line").val());
|
|
|
|
getTraffic($("#linesFilter").val(), $("#line").val());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#stations").change(function() {
|
|
|
|
if ($("#stations").val().length != 0) {
|
|
|
|
$("#lines button").removeAttr("disabled");
|
|
|
|
} else {
|
|
|
|
$("#lines button").attr("disabled", true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#lines").submit(function(event) {
|
|
|
|
event.preventDefault();
|
2018-06-06 18:51:10 +00:00
|
|
|
var v = [$("#linesFilter").val(), $("#line").val(), $("#stations").val(), $("#destinations").val().length != 0?$("#destinations").val():""]
|
|
|
|
history.pushState(null, $("title").text(), "/" + v.join("/"));
|
|
|
|
main(v, false);
|
2018-06-06 17:14:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function findLines(type) {
|
|
|
|
$.getJSON( BASE_API + "/lines/" + type, function( data ) {
|
|
|
|
$("#line").empty();
|
|
|
|
$("#line").append("<option/>");
|
|
|
|
$.each( data["result"][type], function( key, val ) {
|
|
|
|
$( "<option/>", { text: val["name"] + " (" + val["directions"] + ")", value: val["code"]} ).appendTo("#line");
|
|
|
|
});
|
|
|
|
$("#line").removeAttr("disabled");
|
|
|
|
|
|
|
|
if (uri.length == 2) {
|
|
|
|
$("#line").val(uri[1]);
|
|
|
|
$("#line").change();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function findDestinations(type, code) {
|
|
|
|
$.getJSON( BASE_API + "/destinations/" + type + "/" + code, function( data ) {
|
|
|
|
$("#destinations").empty();
|
|
|
|
$("#destinations").append("<option/>");
|
|
|
|
$.each( data["result"]["destinations"], function( key, val ) {
|
|
|
|
$( "<option/>", { text: val["name"], value: val["way"]} ).appendTo("#destinations");
|
|
|
|
});
|
|
|
|
$("#destinations").removeAttr("disabled");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function findStations(type, code) {
|
|
|
|
$.getJSON( BASE_API + "/stations/" + type + "/" + code, function( data ) {
|
|
|
|
$("#stations").empty();
|
|
|
|
$("#stations").append("<option/>");
|
|
|
|
$.each( data["result"]["stations"], function( key, val ) {
|
|
|
|
$( "<option/>", { text: val["name"], value: val["slug"]} ).appendTo("#stations");
|
|
|
|
});
|
|
|
|
$("#stations").removeAttr("disabled");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTraffic(type, code) {
|
|
|
|
$("#traffic").hide();
|
2018-06-06 17:27:14 +00:00
|
|
|
if (nextTraffic != null) {
|
|
|
|
clearTimeout(nextTraffic);
|
|
|
|
nextTraffic = null;
|
|
|
|
}
|
|
|
|
|
2018-06-06 17:14:27 +00:00
|
|
|
$.getJSON( BASE_API + "/traffic/" + type + "/" + code, function( data ) {
|
|
|
|
$("#traffic h3").text(data["result"]["line"] + " : " + data["result"]["title"]);
|
|
|
|
$("#traffic p").text(data["result"]["message"]);
|
|
|
|
$("#traffic").show();
|
2018-06-06 17:27:14 +00:00
|
|
|
|
|
|
|
nextTraffic = setTimeout(function() {
|
|
|
|
getSchedule(type, code);
|
|
|
|
}, 150000);
|
2018-06-06 17:14:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSchedule(type, code, station, way) {
|
|
|
|
if (nextSchedule != null) {
|
|
|
|
clearTimeout(nextSchedule);
|
|
|
|
nextSchedule = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$("#schedule").hide();
|
|
|
|
$.getJSON( BASE_API + "/schedules/" + type + "/" + code + "/" + station + "/" + way, function( data ) {
|
|
|
|
$("#schedule dl").empty();
|
|
|
|
$.each( data["result"]["schedules"], function( key, val ) {
|
|
|
|
$("<dt/>", { text: val["destination"] }).appendTo("#schedule dl");
|
|
|
|
$("<dd/>", { text: val["message"] }).appendTo("#schedule dl");
|
|
|
|
});
|
|
|
|
$("#schedule").show();
|
2018-06-06 19:07:26 +00:00
|
|
|
|
|
|
|
updateTime(new Date(data["_metadata"]["date"]));
|
2018-06-06 17:14:27 +00:00
|
|
|
|
|
|
|
nextSchedule = setTimeout(function() {
|
|
|
|
getSchedule(type, code, station, way);
|
|
|
|
}, 15000);
|
2018-06-06 20:06:11 +00:00
|
|
|
})
|
|
|
|
.fail(function(data) {
|
|
|
|
if (data.responseJSON && data.responseJSON.result && data.responseJSON.result.message)
|
|
|
|
{
|
|
|
|
$("#mainerror").text(data.responseJSON.result.message);
|
|
|
|
$("#mainerror").show();
|
|
|
|
}
|
|
|
|
});
|
2018-06-06 17:14:27 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 19:24:50 +00:00
|
|
|
function defineTag(type, station) {
|
|
|
|
station = station.replace(/\+/g, " ").replace(/\b[a-z]/g, function(letter) {
|
|
|
|
return letter.toUpperCase();
|
|
|
|
}).replace(/\b(de|du|sur)\b/gi, function(word) {
|
|
|
|
return word.toLowerCase();
|
|
|
|
});
|
|
|
|
$("#station").text(station);
|
|
|
|
$(".navbar-brand").hide();
|
|
|
|
$("#station").show();
|
|
|
|
$("title").text("RATP - Prochains passages à " + (type == "bus" ? "l'arrêt" : "la station") + " " + station);
|
|
|
|
}
|
|
|
|
|
2018-06-06 19:07:26 +00:00
|
|
|
function updateTime(time) {
|
|
|
|
var hours = time.getHours();
|
|
|
|
var minutes = time.getMinutes();
|
|
|
|
|
|
|
|
$("#update").text(hours + ":" + (minutes < 10 ? '0' + minutes : minutes));
|
|
|
|
}
|
|
|
|
updateTime(new Date());
|
|
|
|
|
2018-06-06 18:51:10 +00:00
|
|
|
function main(uri, fetchTrafic) {
|
|
|
|
$("#lines").hide();
|
|
|
|
$("#line").attr("disabled", true);
|
|
|
|
$("#destinations").attr("disabled", true);
|
|
|
|
$("#stations").attr("disabled", true);
|
|
|
|
$("#schedule").hide();
|
|
|
|
$("#loading").hide();
|
2018-06-06 20:06:11 +00:00
|
|
|
$("#mainerror").hide();
|
2018-06-06 18:51:10 +00:00
|
|
|
|
|
|
|
if (nextTraffic != null) {
|
|
|
|
clearTimeout(nextTraffic);
|
|
|
|
nextTraffic = null;
|
|
|
|
}
|
|
|
|
if (nextSchedule != null) {
|
|
|
|
clearTimeout(nextSchedule);
|
|
|
|
nextSchedule = null;
|
|
|
|
}
|
|
|
|
|
2018-06-06 17:14:27 +00:00
|
|
|
if (uri.length == 4 || uri.length == 3) {
|
2018-06-06 18:51:10 +00:00
|
|
|
if (uri[3] == "")
|
|
|
|
uri.pop();
|
2018-06-06 17:14:27 +00:00
|
|
|
if (uri.length == 3)
|
|
|
|
uri.push("A+R");
|
|
|
|
|
2018-06-06 19:24:50 +00:00
|
|
|
defineTag(uri[0], uri[2]);
|
2018-06-06 17:14:27 +00:00
|
|
|
getSchedule(uri[0], uri[1], uri[2], uri[3]);
|
2018-06-06 18:51:10 +00:00
|
|
|
if (fetchTrafic)
|
|
|
|
getTraffic(uri[0], uri[1]);
|
2018-06-06 17:14:27 +00:00
|
|
|
} else if (uri.length == 1 || uri.length == 2) {
|
2018-06-06 18:51:10 +00:00
|
|
|
$("#traffic").hide();
|
|
|
|
$("#station").hide();
|
|
|
|
$("title").text("Application RATP");
|
|
|
|
|
2018-06-06 17:14:27 +00:00
|
|
|
switch(uri[0]) {
|
|
|
|
case "metros":
|
|
|
|
case "rers":
|
|
|
|
case "bus":
|
|
|
|
case "tramways":
|
|
|
|
case "noctiliens":
|
|
|
|
$("#lines").show();
|
|
|
|
$("#linesFilter").val(uri[0]);
|
|
|
|
$("#linesFilter").change();
|
|
|
|
break;
|
|
|
|
default:
|
2018-06-06 18:51:10 +00:00
|
|
|
window.location.pathname = "/";
|
2018-06-06 17:14:27 +00:00
|
|
|
}
|
|
|
|
} else if (uri.length == 0) {
|
|
|
|
if ($("#linesFilter").val().length != 0)
|
2018-06-06 18:51:10 +00:00
|
|
|
main([ $("#linesFilter").val() ], true);
|
2018-06-06 17:14:27 +00:00
|
|
|
else
|
|
|
|
$("#lines").show();
|
|
|
|
}
|
|
|
|
}
|
2018-06-06 18:51:10 +00:00
|
|
|
main(uri, true);
|
|
|
|
|
|
|
|
window.onpopstate = function(event) {
|
|
|
|
main(window.location.pathname.split("/").filter(function(v) {
|
|
|
|
return v.length > 0;
|
|
|
|
}), true);
|
|
|
|
}
|