simplex/static/index.js
2018-04-24 18:08:06 -07:00

67 lines
1.8 KiB
JavaScript

function check(id) {
let checkbox = $("#task-" + id);
let uri = "/v1/undo";
if (checkbox.prop("checked")) {
uri = "/v1/do";
}
$.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)
}
});
}
function get_template(id) {
let template = $(id).html();
return $(template);
}
function new_task() {
let input = $("#new-task-input");
let template;
$.post("/v1/new", {api_key: API_KEY, 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_template("#api-key-template");
$.get("/v1/key/new", function(data) {
$("code", template).text(data.api_key.key);
$("#new-api-key").before(template);
})
.fail(function(request) {
$("code", template)
.css("background-color", "red")
.css("color", "white")
.text("ERROR: " + request.responseJSON.errors.join(" "));
$("#new-api-key").before(template);
});
}
function delete_item(e) {
e = $(e);
while (!e.is("li")) {
e = e.parent();
}
// TODO here, grab content to delete by (for api_key), send delete request, hide element (delete on success, reappear with red on failure)
e.remove();
}