martin-g commented on a change in pull request #361: WICKET-6666 initial 
checkin of new ModalDialog
URL: https://github.com/apache/wicket/pull/361#discussion_r283856955
 
 

 ##########
 File path: 
wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/ModalDialog.java
 ##########
 @@ -0,0 +1,240 @@
+package org.apache.wicket.extensions.ajax.markup.html.modal;
+
+import java.io.Serializable;
+
+import org.apache.wicket.Application;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.markup.head.IHeaderResponse;
+import org.apache.wicket.markup.head.JavaScriptHeaderItem;
+import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
+import org.apache.wicket.markup.html.WebMarkupContainer;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.panel.EmptyPanel;
+import org.apache.wicket.markup.html.panel.Panel;
+
+import com.github.openjson.JSONStringer;
+
+/**
+ * Presents a modal dialog to the user. See open and close methods.
+ * 
+ * @author Igor Vaynberg (ivaynberg)
+ */
+public class ModalDialog extends Panel
+{
+
+       public static final String CONTENT_ID = "content";
+
+       private final WebMarkupContainer container;
+       private final WebMarkupContainer contentContainer;
+       private boolean open = false;
+       private transient boolean openedInThisRequest = false;
+       private Options options;
+
+       public ModalDialog(String id)
+       {
+               super(id);
+               setOutputMarkupId(true);
+
+               // Container controls the overall visibility of the modal form 
innards. In its initial state
+               // this is set to
+               // hidden so only the external tag renders. When the modal is 
openedInThisRequest it is
+               // first repainted with
+               // this shown to
+               // insert the markup into the dom, and then the modal 
javascript is invoked to rip out this
+               // tag out of the dom
+               // and make it modal.
+               container = new WebMarkupContainer("container");
+               container.setOutputMarkupId(true);
+               container.setVisible(false);
+               add(container);
+
+               // We need this here in case the modal itself is placed inside 
a form. If that is the case
+               // and the content
+               // contains a form that form's markup form tag will be rendered 
as div because as far as
+               // wicket is concerned it
+               // is part of the same dom as the page and nested forms are 
forbidden. By overriding
+               // isRootForm() to true we are
+               // forcing this form - which will be ripped out of the dom 
along with the content form if
+               // there is one to always
+               // render its tag as 'form' instead of 'div'.
+               var form = new Form<Void>("form")
+               {
+                       @Override
+                       public boolean isRootForm()
+                       {
+                               return true;
+                       }
+               };
+               form.setOutputMarkupId(true);
+               container.add(form);
+
+               contentContainer = form;
+               contentContainer.add(new EmptyPanel(CONTENT_ID));
+       }
+
+       @Override
+       public void renderHead(IHeaderResponse response)
+       {
+               super.renderHead(response);
+               
response.render(JavaScriptHeaderItem.forReference(ModalDialogReferences.JS));
+
+               // if the page is refreshed and the window was open in the 
previous request we need to
+               // re-open it
+               if (open == true && openedInThisRequest == false)
+               {
+                       
response.render(OnDomReadyHeaderItem.forScript(getOpenJavascript()));
+               }
+       }
+
+       public ModalDialog open(AjaxRequestTarget target, WebMarkupContainer 
content)
+       {
+               open(target, null, content);
+               return this;
+       }
+
+       public ModalDialog open(AjaxRequestTarget target, Options options, 
WebMarkupContainer content)
+       {
+
+               if (!content.getId().equals(CONTENT_ID))
+               {
+                       throw new IllegalArgumentException(
+                               "Content must have wicket id set to 
ModalDialog.CONTENT_ID");
+               }
+
+               contentContainer.replace(content);
+
+               container.setVisible(true);
+
+               open = true;
+               openedInThisRequest = true;
+
+               target.add(this);
+
+               this.options = options;
+
+               target.prependJavaScript(getOpenJavascript());
 
 Review comment:
   I have to double check but I think the order is:
   - prepend JS
   - replace DOM
   - append JS

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to