let's move some logic to the dialog

This commit is contained in:
Jakob Ketterl 2020-05-04 00:20:01 +02:00
parent e2cacc1fa0
commit efa9771ad7
2 changed files with 28 additions and 18 deletions

View File

@ -102,40 +102,26 @@ BookmarkBar.prototype.render = function(){
}; };
BookmarkBar.prototype.showEditDialog = function(bookmark) { BookmarkBar.prototype.showEditDialog = function(bookmark) {
var $form = this.$dialog.find("form");
if (!bookmark) { if (!bookmark) {
bookmark = { bookmark = {
name: "", name: "",
frequency: center_freq + this.getDemodulator().get_offset_frequency(), frequency: center_freq + this.getDemodulator().get_offset_frequency(),
modulation: this.getDemodulator().get_modulation() modulation: this.getDemodulator().get_secondary_demod() || this.getDemodulator().get_modulation()
} }
} }
['name', 'frequency', 'modulation'].forEach(function(key){ this.$dialog.bookmarkDialog().setValues(bookmark);
$form.find('#' + key).val(bookmark[key]);
});
this.$dialog.data('id', bookmark.id);
this.$dialog.show(); this.$dialog.show();
this.$dialog.find('#name').focus(); this.$dialog.find('#name').focus();
}; };
BookmarkBar.prototype.storeBookmark = function() { BookmarkBar.prototype.storeBookmark = function() {
var me = this; var me = this;
var bookmark = {}; var bookmark = this.$dialog.bookmarkDialog().getValues();
var valid = true; if (!bookmark) return;
['name', 'frequency', 'modulation'].forEach(function(key){
var $input = me.$dialog.find('#' + key);
valid = valid && $input[0].checkValidity();
bookmark[key] = $input.val();
});
if (!valid) {
me.$dialog.find("form :submit").click();
return;
}
bookmark.frequency = Number(bookmark.frequency); bookmark.frequency = Number(bookmark.frequency);
var bookmarks = me.localBookmarks.getBookmarks(); var bookmarks = me.localBookmarks.getBookmarks();
bookmark.id = me.$dialog.data('id');
if (!bookmark.id) { if (!bookmark.id) {
if (bookmarks.length) { if (bookmarks.length) {
bookmark.id = 1 + Math.max.apply(Math, bookmarks.map(function(b){ return b.id || 0; })); bookmark.id = 1 + Math.max.apply(Math, bookmarks.map(function(b){ return b.id || 0; }));

View File

@ -7,6 +7,30 @@ $.fn.bookmarkDialog = function() {
}).map(function(m) { }).map(function(m) {
return '<option value="' + m.modulation + '">' + m.name + '</option>'; return '<option value="' + m.modulation + '">' + m.name + '</option>';
}).join('')); }).join(''));
return this;
},
setValues: function(bookmark) {
var $form = $el.find('form');
['name', 'frequency', 'modulation'].forEach(function(key){
$form.find('#' + key).val(bookmark[key]);
});
$el.data('id', bookmark.id || false);
return this;
},
getValues: function() {
var bookmark = {};
var valid = true;
['name', 'frequency', 'modulation'].forEach(function(key){
var $input = $el.find('#' + key);
valid = valid && $input[0].checkValidity();
bookmark[key] = $input.val();
});
if (!valid) {
$el.find("form :submit").click();
return;
}
bookmark.id = $el.data('id');
return bookmark;
} }
} }
} }