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.event;
16
17 import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
18 import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
19
20 import org.htmlunit.corejs.javascript.Scriptable;
21 import org.htmlunit.corejs.javascript.ScriptableObject;
22 import org.htmlunit.html.DomNode;
23 import org.htmlunit.javascript.JavaScriptEngine;
24 import org.htmlunit.javascript.configuration.JsxClass;
25 import org.htmlunit.javascript.configuration.JsxConstant;
26 import org.htmlunit.javascript.configuration.JsxConstructor;
27 import org.htmlunit.javascript.configuration.JsxFunction;
28 import org.htmlunit.javascript.configuration.JsxGetter;
29 import org.htmlunit.javascript.host.Window;
30
31 /**
32 * JavaScript object representing a UI event. For general information on which properties and functions should be
33 * supported, see <a href="http://www.w3.org/TR/DOM-Level-3-Events/events.html#Events-UIEvent">DOM Level 3 Events</a>.
34 *
35 * @author Daniel Gredler
36 * @author Ahmed Ashour
37 * @author Frank Danek
38 * @author Ronald Brill
39 */
40 @JsxClass
41 public class UIEvent extends Event {
42
43 /** Constant. */
44 @JsxConstant({FF, FF_ESR})
45 public static final int SCROLL_PAGE_DOWN = 0x8000;
46
47 /** Constant. */
48 @JsxConstant({FF, FF_ESR})
49 public static final int SCROLL_PAGE_UP = 0xFFFF8000;
50
51 /** Specifies some detail information about the event. */
52 private long detail_;
53
54 /** Specifies some view information about the event. */
55 private Object view_;
56 private static final Object NO_VIEW = new Object();
57
58 /**
59 * Creates a new UI event instance.
60 */
61 public UIEvent() {
62 super();
63 }
64
65 /**
66 * JavaScript constructor.
67 *
68 * @param type the event type
69 * @param details the event details (optional)
70 */
71 @JsxConstructor
72 @Override
73 public void jsConstructor(final String type, final ScriptableObject details) {
74 super.jsConstructor(type, details);
75
76 view_ = NO_VIEW;
77 if (details != null && !JavaScriptEngine.isUndefined(details)) {
78 final Object view = details.get("view", details);
79 if (view instanceof Window) {
80 view_ = view;
81 }
82 else if (view != Scriptable.NOT_FOUND) {
83 throw JavaScriptEngine.typeError("View must be a window.");
84 }
85 }
86 }
87
88 /**
89 * Creates a new UI event instance.
90 *
91 * @param domNode the DOM node that triggered the event
92 * @param type the event type
93 */
94 public UIEvent(final DomNode domNode, final String type) {
95 super(domNode, type);
96 }
97
98 /**
99 * Creates a new event instance.
100 * @param target the event target
101 * @param type the event type
102 */
103 public UIEvent(final EventTarget target, final String type) {
104 super(target, type);
105 }
106
107 /**
108 * Returns some detail information about the event, depending on the event type. For mouse events,
109 * the detail property indicates how many times the mouse has been clicked in the same location for
110 * this event.
111 *
112 * @return some detail information about the event, depending on the event type
113 */
114 @JsxGetter
115 public long getDetail() {
116 return detail_;
117 }
118
119 /**
120 * Sets the detail information for this event.
121 *
122 * @param detail the detail information for this event
123 */
124 protected void setDetail(final long detail) {
125 detail_ = detail;
126 }
127
128 /**
129 * Returns the view from which the event was generated. In browsers, this is the originating window.
130 *
131 * @return the view from which the event was generated
132 */
133 @JsxGetter
134 public Window getView() {
135 if (view_ == NO_VIEW) {
136 return null;
137 }
138 if (view_ != null) {
139 return (Window) view_;
140 }
141 return getWindow();
142 }
143
144 /**
145 * Implementation of the DOM Level 3 Event method for initializing the UI event.
146 *
147 * @param type the event type
148 * @param bubbles can the event bubble
149 * @param cancelable can the event be canceled
150 * @param view the view to use for this event
151 * @param detail the detail to set for the event
152 */
153 @JsxFunction
154 public void initUIEvent(
155 final String type,
156 final boolean bubbles,
157 final boolean cancelable,
158 final Object view,
159 final int detail) {
160 initEvent(type, bubbles, cancelable);
161 // Ignore the view parameter; we always use the window.
162 setDetail(detail);
163 }
164
165 /**
166 * @return a number that indicates which button was pressed on the mouse,
167 * or the numeric keyCode or the character code (charCode) of the key pressed on the keyboard
168 */
169 @JsxGetter
170 public int getWhich() {
171 return 0;
172 }
173 }