SocialDragon/socialdragon/assets/js/app.js
2017-08-18 09:07:37 +02:00

109 lines
3.3 KiB
JavaScript

//$(document).foundation()
Vue.component('modal', {
template: '#modal-template'
})
var app = new Vue({
delimiters: ['[[', ']]'],
el: '#app',
data: {
mode: state,
items: data,
currentItem: false,
showModal: false,
inboxCount: false
},
created: function () {
window.addEventListener('keyup', this.handleKeyup);
this.loadStats();
},
methods: {
detailPopup: function (itm, event) {
// `this` inside methods points to the Vue instance
this.currentItem = itm;
this.showModal = true;
},
approveItem: function (id) {
var req = new XMLHttpRequest();
req.addEventListener("load", function () {
console.log("Approved ID " + id);
});
req.open("POST", "/admin/approve/" + id);
req.send();
this.showModal = false;
var index = this.items.indexOf(this.currentItem);
if (index > -1) {
this.items.splice(index, 1);
}
},
rejectItem: function (id) {
var req = new XMLHttpRequest();
req.addEventListener("load", function () {
console.log("Rejected ID " + id);
});
req.open("POST", "/admin/reject/" + id);
req.send();
this.showModal = false;
var index = this.items.indexOf(this.currentItem);
if (index > -1) {
this.items.splice(index, 1);
}
},
handleKeyup: function (event) {
if (this.showModal) {
enter = 13;
backspace = 8;
remove = 46;
esc = 27;
if (event.keyCode === enter) {
if (this.mode === 1) {
return;
}
this.approveItem(this.currentItem.ID);
} else if (event.keyCode === backspace || event.keyCode === remove) {
if (this.mode === 2) {
return;
}
this.rejectItem(this.currentItem.ID);
} else if (event.keyCode === esc) {
this.showModal = false;
}
}
},
loadStats: function () {
var req = new XMLHttpRequest();
req.addEventListener("load", function () {
stats = JSON.parse(req.responseText);
if (stats) {
app.inboxCount = stats.Count.Items.Inbox;
} else {
console.warn("Loading stats failed!");
}
});
req.open("GET", "/admin/stats");
req.send();
}
}
})
// Construct websocket url
var loc = window.location;
var ws_url = ((loc.protocol === "https:") ? "wss://" : "ws://") + loc.host + "/ws";
var exampleSocket = new WebSocket(ws_url);
exampleSocket.onopen = function (event) {
console.log("WS: Connection open!");
console.log("Proto: " + exampleSocket.protocol);
};
exampleSocket.onmessage = function (event) {
item = JSON.parse(event.data);
if (item.State === 0) {
app.inboxCount++;
} else {
app.loadStats();
}
if (item.State === state) {
app.items.push(item);
}
}