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.javascript.host.html;
16  
17  import org.htmlunit.html.HtmlDialog;
18  import org.htmlunit.javascript.JavaScriptEngine;
19  import org.htmlunit.javascript.configuration.JsxClass;
20  import org.htmlunit.javascript.configuration.JsxConstructor;
21  import org.htmlunit.javascript.configuration.JsxFunction;
22  import org.htmlunit.javascript.configuration.JsxGetter;
23  import org.htmlunit.javascript.configuration.JsxSetter;
24  
25  /**
26   * The JavaScript object {@code HTMLDialogElement}.
27   *
28   * @author Ahmed Ashour
29   * @author Ronald Brill
30   */
31  @JsxClass(domClass = HtmlDialog.class)
32  public class HTMLDialogElement extends HTMLElement {
33  
34      /**
35       * JavaScript constructor.
36       */
37      @Override
38      @JsxConstructor
39      public void jsConstructor() {
40          super.jsConstructor();
41      }
42  
43      /**
44       * @return the {@code open} property
45       */
46      @JsxGetter
47      public boolean isOpen() {
48          return ((HtmlDialog) getDomNodeOrDie()).isOpen();
49      }
50  
51      /**
52       * Sets the open attribute.
53       * @param newValue the new value to set
54       */
55      @JsxSetter
56      public void setOpen(final Object newValue) {
57          final boolean bool = JavaScriptEngine.toBoolean(newValue);
58  
59          ((HtmlDialog) getDomNodeOrDie()).setOpen(bool);
60      }
61  
62      /**
63       *  Displays the dialog modelessly.
64       */
65      @JsxFunction
66      public void show() {
67          final HtmlDialog dialog = (HtmlDialog) getDomNodeOrDie();
68  
69          if (dialog.isOpen()) {
70              if (dialog.isModal()) {
71                  throw JavaScriptEngine.reportRuntimeError("InvalidStateError: Dialog is already open.");
72              }
73          }
74  
75          dialog.show();
76      }
77  
78      /**
79       *  Closes the dialog.
80       *  @param returnValue the return value
81       */
82      @JsxFunction
83      public void close(final Object returnValue) {
84          if (returnValue == null || JavaScriptEngine.isUndefined(returnValue)) {
85              ((HtmlDialog) getDomNodeOrDie()).close("");
86          }
87  
88          ((HtmlDialog) getDomNodeOrDie()).close(JavaScriptEngine.toString(returnValue));
89      }
90  
91      /**
92       *  Displays the dialog modal.
93       */
94      @JsxFunction
95      public void showModal() {
96          final HtmlDialog dialog = (HtmlDialog) getDomNodeOrDie();
97  
98          if (dialog.isOpen()) {
99              if (!dialog.isModal()) {
100                 throw JavaScriptEngine.reportRuntimeError("InvalidStateError: Dialog is already open.");
101             }
102         }
103 
104         dialog.showModal();
105     }
106 
107     /**
108      * @return the {@code returnValue} property
109      */
110     @JsxGetter
111     public String getReturnValue() {
112         return ((HtmlDialog) getDomNodeOrDie()).getReturnValue();
113     }
114 
115     /**
116      * Sets the returnValue attribute.
117      * @param newValue the new value to set
118      */
119     @JsxSetter
120     public void setReturnValue(final Object newValue) {
121         if (newValue == null || JavaScriptEngine.isUndefined(newValue)) {
122             ((HtmlDialog) getDomNodeOrDie()).setReturnValue("");
123         }
124 
125         ((HtmlDialog) getDomNodeOrDie()).setReturnValue(JavaScriptEngine.toString(newValue));
126     }
127 }