107 lines
2.5 KiB
JavaScript
107 lines
2.5 KiB
JavaScript
function add_error(e) {
|
|
while (!e.is(".row")) {
|
|
e = e.parent();
|
|
}
|
|
e.addClass("error");
|
|
}
|
|
|
|
function remove_error(e) {
|
|
while (!e.is(".row")) {
|
|
e = e.parent();
|
|
}
|
|
e.removeClass("error");
|
|
}
|
|
|
|
function check(id) {
|
|
let checkbox = $("#task-" + id);
|
|
remove_error(checkbox);
|
|
let uri = "/v1/undo";
|
|
if (checkbox.prop("checked")) {
|
|
uri = "/v1/do";
|
|
// TODO hide it!
|
|
}
|
|
$.post(uri, {id: id}, function(data) {
|
|
checkbox.prop("checked", data.task.done);
|
|
})
|
|
.fail(function(request) {
|
|
if (checkbox.prop("checked")) {
|
|
checkbox.prop("checked", false);
|
|
} else {
|
|
checkbox.prop("checked", true);
|
|
}
|
|
add_error(checkbox); // TODO unhide it
|
|
});
|
|
}
|
|
|
|
function get_template(id) {
|
|
let template = $(id).html();
|
|
return $(template);
|
|
}
|
|
|
|
function new_task() {
|
|
let input = $("#new-task-input");
|
|
let template;
|
|
$.post("/v1/new", {content: input.val()}, function(data) {
|
|
template = get_template("#task-template");
|
|
let input = $("input", template);
|
|
input.prop("id", "task-" + data.task.id);
|
|
input.change(function() {
|
|
check(data.task.id);
|
|
});
|
|
input.after(" " + data.task.content);
|
|
})
|
|
.fail(function(request) {
|
|
template = get_template("#error-template");
|
|
$(".error-text", template).text("ERROR:" + request.responseJSON.errors.join(" "));
|
|
})
|
|
.always(function() {
|
|
$("#new-task").before(template);
|
|
});
|
|
input.val("");
|
|
return false; // prevent form submission
|
|
}
|
|
|
|
function new_api_key() {
|
|
let template;
|
|
$.get("/v1/key/new", function(data) {
|
|
template = get_template("#api-key-template");
|
|
$("code", template).text(data.api_key.key);
|
|
})
|
|
.fail(function(request) {
|
|
template = get_template("#error-template");
|
|
$(".error-text", template).text("ERROR: " + request.responseJSON.errors.join(" "));
|
|
})
|
|
.always(function() {
|
|
$("#new-api-key").before(template);
|
|
});
|
|
}
|
|
|
|
function delete_item(e) {
|
|
e = $(e);
|
|
while (!e.is("li")) {
|
|
e = e.parent();
|
|
}
|
|
|
|
checkbox = $("input:checkbox", e);
|
|
if (checkbox.length) {
|
|
// TODO find task id, send delete request, hide item
|
|
// success? delete item, failure? add_error to item and unhide it (how? display: block;)
|
|
return;
|
|
}
|
|
|
|
code = $("code", e);
|
|
if (code) {
|
|
e.css("display", "hidden"); // hide it
|
|
$.post("/v1/key/delete", {key: code.text()}, function(data) {
|
|
e.remove();
|
|
})
|
|
.fail(function(request) {
|
|
add_error(e);
|
|
e.css("display", "block");
|
|
});
|
|
return;
|
|
}
|
|
|
|
e.remove(); // for errors / others, client-side only
|
|
}
|