View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html;
16  
17  import java.util.Map;
18  
19  import org.htmlunit.SgmlPage;
20  import org.htmlunit.WebClient;
21  import org.htmlunit.javascript.AbstractJavaScriptEngine;
22  import org.htmlunit.javascript.HtmlUnitContextFactory;
23  import org.htmlunit.javascript.PostponedAction;
24  import org.htmlunit.javascript.host.event.Event;
25  import org.htmlunit.javascript.host.html.HTMLDialogElement;
26  
27  /**
28   * Wrapper for the HTML element "dialog".
29   *
30   * @author Ahmed Ashour
31   * @author Ronald Brill
32   */
33  public class HtmlDialog extends HtmlElement {
34  
35      /** The HTML tag represented by this element. */
36      public static final String TAG_NAME = "dialog";
37  
38      private boolean modal_;
39      private String returnValue_;
40  
41      /**
42       * Creates a new instance.
43       *
44       * @param qualifiedName the qualified name of the element type to instantiate
45       * @param page the HtmlPage that contains this element
46       * @param attributes the initial attributes
47       */
48      HtmlDialog(final String qualifiedName, final SgmlPage page,
49              final Map<String, DomAttr> attributes) {
50          super(qualifiedName, page, attributes);
51  
52          returnValue_ = "";
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public DisplayStyle getDefaultStyleDisplay() {
60          return DisplayStyle.NONE;
61      }
62  
63      /**
64       * @return the {@code open} property
65       */
66      public boolean isOpen() {
67          return hasAttribute("open");
68      }
69  
70      /**
71       * @return true if the dialog is open and modal.
72       */
73      public boolean isModal() {
74          return modal_;
75      }
76  
77      /**
78       * Sets the open state.
79       *
80       * @param newValue the new value
81       */
82      public void setOpen(final boolean newValue) {
83          if (newValue) {
84              setAttribute("open", "");
85          }
86          else {
87              removeAttribute("open");
88              modal_ = false;
89          }
90      }
91  
92      /**
93       *  Displays the dialog modelessly.
94       */
95      public void show() {
96          if (!isOpen()) {
97              setOpen(true);
98          }
99      }
100 
101     /**
102      *  Displays the dialog modal.
103      */
104     public void showModal() {
105         if (!isOpen()) {
106             setOpen(true);
107             modal_ = true;
108         }
109     }
110 
111     /**
112      *  Displays the dialog modal.
113      *  @param returnValue the return value
114      */
115     public void close(final String returnValue) {
116         if (isOpen()) {
117             setReturnValue(returnValue);
118             setOpen(false);
119 
120             final SgmlPage page = getPage();
121             final WebClient client = page.getWebClient();
122             if (client.isJavaScriptEnabled()) {
123                 final HTMLDialogElement dialogElement = getScriptableObject();
124                 final Event event = new Event(dialogElement, Event.TYPE_CLOSE);
125 
126                 final AbstractJavaScriptEngine<?> jsEngine = client.getJavaScriptEngine();
127                 final PostponedAction action = new PostponedAction(page, "Dialog.CloseEvent") {
128                     @Override
129                     public void execute() {
130                         final HtmlUnitContextFactory cf = jsEngine.getContextFactory();
131                         cf.call(cx -> dialogElement.dispatchEvent(event));
132                     }
133                 };
134                 jsEngine.addPostponedAction(action);
135             }
136         }
137         modal_ = false;
138     }
139 
140     /**
141      * @return the {@code returnValue} property
142      */
143     public String getReturnValue() {
144         return returnValue_;
145     }
146 
147     /**
148      * Sets the open state.
149      *
150      * @param newValue the new value
151      */
152     public void setReturnValue(final String newValue) {
153         if (newValue == null) {
154             returnValue_ = "";
155             return;
156         }
157         returnValue_ = newValue;
158     }
159 }