From 8267aa8d9dc84f7e1b4958ac9a2e80369159972e Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Mon, 15 Feb 2021 22:57:21 +0100 Subject: [PATCH] implement removal --- .../lib/settings/WsjtDecodingDepthsInput.js | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) 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