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/03 11:45:21 UTC
(myfaces-tobago) 01/02: feature(collapsible): JavaScript API to show/hide 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 b88038868926274c233922a923c6330d6961ae92
Author: Henning Noeth <hn...@apache.org>
AuthorDate: Tue Sep 1 15:15:38 2026 +0200
feature(collapsible): JavaScript API to show/hide collapsible components
Refactor JavaScript API. "tobago.collapsible.*" doesn't exist anymore. I am not sure if this is the right way. So better don't add something we cannot change easily in the future.
Refactor show/hide mechanic. The collapsible tobago-* custom elements don't listen to a "show/hide" JavaScript event. Instead, there is a "collapse/expand" method for tobago-box, tobago-panel, tobago-popup, tobago-offcanvas and tobago-section.
Remove executeCollapseOperation() from CollapsibleBase, because this seems to be logic which fit more in tobago-behavior.ts. Add a trigger() method in addition to collapse/expand. This aligns with CollapseOperation.toggle.
Update dokumentation in the overview for collapsible components.
The TypeScript definition is now added in the root package.json. Now other projects which includes Tobago as a NPM dependency only have to add "myfaces-tobago" as a type in the tsconfig.json.
Issue: TOBAGO-2515
---
package.json | 1 +
.../130-collapsible/01-overview/Overview.xhtml | 9 +-
tobago-theme/eslint.config.mjs | 1 +
.../src/main/ts/tobago-api.ts | 16 ----
.../src/main/ts/tobago-behavior.ts | 13 ++-
.../src/main/ts/tobago-collapsible-base.ts | 97 +++++++++-------------
.../src/main/ts/tobago-offcanvas.ts | 42 +++-------
.../src/main/ts/tobago-popup.ts | 44 +++-------
.../tobago-theme-standard/src/main/ts/tobago.d.ts | 19 -----
9 files changed, 80 insertions(+), 162 deletions(-)
diff --git a/package.json b/package.json
index 9c56acd9b0..d15833bf38 100644
--- a/package.json
+++ b/package.json
@@ -2,6 +2,7 @@
"name": "myfaces-tobago",
"version": "6.12.0-SNAPSHOT",
"description": "The goal of Tobago is to provide the community with a well designed set of user interface components based on JSF and run on MyFaces.",
+ "types": "tobago-theme/tobago-theme-standard/src/main/ts/tobago.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/apache/myfaces-tobago.git"
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/130-collapsible/01-overview/Overview.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/130-collapsible/01-overview/Overview.xhtml
index e5403001f3..b860981402 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/content/130-collapsible/01-overview/Overview.xhtml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/130-collapsible/01-overview/Overview.xhtml
@@ -99,8 +99,11 @@
</tc:section>
<tc:section label="JavaScript API">
- <p>Use <code class="language-js">tobago.collapsible.show()</code> or
- <code class="language-js">tobago.collapsible.hide()</code> to change the state of a collapsible component on the
- client. Pass the client ID or the DOM element of the component.</p>
+ <p>All collapsible tobago-* custom elements (<code>tobago-box</code>, <code>tobago-offcanvas</code>,
+ <code>tobago-panel</code>, <code>tobago-popup</code> and <code>tobago-section</code>) provide
+ <code class="language-js">expand()</code> and <code class="language-js">collapse()</code> methods for changing
+ their collapsed state on the client.</p>
+ <p>For example, show a popup with
+ <code class="language-js">document.querySelector("tobago-popup").expand();</code></p>
</tc:section>
</ui:composition>
diff --git a/tobago-theme/eslint.config.mjs b/tobago-theme/eslint.config.mjs
index 6c92e3381a..a0afdd99d1 100644
--- a/tobago-theme/eslint.config.mjs
+++ b/tobago-theme/eslint.config.mjs
@@ -45,6 +45,7 @@ export default defineConfig([
rules: {
"@stylistic/semi": "error",
"@typescript-eslint/no-explicit-any": "off",
+ "@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-unsafe-function-type": "off",
"@typescript-eslint/no-unused-expressions": "off",
"@typescript-eslint/no-unused-vars": "off",
diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-api.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-api.ts
index 51c98951b7..bcdf721864 100644
--- a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-api.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-api.ts
@@ -18,8 +18,6 @@
*/
import {ajaxQueue} from "./tobago-ajax-queue";
-import {BehaviorMode} from "./tobago-behavior-mode";
-import {CollapsibleBase} from "./tobago-collapsible-base";
declare global {
interface Window {
@@ -31,19 +29,5 @@ window.tobago = {
ajax: {
request: (elementOrId: Element | string, event?: Event, options?: faces.ajax.RequestOptions) =>
ajaxQueue.request(elementOrId, event, options)
- },
- collapsible: {
- show: (elementOrId: Element | string): void => {
- const element: CollapsibleBase = elementOrId instanceof Element
- ? elementOrId as CollapsibleBase
- : document.getElementById(elementOrId) as CollapsibleBase;
- element.fireEvent("show", BehaviorMode.client);
- },
- hide: (elementOrId: Element | string,): void => {
- const element: CollapsibleBase = elementOrId instanceof Element
- ? elementOrId as CollapsibleBase
- : document.getElementById(elementOrId) as CollapsibleBase;
- element.fireEvent("hide", BehaviorMode.client);
- }
}
};
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 b94eaf9044..40fb336d95 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
@@ -96,7 +96,18 @@ class Behavior extends HTMLElement {
if (this.collapseOperation && this.collapseTarget) {
const collapseTarget: CollapsibleBase = document.getElementById(this.collapseTarget) as CollapsibleBase;
- collapseTarget.executeCollapseOperation(this.collapseOperation, this.mode);
+ const clientSideAnimation: boolean = this.mode === BehaviorMode.client;
+ switch (this.collapseOperation) {
+ case CollapseOperation.show:
+ collapseTarget.expand(clientSideAnimation);
+ break;
+ case CollapseOperation.hide:
+ collapseTarget.collapse(clientSideAnimation);
+ break;
+ case CollapseOperation.toggle:
+ collapseTarget.toggle(clientSideAnimation);
+ break;
+ }
}
switch (this.mode) {
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 1301ea8d5b..a40abfae0f 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
@@ -17,72 +17,61 @@
* under the License.
*/
-import {EventListenerStore} from "./tobago-event-listener-store";
-import {BehaviorMode} from "./tobago-behavior-mode";
import {Css} from "./tobago-css";
-import {CollapseOperation} from "./tobago-collapse-operation";
-
-export interface CollapsibleEventDetail {
- behaviorMode: BehaviorMode;
-}
export abstract class CollapsibleBase extends HTMLElement {
- protected listeners: EventListenerStore = new EventListenerStore();
- connectedCallback(): void {
- this.listeners.add(this, this.showEventName, (event: CustomEvent<CollapsibleEventDetail>) => {
- if (event.target === this) {
- this.clientBehaviorShow(event);
- }
- });
- this.listeners.add(this, this.hideEventName, (event: CustomEvent<CollapsibleEventDetail>) => {
- if (event.target === this) {
- this.clientBehaviorHide(event);
- }
- });
+ /**
+ * Toggle the expand/collapse state of the component.
+ * @param clientSideAnimation enabled animated collapsing in the browser (optional - default is true).
+ */
+ public toggle(clientSideAnimation: boolean = true): void {
+ if (this.collapsed) {
+ this.expand(clientSideAnimation);
+ } else {
+ this.collapse(clientSideAnimation);
+ }
}
- disconnectedCallback(): void {
- this.listeners.disconnect();
+ /**
+ * Expand (show) the component.
+ * @param clientSideAnimation enabled animated collapsing in the browser (optional - default is true).
+ */
+ public expand(clientSideAnimation: boolean = true): void { //this method must not be named 'show' (TOBAGO-2148)
+ if (this.fireEvent("show", true)) {
+ this.collapsed = false;
+ if (clientSideAnimation) {
+ this.clientSideExpandAnimation();
+ }
+ this.fireEvent("shown", false);
+ }
}
- //this method must not be named 'show' (TOBAGO-2148)
- protected clientBehaviorShow(event: CustomEvent<CollapsibleEventDetail>): void {
+ protected clientSideExpandAnimation(): void {
this.classList.remove(Css.TOBAGO_COLLAPSED);
- this.collapsed = false;
- this.fireEvent("shown", event.detail.behaviorMode);
- }
-
- //this method must not be named 'hide' (TOBAGO-2148)
- protected clientBehaviorHide(event: CustomEvent<CollapsibleEventDetail>): void {
- this.classList.add(Css.TOBAGO_COLLAPSED);
- this.collapsed = true;
- this.fireEvent("hidden", event.detail.behaviorMode);
}
- executeCollapseOperation(collapseOperation: CollapseOperation, mode: BehaviorMode): void {
- if (CollapseOperation.show === collapseOperation
- || (CollapseOperation.toggle === collapseOperation && this.collapsed)) {
- this.fireEvent("show", mode);
- } else if (CollapseOperation.hide === collapseOperation
- || (CollapseOperation.toggle === collapseOperation && !this.collapsed)) {
- this.fireEvent("hide", mode);
+ /**
+ * Collapse (hide) the component.
+ * @param clientSideAnimation enabled animated collapsing in the browser (optional - default is true).
+ */
+ public collapse(clientSideAnimation: boolean = true): void { //this method must not be named 'hide' (TOBAGO-2148)
+ if (this.fireEvent("hide", true)) {
+ this.collapsed = true;
+ if (clientSideAnimation) {
+ this.clientSideCollapseAnimation();
+ }
+ this.fireEvent("hidden", false);
}
}
- fireEvent(eventName: string, behaviorMode: BehaviorMode) {
- const fullEventName = this.getFullEventName(eventName);
-
- this.dispatchEvent(new CustomEvent<CollapsibleEventDetail>(fullEventName, {
- bubbles: true,
- detail: {
- behaviorMode: behaviorMode
- }
- }));
+ protected clientSideCollapseAnimation(): void {
+ this.classList.add(Css.TOBAGO_COLLAPSED);
}
- private getFullEventName(eventName: string): string {
- return "tobago." + this.tagName.substring(7).toLowerCase() + "." + eventName;
+ private fireEvent(eventName: string, cancelable: boolean): boolean {
+ const fullEventName = "tobago." + this.tagName.substring(7).toLowerCase() + "." + eventName;
+ return this.dispatchEvent(new CustomEvent(fullEventName, {bubbles: true, cancelable: cancelable}));
}
get collapsed(): boolean {
@@ -96,12 +85,4 @@ export abstract class CollapsibleBase extends HTMLElement {
get collapseField(): HTMLInputElement {
return this.querySelector("input[id$='::collapse']");
}
-
- get showEventName(): string {
- return this.getFullEventName("show");
- }
-
- get hideEventName(): string {
- return this.getFullEventName("hide");
- }
}
diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-offcanvas.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-offcanvas.ts
index 10794934eb..83dddb8176 100644
--- a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-offcanvas.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-offcanvas.ts
@@ -16,8 +16,8 @@
*/
import {Offcanvas as BootstrapOffcanvas} from "bootstrap";
-import {BehaviorMode} from "./tobago-behavior-mode";
-import {CollapsibleBase, CollapsibleEventDetail} from "./tobago-collapsible-base";
+import {CollapsibleBase} from "./tobago-collapsible-base";
+import {EventListenerStore} from "./tobago-event-listener-store";
const BootstrapOffcanvasEvent = {
HIDE: "hide.bs.offcanvas",
@@ -28,6 +28,7 @@ const BootstrapOffcanvasEvent = {
};
export class Offcanvas extends CollapsibleBase {
+ private listeners: EventListenerStore = new EventListenerStore();
private offcanvas: BootstrapOffcanvas;
constructor() {
@@ -35,11 +36,10 @@ export class Offcanvas extends CollapsibleBase {
}
connectedCallback(): void {
- super.connectedCallback();
const options = {};
this.offcanvas = new BootstrapOffcanvas(this, options);
if (!this.collapsed) {
- this.clientBehaviorShow(null);
+ this.offcanvas.show();
}
this.listeners.add(this, BootstrapOffcanvasEvent.HIDDEN, () => {
@@ -53,38 +53,16 @@ export class Offcanvas extends CollapsibleBase {
}
disconnectedCallback(): void {
- this.clientBehaviorHide(null);
- super.disconnectedCallback();
+ this.offcanvas.hide();
+ this.listeners.disconnect();
}
- clientBehaviorShow(event: CustomEvent<CollapsibleEventDetail>): void {
- const behaviorMode = event && event.detail ? event?.detail.behaviorMode : null;
- console.debug("show - behaviorMode:", behaviorMode);
-
- this.collapsed = false;
-
- if (behaviorMode == null || behaviorMode == BehaviorMode.client) {
- this.offcanvas.show();
- } else {
- // otherwise the update from server will show the offcanvas
- }
-
- this.fireEvent("shown", behaviorMode);
+ protected clientSideExpandAnimation(): void {
+ this.offcanvas.show();
}
- clientBehaviorHide(event: CustomEvent<CollapsibleEventDetail>): void {
- const behaviorMode = event && event.detail ? event?.detail.behaviorMode : null;
- console.debug("hide - behaviorMode:", behaviorMode);
-
- this.collapsed = true;
-
- if (behaviorMode == null || behaviorMode == BehaviorMode.client) {
- this.offcanvas.hide();
- } else {
- // otherwise the update from server will hide the offcanvas
- }
-
- this.fireEvent("hidden", behaviorMode);
+ protected clientSideCollapseAnimation(): void {
+ this.offcanvas.hide();
}
get connected(): boolean {
diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-popup.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-popup.ts
index 008669389e..5b4a253517 100644
--- a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-popup.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-popup.ts
@@ -16,10 +16,10 @@
*/
import {Modal} from "bootstrap";
-import {BehaviorMode} from "./tobago-behavior-mode";
import {FocusableElement, tabbable} from "tabbable";
import {Key} from "./tobago-key";
-import {CollapsibleBase, CollapsibleEventDetail} from "./tobago-collapsible-base";
+import {CollapsibleBase} from "./tobago-collapsible-base";
+import {EventListenerStore} from "./tobago-event-listener-store";
const BootstrapPopupEvent = {
HIDE: "hide.bs.modal",
@@ -30,14 +30,14 @@ const BootstrapPopupEvent = {
};
export class Popup extends CollapsibleBase {
- modal: Modal;
+ private listeners: EventListenerStore = new EventListenerStore();
+ private modal: Modal;
constructor() {
super();
}
connectedCallback(): void {
- super.connectedCallback();
this.modal = new Modal(this, {focus: false});
this.listeners.add(this, "keydown", this.keydownEvent.bind(this));
this.listeners.add(this, BootstrapPopupEvent.SHOWN, () => this.focus());
@@ -51,45 +51,23 @@ export class Popup extends CollapsibleBase {
});
if (!this.collapsed) {
- this.clientBehaviorShow(null);
+ this.modal.show();
}
}
disconnectedCallback(): void {
- this.clientBehaviorHide(null);
- super.disconnectedCallback();
+ this.modal.hide();
// dispose seems to make trouble here: Scrolling is out or order after this call.
// this.modal.dispose();
+ this.listeners.disconnect();
}
- clientBehaviorShow(event: CustomEvent<CollapsibleEventDetail>): void {
- const behaviorMode = event && event.detail ? event?.detail.behaviorMode : null;
- console.debug("show - behaviorMode:", behaviorMode);
-
- this.collapsed = false;
-
- if (behaviorMode == null || behaviorMode == BehaviorMode.client) {
- this.modal.show();
- } else {
- // otherwise the update from server will show the popup
- }
-
- this.fireEvent("shown", behaviorMode);
+ protected clientSideExpandAnimation(): void {
+ this.modal.show();
}
- clientBehaviorHide(event: CustomEvent<CollapsibleEventDetail>): void {
- const behaviorMode = event && event.detail ? event?.detail.behaviorMode : null;
- console.debug("hide - behaviorMode:", behaviorMode);
-
- this.collapsed = true;
-
- if (behaviorMode == null || behaviorMode == BehaviorMode.client) {
- this.modal.hide();
- } else {
- // otherwise the update from server will hide the popup
- }
-
- this.fireEvent("hidden", behaviorMode);
+ protected clientSideCollapseAnimation(): void {
+ this.modal.hide();
}
private keydownEvent(event: KeyboardEvent): void {
diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago.d.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago.d.ts
index 5ee7dc7d01..c4cab12df9 100644
--- a/tobago-theme/tobago-theme-standard/src/main/ts/tobago.d.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago.d.ts
@@ -40,25 +40,6 @@ declare global {
options?: faces.ajax.RequestOptions
): void;
};
- /**
- * Handle the collapse state for collapsible components, such as tc:box, tc:panel, tc:popup, tc:offcanvas, tc:section.
- */
- collapsible: {
- /**
- * Show (expand) a collapsible component.
- *
- * @param {Element|string} elementOrId The root element of a collapsible component (e.g., tobago-popup for tc:popup) or its ID.
- * @throws {TypeError} Throws an error if the element is not found or is not valid.
- */
- show(elementOrId: Element | string): void;
- /**
- * Hide (collapse) a collapsible component.
- *
- * @param {Element|string} elementOrId The root element of a collapsible component (e.g., tobago-popup for tc:popup) or its ID.
- * @throws {TypeError} Throws an error if the element is not found or is not valid.
- */
- hide(elementOrId: Element | string): void;
- };
}
const tobago: Tobago;