Odjezdova-tabule-MHD/server/static/admin.js

63 lines
1.6 KiB
JavaScript

const { createApp } = Vue;
let app = createApp({
data() {
return {
// logged: false,
// secret: "",
logged: true,
secret: "TajneHeslo",
stops: {},
devices: []
}
},
methods: {
async update() {
let devices = await api("/admin/devices", {secret: this.secret});
let stops = await api("/stops");
if(devices.error) {
alert("Neplatné heslo!");
return;
}
this.devices = devices.devices;
this.stops = stops.stops;
this.logged = true;
},
async device_clear(index) {
await api("/admin/devices/clear", {index, secret: this.secret});
},
async device_resend(index) {
await api("/admin/devices/resend", {index, secret: this.secret});
},
async device_change_stop(index) {
let stop_id = prompt("Zadejte ID zastávky:");
if(!stop_id || stop_id.trim() == "") return;
this.devices[index].stop_id = stop_id;
await api("/admin/devices/change_stop", {index, stop_id, secret: this.secret});
}
}
}).mount('#app');
async function api(url, data) {
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(data ? "POST" : "GET", url, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var str = [];
for (var key in data) {
if (data.hasOwnProperty(key)) {
str.push(encodeURIComponent(key) + "=" + encodeURIComponent(data[key]))
}
}
xhr.send(str.join("&"));
});
}
app.update();