2018-04-23 12:22:27 +00:00
|
|
|
function request(url) {
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("POST", url, true);
|
|
|
|
xhr.setRequestHeader("Content-Type", "application/json");
|
|
|
|
return xhr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function check(id) {
|
2018-04-23 13:17:10 +00:00
|
|
|
var checkbox = document.getElementById("task-" + id);
|
2018-04-23 12:22:27 +00:00
|
|
|
if (checkbox.checked) {
|
|
|
|
var xhr = request("/v1/do");
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState === 4) {
|
2018-04-24 05:42:56 +00:00
|
|
|
data = JSON.parse(xhr.responseText);
|
|
|
|
if (data.task) {
|
|
|
|
$("#task-" + data.task.id).prop("checked", data.task.done);
|
|
|
|
} else {
|
|
|
|
console.log(data); // NOTE TEMPORARY (handle errors better!)
|
|
|
|
}
|
2018-04-23 12:22:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
xhr.send(JSON.stringify({api_key: API_KEY, id: id}));
|
|
|
|
} else {
|
|
|
|
var xhr = request("/v1/undo");
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState === 4) {
|
2018-04-24 05:42:56 +00:00
|
|
|
data = JSON.parse(xhr.responseText);
|
|
|
|
if (data.task) {
|
|
|
|
$("#task-" + data.task.id).prop("checked", data.task.done);
|
|
|
|
} else {
|
|
|
|
console.log(data); // NOTE TEMPORARY (handle errors better!)
|
|
|
|
}
|
2018-04-23 12:22:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
xhr.send(JSON.stringify({api_key: API_KEY, id: id}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function new_task() {
|
2018-04-24 05:42:56 +00:00
|
|
|
var text = document.getElementById("new-task-input");
|
2018-04-23 12:22:27 +00:00
|
|
|
var xhr = request("/v1/new");
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState === 4) {
|
2018-04-24 05:42:56 +00:00
|
|
|
data = JSON.parse(xhr.responseText);
|
|
|
|
if (data.task) {
|
2018-04-24 13:27:57 +00:00
|
|
|
$("#new-task").before("<li><input type='checkbox' id='task-" + data.task.id + "' onchange='check(" + data.task.id + ")'> " + data.task.content + "</li>");
|
2018-04-24 05:42:56 +00:00
|
|
|
} else {
|
|
|
|
console.log(data); // NOTE TEMPORARY (handle errors better!)
|
|
|
|
}
|
2018-04-23 12:22:27 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-23 13:17:10 +00:00
|
|
|
xhr.send(JSON.stringify({api_key: API_KEY, content: text.value}));
|
2018-04-23 12:22:27 +00:00
|
|
|
|
|
|
|
return false; // prevent form submission
|
|
|
|
}
|
|
|
|
|
|
|
|
function new_api_key() {
|
2018-04-24 05:42:56 +00:00
|
|
|
var xhr = request("/new-api-key");
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
data = JSON.parse(xhr.responseText);
|
|
|
|
if (data.api_key) {
|
|
|
|
$("#new-api-key").before("<li>" + data.api_key.key + "</li>");
|
|
|
|
} else {
|
|
|
|
console.log(data); // NOTE TEMPORARY (handle errors better!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xhr.send(); // NOTE may need to send empty string or empty JSON object
|
2018-04-23 12:22:27 +00:00
|
|
|
}
|