openwebrx/htdocs/lib/MetaPanel.js

648 lines
20 KiB
JavaScript
Raw Normal View History

function MetaPanel(el) {
this.el = el;
this.modes = [];
}
MetaPanel.prototype.update = function(data) {
};
MetaPanel.prototype.isSupported = function(data) {
return this.modes.includes(data.protocol);
};
MetaPanel.prototype.clear = function() {
this.el.find(".openwebrx-meta-slot").removeClass("active").removeClass("sync");
};
function DmrMetaSlot(el) {
this.el = $(el);
2021-01-16 22:07:55 +01:00
this.clear();
}
DmrMetaSlot.prototype.update = function(data) {
this.el[data['sync'] ? "addClass" : "removeClass"]("sync");
if (data['sync'] && data['sync'] === "voice") {
this.setId(data['additional'] && data['additional']['callsign'] || data['talkeralias'] || data['source']);
2021-01-16 22:07:55 +01:00
this.setName(data['additional'] && data['additional']['fname']);
2021-01-19 20:54:35 +01:00
this.setMode(['group', 'direct'].includes(data['type']) ? data['type'] : undefined);
this.setTarget(data['target']);
2021-09-17 18:24:33 +02:00
this.setLocation(data['lat'], data['lon'], this.getCallsign(data));
this.el.addClass("active");
} else {
2021-01-16 22:07:55 +01:00
this.clear();
}
2021-01-16 22:07:55 +01:00
};
2021-09-17 18:24:33 +02:00
DmrMetaSlot.prototype.getCallsign = function(data) {
if ('additional' in data) {
return data['additional']['callsign'];
}
if ('talkeralias' in data) {
var matches = /^([A-Z0-9]+)(\s.*)?$/.exec(data['talkeralias']);
if (matches) return matches[1];
}
};
2021-01-16 22:07:55 +01:00
DmrMetaSlot.prototype.setId = function(id) {
if (this.id === id) return;
this.id = id;
2021-09-17 18:24:33 +02:00
this.el.find('.openwebrx-dmr-id .dmr-id').text(id || '');
}
2021-01-16 22:07:55 +01:00
DmrMetaSlot.prototype.setName = function(name) {
if (this.name === name) return;
this.name = name;
this.el.find('.openwebrx-dmr-name').text(name || '');
};
2021-01-19 20:54:35 +01:00
DmrMetaSlot.prototype.setMode = function(mode) {
2021-01-19 22:04:33 +01:00
if (this.mode === mode) return;
this.mode = mode;
2021-01-19 20:54:35 +01:00
var classes = ['group', 'direct'].filter(function(c){
return c !== mode;
});
this.el.removeClass(classes.join(' ')).addClass(mode);
}
DmrMetaSlot.prototype.setTarget = function(target) {
if (this.target === target) return;
this.target = target;
this.el.find('.openwebrx-dmr-target').text(target || '');
}
2021-09-17 18:24:33 +02:00
DmrMetaSlot.prototype.setLocation = function(lat, lon, callsign) {
var hasLocation = lat && lon && callsign && callsign != '';
if (hasLocation === this.hasLocation && this.callsign === callsign) return;
this.hasLocation = hasLocation; this.callsign = callsign;
var html = '';
if (hasLocation) {
html = '<a class="openwebrx-maps-pin" href="map?callsign=' + encodeURIComponent(callsign) + '" target="_blank"><svg viewBox="0 0 20 35"><use xlink:href="static/gfx/svg-defs.svg#maps-pin"></use></svg></a>';
}
this.el.find('.openwebrx-dmr-id .location').html(html);
}
2021-01-16 22:07:55 +01:00
DmrMetaSlot.prototype.clear = function() {
this.setId();
this.setName();
2021-01-19 20:54:35 +01:00
this.setMode();
this.setTarget();
2021-09-17 18:24:33 +02:00
this.setLocation();
2021-01-16 22:07:55 +01:00
this.el.removeClass("active");
};
function DmrMetaPanel(el) {
MetaPanel.call(this, el);
this.modes = ['DMR'];
this.slots = this.el.find('.openwebrx-meta-slot').toArray().map(function(el){
return new DmrMetaSlot(el);
});
}
DmrMetaPanel.prototype = new MetaPanel();
DmrMetaPanel.prototype.update = function(data) {
if (!this.isSupported(data)) return;
if (data['slot']) {
var slot = this.slots[data['slot']];
slot.update(data);
} else {
this.clear();
}
}
DmrMetaPanel.prototype.clear = function() {
MetaPanel.prototype.clear.call(this);
this.el.find(".openwebrx-dmr-timeslot-panel").removeClass("muted");
2021-01-16 22:07:55 +01:00
this.slots.forEach(function(slot) {
slot.clear();
});
};
function YsfMetaPanel(el) {
MetaPanel.call(this, el);
this.modes = ['YSF'];
this.clear();
}
YsfMetaPanel.prototype = new MetaPanel();
YsfMetaPanel.prototype.update = function(data) {
if (!this.isSupported(data)) return;
this.setMode(data['mode']);
if (data['mode'] && data['mode'] !== "") {
this.setSource(data['source']);
this.setLocation(data['lat'], data['lon'], data['source']);
this.setUp(data['up']);
this.setDown(data['down']);
2021-08-11 14:10:52 +02:00
if (data['mode'].indexOf('data') < 0) {
this.el.find(".openwebrx-meta-slot").addClass("active");
}
} else {
2021-01-16 21:16:49 +01:00
this.clear();
}
};
YsfMetaPanel.prototype.clear = function() {
MetaPanel.prototype.clear.call(this);
2021-01-16 21:16:49 +01:00
this.setMode();
this.setSource();
this.setLocation();
this.setUp();
this.setDown();
};
YsfMetaPanel.prototype.setMode = function(mode) {
if (this.mode === mode) return;
this.mode = mode;
2021-01-19 00:36:55 +01:00
this.el.find('.openwebrx-ysf-mode').text(mode || '');
};
YsfMetaPanel.prototype.setSource = function(source) {
if (this.source === source) return;
this.source = source;
this.el.find('.openwebrx-ysf-source .callsign').text(source || '');
};
YsfMetaPanel.prototype.setLocation = function(lat, lon, callsign) {
2021-01-16 21:16:49 +01:00
var hasLocation = lat && lon && callsign && callsign != '';
if (hasLocation === this.hasLocation && this.callsign === callsign) return;
this.hasLocation = hasLocation; this.callsign = callsign;
var html = '';
2021-01-16 21:16:49 +01:00
if (hasLocation) {
2021-05-16 17:30:34 +02:00
html = '<a class="openwebrx-maps-pin" href="map?callsign=' + encodeURIComponent(callsign) + '" target="_blank"><svg viewBox="0 0 20 35"><use xlink:href="static/gfx/svg-defs.svg#maps-pin"></use></svg></a>';
}
this.el.find('.openwebrx-ysf-source .location').html(html);
};
YsfMetaPanel.prototype.setUp = function(up) {
if (this.up === up) return;
this.up = up;
2021-01-19 00:36:55 +01:00
this.el.find('.openwebrx-ysf-up').text(up || '');
};
YsfMetaPanel.prototype.setDown = function(down) {
if (this.down === down) return;
this.down = down;
2021-01-19 00:36:55 +01:00
this.el.find('.openwebrx-ysf-down').text(down || '');
}
2021-06-08 13:37:13 +02:00
function DStarMetaPanel(el) {
MetaPanel.call(this, el);
2021-06-08 14:04:54 +02:00
this.modes = ['DSTAR'];
2021-06-08 13:37:13 +02:00
this.clear();
}
DStarMetaPanel.prototype = new MetaPanel();
DStarMetaPanel.prototype.update = function(data) {
if (!this.isSupported(data)) return;
if (data['sync'] && data['sync'] == 'voice') {
this.el.find(".openwebrx-meta-slot").addClass("active");
this.setOurCall(data['ourcall']);
this.setYourCall(data['yourcall']);
this.setDeparture(data['departure']);
this.setDestination(data['destination']);
this.setMessage(data['message']);
2021-06-08 18:38:53 +02:00
this.setLocation(data['lat'], data['lon'], data['ourcall']);
} else {
this.clear();
2021-06-08 13:37:13 +02:00
}
};
DStarMetaPanel.prototype.setOurCall = function(ourcall) {
if (this.ourcall === ourcall) return;
this.ourcall = ourcall;
2021-06-08 18:38:53 +02:00
this.el.find('.openwebrx-dstar-ourcall .callsign').text(ourcall || '');
2021-06-08 13:37:13 +02:00
};
DStarMetaPanel.prototype.setYourCall = function(yourcall) {
if (this.yourcall === yourcall) return;
this.yourcall = yourcall;
this.el.find('.openwebrx-dstar-yourcall').text(yourcall || '');
};
DStarMetaPanel.prototype.setDeparture = function(departure) {
if (this.departure === departure) return;
this.departure = departure;
this.el.find('.openwebrx-dstar-departure').text(departure || '');
};
DStarMetaPanel.prototype.setDestination = function(destination) {
if (this.destination === destination) return;
this.destination = destination;
this.el.find('.openwebrx-dstar-destination').text(destination || '');
};
DStarMetaPanel.prototype.setMessage = function(message) {
if (this.message === message) return;
this.message = message;
this.el.find('.openwebrx-dstar-message').text(message || '');
}
DStarMetaPanel.prototype.clear = function() {
MetaPanel.prototype.clear.call(this);
this.setOurCall();
this.setYourCall();
this.setDeparture();
this.setDestination();
this.setMessage();
2021-06-08 18:38:53 +02:00
this.setLocation();
};
DStarMetaPanel.prototype.setLocation = function(lat, lon, callsign) {
var hasLocation = lat && lon && callsign && callsign != '';
if (hasLocation === this.hasLocation && this.callsign === callsign) return;
this.hasLocation = hasLocation; this.callsign = callsign;
var html = '';
if (hasLocation) {
html = '<a class="openwebrx-maps-pin" href="map?callsign=' + encodeURIComponent(callsign) + '" target="_blank"><svg viewBox="0 0 20 35"><use xlink:href="static/gfx/svg-defs.svg#maps-pin"></use></svg></a>';
}
2021-06-11 14:36:11 +02:00
this.el.find('.openwebrx-dstar-ourcall .location').html(html);
2021-06-08 13:37:13 +02:00
};
function NxdnMetaPanel(el) {
MetaPanel.call(this, el);
this.modes = ['NXDN'];
this.clear();
}
NxdnMetaPanel.prototype = new MetaPanel();
NxdnMetaPanel.prototype.update = function(data) {
if (!this.isSupported(data)) return;
2021-09-17 18:24:33 +02:00
if (data['sync'] && data['sync'] === 'voice') {
this.el.find(".openwebrx-meta-slot").addClass("active");
this.setSource(data['additional'] && data['additional']['callsign'] || data['source']);
this.setName(data['additional'] && data['additional']['fname']);
this.setDestination(data['destination']);
this.setMode(['conference', 'individual'].includes(data['type']) ? data['type'] : undefined);
} else {
this.clear();
}
};
NxdnMetaPanel.prototype.setSource = function(source) {
if (this.source === source) return;
this.source = source;
this.el.find('.openwebrx-nxdn-source').text(source || '');
};
NxdnMetaPanel.prototype.setName = function(name) {
if (this.name === name) return;
this.name = name;
this.el.find('.openwebrx-nxdn-name').text(name || '');
};
NxdnMetaPanel.prototype.setDestination = function(destination) {
if (this.destination === destination) return;
this.destination = destination;
this.el.find('.openwebrx-nxdn-destination').text(destination || '');
};
NxdnMetaPanel.prototype.setMode = function(mode) {
if (this.mode === mode) return;
this.mode = mode;
var modes = ['individual', 'conference'];
var classes = modes.filter(function(c){
return c !== mode;
});
this.el.find('.openwebrx-meta-slot').removeClass(classes.join(' ')).addClass(mode);
};
NxdnMetaPanel.prototype.clear = function() {
MetaPanel.prototype.clear.call(this);
this.setMode();
this.setSource();
this.setName();
this.setDestination();
};
function M17MetaPanel(el) {
MetaPanel.call(this, el);
this.modes = ['M17'];
this.clear();
}
M17MetaPanel.prototype = new MetaPanel();
M17MetaPanel.prototype.update = function(data) {
if (!this.isSupported(data)) return;
if (data['sync'] && data['sync'] === 'voice') {
this.el.find(".openwebrx-meta-slot").addClass("active");
this.setSource(data['source']);
this.setDestination(data['destination']);
} else {
this.clear();
}
};
M17MetaPanel.prototype.setSource = function(source) {
if (this.source === source) return;
this.source = source;
this.el.find('.openwebrx-m17-source').text(source || '');
};
M17MetaPanel.prototype.setDestination = function(destination) {
if (this.destination === destination) return;
this.destination = destination;
this.el.find('.openwebrx-m17-destination').text(destination || '');
};
M17MetaPanel.prototype.clear = function() {
MetaPanel.prototype.clear.call(this);
this.setSource();
this.setDestination();
};
2024-01-18 16:11:13 +01:00
function WfmMetaPanel(el) {
MetaPanel.call(this, el);
this.modes = ['WFM'];
this.enabled = false;
this.timeout = false;
this.clear();
}
WfmMetaPanel.prototype = new MetaPanel();
WfmMetaPanel.prototype.update = function(data) {
2024-01-18 23:55:20 +01:00
if (!this.isSupported(data)) return;
2024-01-18 16:11:13 +01:00
var me = this;
// automatically clear metadata panel when no RDS data is received for more than ten seconds
if (this.timeout) clearTimeout(this.timeout);
this.timeout = setTimeout(function(){
me.clear();
}, 10000);
if ('pi' in data && data.pi !== this.pi) {
this.clear();
this.pi = data.pi;
}
var $el = $(this.el);
2024-01-18 23:55:20 +01:00
if ('ps' in data) {
this.ps = data.ps;
}
if ('prog_type' in data) {
$el.find('.rds-prog_type').text(data['prog_type']);
}
2024-01-18 16:11:13 +01:00
if ('callsign' in data) {
2024-01-18 23:55:20 +01:00
this.callsign = data.callsign;
2024-01-18 16:11:13 +01:00
}
2024-01-18 23:55:20 +01:00
if ('pi' in data) {
this.pi = data.pi
2024-01-18 16:11:13 +01:00
}
2024-01-18 17:30:26 +01:00
if ('clock_time' in data) {
var date = new Date(Date.parse(data.clock_time));
$el.find('.rds-clock').text(date.toLocaleString([], {dateStyle: 'short', timeStyle: 'short'}));
}
2024-01-18 23:55:20 +01:00
if ('radiotext_plus' in data) {
// prefer displaying radiotext plus over radiotext
this.radiotext_plus = this.radiotext_plus || {
item_toggle: -1,
news: []
2024-01-18 23:55:20 +01:00
};
var tags = {};
if ('tags' in data.radiotext_plus) {
tags = Object.fromEntries(data.radiotext_plus.tags.map(function (tag) {
return [tag['content-type'], tag['data']]
}));
}
if (data.radiotext_plus.item_toggle !== this.radiotext_plus.item_toggle) {
this.radiotext_plus.item_toggle = data.radiotext_plus.item_toggle;
this.radiotext_plus.item = '';
}
this.radiotext_plus.item_running = !!data.radiotext_plus.item_running;
if ('item.artist' in tags && 'item.title' in tags) {
this.radiotext_plus.item = tags['item.artist'] + ' - ' + tags['item.title'];
2024-01-19 17:13:13 +01:00
} else {
var items = Object.entries(tags).filter(function (e) {
return e[0].startsWith("item.")
})
if (items.length) {
this.radiotext_plus.item = items.map(function (e) {
return e[0].substr(5, 1).toUpperCase() + e[0].substr(6) + ': ' + e[1];
}).join('; ');
}
2024-01-18 23:55:20 +01:00
}
if ('programme.now' in tags) {
this.radiotext_plus.programme = tags['programme.now'];
}
if ('programme.homepage' in tags) {
this.radiotext_plus.homepage = tags['programme.homepage'];
}
if ('stationname.long' in tags) {
this.long_stationname = tags['stationname.long'];
}
if ('stationname.short' in tags) {
this.short_stationname = tags['stationname.short'];
}
if ('info.news' in tags) {
var n = tags['info.news'];
var i = this.radiotext_plus.news.indexOf(n);
if (i >= 0) {
this.radiotext_plus.news.splice(i, 1);
}
this.radiotext_plus.news.push(n);
// limit the number of items
this.radiotext_plus.news = this.radiotext_plus.news.slice(-5);
2024-01-18 23:55:20 +01:00
}
2024-01-19 01:21:25 +01:00
if ('info.weather' in tags) {
this.radiotext_plus.weather = tags['info.weather'];
}
2024-01-18 23:55:20 +01:00
}
if ('radiotext' in data && !this.radiotext_plus) {
this.radiotext = data.radiotext;
}
if (this.radiotext_plus) {
$el.find('.rds-radiotext').empty();
if (this.radiotext_plus.item_running) {
$el.find('.rds-rtplus-item').text(this.radiotext_plus.item || '');
} else {
$el.find('.rds-rtplus-item').empty();
}
$el.find('.rds-rtplus-programme').text(this.radiotext_plus.programme || '');
$el.find('.rds-rtplus-news').empty().html(this.radiotext_plus.news.map(function(n){
2024-01-19 17:13:27 +01:00
return $('<li>').text(n);
}));
2024-01-19 01:21:25 +01:00
$el.find('.rds-rtplus-weather').text(this.radiotext_plus.weather || '');
2024-01-18 23:55:20 +01:00
if (this.radiotext_plus.homepage) {
2024-01-19 18:34:52 +01:00
var url = this.radiotext_plus.homepage;
// prefix with a protcol if not present. we'll assume https, should be generally available these days.
if (url.indexOf('://') < 0) url = 'https://' + url;
// avoid updating the link if not necessary since that would prevent the user from clicking it
if ($el.find('.rds-rtplus-homepage a').attr('href') !== url) {
var link = $('<a href="' + url + '" target="_blank"></a>').text(this.radiotext_plus.homepage);
$el.find('.rds-rtplus-homepage').html(link);
}
2024-01-18 23:55:20 +01:00
}
} else {
$el.find('.rds-radiotext-plus .autoclear').empty();
$el.find('.rds-radiotext').text(this.radiotext || '');
}
$el.find('.rds-stationname').text(this.long_stationname || this.ps);
$el.find('.rds-identifier').text(this.short_stationname || this.callsign || this.pi);
2024-01-18 16:11:13 +01:00
};
WfmMetaPanel.prototype.isSupported = function(data) {
return this.modes.includes(data.mode);
};
WfmMetaPanel.prototype.setEnabled = function(enabled) {
if (enabled === this.enabled) return;
this.enabled = enabled;
if (enabled) {
$(this.el).removeClass('disabled').html(
2024-01-18 16:11:13 +01:00
'<div class="rds-container">' +
2024-01-18 23:55:20 +01:00
'<div class="rds-identifier rds-autoclear"></div>' +
'<div class="rds-stationname rds-autoclear"></div>' +
'<div class="rds-radiotext rds-autoclear"></div>' +
'<div class="rds-radiotext-plus">' +
'<div class="rds-rtplus-programme rds-autoclear"></div>' +
'<div class="rds-rtplus-item rds-autoclear"></div>' +
'<ul class="rds-rtplus-news rds-autoclear"></ul>' +
2024-01-19 01:21:25 +01:00
'<div class="rds-rtplus-weather rds-autoclear"></div>' +
'<div class="rds-rtplus-homepage rds-autoclear"></div>' +
2024-01-18 23:55:20 +01:00
'</div>' +
'<div class="rds-prog_type rds-autoclear"></div>' +
'<div class="rds-clock rds-autoclear"></div>' +
2024-01-18 16:11:13 +01:00
'</div>'
);
} else {
$(this.el).addClass('disabled').emtpy()
2024-01-18 16:11:13 +01:00
}
};
WfmMetaPanel.prototype.clear = function() {
2024-01-18 23:55:20 +01:00
$(this.el).find('.rds-autoclear').empty();
this.pi = '';
this.ps = '';
this.callsign = '';
this.long_stationname = '';
this.short_stationname = '';
this.radiotext = '';
this.radiotext_plus = false;
2024-01-18 16:11:13 +01:00
};
function DabMetaPanel(el) {
MetaPanel.call(this, el);
var me = this;
this.modes = ['DAB'];
this.service_id = 0;
2024-01-25 00:10:25 +01:00
this.$select = $('<select id="dab-service-id"></select>');
this.$select.on("change", function() {
me.service_id = parseInt($(this).val());
$('#openwebrx-panel-receiver').demodulatorPanel().getDemodulator().setDabServiceId(me.service_id);
2024-01-25 00:10:25 +01:00
});
2024-01-25 00:22:10 +01:00
var $container = $(
'<div class="dab-container">' +
2024-01-25 18:40:31 +01:00
'<div class="dab-auto-clear dab-ensemble-id"></div>' +
'<div class="dab-auto-clear dab-ensemble-label"></div>' +
2024-01-26 18:42:54 +01:00
'<div class="dab-auto-clear dab-timestamp"></div>' +
'<label for="dab-service-id">DAB Programme:</label>' +
2024-01-25 00:22:10 +01:00
'</div>'
);
$container.append(this.$select);
$(this.el).append($container);
this.clear();
this.programmeTimeout = false;
}
DabMetaPanel.prototype = new MetaPanel();
DabMetaPanel.prototype.isSupported = function(data) {
return this.modes.includes(data.mode);
}
DabMetaPanel.prototype.update = function(data) {
if (!this.isSupported(data)) return;
2024-01-25 18:40:31 +01:00
if ('ensemble_id' in data) {
2024-01-26 18:42:54 +01:00
$(this.el).find('.dab-ensemble-id').text('0x' + data.ensemble_id.toString(16));
2024-01-25 18:40:31 +01:00
}
if ('ensemble_label' in data) {
$(this.el).find('.dab-ensemble-label').text(data.ensemble_label);
}
2024-01-26 18:42:54 +01:00
if ('timestamp' in data) {
var date = new Date(data.timestamp * 1000);
$(this.el).find('.dab-timestamp').text(date.toLocaleString([], {dateStyle: 'short', timeStyle: 'medium'}));
}
if ('programmes' in data) {
var options = Object.entries(data.programmes).map(function(e) {
return '<option value="' + e[0] + '">' + e[1] + '</option>';
});
this.$select.html(
options.join('') +
'<option value="" disabled selected hidden>Loading...</option>'
2024-01-25 00:10:25 +01:00
);
var me = this;
if (this.programmeTimeout) clearTimeout(this.programmeTimeout);
this.programmeTimeout = setTimeout(function() {
// user has selected a programme to play. don't interfere.
me.$select.val(this.service_id);
if (me.$select.val()) return;
me.$select.val(me.$select.find('option:first').val()).change();
}, 1000);
}
}
DabMetaPanel.prototype.clear = function() {
this.service_id = 0;
2024-01-25 18:40:31 +01:00
$(this.el).find('.dab-auto-clear').empty();
this.$select.html(
'<option value="" disabled selected hidden>Loading...</option>'
);
}
MetaPanel.types = {
dmr: DmrMetaPanel,
2021-06-08 13:37:13 +02:00
ysf: YsfMetaPanel,
dstar: DStarMetaPanel,
nxdn: NxdnMetaPanel,
m17: M17MetaPanel,
2024-01-18 16:11:13 +01:00
wfm: WfmMetaPanel,
dab: DabMetaPanel,
};
$.fn.metaPanel = function() {
return this.map(function() {
var $self = $(this);
if (!$self.data('metapanel')) {
var matches = /^openwebrx-panel-metadata-([a-z0-9]+)$/.exec($self.prop('id'));
var constructor = matches && MetaPanel.types[matches[1]] || MetaPanel;
$self.data('metapanel', new constructor($self));
}
return $self.data('metapanel');
});
};