simplex2/static/index.js

45 lines
1.2 KiB
JavaScript

function request(url) {
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
return xhr;
}
function check(id) {
var checkbox = document.getElementById("task-"+id);
if (checkbox.checked) {
var xhr = request("/do");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
// TODO handle success / errors
console.log(xhr.responseText);
}
}
xhr.send(JSON.stringify({api_key: API_KEY, id: id}));
} else {
var xhr = request("/undo");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
// TODO handle success / errors
console.log(xhr.responseText);
}
}
xhr.send(JSON.stringify({api_key: API_KEY, id: id}));
}
}
function new() {
var text = document.getElementById("new-task");
console.log(text); // TEMPORARY
var xhr = request("/new");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
// TODO handle success / errors
console.log(xhr.responseText);
}
}
xhr.send(JSON.stringify({api_key: API_KEY, text: text.value}));
return false; // prevent form submission
}