ratp-pwa/app.js

205 lines
5.6 KiB
JavaScript

const BASE_API = "/api";
var nextSchedule = null;
var nextTraffic = null;
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();
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) {
$.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();
if (nextTraffic != null) {
clearTimeout(nextTraffic);
nextTraffic = null;
}
$.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();
nextTraffic = setTimeout(function() {
getSchedule(type, code);
}, 150000);
});
}
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();
updateTime(new Date(data["_metadata"]["date"]));
nextSchedule = setTimeout(function() {
getSchedule(type, code, station, way);
}, 15000);
})
.fail(function(data) {
if (data.responseJSON && data.responseJSON.result && data.responseJSON.result.message)
{
$("#mainerror").text(data.responseJSON.result.message);
$("#mainerror").show();
}
});
}
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);
}
function updateTime(time) {
var hours = time.getHours();
var minutes = time.getMinutes();
$("#update").text(hours + ":" + (minutes < 10 ? '0' + minutes : minutes));
}
updateTime(new Date());
function main(uri, fetchTrafic) {
$("#lines").hide();
$("#line").attr("disabled", true);
$("#destinations").attr("disabled", true);
$("#stations").attr("disabled", true);
$("#schedule").hide();
$("#loading").hide();
$("#mainerror").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");
defineTag(uri[0], uri[2]);
getSchedule(uri[0], uri[1], uri[2], uri[3]);
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":
case "bus":
case "tramways":
case "noctiliens":
$("#lines").show();
$("#linesFilter").val(uri[0]);
$("#linesFilter").change();
break;
default:
window.location.pathname = "/";
}
} else if (uri.length == 0) {
if ($("#linesFilter").val().length != 0)
main([ $("#linesFilter").val() ], true);
else
$("#lines").show();
}
}
main(uri, true);
window.onpopstate = function(event) {
main(window.location.pathname.split("/").filter(function(v) {
return v.length > 0;
}), true);
}