openwebrx-clone/htdocs/lib/BookmarkBar.js

148 lines
5.1 KiB
JavaScript
Raw Permalink Normal View History

function BookmarkBar() {
var me = this;
me.localBookmarks = new BookmarkLocalStorage();
me.$container = $("#openwebrx-bookmarks-container");
me.bookmarks = {};
me.$container.on('click', '.bookmark', function(e){
var $bookmark = $(e.target).closest('.bookmark');
me.$container.find('.bookmark').removeClass('selected');
var b = $bookmark.data();
2020-05-01 23:10:41 +00:00
if (!b || !b.frequency || !b.modulation) return;
2020-05-01 22:05:20 +00:00
me.getDemodulator().set_offset_frequency(b.frequency - center_freq);
2019-10-12 15:14:28 +00:00
if (b.modulation) {
2020-05-01 22:05:20 +00:00
me.getDemodulatorPanel().setMode(b.modulation);
2019-10-12 15:14:28 +00:00
}
$bookmark.addClass('selected');
});
me.$container.on('click', '.action[data-action=edit]', function(e){
e.stopPropagation();
var $bookmark = $(e.target).closest('.bookmark');
me.showEditDialog($bookmark.data());
});
me.$container.on('click', '.action[data-action=delete]', function(e){
e.stopPropagation();
var $bookmark = $(e.target).closest('.bookmark');
me.localBookmarks.deleteBookmark($bookmark.data());
me.loadLocalBookmarks();
});
2019-10-23 09:27:05 +00:00
var $bookmarkButton = $('#openwebrx-panel-receiver').find('.openwebrx-bookmark-button');
if (typeof(Storage) !== 'undefined') {
$bookmarkButton.show();
} else {
$bookmarkButton.hide();
}
$bookmarkButton.click(function(){
me.showEditDialog();
});
me.$dialog = $("#openwebrx-dialog-bookmark");
me.$dialog.find('.openwebrx-button[data-action=cancel]').click(function(){
me.$dialog.hide();
});
me.$dialog.find('.openwebrx-button[data-action=submit]').click(function(){
me.storeBookmark();
});
me.$dialog.find('form').on('submit', function(e){
e.preventDefault();
me.storeBookmark();
});
}
BookmarkBar.prototype.position = function(){
var range = get_visible_freq_range();
2019-10-23 09:27:05 +00:00
$('#openwebrx-bookmarks-container').find('.bookmark').each(function(){
$(this).css('left', scale_px_from_freq($(this).data('frequency'), range));
});
2019-10-23 09:27:05 +00:00
};
BookmarkBar.prototype.loadLocalBookmarks = function(){
var bwh = bandwidth / 2;
var start = center_freq - bwh;
var end = center_freq + bwh;
var bookmarks = this.localBookmarks.getBookmarks().filter(function(b){
return b.frequency >= start && b.frequency <= end;
});
this.replace_bookmarks(bookmarks, 'local', true);
2019-10-23 09:27:05 +00:00
};
BookmarkBar.prototype.replace_bookmarks = function(bookmarks, source, editable) {
editable = !!editable;
bookmarks = bookmarks.map(function(b){
b.source = source;
b.editable = editable;
return b;
});
this.bookmarks[source] = bookmarks;
this.render();
2019-10-23 09:27:05 +00:00
};
BookmarkBar.prototype.render = function(){
var bookmarks = Object.values(this.bookmarks).reduce(function(l, v){ return l.concat(v); });
bookmarks = bookmarks.sort(function(a, b){ return a.frequency - b.frequency; });
var elements = bookmarks.map(function(b){
2019-10-23 09:27:05 +00:00
var $bookmark = $(
'<div class="bookmark" data-source="' + b.source + '"' + (b.editable?' editable="editable"':'') + '>' +
'<div class="bookmark-actions">' +
2021-05-15 23:06:57 +00:00
'<div class="openwebrx-button action" data-action="edit"><svg viewBox="0 0 80 80"><use xlink:href="static/gfx/svg-defs.svg#edit"></use></svg></div>' +
'<div class="openwebrx-button action" data-action="delete"><svg viewBox="0 0 80 80"><use xlink:href="static/gfx/svg-defs.svg#trashcan"></use></svg></div>' +
'</div>' +
'<div class="bookmark-content">' + b.name + '</div>' +
'</div>'
);
$bookmark.data(b);
return $bookmark;
});
this.$container.find('.bookmark').remove();
this.$container.append(elements);
this.position();
2019-10-23 09:27:05 +00:00
};
BookmarkBar.prototype.showEditDialog = function(bookmark) {
if (!bookmark) {
bookmark = {
name: "",
2020-05-01 22:05:20 +00:00
frequency: center_freq + this.getDemodulator().get_offset_frequency(),
2020-05-03 22:20:01 +00:00
modulation: this.getDemodulator().get_secondary_demod() || this.getDemodulator().get_modulation()
}
}
2020-05-03 22:20:01 +00:00
this.$dialog.bookmarkDialog().setValues(bookmark);
this.$dialog.show();
this.$dialog.find('#name').focus();
2019-10-23 09:27:05 +00:00
};
BookmarkBar.prototype.storeBookmark = function() {
var me = this;
2020-05-03 22:20:01 +00:00
var bookmark = this.$dialog.bookmarkDialog().getValues();
if (!bookmark) return;
bookmark.frequency = Number(bookmark.frequency);
var bookmarks = me.localBookmarks.getBookmarks();
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;
}
}
2019-10-23 09:27:05 +00:00
bookmarks = bookmarks.filter(function(b) { return b.id !== bookmark.id; });
bookmarks.push(bookmark);
me.localBookmarks.setBookmarks(bookmarks);
me.loadLocalBookmarks();
me.$dialog.hide();
2019-10-23 09:27:05 +00:00
};
2020-05-01 22:05:20 +00:00
BookmarkBar.prototype.getDemodulatorPanel = function() {
return $('#openwebrx-panel-receiver').demodulatorPanel();
};
BookmarkBar.prototype.getDemodulator = function() {
return this.getDemodulatorPanel().getDemodulator();
};