implement edit and delete

This commit is contained in:
Jakob Ketterl 2019-09-29 15:29:53 +02:00
parent 4a7b42202e
commit eed520daac

View File

@ -1376,16 +1376,28 @@ function position_bookmarks() {
function init_bookmarks() { function init_bookmarks() {
var $container = $("#openwebrx-bookmarks-container") var $container = $("#openwebrx-bookmarks-container")
$container.click(function(e){ $container.on('click', '.bookmark', function(e){
var $bookmark = $(e.target).closest('.bookmark');
$container.find('.bookmark').removeClass('selected'); $container.find('.bookmark').removeClass('selected');
$bookmark = $(e.target); var b = $bookmark.data();
b = $bookmark.closest('.bookmark').data();
if (!b || !b.frequency || !b.modulation) return; if (!b || !b.frequency || !b.modulation) return;
demodulator_set_offset_frequency(0, b.frequency - center_freq); demodulator_set_offset_frequency(0, b.frequency - center_freq);
demodulator_analog_replace(b.modulation); demodulator_analog_replace(b.modulation);
$bookmark.addClass('selected'); $bookmark.addClass('selected');
}); });
$container.on('click', '.action[data-action=edit]', function(e){
e.stopPropagation();
var $bookmark = $(e.target).closest('.bookmark');
showBookmarkEditDialog($bookmark.data());
});
$container.on('click', '.action[data-action=delete]', function(e){
e.stopPropagation();
var $bookmark = $(e.target).closest('.bookmark');
deleteBookmark($bookmark.data());
});
var $bookmarkButton = $('#openwebrx-panel-receiver .openwebrx-bookmark-button'); var $bookmarkButton = $('#openwebrx-panel-receiver .openwebrx-bookmark-button');
if (typeof(Storage) !== 'undefined') { if (typeof(Storage) !== 'undefined') {
$bookmarkButton.show(); $bookmarkButton.show();
@ -1401,20 +1413,28 @@ function init_bookmarks() {
$dialog.hide(); $dialog.hide();
}); });
$dialog.find('.openwebrx-button[data-action=submit]').click(function(){ $dialog.find('.openwebrx-button[data-action=submit]').click(function(){
storeNewBookmark(); storeBookmark();
}); });
} }
function showBookmarkEditDialog() { function showBookmarkEditDialog(bookmark) {
var $dialog = $("#openwebrx-dialog-bookmark"); var $dialog = $("#openwebrx-dialog-bookmark");
var $form = $dialog.find("form"); var $form = $dialog.find("form");
$form.find("#name").val(""); if (!bookmark) {
$form.find("#frequency").val(center_freq + demodulators[0].offset_frequency); bookmark = {
$form.find("#modulation").val(demodulators[0].subtype); name: "",
frequency: center_freq + demodulators[0].offset_frequency,
modulation: demodulators[0].subtype
}
}
['name', 'frequency', 'modulation'].forEach(function(key){
$form.find('#' + key).val(bookmark[key]);
});
$dialog.data('id', bookmark.id);
$dialog.show(); $dialog.show();
} }
function storeNewBookmark() { function storeBookmark() {
var $dialog = $("#openwebrx-dialog-bookmark"); var $dialog = $("#openwebrx-dialog-bookmark");
var bookmark = {}; var bookmark = {};
var valid = true; var valid = true;
@ -1428,13 +1448,34 @@ function storeNewBookmark() {
return; return;
} }
bookmark.frequency = Number(bookmark.frequency); bookmark.frequency = Number(bookmark.frequency);
var bookmarks = getLocalBookmarks(); var bookmarks = getLocalBookmarks();
bookmark.id = $dialog.data('id');
if (!bookmark.id) {
if (bookmarks.length) {
bookmark.id = 1 + Math.max.apply(Math, bookmarks.map(function(b){ return b.id || 0; }));
} else {
bookmark.id = 1;
}
}
bookmarks = bookmarks.filter(function(b) { return b.id != bookmark.id; });
bookmarks.push(bookmark); bookmarks.push(bookmark);
setLocalBookmarks(bookmarks); setLocalBookmarks(bookmarks);
loadLocalBookmarks(); loadLocalBookmarks();
$dialog.hide(); $dialog.hide();
} }
function deleteBookmark(data) {
if (data.id) data = data.id;
var bookmarks = getLocalBookmarks();
bookmarks = bookmarks.filter(function(b) { return b.id != data; });
setLocalBookmarks(bookmarks);
loadLocalBookmarks();
}
function getLocalBookmarks(){ function getLocalBookmarks(){
return JSON.parse(window.localStorage.getItem("bookmarks")) || []; return JSON.parse(window.localStorage.getItem("bookmarks")) || [];
} }