1
2
3
4
5
6
7
8
9
10
11
12
13
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
29
30
31
32
33 public class HtmlDialog extends HtmlElement {
34
35
36 public static final String TAG_NAME = "dialog";
37
38 private boolean modal_;
39 private String returnValue_;
40
41
42
43
44
45
46
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
57
58 @Override
59 public DisplayStyle getDefaultStyleDisplay() {
60 return DisplayStyle.NONE;
61 }
62
63
64
65
66 public boolean isOpen() {
67 return hasAttribute("open");
68 }
69
70
71
72
73 public boolean isModal() {
74 return modal_;
75 }
76
77
78
79
80
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
94
95 public void show() {
96 if (!isOpen()) {
97 setOpen(true);
98 }
99 }
100
101
102
103
104 public void showModal() {
105 if (!isOpen()) {
106 setOpen(true);
107 modal_ = true;
108 }
109 }
110
111
112
113
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
142
143 public String getReturnValue() {
144 return returnValue_;
145 }
146
147
148
149
150
151
152 public void setReturnValue(final String newValue) {
153 if (newValue == null) {
154 returnValue_ = "";
155 return;
156 }
157 returnValue_ = newValue;
158 }
159 }