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
|
|
|
}
|
|
|
|
|
2018-04-25 01:04:55 +00:00
|
|
|
function get_template(id) {
|
|
|
|
let template = $(id).html();
|
|
|
|
return $(template);
|
|
|
|
}
|
|
|
|
|
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-25 01:04:55 +00:00
|
|
|
let template;
|
2018-04-24 23:35:27 +00:00
|
|
|
$.post("/v1/new", {api_key: API_KEY, content: input.val()}, function(data) {
|
2018-04-25 01:04:55 +00:00
|
|
|
template = get_template("#task-template");
|
2018-04-24 23:35:27 +00:00
|
|
|
let input = $("input", template);
|
|
|
|
input.prop("id", "task-" + data.task.id);
|
2018-04-24 23:42:01 +00:00
|
|
|
input.change(function() {
|
|
|
|
check(data.task.id);
|
|
|
|
});
|
2018-04-24 23:49:36 +00:00
|
|
|
input.after(" " + data.task.content);
|
2018-04-24 23:35:27 +00:00
|
|
|
})
|
|
|
|
.fail(function(request) {
|
2018-04-25 01:04:55 +00:00
|
|
|
template = get_template("#error-template");
|
2018-04-25 01:08:06 +00:00
|
|
|
$(".error-text", template).text("ERROR:" + request.responseJSON.errors.join(" "));
|
2018-04-25 00:28:23 +00:00
|
|
|
})
|
|
|
|
.always(function() {
|
|
|
|
$("#new-task").before(template);
|
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-25 01:08:06 +00:00
|
|
|
let template = get_template("#api-key-template");
|
2018-04-24 23:22:50 +00:00
|
|
|
$.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();
|
|
|
|
}
|