2018-04-25 01:33:18 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2018-04-23 12:22:27 +00:00
|
|
|
function check(id) {
|
2018-04-24 22:02:13 +00:00
|
|
|
let checkbox = $("#task-" + id);
|
2018-04-25 01:33:18 +00:00
|
|
|
remove_error(checkbox);
|
2018-04-24 22:02:13 +00:00
|
|
|
let uri = "/v1/undo";
|
|
|
|
if (checkbox.prop("checked")) {
|
|
|
|
uri = "/v1/do";
|
2018-04-25 01:33:18 +00:00
|
|
|
// TODO hide it!
|
2018-04-23 12:22:27 +00:00
|
|
|
}
|
2018-04-25 01:44:50 +00:00
|
|
|
$.post(uri, {id: id}, function(data) {
|
2018-04-25 01:33:18 +00:00
|
|
|
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
|
2018-04-24 22:02:13 +00:00
|
|
|
});
|
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-25 01:44:50 +00:00
|
|
|
$.post("/v1/new", {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:33:18 +00:00
|
|
|
let template;
|
2018-04-24 23:22:50 +00:00
|
|
|
$.get("/v1/key/new", function(data) {
|
2018-04-25 01:33:18 +00:00
|
|
|
template = get_template("#api-key-template");
|
2018-04-24 23:04:37 +00:00
|
|
|
$("code", template).text(data.api_key.key);
|
2018-04-24 23:35:27 +00:00
|
|
|
})
|
|
|
|
.fail(function(request) {
|
2018-04-25 01:33:18 +00:00
|
|
|
template = get_template("#error-template");
|
|
|
|
$(".error-text", template).text("ERROR: " + request.responseJSON.errors.join(" "));
|
|
|
|
})
|
|
|
|
.always(function() {
|
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-25 02:08:59 +00:00
|
|
|
|
|
|
|
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
|
2018-04-24 22:56:40 +00:00
|
|
|
}
|