CustomEvent.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/DOM/Event.h>
  8. namespace Web::DOM {
  9. struct CustomEventInit : public EventInit {
  10. JS::Value detail { JS::js_null() };
  11. };
  12. // https://dom.spec.whatwg.org/#customevent
  13. class CustomEvent : public Event {
  14. public:
  15. using WrapperType = Bindings::CustomEventWrapper;
  16. static NonnullRefPtr<CustomEvent> create(FlyString const& event_name, CustomEventInit const& event_init = {})
  17. {
  18. return adopt_ref(*new CustomEvent(event_name, event_init));
  19. }
  20. static NonnullRefPtr<CustomEvent> create_with_global_object(Bindings::WindowObject&, const FlyString& event_name, CustomEventInit const& event_init)
  21. {
  22. return CustomEvent::create(event_name, event_init);
  23. }
  24. virtual ~CustomEvent() override = default;
  25. // https://dom.spec.whatwg.org/#dom-customevent-detail
  26. JS::Value detail() const { return m_detail; }
  27. void visit_edges(JS::Cell::Visitor&);
  28. void init_custom_event(String const& type, bool bubbles, bool cancelable, JS::Value detail);
  29. private:
  30. explicit CustomEvent(FlyString const& event_name)
  31. : Event(event_name)
  32. {
  33. }
  34. CustomEvent(FlyString const& event_name, CustomEventInit const& event_init)
  35. : Event(event_name, event_init)
  36. , m_detail(event_init.detail)
  37. {
  38. }
  39. // https://dom.spec.whatwg.org/#dom-customevent-initcustomevent-type-bubbles-cancelable-detail-detail
  40. JS::Value m_detail { JS::js_null() };
  41. };
  42. }