hash handling fits better into here now

This commit is contained in:
Jakob Ketterl 2020-05-02 15:17:09 +02:00
parent 3405bc485b
commit 1bfe768601
2 changed files with 59 additions and 56 deletions

View File

@ -26,6 +26,9 @@ function DemodulatorPanel(el) {
self.setMode(value); self.setMode(value);
} }
}); });
window.addEventListener('hashchange', function() {
self.onHashChange();
});
}; };
DemodulatorPanel.prototype.render = function() { DemodulatorPanel.prototype.render = function() {
@ -102,7 +105,7 @@ DemodulatorPanel.prototype.setMode = function(modulation) {
var self = this; var self = this;
this.demodulator.on("frequencychange", function(freq) { this.demodulator.on("frequencychange", function(freq) {
self.tuneableFrequencyDisplay.setFrequency(self.center_freq + freq); self.tuneableFrequencyDisplay.setFrequency(self.center_freq + freq);
updateHash(); self.updateHash();
}); });
} }
if (mode.type === 'digimode') { if (mode.type === 'digimode') {
@ -117,7 +120,7 @@ DemodulatorPanel.prototype.setMode = function(modulation) {
this.updateButtons(); this.updateButtons();
this.updatePanels(); this.updatePanels();
updateHash(); this.updateHash();
}; };
DemodulatorPanel.prototype.disableDigiMode = function() { DemodulatorPanel.prototype.disableDigiMode = function() {
@ -141,7 +144,7 @@ DemodulatorPanel.prototype.getDemodulator = function() {
DemodulatorPanel.prototype.startDemodulator = function() { DemodulatorPanel.prototype.startDemodulator = function() {
if (!Modes.initComplete()) return; if (!Modes.initComplete()) return;
var params = $.extend({}, this.initialParams || {}, this.transformHashParams(validateHash())); var params = $.extend({}, this.initialParams || {}, this.transformHashParams(this.parseHash()));
this._apply(params); this._apply(params);
}; };
@ -163,8 +166,8 @@ DemodulatorPanel.prototype.setInitialParams = function(params) {
this.initialParams = params; this.initialParams = params;
}; };
DemodulatorPanel.prototype.setHashParams = function(params) { DemodulatorPanel.prototype.onHashChange = function() {
this._apply(this.transformHashParams(params)); this._apply(this.transformHashParams(this.parseHash()));
}; };
DemodulatorPanel.prototype.transformHashParams = function(params) { DemodulatorPanel.prototype.transformHashParams = function(params) {
@ -205,6 +208,57 @@ DemodulatorPanel.prototype.setCenterFrequency = function(center_freq) {
this.startDemodulator(); this.startDemodulator();
}; };
DemodulatorPanel.prototype.parseHash = function() {
if (!window.location.hash) {
return {};
}
var params = window.location.hash.substring(1).split(",").map(function(x) {
var harr = x.split('=');
return [harr[0], harr.slice(1).join('=')];
}).reduce(function(params, p){
params[p[0]] = p[1];
return params;
}, {});
return this.validateHash(params);
};
DemodulatorPanel.prototype.validateHash = function(params) {
var self = this;
params = Object.keys(params).filter(function(key) {
if (key == 'freq' || key == 'mod' || key == 'secondary_mod') {
return params.freq && Math.abs(params.freq - self.center_freq) < bandwidth;
}
return true;
}).reduce(function(p, key) {
p[key] = params[key];
return p;
}, {});
if (params['freq']) {
params['offset_frequency'] = params['freq'] - self.center_freq;
delete params['freq'];
}
return params;
};
DemodulatorPanel.prototype.updateHash = function() {
var demod = this.getDemodulator();
if (!demod) return;
var self = this;
window.location.hash = $.map({
freq: demod.get_offset_frequency() + self.center_freq,
mod: demod.get_modulation(),
secondary_mod: demod.get_secondary_demod()
}, function(value, key){
if (!value) return undefined;
return key + '=' + value;
}).filter(function(v) {
return !!v;
}).join(',');
};
$.fn.demodulatorPanel = function(){ $.fn.demodulatorPanel = function(){
if (!this.data('panel')) { if (!this.data('panel')) {
this.data('panel', new DemodulatorPanel(this)); this.data('panel', new DemodulatorPanel(this));

View File

@ -1176,54 +1176,6 @@ var mute = false;
// Optimalise these if audio lags or is choppy: // Optimalise these if audio lags or is choppy:
var audio_buffer_maximal_length_sec = 1; //actual number of samples are calculated from sample rate var audio_buffer_maximal_length_sec = 1; //actual number of samples are calculated from sample rate
function parseHash() {
if (!window.location.hash) {
return {};
}
return window.location.hash.substring(1).split(",").map(function(x) {
var harr = x.split('=');
return [harr[0], harr.slice(1).join('=')];
}).reduce(function(params, p){
params[p[0]] = p[1];
return params;
}, {});
}
function validateHash() {
var params = parseHash();
params = Object.keys(params).filter(function(key) {
if (key == 'freq' || key == 'mod' || key == 'secondary_mod') {
return params.freq && Math.abs(params.freq - center_freq) < bandwidth;
}
return true;
}).reduce(function(p, key) {
p[key] = params[key];
return p;
}, {});
if (params['freq']) {
params['offset_frequency'] = params['freq'] - center_freq;
delete params['freq'];
}
return params;
}
function updateHash() {
var demod = $('#openwebrx-panel-receiver').demodulatorPanel().getDemodulator();
if (!demod) return;
window.location.hash = $.map({
freq: demod.get_offset_frequency() + center_freq,
mod: demod.get_modulation(),
secondary_mod: demod.get_secondary_demod()
}, function(value, key){
if (!value) return undefined;
return key + '=' + value;
}).filter(function(v) {
return !!v;
}).join(',');
}
function onAudioStart(success, apiType){ function onAudioStart(success, apiType){
divlog('Web Audio API succesfully initialized, using ' + apiType + ' API, sample rate: ' + audioEngine.getSampleRate() + " Hz"); divlog('Web Audio API succesfully initialized, using ' + apiType + ' API, sample rate: ' + audioEngine.getSampleRate() + " Hz");
@ -1477,9 +1429,6 @@ function openwebrx_init() {
init_header(); init_header();
bookmarks = new BookmarkBar(); bookmarks = new BookmarkBar();
initSliders(); initSliders();
window.addEventListener('hashchange', function() {
$('#openwebrx-panel-receiver').demodulatorPanel().setHashParams(validateHash());
});
} }
function initSliders() { function initSliders() {