This is an automated email from the ASF dual-hosted git repository. mgrigorov pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/wicket.git
The following commit(s) were added to refs/heads/master by this push: new 550858b77c WICKET-7103 (#833) 550858b77c is described below commit 550858b77c2209529517a07fe4b6f61372871e29 Author: Johannes Renoth <ren...@users.noreply.github.com> AuthorDate: Tue Apr 16 09:27:24 2024 +0200 WICKET-7103 (#833) * WICKET-7103 Make it possible to post process the Ajax attributes for ModalDialog --------- Co-authored-by: renoth <johan...@renoth.dev> --- .../ajax/markup/html/modal/ModalDialog.java | 63 +++++++++++++--------- .../ajax/markup/html/modal/ModalDialogTest.java | 60 +++++++++++++++++++++ 2 files changed, 97 insertions(+), 26 deletions(-) diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/ModalDialog.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/ModalDialog.java index 3b99248df2..22ac209a2c 100644 --- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/ModalDialog.java +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/ModalDialog.java @@ -41,7 +41,7 @@ import org.apache.wicket.markup.html.panel.Panel; * <dt>modal-dialog-content</dt> * <dd>any additional styling for the content of this dialog</dd> * </dl> - * + * * @author Igor Vaynberg (ivaynberg) * @author svenmeier */ @@ -56,7 +56,7 @@ public class ModalDialog extends Panel /** * The id for the content of this dialoh. - * + * * @see #setContent(Component) * @see #open(Component, AjaxRequestTarget) */ @@ -84,9 +84,9 @@ public class ModalDialog extends Panel /** * Factory method for the overlay markup around the dialog. - * + * * @param overlayId - * id + * id * @return overlay */ protected WebMarkupContainer newOverlay(String overlayId) @@ -96,9 +96,9 @@ public class ModalDialog extends Panel /** * Factory method for the dialog markup around the content. - * + * * @param dialogId - * id + * id * @return overlay */ protected WebMarkupContainer newDialog(String dialogId) @@ -108,9 +108,8 @@ public class ModalDialog extends Panel /** * Set a content. - * + * * @param content - * * @see #open(AjaxRequestTarget) */ public void setContent(Component content) @@ -130,13 +129,12 @@ public class ModalDialog extends Panel * Open the dialog with a content. * <p> * The content will be removed on close of the dialog. - * + * * @param content - * the content + * the content * @param target - * an optional Ajax target + * an optional Ajax target * @return this - * * @see #setContent(Component) * @see #close(AjaxRequestTarget) */ @@ -157,18 +155,18 @@ public class ModalDialog extends Panel /** * Open the dialog. - * + * * @param target - * an optional Ajax target + * an optional Ajax target * @return this - * * @see #setContent(Component) */ public ModalDialog open(AjaxRequestTarget target) { if (overlay.size() == 0) { - throw new WicketRuntimeException(String.format("ModalDialog with id '%s' has no content set!", getId())); + throw new WicketRuntimeException( + String.format("ModalDialog with id '%s' has no content set!", getId())); } overlay.setVisible(true); @@ -183,7 +181,7 @@ public class ModalDialog extends Panel /** * Is this dialog open. - * + * * @return <code>true</code> if open */ public boolean isOpen() @@ -196,11 +194,10 @@ public class ModalDialog extends Panel * <p> * If opened via {@link #open(Component, AjaxRequestTarget)}, the content is removed from the * component tree - * + * * @param target - * an optional Ajax target + * an optional Ajax target * @return this - * * @see #open(Component, AjaxRequestTarget) */ public ModalDialog close(AjaxRequestTarget target) @@ -221,7 +218,7 @@ public class ModalDialog extends Panel /** * Close this dialog on press of escape key. - * + * * @return this */ public ModalDialog closeOnEscape() @@ -238,7 +235,7 @@ public class ModalDialog extends Panel /** * Close this dialog on click outside. - * + * * @return this */ public ModalDialog closeOnClick() @@ -247,7 +244,8 @@ public class ModalDialog extends Panel { protected CharSequence getPrecondition() { - return String.format("return attrs.event.target.id === '%s';", overlay.getMarkupId()); + return String.format("return attrs.event.target.id === '%s';", + overlay.getMarkupId()); } }); return this; @@ -255,10 +253,9 @@ public class ModalDialog extends Panel /** * Convenience method to trap focus inside the overlay. - * - * @see TrapFocusBehavior - * + * * @return this + * @see TrapFocusBehavior */ public ModalDialog trapFocus() { @@ -267,6 +264,18 @@ public class ModalDialog extends Panel return this; } + /** + * can be overridden to change the {@link AjaxRequestAttributes} of the default + * {@link CloseBehavior} executed on close. For example to change the Ajax Channel. + * + * @param attributes + * the {@link AjaxRequestAttributes} of the default {@link CloseBehavior} + */ + protected void postProcessCloseBehaviorAjaxAttributes(AjaxRequestAttributes attributes) + { + // no op + } + private abstract class CloseBehavior extends AjaxEventBehavior { private CloseBehavior(String event) @@ -290,6 +299,8 @@ public class ModalDialog extends Panel return CloseBehavior.this.getPrecondition(); } }); + + postProcessCloseBehaviorAjaxAttributes(attributes); } protected CharSequence getPrecondition() diff --git a/wicket-extensions/src/test/java/org/apache/wicket/extensions/ajax/markup/html/modal/ModalDialogTest.java b/wicket-extensions/src/test/java/org/apache/wicket/extensions/ajax/markup/html/modal/ModalDialogTest.java new file mode 100644 index 0000000000..6ef2cb1180 --- /dev/null +++ b/wicket-extensions/src/test/java/org/apache/wicket/extensions/ajax/markup/html/modal/ModalDialogTest.java @@ -0,0 +1,60 @@ +/* + * 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. + */ +package org.apache.wicket.extensions.ajax.markup.html.modal; + +import static org.apache.wicket.extensions.ajax.markup.html.modal.ModalDialog.CONTENT_ID; +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.wicket.ajax.AjaxRequestHandler; +import org.apache.wicket.markup.html.WebMarkupContainer; +import org.apache.wicket.util.tester.WicketTestCase; +import org.junit.jupiter.api.Test; + +class ModalDialogTest extends WicketTestCase +{ + + @Test + void smokeTest() + { + // Arrange + var cut = new ModalDialog("id"); + + // Act + tester.startComponentInPage(cut); + + //Assert + tester.assertInvisible("id:overlay"); + } + + @Test + void open_showModal() + { + // Arrange + var cut = new ModalDialog("id"); + tester.startComponentInPage(cut); + + // Act + final var ajaxRequestHandler = new AjaxRequestHandler(tester.getLastRenderedPage()); + cut.open(new WebMarkupContainer(CONTENT_ID), ajaxRequestHandler); + tester.processRequest(ajaxRequestHandler); + + //Assert + tester.assertVisible("id:overlay"); + assertThat( + tester.getFirstComponentByWicketId(CONTENT_ID).get().isVisibleInHierarchy()).isTrue(); + } +} \ No newline at end of file