use rong::{function::*, *};
use super::Event;
#[derive(FromJSObj, Default)]
pub struct CustomEventOptions {
#[js_default]
pub bubbles: bool,
#[js_default]
pub cancelable: bool,
#[js_default]
pub composed: bool,
pub detail: Option<JSValue>,
}
#[js_export]
pub struct CustomEvent {
detail: Option<JSValue>,
event: Event,
}
#[js_class]
impl CustomEvent {
#[js_method(constructor)]
fn new(type_: String, options: Optional<CustomEventOptions>) -> Self {
let opts = options.0.unwrap_or_default();
use super::event::EventOptions;
let event_opts = EventOptions {
bubbles: opts.bubbles,
cancelable: opts.cancelable,
composed: opts.composed,
};
Self {
detail: opts.detail,
event: Event::new(type_, Optional(Some(event_opts))),
}
}
#[js_method(getter)]
fn detail(&self) -> Option<JSValue> {
self.detail.clone()
}
#[js_method(getter, rename = "type")]
fn type_(&self) -> String {
self.event.type_()
}
#[js_method(getter)]
fn bubbles(&self) -> bool {
self.event.bubbles()
}
#[js_method(getter)]
fn cancelable(&self) -> bool {
self.event.cancelable()
}
#[js_method(getter)]
fn composed(&self) -> bool {
self.event.composed()
}
#[js_method(gc_mark)]
fn gc_mark_with<F>(&self, mut mark_fn: F)
where
F: FnMut(&JSValue),
{
if let Some(detail) = self.detail.as_ref() {
mark_fn(detail);
}
}
}