2018-04-23 12:22:27 +00:00
|
|
|
function check(id) {
|
2018-04-24 22:02:13 +00:00
|
|
|
let checkbox = $("#task-" + id);
|
|
|
|
let uri = "/v1/undo";
|
|
|
|
if (checkbox.prop("checked")) {
|
|
|
|
uri = "/v1/do";
|
2018-04-23 12:22:27 +00:00
|
|
|
}
|
2018-04-24 22:02:13 +00:00
|
|
|
$.post(uri, {api_key: API_KEY, id: id}, function(data, status) {
|
|
|
|
if (status == "success") {
|
|
|
|
checkbox.prop("checked", data.task.done);
|
|
|
|
} else {
|
|
|
|
console.log(data); // NOTE TEMPORARY (need to handle errors better)
|
|
|
|
}
|
|
|
|
});
|
2018-04-23 12:22:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function new_task() {
|
2018-04-24 22:02:13 +00:00
|
|
|
let input = $("#new-task-input");
|
2018-04-24 23:35:27 +00:00
|
|
|
let template = $("#task-template").html();
|
|
|
|
template = $(template);
|
|
|
|
$.post("/v1/new", {api_key: API_KEY, content: input.val()}, function(data) {
|
|
|
|
let input = $("input", template);
|
|
|
|
input.prop("id", "task-" + data.task.id);
|
2018-04-24 23:42:01 +00:00
|
|
|
// input.prop("onchange", "check('" + data.task.id + "')");
|
|
|
|
input.change(function() {
|
|
|
|
check(data.task.id);
|
|
|
|
});
|
2018-04-24 23:46:09 +00:00
|
|
|
input.next().replace(" " + data.task.content);
|
2018-04-24 23:42:01 +00:00
|
|
|
$("#new-task").before(template);
|
2018-04-24 23:35:27 +00:00
|
|
|
})
|
|
|
|
.fail(function(request) {
|
|
|
|
// TODO handle error
|
2018-04-24 22:02:13 +00:00
|
|
|
});
|
2018-04-24 22:56:40 +00:00
|
|
|
input.val("");
|
2018-04-23 12:22:27 +00:00
|
|
|
return false; // prevent form submission
|
|
|
|
}
|
|
|
|
|
|
|
|
function new_api_key() {
|
2018-04-24 23:22:50 +00:00
|
|
|
let template = $("#api-key-template").html();
|
|
|
|
template = $(template);
|
|
|
|
$.get("/v1/key/new", function(data) {
|
2018-04-24 23:04:37 +00:00
|
|
|
$("code", template).text(data.api_key.key);
|
|
|
|
$("#new-api-key").before(template);
|
2018-04-24 23:35:27 +00:00
|
|
|
})
|
|
|
|
.fail(function(request) {
|
|
|
|
$("code", template)
|
|
|
|
.css("background-color", "red")
|
|
|
|
.css("color", "white")
|
2018-04-24 23:38:38 +00:00
|
|
|
.text("ERROR: " + request.responseJSON.errors.join(" "));
|
2018-04-24 23:35:27 +00:00
|
|
|
$("#new-api-key").before(template);
|
2018-04-24 22:02:13 +00:00
|
|
|
});
|
2018-04-23 12:22:27 +00:00
|
|
|
}
|
2018-04-24 22:56:40 +00:00
|
|
|
|
|
|
|
function delete_item(e) {
|
|
|
|
e = $(e);
|
|
|
|
while (!e.is("li")) {
|
|
|
|
e = e.parent();
|
|
|
|
}
|
2018-04-24 23:22:50 +00:00
|
|
|
// TODO here, grab content to delete by (for api_key), send delete request, hide element (delete on success, reappear with red on failure)
|
2018-04-24 22:56:40 +00:00
|
|
|
e.remove();
|
|
|
|
}
|