implement bookmark deletion

This commit is contained in:
Jakob Ketterl 2021-02-14 16:51:16 +01:00
parent 29a161b7b7
commit 48c594fdae
4 changed files with 36 additions and 3 deletions

View File

@ -22,7 +22,7 @@ $.fn.bookmarktable = function() {
var $cell = $(e.target); var $cell = $(e.target);
var html = $cell.html(); var html = $cell.html();
var $row = $cell.parent('tr'); var $row = $cell.parents('tr');
var index = $row.find('td').index($cell); var index = $row.find('td').index($cell);
var $input = inputs[index]; var $input = inputs[index];
@ -53,7 +53,20 @@ $.fn.bookmarktable = function() {
}); });
}); });
$(this).find('.bookmark-add').on('click', function() { $table.on('click', '.bookmark-delete', function(e) {
var $button = $(e.target);
$button.prop('disabled', true);
var $row = $button.parents('tr');
$.ajax(document.location.href + "/" + $row.data('id'), {
data: "{}",
contentType: 'application/json',
method: 'DELETE'
}).then(function(){
$row.remove();
});
});
$(this).on('click', '.bookmark-add', function() {
if ($table.find('tr[data-id="new"]').length) return; if ($table.find('tr[data-id="new"]').length) return;
var row = $('<tr data-id="new">'); var row = $('<tr data-id="new">');

View File

@ -99,3 +99,8 @@ class Bookmarks(object):
def addBookmark(self, bookmark: Bookmark): def addBookmark(self, bookmark: Bookmark):
self.bookmarks.append(bookmark) self.bookmarks.append(bookmark)
def removeBookmark(self, bookmark: Bookmark):
if bookmark not in self.bookmarks:
return
self.bookmarks.remove(bookmark)

View File

@ -60,7 +60,7 @@ class BookmarksController(AuthorizationMixin, WebpageController):
<td class="frequency">{frequency}</td> <td class="frequency">{frequency}</td>
<td data-value="{modulation}">{modulation_name}</td> <td data-value="{modulation}">{modulation_name}</td>
<td> <td>
<div class="btn btn-sm btn-danger bookmark-delete">delete</div> <button class="btn btn-sm btn-danger bookmark-delete">delete</button>
</td> </td>
</tr> </tr>
""".format( """.format(
@ -111,5 +111,16 @@ class BookmarksController(AuthorizationMixin, WebpageController):
except json.JSONDecodeError: except json.JSONDecodeError:
self.send_response("{}", content_type="application/json", code=400) self.send_response("{}", content_type="application/json", code=400)
def delete(self):
bookmark_id = int(self.request.matches.group(1))
bookmark = self._findBookmark(bookmark_id)
if bookmark is None:
self.send_response("{}", content_type="application/json", code=404)
return
bookmarks = Bookmarks.getSharedInstance()
bookmarks.removeBookmark(bookmark)
bookmarks.store()
self.send_response("{}", content_type="application/json", code=200)
def indexAction(self): def indexAction(self):
self.serve_template("settings/bookmarks.html", **self.template_variables()) self.serve_template("settings/bookmarks.html", **self.template_variables())

View File

@ -35,6 +35,9 @@ class RequestHandler(BaseHTTPRequestHandler):
def do_POST(self): def do_POST(self):
self.router.route(self, self.get_request("POST")) self.router.route(self, self.get_request("POST"))
def do_DELETE(self):
self.router.route(self, self.get_request("DELETE"))
def get_request(self, method): def get_request(self, method):
url = urlparse(self.path) url = urlparse(self.path)
return Request(url, method, self.headers) return Request(url, method, self.headers)
@ -112,6 +115,7 @@ class Router(object):
StaticRoute("/settings/bookmarks", BookmarksController), StaticRoute("/settings/bookmarks", BookmarksController),
StaticRoute("/settings/bookmarks", BookmarksController, method="POST", options={"action": "new"}), StaticRoute("/settings/bookmarks", BookmarksController, method="POST", options={"action": "new"}),
RegexRoute("/settings/bookmarks/(.+)", BookmarksController, method="POST", options={"action": "update"}), RegexRoute("/settings/bookmarks/(.+)", BookmarksController, method="POST", options={"action": "update"}),
RegexRoute("/settings/bookmarks/(.+)", BookmarksController, method="DELETE", options={"action": "delete"}),
StaticRoute("/login", SessionController, options={"action": "loginAction"}), StaticRoute("/login", SessionController, options={"action": "loginAction"}),
StaticRoute("/login", SessionController, method="POST", options={"action": "processLoginAction"}), StaticRoute("/login", SessionController, method="POST", options={"action": "processLoginAction"}),
StaticRoute("/logout", SessionController, options={"action": "logoutAction"}), StaticRoute("/logout", SessionController, options={"action": "logoutAction"}),