diff --git a/htdocs/settings.js b/htdocs/settings.js index 55abf45..2d21188 100644 --- a/htdocs/settings.js +++ b/htdocs/settings.js @@ -1,13 +1,14 @@ -function Input(name, value) { +function Input(name, value, label) { this.name = name; this.value = value; + this.label = label; }; -Input.prototype.bootstrapify = function(input, label) { +Input.prototype.bootstrapify = function(input) { input.addClass('form-control').addClass('form-control-sm'); return [ '
', - '', + '', '
', input[0].outerHTML, '
', @@ -55,34 +56,104 @@ SchedulerInput.prototype.render = function() { return $('

Scheduler

'); }; -Input.mappings = { - "name": TextInput, - "type": TextInput, - "ppm": NumberInput, - "profiles": ProfileInput, - "scheduler": SchedulerInput -}; - -function SdrDevice(el) { +function SdrDevice(el, data) { this.el = el; - this.data = JSON.parse(decodeURIComponent(el.data('config'))); + this.data = data; this.inputs = {}; this.render(); var self = this; el.on('click', '.fieldselector .btn', function() { var key = el.find('.fieldselector select').val(); - self.data[key] = false; + self.data[key] = self.getInitialValue(key); self.render(); }); }; +SdrDevice.create = function(el) { + var data = JSON.parse(decodeURIComponent(el.data('config'))); + var type = data.type; + var constructor = SdrDevice.types[type] || SdrDevice; + return new constructor(el, data); +}; + +SdrDevice.prototype.getData = function() { + return $.extend(new Object(), this.getDefaults(), this.data); +}; + +SdrDevice.prototype.getDefaults = function() { + var defaults = {} + $.each(this.getMappings(), function(k, v) { + if (!v.includeInDefault) return; + defaults[k] = 'initialValue' in v ? v['initialValue'] : false; + }); + return defaults; +}; + +SdrDevice.prototype.getMappings = function() { + return { + "name": { + constructor: TextInput, + label: "Name", + initialValue: "", + includeInDefault: true + }, + "type": { + constructor: TextInput, + label: "Type", + initialValue: '', + includeInDefault: true + }, + "ppm": { + constructor: NumberInput, + label: "PPM", + initialValue: 0 + }, + "profiles": { + constructor: ProfileInput, + label: "Profiles", + initialValue: [], + includeInDefault: true, + }, + "scheduler": { + constructor: SchedulerInput, + label: "Scheduler", + initialValue: {} + }, + "rf_gain": { + constructor: TextInput, + label: "Gain", + initialValue: 0 + } + }; +}; + +SdrDevice.prototype.getMapping = function(key) { + var mappings = this.getMappings(); + return mappings[key]; +}; + +SdrDevice.prototype.getInputClass = function(key) { + var mapping = this.getMapping(key); + return mapping && mapping.constructor || TextInput; +}; + +SdrDevice.prototype.getLabel = function(key) { + var mapping = this.getMapping(key); + return mapping && mapping.label || key; +}; + +SdrDevice.prototype.getInitialValue = function(key) { + var mapping = this.getMapping(key); + return mapping && ('initialValue' in mapping) ? mapping['initialValue'] : false; +}; + SdrDevice.prototype.render = function() { var self = this; self.el.empty(); - $.each(this.data, function(key, value) { - var inputClass = Input.mappings[key] || TextInput; - var input = new inputClass(key, value); + $.each(this.getData(), function(key, value) { + var inputClass = self.getInputClass(key); + var input = new inputClass(key, value, self.getLabel(key)); self.inputs[key] = input; self.el.append(input.render()); }); @@ -95,10 +166,10 @@ SdrDevice.prototype.renderFieldSelector = function() { '

Add new configuration options

' + '
' + '
' + '
' + @@ -108,11 +179,33 @@ SdrDevice.prototype.renderFieldSelector = function() { '
'; }; +RtlSdrDevice = function(el, data) { + SdrDevice.apply(this, arguments); +}; + +RtlSdrDevice.prototype = Object.create(SdrDevice.prototype); +RtlSdrDevice.prototype.constructor = RtlSdrDevice; + +RtlSdrDevice.prototype.getMappings = function() { + var mappings = SdrDevice.prototype.getMappings.apply(this, arguments); + return $.extend(new Object(), mappings, { + "device": { + constructor: TextInput, + label: "Serial Number", + initialValue: "" + } + }); +}; + +SdrDevice.types = { + 'rtl_sdr': RtlSdrDevice +}; + $.fn.sdrdevice = function() { return this.map(function(){ var el = $(this); if (!el.data('sdrdevice')) { - el.data('sdrdevice', new SdrDevice(el)); + el.data('sdrdevice', SdrDevice.create(el)); } return el.data('sdrdevice'); });