simplex/static/index.js

60 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-04-23 12:22:27 +00:00
function check(id) {
2018-04-24 22:02:13 +00:00
let checkbox = $("#task-" + id);
let uri = "/v1/undo";
if (checkbox.prop("checked")) {
uri = "/v1/do";
2018-04-23 12:22:27 +00:00
}
2018-04-24 22:02:13 +00:00
$.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)
}
});
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-24 23:35:27 +00:00
let template = $("#task-template").html();
template = $(template);
$.post("/v1/new", {api_key: API_KEY, content: input.val()}, function(data) {
// $("#new-task").before("<li><input type='checkbox' id='task-" + data.task.id + "' onchange='check(" + data.task.id + ")'> " + data.task.content + "</li>");
let input = $("input", template);
2018-04-24 23:38:38 +00:00
console.log(input);
2018-04-24 23:35:27 +00:00
input.prop("id", "task-" + data.task.id);
input.prop("onchange", "check('" + data.task.id + "')");
input.after(data.task.content); // not sure if will work
2018-04-24 23:38:38 +00:00
console.log(input);
2018-04-24 23:35:27 +00:00
})
.fail(function(request) {
// TODO handle error
2018-04-24 22:02:13 +00:00
});
input.val("");
2018-04-23 12:22:27 +00:00
return false; // prevent form submission
}
function new_api_key() {
2018-04-24 23:22:50 +00:00
let template = $("#api-key-template").html();
template = $(template);
$.get("/v1/key/new", function(data) {
2018-04-24 23:04:37 +00:00
$("code", template).text(data.api_key.key);
$("#new-api-key").before(template);
2018-04-24 23:35:27 +00:00
})
.fail(function(request) {
$("code", template)
.css("background-color", "red")
.css("color", "white")
2018-04-24 23:38:38 +00:00
.text("ERROR: " + request.responseJSON.errors.join(" "));
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
}
function delete_item(e) {
e = $(e);
while (!e.is("li")) {
e = e.parent();
}
2018-04-24 23:22:50 +00:00
// 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();
}