You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by hn...@apache.org on 2026/09/15 21:12:30 UTC

(myfaces-tobago) 01/02: fix(ajax): collapsible components

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

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

commit b3a18f7cedf87566bca3eec63a95dbf0f06bf206
Author: Henning Noeth <hn...@apache.org>
AuthorDate: Tue Sep 15 12:09:37 2026 +0200

    fix(ajax): collapsible components
    
    Add test: Jest and Playwright
    fix:
    -> animation on the client only if not rendered by Ajax or full page reload
    -> Ajax "execute" value contains ID of collapsible component if it is rendered by Ajax
    
    Issue: TOBAGO-2543
---
 .../main/webapp/content/900-test/panel/Panel.xhtml |  42 ++++
 .../content/900-test/panel/Panel.test.ts           |  53 +++++
 .../src/main/ts/tobago-behavior.test.ts            | 246 +++++++++++++++++++++
 .../src/main/ts/tobago-behavior.ts                 |  38 +++-
 .../src/main/ts/tobago-collapsible-base.ts         |  14 +-
 5 files changed, 383 insertions(+), 10 deletions(-)

diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/panel/Panel.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/panel/Panel.xhtml
new file mode 100644
index 0000000000..77e99d3437
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/panel/Panel.xhtml
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+  - 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.
+  -->
+
+<ui:composition template="/main.xhtml"
+                xmlns="http://www.w3.org/1999/xhtml"
+                xmlns:f="jakarta.faces.core"
+                xmlns:tc="http://myfaces.apache.org/tobago/component"
+                xmlns:ui="jakarta.faces.facelets">
+
+  <tc:section label="TOBAGO-2543">
+    <tc:out id="timestamp" label="Timestamp" value="#{eventController.currentTimestamp}"/>
+    <tc:panel id="tobago2543" collapsedMode="hidden">
+      <tc:in id="tobago2543Input" value="Content"/>
+    </tc:panel>
+    <tc:button id="showButton" label="Show" omit="true">
+      <tc:operation for="tobago2543" name="show"/>
+      <f:ajax render="timestamp tobago2543"/>
+    </tc:button>
+    <tc:button id="hideButton" label="Hide" omit="true">
+      <tc:operation for="tobago2543" name="hide"/>
+      <f:ajax render="timestamp tobago2543"/>
+    </tc:button>
+  </tc:section>
+</ui:composition>
diff --git a/tobago-example/tobago-example-demo/src/test/typescript/content/900-test/panel/Panel.test.ts b/tobago-example/tobago-example-demo/src/test/typescript/content/900-test/panel/Panel.test.ts
new file mode 100644
index 0000000000..203d7470a9
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/test/typescript/content/900-test/panel/Panel.test.ts
@@ -0,0 +1,53 @@
+/*
+ * 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.
+ */
+
+import {expect, test} from "@playwright/test";
+
+test.describe("900-test/panel/Panel.xhtml", () => {
+
+  test.beforeEach(async ({page}) => {
+    await page.goto("/content/900-test/panel/Panel.xhtml");
+  });
+
+  test("Open 'Client Popup' and press 'Cancel'.", async ({page}) => {
+    const timestamp = page.locator("[id='page:mainForm:timestamp'] .form-control-plaintext");
+    const panel = page.locator("[id='page:mainForm:tobago2543']");
+    const panelHiddenCollapseInput = page.locator("[id='page:mainForm:tobago2543::collapse']");
+    const inputField = page.locator("[id='page:mainForm:tobago2543Input']");
+    const show = page.locator("[id='page:mainForm:showButton']");
+    const hide = page.locator("[id='page:mainForm:hideButton']");
+
+    let timestampValue = await timestamp.textContent() as string;
+    await expect(timestamp).toHaveText(timestampValue);
+    await expect(panelHiddenCollapseInput).toHaveAttribute("value", "false");
+    await expect(inputField).toBeVisible();
+
+    await hide.click();
+    await expect(timestamp).not.toHaveText(timestampValue);
+    timestampValue = await timestamp.textContent() as string;
+    await expect(timestamp).toHaveText(timestampValue);
+    await expect(panelHiddenCollapseInput).toHaveAttribute("value", "true");
+    await expect(inputField).not.toBeVisible();
+
+    await show.click();
+    await expect(timestamp).not.toHaveText(timestampValue);
+    timestampValue = await timestamp.textContent() as string;
+    await expect(timestamp).toHaveText(timestampValue);
+    await expect(panelHiddenCollapseInput).toHaveAttribute("value", "false");
+    await expect(inputField).toBeVisible();
+  });
+});
diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-behavior.test.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-behavior.test.ts
new file mode 100644
index 0000000000..31b16235de
--- /dev/null
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-behavior.test.ts
@@ -0,0 +1,246 @@
+/*
+ * 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.
+ */
+
+import {Behavior} from "./tobago-behavior";
+import {BehaviorMode} from "./tobago-behavior-mode";
+
+const addOnEvent = jest.fn();
+beforeAll(() => {
+  Object.defineProperty(globalThis, "faces", {
+    configurable: true,
+    value: {
+      ajax: {
+        addOnEvent
+      }
+    }
+  });
+  document.dispatchEvent(new Event("tobago.init"));
+});
+
+beforeEach(() => {
+  document.body.innerHTML = `<tobago-page><form id="page:mainForm"></form></tobago-page>`;
+});
+
+afterEach(() => {
+  jest.restoreAllMocks();
+  document.body.replaceChildren();
+});
+
+afterAll(() => {
+  Reflect.deleteProperty(globalThis, "faces");
+});
+
+test("BehaviorMode.client", () => {
+  document.querySelector("form").insertAdjacentHTML("beforeend", `
+<tobago-section id="page:mainForm:section" class="tobago-auto-spacing" data-tobago-level="2">
+  <div class="tobago-header">
+    <h2>
+      <span>Section</span></h2></div>
+  <div class="tobago-section-content">
+    <tobago-out id="page:mainForm:timestamp" class="tobago-label-container tobago-auto-spacing"><label
+            for="page:mainForm:timestamp" class="col-form-label">Timestamp</label><span class="form-control-plaintext">2026-12-31</span>
+    </tobago-out>
+    <tobago-panel id="page:mainForm:panel">
+      <input type="hidden" name="page:mainForm:panel::collapse" id="page:mainForm:panel::collapse" value="false">
+      <tobago-in id="page:mainForm:input" class="tobago-auto-spacing">
+        <input type="text" name="page:mainForm:input" id="page:mainForm:input::field" value="Content"
+               class="form-control"></tobago-in>
+    </tobago-panel>
+    <button type="button" id="page:mainForm:hide" name="page:mainForm:hide"
+            class="tobago-button btn btn-secondary tobago-auto-spacing">
+      <tobago-behavior event="click" client-id="page:mainForm:hide" omit="omit" collapse-operation="hide"
+                       collapse-target="page:mainForm:panel"></tobago-behavior>
+      <span>Hide</span></button>
+  </div>
+</tobago-section>
+`);
+
+  const behavior: Behavior = document.querySelector("tobago-behavior[client-id='page:mainForm:hide']") as Behavior;
+  expect(behavior).toBeInstanceOf(Behavior);
+  expect(behavior.mode).toBe(BehaviorMode.client);
+  expect(behavior.clientSideAnimation).toBe(true);
+});
+
+test("BehaviorMode.ajax renderer='timestamp'", () => {
+  document.querySelector("form").insertAdjacentHTML("beforeend", `
+<tobago-section id="page:mainForm:section" class="tobago-auto-spacing" data-tobago-level="2">
+  <div class="tobago-header">
+    <h2>
+      <span>Section</span></h2></div>
+  <div class="tobago-section-content">
+    <tobago-out id="page:mainForm:timestamp" class="tobago-label-container tobago-auto-spacing"><label
+            for="page:mainForm:timestamp" class="col-form-label">Timestamp</label><span class="form-control-plaintext">2026-12-31</span>
+    </tobago-out>
+    <tobago-panel id="page:mainForm:panel">
+      <input type="hidden" name="page:mainForm:panel::collapse" id="page:mainForm:panel::collapse" value="false">
+      <tobago-in id="page:mainForm:input" class="tobago-auto-spacing">
+        <input type="text" name="page:mainForm:input" id="page:mainForm:input::field" value="Content"
+               class="form-control"></tobago-in>
+    </tobago-panel>
+    <button type="button" id="page:mainForm:hide" name="page:mainForm:hide"
+            class="tobago-button btn btn-secondary tobago-auto-spacing">
+      <tobago-behavior event="click" client-id="page:mainForm:hide" execute="page:mainForm:hide"
+                       render="page:mainForm:timestamp" omit="omit" collapse-operation="hide"
+                       collapse-target="page:mainForm:panel"></tobago-behavior>
+      <span>Hide</span></button>
+  </div>
+</tobago-section>
+`);
+
+  const behavior: Behavior = document.querySelector("tobago-behavior[client-id='page:mainForm:hide']") as Behavior;
+  expect(behavior).toBeInstanceOf(Behavior);
+  expect(behavior.mode).toBe(BehaviorMode.ajax);
+  expect(behavior.clientSideAnimation).toBe(true);
+  expect(behavior.collapseOperationExecute).toBe("page:mainForm:hide");
+});
+
+test("BehaviorMode.ajax renderer='panel'", () => {
+  document.querySelector("form").insertAdjacentHTML("beforeend", `
+<tobago-section id="page:mainForm:section" class="tobago-auto-spacing" data-tobago-level="2">
+  <div class="tobago-header">
+    <h2>
+      <span>Section</span></h2></div>
+  <div class="tobago-section-content">
+    <tobago-out id="page:mainForm:timestamp" class="tobago-label-container tobago-auto-spacing"><label
+            for="page:mainForm:timestamp" class="col-form-label">Timestamp</label><span class="form-control-plaintext">2026-12-31</span>
+    </tobago-out>
+    <tobago-panel id="page:mainForm:panel">
+      <input type="hidden" name="page:mainForm:panel::collapse" id="page:mainForm:panel::collapse" value="false">
+      <tobago-in id="page:mainForm:input" class="tobago-auto-spacing">
+        <input type="text" name="page:mainForm:input" id="page:mainForm:input::field" value="Content"
+               class="form-control"></tobago-in>
+    </tobago-panel>
+    <button type="button" id="page:mainForm:hide" name="page:mainForm:hide"
+            class="tobago-button btn btn-secondary tobago-auto-spacing">
+      <tobago-behavior event="click" client-id="page:mainForm:hide" execute="page:mainForm:hide"
+                       render="page:mainForm:panel" omit="omit" collapse-operation="hide"
+                       collapse-target="page:mainForm:panel"></tobago-behavior>
+      <span>Hide</span></button>
+  </div>
+</tobago-section>
+`);
+
+  const behavior: Behavior = document.querySelector("tobago-behavior[client-id='page:mainForm:hide']") as Behavior;
+  expect(behavior).toBeInstanceOf(Behavior);
+  expect(behavior.mode).toBe(BehaviorMode.ajax);
+  expect(behavior.clientSideAnimation).toBe(false);
+  expect(behavior.collapseOperationExecute).toBe("page:mainForm:hide page:mainForm:panel");
+});
+
+test("BehaviorMode.ajax renderer='panel parent'", () => {
+  document.querySelector("form").insertAdjacentHTML("beforeend", `
+<tobago-section id="page:mainForm:section" class="tobago-auto-spacing" data-tobago-level="2">
+  <div class="tobago-header">
+    <h2>
+      <span>Section</span></h2></div>
+  <div class="tobago-section-content">
+    <tobago-out id="page:mainForm:timestamp" class="tobago-label-container tobago-auto-spacing"><label
+            for="page:mainForm:timestamp" class="col-form-label">Timestamp</label><span class="form-control-plaintext">2026-12-31</span>
+    </tobago-out>
+    <tobago-panel id="page:mainForm:panel">
+      <input type="hidden" name="page:mainForm:panel::collapse" id="page:mainForm:panel::collapse" value="false">
+      <tobago-in id="page:mainForm:input" class="tobago-auto-spacing">
+        <input type="text" name="page:mainForm:input" id="page:mainForm:input::field" value="Content"
+               class="form-control"></tobago-in>
+    </tobago-panel>
+    <button type="button" id="page:mainForm:hide" name="page:mainForm:hide"
+            class="tobago-button btn btn-secondary tobago-auto-spacing">
+      <tobago-behavior event="click" client-id="page:mainForm:hide" execute="page:mainForm:hide"
+                       render="page:mainForm:section" omit="omit" collapse-operation="hide"
+                       collapse-target="page:mainForm:panel"></tobago-behavior>
+      <span>Hide</span></button>
+  </div>
+</tobago-section>
+`);
+
+  const behavior: Behavior = document.querySelector("tobago-behavior[client-id='page:mainForm:hide']") as Behavior;
+  expect(behavior).toBeInstanceOf(Behavior);
+  expect(behavior.mode).toBe(BehaviorMode.ajax);
+  expect(behavior.clientSideAnimation).toBe(false);
+  expect(behavior.collapseOperationExecute).toBe("page:mainForm:hide page:mainForm:panel");
+});
+
+test("BehaviorMode.ajax renderer='panel child'", () => {
+  document.querySelector("form").insertAdjacentHTML("beforeend", `
+<tobago-section id="page:mainForm:section" class="tobago-auto-spacing" data-tobago-level="2">
+  <div class="tobago-header">
+    <h2>
+      <span>Section</span></h2></div>
+  <div class="tobago-section-content">
+    <tobago-out id="page:mainForm:timestamp" class="tobago-label-container tobago-auto-spacing"><label
+            for="page:mainForm:timestamp" class="col-form-label">Timestamp</label><span class="form-control-plaintext">2026-12-31</span>
+    </tobago-out>
+    <tobago-panel id="page:mainForm:panel">
+      <input type="hidden" name="page:mainForm:panel::collapse" id="page:mainForm:panel::collapse" value="false">
+      <tobago-in id="page:mainForm:input" class="tobago-auto-spacing">
+        <input type="text" name="page:mainForm:input" id="page:mainForm:input::field" value="Content"
+               class="form-control"></tobago-in>
+    </tobago-panel>
+    <button type="button" id="page:mainForm:hide" name="page:mainForm:hide"
+            class="tobago-button btn btn-secondary tobago-auto-spacing">
+      <tobago-behavior event="click" client-id="page:mainForm:hide" execute="page:mainForm:hide"
+                       render="page:mainForm:input" omit="omit" collapse-operation="hide"
+                       collapse-target="page:mainForm:panel"></tobago-behavior>
+      <span>Hide</span></button>
+  </div>
+</tobago-section>
+`);
+
+  const behavior: Behavior = document.querySelector("tobago-behavior[client-id='page:mainForm:hide']") as Behavior;
+  expect(behavior).toBeInstanceOf(Behavior);
+  expect(behavior.mode).toBe(BehaviorMode.ajax);
+  expect(behavior.clientSideAnimation).toBe(true);
+  expect(behavior.collapseOperationExecute).toBe("page:mainForm:hide");
+});
+
+test("BehaviorMode.full", () => {
+  document.querySelector("form").insertAdjacentHTML("beforeend", `
+<tobago-section id="page:mainForm:section" class="tobago-auto-spacing" data-tobago-level="2">
+  <div class="tobago-header">
+    <h2>
+      <span>Section</span></h2></div>
+  <div class="tobago-section-content">
+    <tobago-out id="page:mainForm:timestamp" class="tobago-label-container tobago-auto-spacing"><label
+            for="page:mainForm:timestamp" class="col-form-label">Timestamp</label><span class="form-control-plaintext">2026-12-31</span>
+    </tobago-out>
+    <tobago-panel id="page:mainForm:panel">
+      <input type="hidden" name="page:mainForm:panel::collapse" id="page:mainForm:panel::collapse" value="false">
+      <tobago-in id="page:mainForm:input" class="tobago-auto-spacing">
+        <input type="text" name="page:mainForm:input" id="page:mainForm:input::field" value="Content"
+               class="form-control"></tobago-in>
+    </tobago-panel>
+    <button type="button" id="page:mainForm:show" name="page:mainForm:show"
+            class="tobago-button btn btn-secondary tobago-auto-spacing">
+      <tobago-behavior event="click" client-id="page:mainForm:show" collapse-operation="show"
+                       collapse-target="page:mainForm:panel"></tobago-behavior>
+      <span>Show</span></button>
+    <button type="button" id="page:mainForm:hide" name="page:mainForm:hide"
+            class="tobago-button btn btn-secondary tobago-auto-spacing">
+      <tobago-behavior event="click" client-id="page:mainForm:hide" collapse-operation="hide"
+                       collapse-target="page:mainForm:panel"></tobago-behavior>
+      <span>Hide</span></button>
+  </div>
+</tobago-section>
+`);
+
+  const behavior: Behavior = document.querySelector("tobago-behavior[client-id='page:mainForm:hide']") as Behavior;
+  expect(behavior).toBeInstanceOf(Behavior);
+  expect(behavior.mode).toBe(BehaviorMode.full);
+  expect(behavior.clientSideAnimation).toBe(false);
+});
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 40fb336d95..db58b0d060 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
@@ -24,7 +24,7 @@ import {EventListenerStore} from "./tobago-event-listener-store";
 import {ClientBehaviors} from "./tobago-client-behaviors";
 import {CollapsibleBase} from "./tobago-collapsible-base";
 
-class Behavior extends HTMLElement {
+export class Behavior extends HTMLElement {
   private listeners: EventListenerStore = new EventListenerStore();
 
   constructor() {
@@ -96,16 +96,15 @@ class Behavior extends HTMLElement {
 
     if (this.collapseOperation && this.collapseTarget) {
       const collapseTarget: CollapsibleBase = document.getElementById(this.collapseTarget) as CollapsibleBase;
-      const clientSideAnimation: boolean = this.mode === BehaviorMode.client;
       switch (this.collapseOperation) {
         case CollapseOperation.show:
-          collapseTarget.expand(clientSideAnimation);
+          collapseTarget.expand(this.clientSideAnimation);
           break;
         case CollapseOperation.hide:
-          collapseTarget.collapse(clientSideAnimation);
+          collapseTarget.collapse(this.clientSideAnimation);
           break;
         case CollapseOperation.toggle:
-          collapseTarget.toggle(clientSideAnimation);
+          collapseTarget.toggle(this.clientSideAnimation);
           break;
       }
     }
@@ -119,7 +118,7 @@ class Behavior extends HTMLElement {
               params: {
                 "jakarta.faces.behavior.event": this.event
               },
-              execute: this.execute,
+              execute: (this.collapseOperation && this.collapseTarget) ? this.collapseOperationExecute : this.execute,
               render: this.render,
               resetValues: this.resetValues,
               delay: this.delay,
@@ -247,6 +246,17 @@ class Behavior extends HTMLElement {
     }
   }
 
+  private willBeRendered(id: string): boolean {
+    const body = this.closest("body");
+
+    for (const renderId of this.render.split(" ")) {
+      if (renderId === id || body.querySelector("[id='" + renderId + "'] [id='" + id + "']") !== null) {
+        return true;
+      }
+    }
+    return false;
+  }
+
   get mode(): BehaviorMode {
     if (this.render || this.execute) {
       return BehaviorMode.ajax;
@@ -361,6 +371,22 @@ class Behavior extends HTMLElement {
     this.setAttribute("collapse-target", collapseTarget);
   }
 
+  get clientSideAnimation(): boolean {
+    return this.mode === BehaviorMode.client
+        || (this.mode === BehaviorMode.ajax && !this.willBeRendered(this.collapseTarget));
+  }
+
+  /**
+   * If {@link collapseTarget} is rendered by Ajax, the {@link collapseTarget} is added to the {@link execute} string.
+   */
+  get collapseOperationExecute(): string {
+    if (this.willBeRendered(this.collapseTarget)) {
+      return this.execute + " " + this.collapseTarget;
+    } else {
+      return this.execute;
+    }
+  }
+
   get decoupled(): boolean {
     return this.hasAttribute("decoupled");
   }
diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-collapsible-base.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-collapsible-base.ts
index a40abfae0f..aa65d54d3e 100644
--- a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-collapsible-base.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-collapsible-base.ts
@@ -75,14 +75,20 @@ export abstract class CollapsibleBase extends HTMLElement {
   }
 
   get collapsed(): boolean {
-    return this.collapseField.value === "true";
+    return this.collapseField?.value === "true";
   }
 
   set collapsed(value: boolean) {
-    this.collapseField.value = String(value);
+    if (this.collapseField) {
+      this.collapseField.value = String(value);
+    }
   }
 
-  get collapseField(): HTMLInputElement {
-    return this.querySelector("input[id$='::collapse']");
+  private get collapseField(): HTMLInputElement {
+    const collapseField = this.querySelector("input[id$='::collapse']") as HTMLInputElement;
+    if (collapseField === null) {
+      console.warn("Collapsible status field not found. Is 'collapsed mode = hidden/absent' set?");
+    }
+    return collapseField;
   }
 }