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/02/27 13:02:41 UTC

(myfaces-tobago) 01/02: feature(shuttle): improve JavaScript change event

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

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

commit 531f6e26cf6d2253a85a5112b41c58e464fa0455
Author: Henning Noeth <hn...@apache.org>
AuthorDate: Fri Feb 27 09:23:50 2026 +0100

    feature(shuttle): improve JavaScript change event
    
    * add details to change event
    * add test
    
    Issue: TOBAGO-2479
---
 .../selectManyShuttle/SelectManyShuttle.xhtml      | 35 ++++++++++
 .../src/test/typescript/selectManyShuttle.test.ts  | 80 ++++++++++++++++++++++
 .../src/main/ts/tobago-select-many-shuttle.ts      | 25 ++++++-
 3 files changed, 139 insertions(+), 1 deletion(-)

diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/selectManyShuttle/SelectManyShuttle.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/selectManyShuttle/SelectManyShuttle.xhtml
new file mode 100644
index 0000000000..150165a44d
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/selectManyShuttle/SelectManyShuttle.xhtml
@@ -0,0 +1,35 @@
+<?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:tc="http://myfaces.apache.org/tobago/component"
+                xmlns:ui="jakarta.faces.facelets">
+  <tc:selectManyShuttle id="shuttle">
+    <tc:selectItem itemValue="Item1"/>
+    <tc:selectItem itemValue="Item2"/>
+    <tc:selectItem itemValue="Item3"/>
+    <tc:selectItem itemValue="Item4"/>
+    <tc:selectItem itemValue="Item5"/>
+    <tc:selectItem itemValue="Item6"/>
+    <tc:selectItem itemValue="Item7"/>
+    <tc:selectItem itemValue="Item8"/>
+    <tc:selectItem itemValue="Item9"/>
+  </tc:selectManyShuttle>
+</ui:composition>
diff --git a/tobago-example/tobago-example-demo/src/test/typescript/selectManyShuttle.test.ts b/tobago-example/tobago-example-demo/src/test/typescript/selectManyShuttle.test.ts
new file mode 100644
index 0000000000..060a53de0d
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/test/typescript/selectManyShuttle.test.ts
@@ -0,0 +1,80 @@
+/*
+ * 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/selectManyShuttle/SelectManyShuttle.xhtml", () => {
+
+  test.beforeEach(async ({page}, testInfo) => {
+    await page.goto("/content/900-test/selectManyShuttle/SelectManyShuttle.xhtml");
+  });
+
+  test("JavaScript 'change' event", async ({page}) => {
+    const shuttle = page.locator("tobago-select-many-shuttle[id='page:mainForm:shuttle']");
+    const unselect = shuttle.locator(".tobago-unselected");
+    const select = shuttle.locator(".tobago-selected");
+    const addAll = shuttle.locator("button[id='page:mainForm:shuttle::addAll']");
+    const add = shuttle.locator("button[id='page:mainForm:shuttle::add']");
+    const remove = shuttle.locator("button[id='page:mainForm:shuttle::remove']");
+    const removeAll = shuttle.locator("button[id='page:mainForm:shuttle::removeAll']");
+
+    const eventPromise = () => page.evaluate(() => {
+      return new Promise(resolve => {
+        const shuttle = document.querySelector("tobago-select-many-shuttle[id='page:mainForm:shuttle']");
+        shuttle.addEventListener("change", (event: CustomEvent) => {
+          resolve(event.detail);
+        }, {once: true});
+      });
+    }) as Promise<any>;
+
+    let promise = eventPromise();
+    await unselect.selectOption("Item5");
+    await add.click();
+    let detail = await promise;
+    expect(detail.added).toEqual(["Item5"]);
+    expect(detail.removed).toBeNull();
+    expect(detail.unselected).toEqual(["Item1", "Item2", "Item3", "Item4", "Item6", "Item7", "Item8", "Item9"]);
+    expect(detail.selected).toEqual(["Item5"]);
+
+    promise = eventPromise();
+    await addAll.click();
+    detail = await promise;
+    expect(detail.added).toEqual(["Item1", "Item2", "Item3", "Item4", "Item6", "Item7", "Item8", "Item9"]);
+    expect(detail.removed).toBeNull();
+    expect(detail.unselected).toEqual([]);
+    expect(detail.selected).toEqual(["Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9"]);
+
+    promise = eventPromise();
+    await select.selectOption("Item8");
+    await remove.click();
+    detail = await promise;
+    expect(detail.added).toBeNull();
+    expect(detail.removed).toEqual(["Item8"]);
+    expect(detail.unselected).toEqual(["Item8"]);
+    expect(detail.selected).toEqual(["Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item9"]);
+
+    promise = eventPromise();
+    await removeAll.click();
+    detail = await promise;
+    expect(detail.added).toBeNull();
+    expect(detail.removed).toEqual(["Item5", "Item1", "Item2", "Item3", "Item4", "Item6", "Item7", "Item9"]);
+    expect(detail.unselected).toEqual(["Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9"]);
+    expect(detail.selected).toEqual([]);
+  });
+});
diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-select-many-shuttle.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-select-many-shuttle.ts
index 48f5fa6cae..2a62f3ad38 100644
--- a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-select-many-shuttle.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-select-many-shuttle.ts
@@ -17,6 +17,13 @@
 
 import {Focus} from "./tobago-focus";
 
+interface SelectManyShuttleDetail {
+  added: string[];
+  removed: string[];
+  unselected: string[];
+  selected: string[];
+}
+
 class SelectManyShuttle extends HTMLElement {
 
   constructor() {
@@ -80,6 +87,7 @@ class SelectManyShuttle extends HTMLElement {
       this.selectedSelect.add(option);
       this.changeHiddenOption(option, true);
     }
+    this.fireChangeEvent(options, null);
   }
 
   private removeItems(options: NodeListOf<HTMLOptionElement>): void {
@@ -87,12 +95,27 @@ class SelectManyShuttle extends HTMLElement {
       this.unselectedSelect.add(option);
       this.changeHiddenOption(option, false);
     }
+    this.fireChangeEvent(null, options);
   }
 
   private changeHiddenOption(option: HTMLOptionElement, select: boolean): void {
     const hiddenOption: HTMLOptionElement = this.hiddenSelect.querySelector(`option[value='${option.value}']`);
     hiddenOption.selected = select;
-    this.dispatchEvent(new Event("change"));
+  }
+
+  private fireChangeEvent(added: NodeListOf<HTMLOptionElement>, removed: NodeListOf<HTMLOptionElement>): void {
+    this.dispatchEvent(new CustomEvent("change", {
+      detail: {
+        added: added ? Array.from(added).map((option: HTMLOptionElement) => option.value) : null,
+        removed: removed ? Array.from(removed).map((option: HTMLOptionElement) => option.value) : null,
+        unselected: Array.from(this.hiddenSelect.options)
+            .filter((option: HTMLOptionElement) => !option.selected)
+            .map((option: HTMLOptionElement) => option.value),
+        selected: Array.from(this.hiddenSelect.options)
+            .filter((option: HTMLOptionElement) => option.selected)
+            .map((option: HTMLOptionElement) => option.value)
+      }
+    }));
   }
 
   get unselectedSelect(): HTMLSelectElement {