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 2024/04/12 09:01:36 UTC

(myfaces-tobago) 01/02: feature(popup): static backdrop attribute

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

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

commit b9e5a6d2d9381bceef50c8d6c234ce1071974be1
Author: Henning Noeth <hn...@apache.org>
AuthorDate: Thu Mar 28 10:31:48 2024 +0100

    feature(popup): static backdrop attribute
    
    * implement staticBackdrop attribute
    * add example in demo
    * notify server if popup closed by clicking on backdrop
    * add test
    
    Issue: TOBAGO-2298
---
 .../myfaces/tobago/component/Attributes.java       |  1 +
 .../tobago/internal/component/AbstractUIPopup.java | 17 ++++-
 .../internal/renderkit/renderer/PopupRenderer.java |  6 +-
 .../taglib/component/PopupTagDeclaration.java      | 11 +++
 .../src/main/webapp/content/060-popup/Popup.xhtml  | 33 ++++++--
 .../2400-popup/Popup_staticBackdrop.test.js        | 88 ++++++++++++++++++++++
 .../900-test/2400-popup/Popup_staticBackdrop.xhtml | 59 +++++++++++++++
 .../src/main/resources/META-INF/tobago-config.xml  |  3 +
 .../src/main/ts/tobago-popup.ts                    | 32 ++++++++
 9 files changed, 241 insertions(+), 9 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/component/Attributes.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/component/Attributes.java
index adc30b0c7e..4130f60bc3 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/component/Attributes.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/component/Attributes.java
@@ -428,6 +428,7 @@ public enum Attributes {
   stateChangeListener,
   stateChangeListenerExpression,
   statePreview,
+  staticBackdrop,
   step,
   style,
   switchType,
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIPopup.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIPopup.java
index 5045062a49..33625aa975 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIPopup.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIPopup.java
@@ -20,10 +20,23 @@
 package org.apache.myfaces.tobago.internal.component;
 
 import javax.faces.component.NamingContainer;
+import javax.faces.context.FacesContext;
+import java.util.Map;
 
 /**
  * {@link org.apache.myfaces.tobago.internal.taglib.component.PopupTagDeclaration}
  */
-public abstract class AbstractUIPopup extends AbstractUICollapsiblePanel
-    implements NamingContainer {
+public abstract class AbstractUIPopup extends AbstractUICollapsiblePanel implements NamingContainer {
+
+  @Override
+  public void processDecodes(final FacesContext facesContext) {
+    super.processDecodes(facesContext);
+
+    final Map<String, String> requestParameterMap = facesContext.getExternalContext().getRequestParameterMap();
+    if (Boolean.parseBoolean(requestParameterMap.get("tobago.collapsible.processState"))) {
+      processState();
+    }
+  }
+
+  public abstract boolean isStaticBackdrop();
 }
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/PopupRenderer.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/PopupRenderer.java
index 365d81cf7b..75817651d7 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/PopupRenderer.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/PopupRenderer.java
@@ -45,12 +45,14 @@ public class PopupRenderer<T extends AbstractUIPopup> extends CollapsiblePanelRe
     final String clientId = component.getClientId(facesContext);
     final boolean collapsed = component.isCollapsed();
     final Markup markup = component.getMarkup();
+    final boolean staticBackdrop = component.isStaticBackdrop();
     final UIComponent labelFacet = ComponentUtils.getFacet(component, Facets.label);
     final UIComponent barFacet = ComponentUtils.getFacet(component, Facets.bar);
     final UIComponent footerFacet = ComponentUtils.getFacet(component, Facets.footer);
 
-    // this makes the popup NOT closable with a click to the background
-    ComponentUtils.putDataAttribute(component, "bs-backdrop", "static");
+    if (staticBackdrop) {
+      ComponentUtils.putDataAttribute(component, "bs-backdrop", "static");
+    }
 
     writer.startElement(HtmlElements.TOBAGO_POPUP);
     writer.writeIdAttribute(clientId);
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/PopupTagDeclaration.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/PopupTagDeclaration.java
index a68321dfe0..9683d66f3e 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/PopupTagDeclaration.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/PopupTagDeclaration.java
@@ -61,6 +61,17 @@ import org.apache.myfaces.tobago.model.CollapseMode;
 public interface PopupTagDeclaration
     extends HasIdBindingAndRendered, IsVisual, HasTip {
 
+  /**
+   * If "true", a click on the backdrop does not close the popup.
+   * Default value is "false" and can be configured in the tobago-config.xml.
+   */
+  @TagAttribute()
+  @UIComponentTagAttribute(
+      type = "boolean",
+      defaultCode = "Boolean.parseBoolean(org.apache.myfaces.tobago.context."
+          + "TobagoContext.getInstance(getFacesContext()).getTheme().getTagAttributeDefault(TAG, \"staticBackdrop\"))")
+  void setStaticBackdrop(String staticBackdrop);
+
   /**
    * Indicating the collapsed state of this component.
    */
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/060-popup/Popup.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/060-popup/Popup.xhtml
index 824deac416..1f02269d41 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/content/060-popup/Popup.xhtml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/060-popup/Popup.xhtml
@@ -162,8 +162,30 @@
     </tc:popup>
   </tc:section>
 
+  <tc:section label="Static backdrop">
+    <p>By default the popup can be closed by clicking on the backdrop or by pressing the ESC key.
+      This can be changed with the <code>staticBackdrop</code> attribute. The default value is "false".
+      It can be changed in the <code>tobago-config.xml</code>.</p>
+
+    <tc:button id="showStaticBackdrop" label="Open staticBackdrop Popup" omit="true">
+      <tc:operation name="show" for="staticBackdrop"/>
+    </tc:button>
+
+    <tc:popup id="staticBackdrop" collapsedMode="hidden" staticBackdrop="true">
+      <f:facet name="label">
+        Static Backdrop
+      </f:facet>
+      <f:facet name="bar">
+        <tc:link id="hideStaticBackdropTrue" image="bi-x-lg" omit="true">
+          <tc:operation name="hide" for="staticBackdrop"/>
+        </tc:link>
+      </f:facet>
+      <p>This popup can only be closed by clicking on the X button.</p>
+    </tc:popup>
+  </tc:section>
+
   <tc:section label="Plain Popup">
-<!--    todo-->
+    <!-- todo-->
     <tc:badge markup="warning">!</tc:badge> missing content
   </tc:section>
 
@@ -189,10 +211,11 @@
 
     <!--    todo-->
     <p>
-    <tc:badge markup="warning">tbd (to be defined)!</tc:badge> in this example the "show" should be
-    called directly, and not after the ajax call is complete. On the other and, to avoid flickering,
-    the popup should be shown after the ajax call.
-    (<tc:link link="https://issues.apache.org/jira/browse/TOBAGO-2083" label="Jira TOBAGO-2083" image="bi-box-arrow-up-right"/>)
+      <tc:badge markup="warning">tbd (to be defined)!</tc:badge> in this example the "show" should be
+      called directly, and not after the ajax call is complete. On the other and, to avoid flickering,
+      the popup should be shown after the ajax call.
+      (<tc:link link="https://issues.apache.org/jira/browse/TOBAGO-2083" label="Jira TOBAGO-2083"
+                image="bi-box-arrow-up-right"/>)
     </p>
 
     <tc:button id="openRefreshPopup" label="Open Popup" immediate="true">
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/2400-popup/Popup_staticBackdrop.test.js b/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/2400-popup/Popup_staticBackdrop.test.js
new file mode 100644
index 0000000000..430ad887ac
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/2400-popup/Popup_staticBackdrop.test.js
@@ -0,0 +1,88 @@
+/*
+ * 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 {JasmineTestTool} from "/tobago/test/tobago-test-tool.js";
+import {elementByIdFn, querySelectorFn} from "/script/tobago-test.js";
+
+it("Open 'staticBackdroo=false'-Popup, close it, press 'Submit'", function (done) {
+  const timestampOutput = querySelectorFn("#page\\:mainForm\\:timestamp .form-control-plaintext");
+  const openButton = elementByIdFn("page:mainForm:showStaticBackdropFalse");
+  const submit = elementByIdFn("page:mainForm:submit");
+  const wrapper = elementByIdFn("page:mainForm:popupWrapper");
+  const popup = elementByIdFn("page:mainForm:staticBackdropFalse");
+  const collapse = elementByIdFn("page:mainForm:staticBackdropFalse::collapse");
+  const backdropClick = elementByIdFn("backdropClick");
+
+  let timestamp;
+  let shownEventCount = 0;
+  let hiddenEventCount = 0;
+  wrapper().addEventListener("shown.bs.modal", () => shownEventCount++);
+  wrapper().addEventListener("hidden.bs.modal", () => hiddenEventCount++);
+
+  const test = new JasmineTestTool(done);
+  test.do(() => expect(popup().classList).not.toContain("show"));
+  test.do(() => expect(collapse().getAttribute("value")).toBe("true"));
+
+  test.event("click", openButton, () => shownEventCount === 1);
+  test.do(() => expect(popup().classList).toContain("show"));
+  test.do(() => expect(collapse().getAttribute("value")).toBe("false"));
+
+  test.event("click", backdropClick, () => hiddenEventCount === 1);
+  test.do(() => expect(popup().classList).not.toContain("show"));
+  test.do(() => expect(collapse().getAttribute("value")).toBe("true"));
+
+  test.do(() => timestamp = Number(timestampOutput().textContent));
+  test.event("click", submit, () => Number(timestampOutput().textContent) > timestamp);
+  test.do(() => expect(popup().classList).not.toContain("show"));
+  test.do(() => expect(collapse().getAttribute("value")).toBe("true"));
+
+  test.start();
+});
+
+it("Open 'staticBackdroo=true'-Popup, close it, press 'Submit'", function (done) {
+  const body = querySelectorFn("body");
+  const timestampOutput = querySelectorFn("#page\\:mainForm\\:timestamp .form-control-plaintext");
+  const openButton = elementByIdFn("page:mainForm:showStaticBackdropTrue");
+  const submit = elementByIdFn("page:mainForm:submit");
+  const wrapper = elementByIdFn("page:mainForm:popupWrapper");
+  const popup = elementByIdFn("page:mainForm:staticBackdropTrue");
+  const collapse = elementByIdFn("page:mainForm:staticBackdropTrue::collapse");
+  const closeButton = elementByIdFn("page:mainForm:staticBackdropTrue:hideStaticBackdropTrue");
+
+  let timestamp;
+  let shownEventCount = 0;
+  wrapper().addEventListener("shown.bs.modal", () => shownEventCount++);
+
+  const test = new JasmineTestTool(done);
+  test.do(() => expect(popup().classList).not.toContain("show"));
+  test.do(() => expect(collapse().getAttribute("value")).toBe("true"));
+
+  test.event("click", openButton, () => shownEventCount === 1);
+  test.do(() => expect(popup().classList).toContain("show"));
+  test.do(() => expect(collapse().getAttribute("value")).toBe("false"));
+
+  test.event("click", closeButton, () => !body().classList.contains("modal-open"));
+  test.do(() => expect(popup().classList).not.toContain("show"));
+  test.do(() => expect(collapse().getAttribute("value")).toBe("true"));
+
+  test.do(() => timestamp = Number(timestampOutput().textContent));
+  test.event("click", submit, () => Number(timestampOutput().textContent) > timestamp);
+  test.do(() => expect(popup().classList).not.toContain("show"));
+  test.do(() => expect(collapse().getAttribute("value")).toBe("true"));
+
+  test.start();
+});
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/2400-popup/Popup_staticBackdrop.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/2400-popup/Popup_staticBackdrop.xhtml
new file mode 100644
index 0000000000..d283602896
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/2400-popup/Popup_staticBackdrop.xhtml
@@ -0,0 +1,59 @@
+<?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="http://xmlns.jcp.org/jsf/core"
+                xmlns:tc="http://myfaces.apache.org/tobago/component"
+                xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
+
+  <tc:out id="timestamp" label="Timestamp" value="#{eventController.currentTimestamp}"/>
+
+  <tc:button id="showStaticBackdropFalse" label="staticBackdrop=false">
+    <tc:operation name="show" for="staticBackdropFalse"/>
+    <f:ajax execute="staticBackdropFalse" render="staticBackdropFalse"/>
+  </tc:button>
+  <tc:button id="showStaticBackdropTrue" label="staticBackdrop=true">
+    <tc:operation name="show" for="staticBackdropTrue"/>
+    <f:ajax execute="staticBackdropTrue" render="staticBackdropTrue"/>
+  </tc:button>
+  <tc:button id="submit" label="Submit"/>
+
+  <tc:panel id="popupWrapper">
+    <tc:popup id="staticBackdropFalse" collapsedMode="hidden" staticBackdrop="false">
+      <f:facet name="label">
+        staticBackdrop=false
+      </f:facet>
+      <p>Close this popup by clicking on the backdrop.</p>
+      <button id="backdropClick" type="button" data-bs-dismiss="modal">Simulate click on backdrop</button>
+    </tc:popup>
+    <tc:popup id="staticBackdropTrue" collapsedMode="hidden" staticBackdrop="true">
+      <f:facet name="label">
+        staticBackdrop=true
+      </f:facet>
+      <f:facet name="bar">
+        <tc:link id="hideStaticBackdropTrue" image="bi-x-lg">
+          <tc:operation name="hide" for="staticBackdropTrue"/>
+          <f:ajax execute="staticBackdropTrue" render="staticBackdropTrue"/>
+        </tc:link>
+      </f:facet>
+      <p>Close this popup by clicking on the X.</p>
+    </tc:popup>
+  </tc:panel>
+</ui:composition>
diff --git a/tobago-theme/tobago-theme-standard/src/main/resources/META-INF/tobago-config.xml b/tobago-theme/tobago-theme-standard/src/main/resources/META-INF/tobago-config.xml
index 548a17ebfc..afc9567f5f 100644
--- a/tobago-theme/tobago-theme-standard/src/main/resources/META-INF/tobago-config.xml
+++ b/tobago-theme/tobago-theme-standard/src/main/resources/META-INF/tobago-config.xml
@@ -73,6 +73,9 @@
           <attribute name="messagePosition" default="buttonRight"/>
           <attribute name="helpPosition" default="buttonRight"/>
         </tag>
+        <tag name="popup">
+          <attribute name="staticBackdrop" default="false"/>
+        </tag>
         <tag name="range">
           <attribute name="labelLayout" default="flexLeft"/>
           <attribute name="messagePosition" default="buttonRight"/>
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 4fcf2d8e84..91176fbb0e 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
@@ -19,6 +19,14 @@ import {Modal} from "bootstrap";
 import {BehaviorMode} from "./tobago-behavior-mode";
 import {Collapse} from "./tobago-collapse";
 
+const BootstrapPopupEvent = {
+  HIDE: "hide.bs.modal",
+  HIDDEN: "hidden.bs.modal",
+  HIDE_PREVENTED: "hidePrevented.bs.modal",
+  SHOW: "show.bs.modal",
+  SHOWN: "shown.bs.modal"
+};
+
 export class Popup extends HTMLElement {
 
   modal: Modal;
@@ -33,6 +41,22 @@ export class Popup extends HTMLElement {
     if (!this.collapsed) {
       this.clientBehaviorShow();
     }
+
+    this.addEventListener(BootstrapPopupEvent.HIDDEN, () => {
+      /**
+       * Notify server if popup is closed by clicking on the backdrop or press ESC.
+       */
+      if (this.connected) {
+        this.collapsed = true;
+
+        jsf.ajax.request(
+            this,
+            null,
+            {
+              "tobago.collapsible.processState": "true"
+            });
+      }
+    });
   }
 
   disconnectedCallback(): void {
@@ -62,6 +86,14 @@ export class Popup extends HTMLElement {
   get collapsed(): boolean {
     return JSON.parse(Collapse.findHidden(this).value);
   }
+
+  set collapsed(collapsed: boolean) {
+    Collapse.findHidden(this).value = String(collapsed);
+  }
+
+  get connected(): boolean {
+    return this.parentElement != null;
+  }
 }
 
 document.addEventListener("tobago.init", function (event: Event): void {