Initial commit
This commit is contained in:
commit
69a5fa5a8a
3 changed files with 215 additions and 0 deletions
140
app.js
Normal file
140
app.js
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
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 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();
|
||||
window.location.pathname = ["", $("#linesFilter").val(), $("#line").val(), $("#stations").val(), $("#destinations").val().length != 0?$("#destinations").val():""].join("/")
|
||||
});
|
||||
|
||||
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();
|
||||
$.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();
|
||||
});
|
||||
}
|
||||
|
||||
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();
|
||||
$("#update").text(data["_metadata"]["date"]);
|
||||
|
||||
nextSchedule = setTimeout(function() {
|
||||
getSchedule(type, code, station, way);
|
||||
}, 15000);
|
||||
});
|
||||
}
|
||||
|
||||
function main(uri) {
|
||||
if (uri.length == 4 || uri.length == 3) {
|
||||
if (uri.length == 3)
|
||||
uri.push("A+R");
|
||||
|
||||
getSchedule(uri[0], uri[1], uri[2], uri[3]);
|
||||
getTraffic(uri[0], uri[1]);
|
||||
} else if (uri.length == 1 || uri.length == 2) {
|
||||
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 = window.location.pathname + "/..";
|
||||
}
|
||||
} else if (uri.length == 0) {
|
||||
if ($("#linesFilter").val().length != 0)
|
||||
main([ $("#linesFilter").val() ]);
|
||||
else
|
||||
$("#lines").show();
|
||||
}
|
||||
}
|
||||
main(uri);
|
||||
Loading…
Add table
Add a link
Reference in a new issue