73 lines
1.8 KiB
JavaScript
73 lines
1.8 KiB
JavaScript
|
|
const { createApp } = Vue;
|
|
let app = createApp({
|
|
data() {
|
|
return {
|
|
top: 0,
|
|
interval: null,
|
|
stop_id: null,
|
|
stops: [],
|
|
departures: [],
|
|
filtered_departures: [],
|
|
filter: false
|
|
}
|
|
},
|
|
methods: {
|
|
async set() {
|
|
localStorage.setItem("favstop", this.stop_id);
|
|
this.update();
|
|
},
|
|
async update() {
|
|
if(!this.stop_id) return;
|
|
let res = await api("/departures/"+this.stop_id);
|
|
let filtered_out = [];
|
|
this.departures = [];
|
|
this.filtered_departures = [];
|
|
for(let i=0; i<res.departures.length; i++) {
|
|
let d = res.departures[i];
|
|
if(d.departure < -.3) continue;
|
|
let hash = d.line + d.last_stop;
|
|
this.departures.push(d);
|
|
if(filtered_out.indexOf(hash) == -1) {
|
|
this.filtered_departures.push(d);
|
|
filtered_out.push(hash);
|
|
}
|
|
}
|
|
},
|
|
async setup() {
|
|
this.stops = (await api("/stops")).stops;
|
|
let ids = Object.keys(this.stops);
|
|
if(ids.length < 1) {
|
|
alert("Žádné zastávky nejsou k dispozici!");
|
|
if(this.interval) clearTimeout(this.interval);
|
|
return;
|
|
}
|
|
if(localStorage.getItem("favstop")) {
|
|
if(ids.indexOf(localStorage.getItem("favstop")) != -1) this.stop_id = localStorage.getItem("favstop");
|
|
}
|
|
if(!this.stop_id) this.stop_id = ids[0];
|
|
window.addEventListener("scroll", () => {
|
|
this.top = document.querySelector('html').scrollTop;
|
|
});
|
|
this.interval = setInterval(this.update, 5000);
|
|
this.update();
|
|
}
|
|
}
|
|
}).mount('#app');
|
|
async function api(url) {
|
|
return new Promise(function(resolve, reject) {
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.onreadystatechange = function() {
|
|
if(this.readyState !== 4) return;
|
|
if(!this.responseText) reject(this);
|
|
try {
|
|
resolve(JSON.parse(this.responseText));
|
|
} catch(e) {
|
|
resolve(null);
|
|
}
|
|
};
|
|
xhr.open("GET", url, true);
|
|
xhr.send();
|
|
});
|
|
}
|
|
app.setup(); |