1 /*
2 * Copyright (c) 2002-2026 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 * Returns whether this dialog is open.
65 *
66 * @return {@code true} if the dialog is open
67 */
68 public boolean isOpen() {
69 return hasAttribute("open");
70 }
71
72 /**
73 * Returns whether this dialog is displayed modally.
74 *
75 * @return {@code true} if the dialog is open and modal
76 */
77 public boolean isModal() {
78 return modal_;
79 }
80
81 /**
82 * Sets the open state.
83 *
84 * @param newValue the new value
85 */
86 public void setOpen(final boolean newValue) {
87 if (newValue) {
88 setAttribute("open", "");
89 }
90 else {
91 removeAttribute("open");
92 modal_ = false;
93 }
94 }
95
96 /**
97 * Displays this dialog modelessly.
98 */
99 public void show() {
100 if (!isOpen()) {
101 setOpen(true);
102 }
103 }
104
105 /**
106 * Displays this dialog modally.
107 */
108 public void showModal() {
109 if (!isOpen()) {
110 setOpen(true);
111 modal_ = true;
112 }
113 }
114
115 /**
116 * Closes this dialog and sets its return value.
117 *
118 * @param returnValue the return value
119 */
120 public void close(final String returnValue) {
121 if (isOpen()) {
122 setReturnValue(returnValue);
123 setOpen(false);
124
125 final SgmlPage page = getPage();
126 final WebClient client = page.getWebClient();
127 if (client.isJavaScriptEnabled()) {
128 final HTMLDialogElement dialogElement = getScriptableObject();
129 final Event event = new Event(dialogElement, Event.TYPE_CLOSE);
130
131 final AbstractJavaScriptEngine<?> jsEngine = client.getJavaScriptEngine();
132 final PostponedAction action = new PostponedAction(page, "Dialog.CloseEvent") {
133 @Override
134 public void execute() {
135 final HtmlUnitContextFactory cf = jsEngine.getContextFactory();
136 cf.call(cx -> dialogElement.dispatchEvent(event));
137 }
138 };
139 jsEngine.addPostponedAction(action);
140 }
141 }
142 modal_ = false;
143 }
144
145 /**
146 * Returns the dialog's return value.
147 *
148 * @return the {@code returnValue} property
149 */
150 public String getReturnValue() {
151 return returnValue_;
152 }
153
154 /**
155 * Sets the dialog's return value.
156 *
157 * @param newValue the new value
158 */
159 public void setReturnValue(final String newValue) {
160 if (newValue == null) {
161 returnValue_ = "";
162 return;
163 }
164 returnValue_ = newValue;
165 }
166 }