From 486926aa8a472f7b82ca9c8175c6eace717709f8 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Mon, 5 Jul 2021 07:47:34 +0200 Subject: [PATCH] WW-5129 Supports dynamic attributes in second control --- .../template/simple/doubleselect.ftl | 5 +- .../template/simple/dynamic-attributes.ftl | 24 ++++++--- .../template/simple/inputtransferselect.ftl | 8 +-- .../template/simple/optiontransferselect.ftl | 3 ++ .../simple/prefixed-dynamic-attributes.ftl | 36 +++++++++++++ .../main/resources/template/simple/select.ftl | 4 +- .../views/jsp/ui/DoubleSelectTest.java | 17 +++++-- .../jsp/ui/InputTransferSelectTagTest.java | 32 +++++++++--- .../jsp/ui/OptionTransferSelectTagTest.java | 32 ++++++++++++ .../struts2/views/jsp/ui/DoubleSelect-5.txt | 39 +++++++++++++++ .../views/jsp/ui/inputtransferselect-2.txt | 30 +++++++++++ .../views/jsp/ui/optiontransferselect-8.txt | 50 +++++++++++++++++++ 12 files changed, 257 insertions(+), 23 deletions(-) create mode 100644 core/src/main/resources/template/simple/prefixed-dynamic-attributes.ftl create mode 100644 core/src/test/resources/org/apache/struts2/views/jsp/ui/DoubleSelect-5.txt create mode 100644 core/src/test/resources/org/apache/struts2/views/jsp/ui/inputtransferselect-2.txt create mode 100644 core/src/test/resources/org/apache/struts2/views/jsp/ui/optiontransferselect-8.txt diff --git a/core/src/main/resources/template/simple/doubleselect.ftl b/core/src/main/resources/template/simple/doubleselect.ftl index f1adf8f6eb..e80ed405d4 100644 --- a/core/src/main/resources/template/simple/doubleselect.ftl +++ b/core/src/main/resources/template/simple/doubleselect.ftl @@ -18,6 +18,7 @@ * under the License. */ --> +<#global dynamic_attributes_ignore = "second-"/> <#include "/${parameters.templateDir}/simple/select.ftl" /> <#assign startCount = 0/> <#if parameters.headerKey?? && parameters.headerValue??> @@ -60,6 +61,8 @@ <#if parameters.doubleDisabled!false> disabled="disabled"<#rt/> +<#include "/${parameters.templateDir}/${parameters.expandTheme}/prefixed-dynamic-attributes.ftl" /> +<@prefixedDynamicAttributes prefix="second-"/> > <#if parameters.doubleMultiple!false> @@ -190,4 +193,4 @@ ${parameters.id}Temp.options[0].selected = true; } } - \ No newline at end of file + diff --git a/core/src/main/resources/template/simple/dynamic-attributes.ftl b/core/src/main/resources/template/simple/dynamic-attributes.ftl index 95de4b76e1..c000e82cdb 100644 --- a/core/src/main/resources/template/simple/dynamic-attributes.ftl +++ b/core/src/main/resources/template/simple/dynamic-attributes.ftl @@ -21,12 +21,24 @@ <#if (parameters.dynamicAttributes?? && parameters.dynamicAttributes?size > 0)><#rt/> <#assign aKeys = parameters.dynamicAttributes.keySet()><#rt/> <#list aKeys as aKey><#rt/> - <#assign keyValue = parameters.dynamicAttributes.get(aKey)/> - <#if keyValue?is_string> - <#assign value = struts.translateVariables(keyValue)!keyValue/> - <#else> - <#assign value = keyValue?string/> - +<#if dynamic_attributes_ignore??> +<#if !aKey?starts_with(dynamic_attributes_ignore)> +<#assign keyValue = parameters.dynamicAttributes.get(aKey)/> +<#if keyValue?is_string> + <#assign value = struts.translateVariables(keyValue)!keyValue/> +<#else> + <#assign value = keyValue?string/> + ${aKey}="${value?html}"<#rt/> + +<#else> +<#assign keyValue = parameters.dynamicAttributes.get(aKey)/> +<#if keyValue?is_string> + <#assign value = struts.translateVariables(keyValue)!keyValue/> +<#else> + <#assign value = keyValue?string/> + + ${aKey}="${value?html}"<#rt/> + <#rt/> <#rt/> diff --git a/core/src/main/resources/template/simple/inputtransferselect.ftl b/core/src/main/resources/template/simple/inputtransferselect.ftl index 0b14f51438..e0bbe3c8b3 100644 --- a/core/src/main/resources/template/simple/inputtransferselect.ftl +++ b/core/src/main/resources/template/simple/inputtransferselect.ftl @@ -29,7 +29,6 @@
<#t/> - name="${parameters.name!""?html}_input"<#rt/> <#if parameters.disabled!false> @@ -55,6 +54,8 @@ <#include "/${parameters.templateDir}/${parameters.expandTheme}/scripting-events.ftl" /> <#include "/${parameters.templateDir}/${parameters.expandTheme}/common-attributes.ftl" /> +<#include "/${parameters.templateDir}/${parameters.expandTheme}/prefixed-dynamic-attributes.ftl" /> +<@prefixedDynamicAttributes prefix="input-"/> /> @@ -94,9 +95,10 @@ <#if parameters.rightTitle?has_content><#t/>
<#t/> +<#global dynamic_attributes_ignore = "input-"/> <#include "/${parameters.templateDir}/simple/select.ftl" /> <#if parameters.allowUpDown!true> - onclick="moveOptionDown(document.getElementById('${parameters.id}'), 'key', '${parameters.headerKey}');" <#else> @@ -106,7 +108,7 @@ value="${parameters.downLabel?html}" /> - onclick="moveOptionUp(document.getElementById('${parameters.id}'), 'key', '${parameters.headerKey}');" <#else> diff --git a/core/src/main/resources/template/simple/optiontransferselect.ftl b/core/src/main/resources/template/simple/optiontransferselect.ftl index 093ec22aba..d46ac65415 100644 --- a/core/src/main/resources/template/simple/optiontransferselect.ftl +++ b/core/src/main/resources/template/simple/optiontransferselect.ftl @@ -28,6 +28,7 @@ <#if parameters.leftTitle??><#t/>
<#t/> +<#global dynamic_attributes_ignore = "right-"/> <#include "/${parameters.templateDir}/simple/select.ftl" /> <#if parameters.allowUpDownOnLeft!true> <#t/> accesskey="${parameters.doubleAccesskey?html}" + <#include "/${parameters.templateDir}/${parameters.expandTheme}/prefixed-dynamic-attributes.ftl" /> + <@prefixedDynamicAttributes prefix="right-"/> > <#if parameters.doubleHeaderKey?? && parameters.doubleHeaderValue??><#t/> diff --git a/core/src/main/resources/template/simple/prefixed-dynamic-attributes.ftl b/core/src/main/resources/template/simple/prefixed-dynamic-attributes.ftl new file mode 100644 index 0000000000..68f2ea6538 --- /dev/null +++ b/core/src/main/resources/template/simple/prefixed-dynamic-attributes.ftl @@ -0,0 +1,36 @@ +<#-- +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +--> +<#macro prefixedDynamicAttributes prefix> +<#if (parameters.dynamicAttributes?? && parameters.dynamicAttributes?size > 0)><#rt/> +<#assign aKeys = parameters.dynamicAttributes.keySet()><#rt/> +<#list aKeys as aKey><#rt/> +<#if aKey?starts_with(prefix)> +<#assign keyValue = parameters.dynamicAttributes.get(aKey)/> +<#if keyValue?is_string> + <#assign value = struts.translateVariables(keyValue)!keyValue/> +<#else> + <#assign value = keyValue?string/> + + ${aKey}="${value}"<#rt/> + +<#rt/> +<#rt/> + diff --git a/core/src/main/resources/template/simple/select.ftl b/core/src/main/resources/template/simple/select.ftl index b0144baf69..0e7605825e 100644 --- a/core/src/main/resources/template/simple/select.ftl +++ b/core/src/main/resources/template/simple/select.ftl @@ -68,7 +68,7 @@ <#assign itemKeyStr = stack.findString('top')> <#if parameters.listValueKey??> - <#-- checks the valueStack for the 'valueKey.' The valueKey is then looked-up in the locale file for it's + <#-- checks the valueStack for the 'valueKey.' The valueKey is then looked-up in the locale file for it's localized value. This is then used as a label --> <#assign valueKey = stack.findString(parameters.listValueKey)!'' /> <#if valueKey?has_content> @@ -139,7 +139,7 @@ <#if ( !parameters.id?? && !parameters.name??)> - + <#if parameters.disabled!false> disabled="disabled"<#rt/> diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/DoubleSelectTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/DoubleSelectTest.java index ff1a7d7109..621fd4ba28 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/ui/DoubleSelectTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/DoubleSelectTest.java @@ -87,13 +87,13 @@ public void testDouble() throws Exception { tag.setCssStyle("s1"); tag.setDoubleCssClass("c2"); tag.setDoubleCssStyle("s2"); - + tag.doStartTag(); tag.doEndTag(); verify(SelectTag.class.getResource("DoubleSelect-1.txt")); } - + public void testOnchange() throws Exception { TestAction testAction = (TestAction) action; @@ -151,7 +151,7 @@ public void testOnchange() throws Exception { tag.setCssStyle("s1"); tag.setDoubleCssClass("c2"); tag.setDoubleCssStyle("s2"); - + tag.doStartTag(); tag.doEndTag(); @@ -221,7 +221,7 @@ public void testDoubleWithDefaultSelectedValues() throws Exception { } - + public void testDoubleWithDotName() throws Exception { TestAction testAction = (TestAction) action; @@ -285,6 +285,15 @@ public void testGenericSimple() throws Exception { verifyGenericProperties(tag, "simple", new String[]{"value"}); } + public void testGenericSimpleWithDynamicAttributes() throws Exception { + DoubleSelectTag tag = new DoubleSelectTag(); + tag.setDynamicAttribute(null, "first-name", "firstName"); + tag.setDynamicAttribute(null, "second-name", "secondName"); + prepareTagGeneric(tag); + verifyGenericProperties(tag, "simple", new String[]{"value"}); + verify(SelectTag.class.getResource("DoubleSelect-5.txt")); + } + public void testGenericXhtml() throws Exception { DoubleSelectTag tag = new DoubleSelectTag(); prepareTagGeneric(tag); diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/InputTransferSelectTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/InputTransferSelectTagTest.java index f2de3c0d7e..53b3cc8712 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/ui/InputTransferSelectTagTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/InputTransferSelectTagTest.java @@ -18,19 +18,16 @@ */ package org.apache.struts2.views.jsp.ui; -import org.apache.struts2.views.jsp.AbstractUITagTest; import org.apache.struts2.TestAction; +import org.apache.struts2.views.jsp.AbstractUITagTest; -import java.util.List; import java.util.ArrayList; +import java.util.List; -/** - * - */ public class InputTransferSelectTagTest extends AbstractUITagTest { public void testWithRequired() throws Exception { - List list = new ArrayList(); + List list = new ArrayList<>(); list.add("Item One"); list.add("Item Two"); @@ -46,7 +43,28 @@ public void testWithRequired() throws Exception { tag.doStartTag(); tag.doEndTag(); - //System.out.println(writer.toString()); verify(InputTransferSelectTagTest.class.getResource("inputtransferselect-1.txt")); } + + public void testDynamicAttributes() throws Exception { + List list = new ArrayList<>(); + list.add("Item One"); + list.add("Item Two"); + + TestAction testaction = (TestAction) action; + testaction.setCollection(list); + + InputTransferSelectTag tag = new InputTransferSelectTag(); + tag.setPageContext(pageContext); + tag.setDynamicAttribute(null, "input-name", "inputName"); + tag.setDynamicAttribute(null, "collection-name", "collectionName"); + + tag.setName("collection"); + tag.setList("collection"); + + tag.doStartTag(); + tag.doEndTag(); + + verify(InputTransferSelectTagTest.class.getResource("inputtransferselect-2.txt")); + } } diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/OptionTransferSelectTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/OptionTransferSelectTagTest.java index 56b7b788af..4278d2b989 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/ui/OptionTransferSelectTagTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/OptionTransferSelectTagTest.java @@ -523,4 +523,36 @@ public void testDisableSomeButtons() throws Exception { //System.out.println(writer.toString()); verify(OptionTransferSelectTagTest.class.getResource("optiontransferselect-7.txt")); } + + public void testDynamicAttributes() throws Exception { + List left = new ArrayList(); + left.add("Left1"); + left.add("Left2"); + + List right = new ArrayList(); + right.add("Right1"); + right.add("Right2"); + + TestAction testaction = (TestAction) action; + testaction.setCollection(left); + testaction.setList2(right); + + OptionTransferSelectTag tag = new OptionTransferSelectTag(); + tag.setPageContext(pageContext); + + tag.setName("collection"); + tag.setList("collection"); + + tag.setDoubleName("list2"); + tag.setDoubleList("list2"); + + tag.setDynamicAttribute(null, "left-name", "leftName"); + tag.setDynamicAttribute(null, "right-name", "rightName"); + + tag.doStartTag(); + tag.doEndTag(); + + //System.out.println(writer.toString()); + verify(OptionTransferSelectTagTest.class.getResource("optiontransferselect-8.txt")); + } } diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/DoubleSelect-5.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/DoubleSelect-5.txt new file mode 100644 index 0000000000..23cf7f2c59 --- /dev/null +++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/DoubleSelect-5.txt @@ -0,0 +1,39 @@ + +
+ + diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/inputtransferselect-2.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/inputtransferselect-2.txt new file mode 100644 index 0000000000..264fb6b680 --- /dev/null +++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/inputtransferselect-2.txt @@ -0,0 +1,30 @@ + + + + + + + + + + +
+ +

+ +

+ +
+
+
+ + + + +
+ + diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/optiontransferselect-8.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/optiontransferselect-8.txt new file mode 100644 index 0000000000..06cfd1cc6c --- /dev/null +++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/optiontransferselect-8.txt @@ -0,0 +1,50 @@ + + + + + + + + + + +
+ + + + + + +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+ + + + +
+ +