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 org.htmlunit.corejs.javascript.ScriptableObject;
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
24 /**
25 * A JavaScript object for {@code CustomEvent}.
26 *
27 * @author Ahmed Ashour
28 * @author Ronald Brill
29 */
30 @JsxClass
31 public class CustomEvent extends Event {
32
33 /** The data passed when initializing the event. */
34 private Object detail_;
35
36 /**
37 * JavaScript constructor.
38 *
39 * @param type the event type
40 * @param details the event details (optional)
41 */
42 @Override
43 @JsxConstructor
44 public void jsConstructor(final String type, final ScriptableObject details) {
45 super.jsConstructor(JavaScriptEngine.toString(type), details);
46
47 if (details != null && !JavaScriptEngine.isUndefined(details)) {
48 final Object detail = details.get("detail", details);
49 if (NOT_FOUND != detail) {
50 detail_ = detail;
51 }
52 }
53 }
54
55 /**
56 * Implementation of the DOM Level 2 Event method for initializing the mouse event.
57 *
58 * @param type the event type
59 * @param bubbles can the event bubble
60 * @param cancelable can the event be canceled
61 * @param detail the detail to set for the event
62 */
63 @JsxFunction
64 public void initCustomEvent(
65 final String type,
66 final boolean bubbles,
67 final boolean cancelable,
68 final Object detail) {
69 initEvent(type, bubbles, cancelable);
70 detail_ = detail;
71 }
72
73 /**
74 * Returns any data passed when initializing the event.
75 * @return any data passed when initializing the event
76 */
77 @JsxGetter
78 public Object getDetail() {
79 return detail_;
80 }
81 }