From 1319f314ff591b7e20543b9049b01fc83dbad9d9 Mon Sep 17 00:00:00 2001 From: James Read Date: Sat, 23 Mar 2024 21:52:53 +0000 Subject: [PATCH] Argument suggestions (#252) * feature: Suggestions for inputs * feature: #103 Completed suggestions support --- OliveTin.proto | 1 + internal/config/config.go | 1 + internal/grpcapi/grpcApiActions.go | 1 + webui.dev/js/ArgumentForm.js | 28 ++++++++++++++++++++++++++++ 4 files changed, 31 insertions(+) diff --git a/OliveTin.proto b/OliveTin.proto index 8071938..5503d36 100644 --- a/OliveTin.proto +++ b/OliveTin.proto @@ -23,6 +23,7 @@ message ActionArgument { repeated ActionArgumentChoice choices = 5; string description = 6; + map suggestions = 7; } message ActionArgumentChoice { diff --git a/internal/config/config.go b/internal/config/config.go index cd9662c..6ecf7e0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -33,6 +33,7 @@ type ActionArgument struct { Default string Choices []ActionArgumentChoice Entity string + Suggestions map[string]string } // ActionArgumentChoice represents a predefined choice for an argument. diff --git a/internal/grpcapi/grpcApiActions.go b/internal/grpcapi/grpcApiActions.go index 32872aa..3487cbe 100644 --- a/internal/grpcapi/grpcApiActions.go +++ b/internal/grpcapi/grpcApiActions.go @@ -94,6 +94,7 @@ func actionCfgToPb(action *config.Action, user *acl.AuthenticatedUser) *pb.Actio Description: cfgArg.Description, DefaultValue: cfgArg.Default, Choices: buildChoices(cfgArg), + Suggestions: cfgArg.Suggestions, } btn.Arguments = append(btn.Arguments, &pbArg) diff --git a/webui.dev/js/ArgumentForm.js b/webui.dev/js/ArgumentForm.js index d9712b4..0a0f4d3 100644 --- a/webui.dev/js/ArgumentForm.js +++ b/webui.dev/js/ArgumentForm.js @@ -64,6 +64,7 @@ class ArgumentForm extends window.HTMLElement { domArgumentWrapper.classList.add('argument-wrapper') domArgumentWrapper.appendChild(this.createDomLabel(arg)) + domArgumentWrapper.appendChild(this.createDomSuggestions(arg)) domArgumentWrapper.appendChild(this.createDomInput(arg)) domArgumentWrapper.appendChild(this.createDomDescription(arg)) @@ -79,6 +80,29 @@ class ArgumentForm extends window.HTMLElement { return domLbl } + createDomSuggestions (arg) { + if (typeof arg.suggestions !== 'object' || arg.suggestions.length === 0) { + return document.createElement('span') + } + + const ret = document.createElement('datalist') + ret.setAttribute('id', arg.name + '-choices') + + for (const suggestion of Object.keys(arg.suggestions)) { + const opt = document.createElement('option') + + opt.setAttribute('value', suggestion) + + if (typeof arg.suggestions[suggestion] !== 'undefined' && arg.suggestions[suggestion].length > 0) { + opt.innerText = arg.suggestions[suggestion] + } + + ret.appendChild(opt) + } + + return ret + } + createDomInput (arg) { let domEl = null @@ -144,6 +168,10 @@ class ArgumentForm extends window.HTMLElement { domEl.name = arg.name domEl.value = arg.defaultValue + if (typeof arg.suggestions === 'object' && Object.keys(arg.suggestions).length > 0) { + domEl.setAttribute('list', arg.name + '-choices') + } + this.argInputs.push(domEl) return domEl