cleaning up JQuery

This commit is contained in:
Paul Liverman III 2018-04-24 20:01:15 -07:00
parent d2d1dc4c5e
commit 9c4af523b0

View File

@ -1,25 +1,13 @@
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);
checkbox.parents(".row").removeClass("error");
let uri = "/v1/undo";
if (checkbox.prop("checked")) {
uri = "/v1/do";
// TODO hide it!
checkbox.parents("li").css("display", "none");
}
$.post(uri, {id: id}, function(data) {
checkbox.prop("checked", data.task.done);
})
@ -29,7 +17,8 @@ function check(id) {
} else {
checkbox.prop("checked", true);
}
add_error(checkbox); // TODO unhide it
checkbox.parents(".row").addClass("error");
checkbox.parents("li").css("display", "block");
});
}
@ -41,6 +30,7 @@ function get_template(id) {
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:checkbox", template);
@ -52,7 +42,7 @@ function new_task() {
})
.fail(function(request) {
template = get_template("#error-template");
$(".error-text", template).text("ERROR:" + request.responseJSON.errors.join(" "));
$(".error-text", template).text("ERROR: " + request.responseJSON.errors.join(" "));
})
.always(function() {
$("#new-task").before(template);
@ -77,21 +67,19 @@ function new_api_key() {
}
function delete_item(e) {
e = $(e);
while (!e.is("li")) {
e = e.parent();
}
e = e.parents("li");
checkbox = $("input:checkbox", e);
if (checkbox.length) {
e.css("display", "hidden");
let id = checkbox.prop("id");
id = Number(id.slice(id.lastIndexOf("-") + 1, id.length))
id = Number(id.slice(id.lastIndexOf("-") + 1, id.length));
$.post("/v1/delete", {id: id}, function(data) {
e.remove();
})
.fail(function(request) {
add_error($(".row", e));
e.children(".row").addClass("error");
e.css("display", "block");
});
return;
@ -104,7 +92,7 @@ function delete_item(e) {
e.remove();
})
.fail(function(request) {
add_error($(".row", e));
e.children(".row").addClass("error");
code.css("color", "black"); // make key still readable
e.css("display", "block");
});