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