You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bo...@apache.org on 2023/11/13 11:35:12 UTC

(myfaces-tobago) branch tobago-5.x updated: feat(f:ajax): Support for resetValues=true

This is an automated email from the ASF dual-hosted git repository.

bommel pushed a commit to branch tobago-5.x
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/tobago-5.x by this push:
     new 8effcb821e feat(f:ajax): Support for resetValues=true
8effcb821e is described below

commit 8effcb821e52008a47376540bca022cfb955e354
Author: Bernd Bohmann <bo...@apache.org>
AuthorDate: Mon Nov 13 12:35:07 2023 +0100

    feat(f:ajax): Support for resetValues=true
    
    issue: TOBAGO-2259
    
    (cherry picked from commit 436a89856187c68a371363f7e1534f4e13ff7a47)
---
 .../myfaces/tobago/internal/renderkit/Command.java     | 12 ++++++++++++
 .../renderer/TobagoClientBehaviorRenderer.java         |  7 +++++++
 .../apache/myfaces/tobago/internal/util/JsonUtils.java |  5 +++++
 .../apache/myfaces/tobago/renderkit/RendererBase.java  |  5 +++++
 .../tobago/renderkit/html/CustomAttributes.java        |  2 ++
 .../renderkit/renderer/ButtonRendererUnitTest.java     | 18 ++++++++++++++++++
 .../tobago/internal/util/JsonUtilsUnitTest.java        |  9 ++++++---
 .../src/test/resources/renderer/button/ajax.html       | 18 ++++++++++++++++++
 .../tobago-theme-standard/src/main/js/tobago.js        |  6 +++---
 .../tobago-theme-standard/src/main/js/tobago.js.map    |  2 +-
 .../src/main/ts/tobago-behavior.ts                     | 15 ++++++++++++++-
 11 files changed, 91 insertions(+), 8 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/Command.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/Command.java
index 0a8ec27977..eebc7490b8 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/Command.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/Command.java
@@ -36,6 +36,7 @@ public class Command {
   private Integer delay;
   private Collapse collapse;
   private Boolean omit;
+  private Boolean resetValues;
 
   public Command(
       final String clientId, final String fieldId, final Boolean transition, final String target, final String execute,
@@ -137,6 +138,14 @@ public class Command {
     this.omit = omit;
   }
 
+  public Boolean getResetValues() {
+    return resetValues;
+  }
+
+  public void setResetValues(final Boolean resetValues) {
+    this.resetValues = resetValues;
+  }
+
   public void merge(final Command c) {
 
     //XXX TBD: check if this is okay.
@@ -180,5 +189,8 @@ public class Command {
     if (omit == null) {
       omit = c.omit;
     }
+    if (resetValues == null) {
+      resetValues = c.resetValues;
+    }
   }
 }
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TobagoClientBehaviorRenderer.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TobagoClientBehaviorRenderer.java
index d2477683c1..eb5b24d14c 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TobagoClientBehaviorRenderer.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TobagoClientBehaviorRenderer.java
@@ -76,12 +76,15 @@ public class TobagoClientBehaviorRenderer extends javax.faces.render.ClientBehav
     String clientId = null;
     String fieldId = null;
     boolean omit = false;
+    boolean resetValues = false;
+
     final String confirmation = ComponentUtils.getConfirmation(uiComponent);
     if (behavior instanceof AjaxBehavior) {
       final AjaxBehavior ajaxBehavior = (AjaxBehavior) behavior;
       if (ajaxBehavior.isDisabled()) {
         return null;
       }
+      resetValues = ajaxBehavior.isResetValues();
       final Collection<String> execute = ajaxBehavior.getExecute();
       final Collection<String> render = ajaxBehavior.getRender();
       clientId = uiComponent.getClientId(facesContext);
@@ -135,6 +138,10 @@ public class TobagoClientBehaviorRenderer extends javax.faces.render.ClientBehav
         collapse,
         omit);
 
+    if (resetValues) {
+      command.setResetValues(true);
+    }
+
     final CommandMap map = new CommandMap();
     map.addCommand(eventName, command);
     CommandMap.storeCommandMap(facesContext, map);
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/JsonUtils.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/JsonUtils.java
index ea13d87f13..3064ac2335 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/JsonUtils.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/JsonUtils.java
@@ -209,6 +209,11 @@ public class JsonUtils {
       encode(builder, "omit", true);
     }
 
+    final Boolean resetValues = command.getResetValues();
+    if (resetValues != null && resetValues) { // false is the default, so encoding is needed.
+      encode(builder, "resetValues", true);
+    }
+
     if (builder.length() - initialLength > 0) {
       assert builder.charAt(builder.length() - 1) == ',';
       builder.deleteCharAt(builder.length() - 1);
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/renderkit/RendererBase.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/renderkit/RendererBase.java
index f2cd447612..ac90232324 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/renderkit/RendererBase.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/renderkit/RendererBase.java
@@ -274,6 +274,11 @@ public abstract class RendererBase<T extends UIComponent> extends Renderer {
     writer.writeAttribute(CustomAttributes.DELAY, command.getDelay());
     writer.writeAttribute(HtmlAttributes.TARGET, command.getTarget(), true);
 
+    final Boolean resetValues = command.getResetValues();
+
+    if (resetValues != null && resetValues) {
+      writer.writeAttribute(CustomAttributes.RESET_VALUES, resetValues);
+    }
     // todo: all the other attributes
     writer.endElement(HtmlElements.TOBAGO_BEHAVIOR);
   }
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/renderkit/html/CustomAttributes.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/renderkit/html/CustomAttributes.java
index 3101717cae..11c14be6e9 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/renderkit/html/CustomAttributes.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/renderkit/html/CustomAttributes.java
@@ -67,6 +67,8 @@ public enum CustomAttributes implements MarkupLanguageAttributes {
    * &lt;f:ajax&gt; attribute
    */
   RENDER("render"),
+
+  RESET_VALUES("reset-values"),
   /**
    * Number of rows to show/load for lazy loading in sheet.
    */
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/ButtonRendererUnitTest.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/ButtonRendererUnitTest.java
index 0a21820a3f..911818c6f7 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/ButtonRendererUnitTest.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/ButtonRendererUnitTest.java
@@ -19,6 +19,7 @@
 
 package org.apache.myfaces.tobago.internal.renderkit.renderer;
 
+import javax.faces.component.behavior.AjaxBehavior;
 import org.apache.myfaces.tobago.component.RendererTypes;
 import org.apache.myfaces.tobago.component.Tags;
 import org.apache.myfaces.tobago.component.UIButton;
@@ -27,6 +28,7 @@ import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 import java.io.IOException;
+import java.util.Collections;
 
 public class ButtonRendererUnitTest extends RendererTestBase {
 
@@ -100,4 +102,20 @@ public class ButtonRendererUnitTest extends RendererTestBase {
 
     Assertions.assertEquals(loadHtml("renderer/button/iconFar.html"), formattedResult());
   }
+
+  @Test
+  public void ajax() throws IOException {
+    final AjaxBehavior behavior =
+        (AjaxBehavior) facesContext.getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);
+    behavior.setExecute(Collections.singletonList("id"));
+    behavior.setRender(Collections.singletonList("id"));
+    behavior.setResetValues(true);
+    final UIButton b = (UIButton) ComponentUtils.createComponent(
+        facesContext, Tags.button.componentType(), RendererTypes.Button, "id");
+    b.setLabel("button");
+    b.addClientBehavior("click", behavior);
+    b.encodeAll(facesContext);
+
+    Assertions.assertEquals(loadHtml("renderer/button/ajax.html"), formattedResult());
+  }
 }
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/JsonUtilsUnitTest.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/JsonUtilsUnitTest.java
index ea8f5ed3c8..432937b51e 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/JsonUtilsUnitTest.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/JsonUtilsUnitTest.java
@@ -92,14 +92,16 @@ public class JsonUtilsUnitTest extends AbstractTobagoTestBase {
         ComponentUtils.createComponent(facesContext, Tags.button.componentType(), RendererTypes.Button, "command");
     ComponentUtils.setAttribute(command, Attributes.popupClose, "immediate");
 
-    map.setClick(new Command(
+    Command click = new Command(
         "ns:actionId",
         null,
         false,
         "_blank",
         StringUtils.join(Arrays.asList("id1", "id2"), ' '),
         StringUtils.join(Arrays.asList("id1", "id2"), ' '),
-        "Really?", 1000, new Collapse(Collapse.Operation.show, "myId"), true));
+        "Really?", 1000, new Collapse(Collapse.Operation.show, "myId"), true);
+    click.setResetValues(true);
+    map.setClick(click);
     final String expected = (
         "{"
             + "'click':{"
@@ -114,7 +116,8 @@ public class JsonUtilsUnitTest extends AbstractTobagoTestBase {
             + "},"
             + "'confirmation':'Really?',"
             + "'delay':1000,"
-            + "'omit':true"
+            + "'omit':true,"
+            + "'resetValues':true"
             + "}"
             + "}").replaceAll("'", "\"");
     Assertions.assertEquals(expected, JsonUtils.encode(map));
diff --git a/tobago-core/src/test/resources/renderer/button/ajax.html b/tobago-core/src/test/resources/renderer/button/ajax.html
new file mode 100644
index 0000000000..aad008ba7b
--- /dev/null
+++ b/tobago-core/src/test/resources/renderer/button/ajax.html
@@ -0,0 +1,18 @@
+<!--
+ * 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.
+-->
+
+<button type='button' id='id' name='id' class='tobago-button btn btn-secondary tobago-auto-spacing'><tobago-behavior event='click' client-id='id' execute='id id' render='id' reset-values='reset-values'></tobago-behavior><span>button</span></button>
diff --git a/tobago-theme/tobago-theme-standard/src/main/js/tobago.js b/tobago-theme/tobago-theme-standard/src/main/js/tobago.js
index 8f0c7ae17d..826d7501a5 100644
--- a/tobago-theme/tobago-theme-standard/src/main/js/tobago.js
+++ b/tobago-theme/tobago-theme-standard/src/main/js/tobago.js
@@ -1,10 +1,10 @@
-!function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";var e;!function(e){e.ACTIVE="active",e.AUTOCOMPLETE="autocomplete",e.AUTOCOMPLETE_INPUT="autocomplete-input",e.BOTTOM_0="bottom-0",e.COLLAPSE="collapse",e.COLLAPSING="collapsing",e.D_NONE="d-none",e.DISABLED="disabled",e.DROPDOWN="dropdown",e.DROPDOWN_ITEM="dropdown-item",e.DROPDOWN_MENU="dropdown-menu",e.DROPDOWN_MENU_END="dropdown-menu-end",e.END_0="end-0",e.FADE="fade",e.FIXED_BOTTOM="fixed-bott [...]
+!function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";var e;!function(e){e.ACTIVE="active",e.AUTOCOMPLETE="autocomplete",e.AUTOCOMPLETE_INPUT="autocomplete-input",e.BOTTOM_0="bottom-0",e.COLLAPSE="collapse",e.COLLAPSING="collapsing",e.D_NONE="d-none",e.DISABLED="disabled",e.DROPDOWN="dropdown",e.DROPDOWN_ITEM="dropdown-item",e.DROPDOWN_MENU="dropdown-menu",e.DROPDOWN_MENU_END="dropdown-menu-end",e.END_0="end-0",e.FADE="fade",e.FIXED_BOTTOM="fixed-bott [...]
 /*!
       * Bootstrap v5.3.2 (https://getbootstrap.com/)
       * Copyright 2011-2023 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
       * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
       */
-const $e=new Map,Me={set(e,t,s){$e.has(e)||$e.set(e,new Map);const n=$e.get(e);n.has(t)||0===n.size?n.set(t,s):console.error(`Bootstrap doesn't allow more than one instance per element. Bound instance: ${Array.from(n.keys())[0]}.`)},get:(e,t)=>$e.has(e)&&$e.get(e).get(t)||null,remove(e,t){if(!$e.has(e))return;const s=$e.get(e);s.delete(t),0===s.size&&$e.delete(e)}},Pe="transitionend",Be=e=>(e&&window.CSS&&window.CSS.escape&&(e=e.replace(/#([^\s"#']+)/g,((e,t)=>`#${CSS.escape(t)}`))),e),H [...]
+const $e=new Map,Me={set(e,t,s){$e.has(e)||$e.set(e,new Map);const n=$e.get(e);n.has(t)||0===n.size?n.set(t,s):console.error(`Bootstrap doesn't allow more than one instance per element. Bound instance: ${Array.from(n.keys())[0]}.`)},get:(e,t)=>$e.has(e)&&$e.get(e).get(t)||null,remove(e,t){if(!$e.has(e))return;const s=$e.get(e);s.delete(t),0===s.size&&$e.delete(e)}},Pe="transitionend",Be=e=>(e&&window.CSS&&window.CSS.escape&&(e=e.replace(/#([^\s"#']+)/g,((e,t)=>`#${CSS.escape(t)}`))),e),H [...]
 /**
      * @license
      * Copyright 2017 Google LLC
@@ -15,5 +15,5 @@ const $e=new Map,Me={set(e,t,s){$e.has(e)||$e.set(e,new Map);const n=$e.get(e);n
       ${s>0?" tabindex='"+String(s)+"'":""}
       @click="${this.removeBadge.bind(this)}"
       @focus="${this.focusEvent.bind(this)}"
-      @blur="${this.blurEvent.bind(this)}"><i class='bi-x-lg'></i></button>`}removeBadge(e){const t=e.target.closest(".btn-group").dataset.tobagoValue,s=this.hiddenSelect.querySelector(`[value="${t}"]`);s.selected=!1;const n=this.selectField.querySelector(`[data-tobago-value="${t}"]`),i=n.previousElementSibling,o="SPAN"===n.nextElementSibling.tagName?n.nextElementSibling:null;i?i.querySelector("button.btn.badge").focus():o?o.querySelector("button.btn.badge").focus():(this.filterInput.dis [...]
+      @blur="${this.blurEvent.bind(this)}"><i class='bi-x-lg'></i></button>`}removeBadge(e){const t=e.target.closest(".btn-group").dataset.tobagoValue,s=this.hiddenSelect.querySelector(`[value="${t}"]`);s.selected=!1;const n=this.selectField.querySelector(`[data-tobago-value="${t}"]`),i=n.previousElementSibling,o="SPAN"===n.nextElementSibling.tagName?n.nextElementSibling:null;i?i.querySelector("button.btn.badge").focus():o?o.querySelector("button.btn.badge").focus():(this.filterInput.dis [...]
 //# sourceMappingURL=tobago.js.map
diff --git a/tobago-theme/tobago-theme-standard/src/main/js/tobago.js.map b/tobago-theme/tobago-theme-standard/src/main/js/tobago.js.map
index 7d65b9a144..ca4b594e6e 100644
--- a/tobago-theme/tobago-theme-standard/src/main/js/tobago.js.map
+++ b/tobago-theme/tobago-theme-standard/src/main/js/tobago.js.map
@@ -1 +1 @@
-{"version":3,"file":"tobago.js","sources":["../ts/tobago-css.ts","../ts/tobago-bar.ts","../../../../node_modules/@popperjs/core/lib/enums.js","../../../../node_modules/@popperjs/core/lib/dom-utils/getNodeName.js","../../../../node_modules/@popperjs/core/lib/dom-utils/getWindow.js","../../../../node_modules/@popperjs/core/lib/dom-utils/instanceOf.js","../../../../node_modules/@popperjs/core/lib/modifiers/applyStyles.js","../../../../node_modules/@popperjs/core/lib/utils/getBasePlacement.j [...]
\ No newline at end of file
+{"version":3,"file":"tobago.js","sources":["../ts/tobago-css.ts","../ts/tobago-bar.ts","../../../../node_modules/@popperjs/core/lib/enums.js","../../../../node_modules/@popperjs/core/lib/dom-utils/getNodeName.js","../../../../node_modules/@popperjs/core/lib/dom-utils/getWindow.js","../../../../node_modules/@popperjs/core/lib/dom-utils/instanceOf.js","../../../../node_modules/@popperjs/core/lib/modifiers/applyStyles.js","../../../../node_modules/@popperjs/core/lib/utils/getBasePlacement.j [...]
\ No newline at end of file
diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-behavior.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-behavior.ts
index a0ac172791..f3fd7a2410 100644
--- a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-behavior.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-behavior.ts
@@ -105,7 +105,8 @@ class Behavior extends HTMLElement {
             {
               "javax.faces.behavior.event": this.event,
               execute: this.execute,
-              render: this.render
+              render: this.render,
+              resetValues: this.resetValues
             });
         break;
       case BehaviorMode.full:
@@ -232,6 +233,18 @@ class Behavior extends HTMLElement {
     }
   }
 
+  get resetValues(): boolean {
+    return this.hasAttribute("reset-values");
+  }
+
+  set resetValues(resetValues: boolean) {
+    if (resetValues) {
+      this.setAttribute("reset-values", "");
+    } else {
+      this.removeAttribute("reset-values");
+    }
+  }
+
   get target(): string {
     return this.getAttribute("target");
   }