diff --git a/htdocs/lib/settings/WsjtDecodingDepthsInput.js b/htdocs/lib/settings/WsjtDecodingDepthsInput.js index 0383630..5a2ddf4 100644 --- a/htdocs/lib/settings/WsjtDecodingDepthsInput.js +++ b/htdocs/lib/settings/WsjtDecodingDepthsInput.js @@ -5,7 +5,9 @@ $.fn.wsjtDecodingDepthsInput = function() { this.modeInput.val(mode); this.valueInput = $(inputs.get(1)).clone(); this.valueInput.val(value); - this.el.append([this.modeInput, this.valueInput].map(function(i) { + this.removeButton = $(''); + this.removeButton.data('row', this); + this.el.append([this.modeInput, this.valueInput, this.removeButton].map(function(i) { return $('').append(i); })); } @@ -14,6 +16,14 @@ $.fn.wsjtDecodingDepthsInput = function() { return this.el; } + WsjtDecodingDepthRow.prototype.getValue = function() { + var value = parseInt(this.valueInput.val()) + if (Number.isNaN(value)) { + return {}; + } + return Object.fromEntries([[this.modeInput.val(), value]]); + } + this.each(function(){ var $input = $(this); var $el = $input.parent(); @@ -27,6 +37,32 @@ $.fn.wsjtDecodingDepthsInput = function() { $table.append(rows.map(function(r) { return r.getEl(); })); + + var updateValue = function(){ + $input.val(JSON.stringify($.extend.apply({}, rows.map(function(r) { + return r.getValue(); + })))); + }; + + $table.on('change', updateValue); $el.append($table); + var $addButton = $(''); + + $addButton.on('click', function() { + var row = new WsjtDecodingDepthRow(inputs) + rows.push(row); + $table.append(row.getEl()); + return false; + }); + $el.on('click', '.btn.remove', function(e){ + var row = $(e.target).data('row'); + var index = rows.indexOf(row); + if (index < 0) return false; + rows.splice(index, 1); + row.getEl().remove(); + updateValue(); + return false; + }); + $el.append($addButton); }); }; \ No newline at end of file