Quellcode durchsuchen

feat: #104 Checkboxes / booleans at last! (#620)

James Read vor 11 Monaten
Ursprung
Commit
e922baa2e0
2 geänderte Dateien mit 42 neuen und 2 gelöschten Zeilen
  1. 25 0
      service/internal/executor/arguments.go
  2. 17 2
      webui.dev/js/ArgumentForm.js

+ 25 - 0
service/internal/executor/arguments.go

@@ -241,6 +241,31 @@ func mangleInvalidArgumentValues(req *ExecutionRequest) {
 		if arg.Type == "datetime" {
 			mangleInvalidDatetimeValues(req, &arg)
 		}
+
+		mangleCheckboxValues(req, &arg)
+	}
+}
+
+func mangleCheckboxValues(req *ExecutionRequest, arg *config.ActionArgument) {
+	if arg.Type != "checkbox" {
+		return
+	}
+
+	log.Infof("Checking checkbox values for argument %s in action %s", arg.Name, req.Action.Title)
+
+	for i, _ := range arg.Choices {
+		choice := &arg.Choices[i]
+
+		if req.Arguments[arg.Name] == choice.Title {
+			log.WithFields(log.Fields{
+				"arg":         arg.Name,
+				"oldValue":    req.Arguments[arg.Name],
+				"newValue":    choice.Value,
+				"actionTitle": req.Action.Title,
+			}).Infof("Mangled checkbox value")
+
+			req.Arguments[arg.Name] = choice.Value
+		}
 	}
 }
 

+ 17 - 2
webui.dev/js/ArgumentForm.js

@@ -36,6 +36,14 @@ class ArgumentForm extends window.HTMLElement {
     const ret = []
 
     for (const arg of this.argInputs) {
+      if (arg.type === 'checkbox') {
+        if (arg.checked) {
+          arg.value = "1"
+        } else {
+          arg.value = "0"
+        }
+      }
+
       ret.push({
         name: arg.name,
         value: arg.value
@@ -114,7 +122,7 @@ class ArgumentForm extends window.HTMLElement {
   createDomInput (arg) {
     let domEl = null
 
-    if (arg.choices.length > 0) {
+    if (arg.choices.length > 0 && (arg.type == "select" || arg.type == "")) {
       domEl = document.createElement('select')
 
       // select/choice elements don't get an onchange/validation because theoretically
@@ -151,6 +159,13 @@ class ArgumentForm extends window.HTMLElement {
           domEl = document.createElement('input')
           domEl.setAttribute('type', 'datetime-local')
           domEl.setAttribute('step', '1')
+          break
+        case 'checkbox':
+          domEl = document.createElement('input')
+          domEl.setAttribute('type', 'checkbox')
+          domEl.setAttribute('name', arg.name)
+          domEl.setAttribute('value', "1")
+          
           break
         case 'password':
         case 'email':
@@ -225,7 +240,7 @@ class ArgumentForm extends window.HTMLElement {
     const url = new URL(window.location.href)
 
     if (ev.target.type === 'password') {
-      return;
+      return
     }
 
     // copy the parameter value