mirror of
https://github.com/fergalmoran/ladybird.git
synced 2025-12-29 04:37:55 +00:00
This patch adds the EventTarget class and makes Node inherit from it. You can register event listeners on an EventTarget, and when you call dispatch_event() on it, the event listeners will get invoked. An event listener is basically a wrapper around a JS::Function*. This is pretty far from how DOM events should eventually work, but it's a place to start and we'll build more on top of this. :^)
34 lines
1.0 KiB
C++
34 lines
1.0 KiB
C++
#include <AK/Function.h>
|
|
#include <LibJS/Runtime/Function.h>
|
|
#include <LibWeb/Bindings/EventListenerWrapper.h>
|
|
#include <LibWeb/Bindings/EventTargetWrapper.h>
|
|
#include <LibWeb/DOM/EventListener.h>
|
|
#include <LibWeb/DOM/EventTarget.h>
|
|
|
|
namespace Web {
|
|
namespace Bindings {
|
|
|
|
EventTargetWrapper::EventTargetWrapper(EventTarget& impl)
|
|
: m_impl(impl)
|
|
{
|
|
put_native_function("addEventListener", [](Object* this_object, const Vector<JS::Value>& arguments) {
|
|
if (arguments.size() < 2)
|
|
return JS::js_undefined();
|
|
|
|
auto event_name = arguments[0].to_string();
|
|
ASSERT(arguments[1].is_object());
|
|
ASSERT(arguments[1].as_object()->is_function());
|
|
auto listener = adopt(*new EventListener(static_cast<JS::Function*>(const_cast<Object*>(arguments[1].as_object()))));
|
|
wrap(this_object->heap(), *listener);
|
|
static_cast<EventTargetWrapper*>(this_object)->impl().add_event_listener(event_name, move(listener));
|
|
return JS::js_undefined();
|
|
});
|
|
}
|
|
|
|
EventTargetWrapper::~EventTargetWrapper()
|
|
{
|
|
}
|
|
|
|
}
|
|
}
|