SimpleCloudNotifier/web/js/logic.js

90 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-09-22 19:40:50 +02:00
function send()
{
let me = document.getElementById("btnSend");
if (me.classList.contains("btn-disabled")) return;
me.innerHTML = "<div class=\"spinnerbox\"><div class=\"spinner primary\"></div></div>";
me.classList.add("btn-disabled");
let uid = document.getElementById("uid");
let key = document.getElementById("ukey");
let msg = document.getElementById("msg");
let txt = document.getElementById("txt");
2018-10-20 14:57:05 +02:00
let pio = document.getElementById("prio");
2018-09-22 19:40:50 +02:00
uid.classList.remove('input-invalid');
key.classList.remove('input-invalid');
msg.classList.remove('input-invalid');
txt.classList.remove('input-invalid');
2018-10-20 14:57:05 +02:00
pio.classList.remove('input-invalid');
2018-09-22 19:40:50 +02:00
let data = new FormData();
data.append('user_id', uid.value);
data.append('user_key', key.value);
data.append('title', msg.value);
data.append('content', txt.value);
2018-10-20 14:57:05 +02:00
data.append('priority', pio.value);
2018-09-22 19:40:50 +02:00
let xhr = new XMLHttpRequest();
xhr.open('POST', '/send.php', true);
xhr.onreadystatechange = function ()
{
if (xhr.readyState !== 4) return;
console.log('Status: ' + xhr.status);
2018-11-17 23:59:57 +01:00
if (xhr.status === 200 || xhr.status === 401 || xhr.status === 403 || xhr.status === 412)
2018-09-22 19:40:50 +02:00
{
let resp = JSON.parse(xhr.responseText);
2018-11-17 23:59:57 +01:00
if (!resp.success || xhr.status !== 200)
2018-09-22 19:40:50 +02:00
{
if (resp.errhighlight === 101) uid.classList.add('input-invalid');
if (resp.errhighlight === 102) key.classList.add('input-invalid');
if (resp.errhighlight === 103) msg.classList.add('input-invalid');
if (resp.errhighlight === 104) txt.classList.add('input-invalid');
2018-10-20 14:57:05 +02:00
if (resp.errhighlight === 105) pio.classList.add('input-invalid');
2018-09-22 19:40:50 +02:00
Toastify({
text: resp.message,
gravity: "top",
positionLeft: false,
backgroundColor: "#D32F2F",
}).showToast();
}
else
{
window.location.href =
2018-11-17 01:30:41 +01:00
'/message_sent.php' +
2018-09-22 19:40:50 +02:00
'?ok=' + 1 +
'&message_count=' + resp.messagecount +
'&quota=' + resp.quota +
2018-09-26 23:13:50 +02:00
'&quota_remain=' + (resp.quota_max-resp.quota) +
2018-09-22 19:57:00 +02:00
'&quota_max=' + resp.quota_max +
2018-09-22 19:40:50 +02:00
'&preset_user_id=' + uid.value +
'&preset_user_key=' + key.value;
}
}
else
{
Toastify({
text: 'Request failed: Statuscode=' + xhr.status,
gravity: "top",
positionLeft: false,
backgroundColor: "#D32F2F",
}).showToast();
}
me.classList.remove("btn-disabled");
me.innerHTML = "Send";
};
xhr.send(data);
}
window.addEventListener("load",function ()
{
let btnSend = document.getElementById("btnSend");
if (btnSend !== undefined) btnSend.onclick = function () { send(); return false; };
},false);