mirror of
https://github.com/RGBCube/serenity
synced 2025-05-23 18:15:06 +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. :^)
41 lines
877 B
C++
41 lines
877 B
C++
#pragma once
|
|
|
|
#include <AK/Noncopyable.h>
|
|
#include <AK/String.h>
|
|
#include <AK/Vector.h>
|
|
#include <LibWeb/Forward.h>
|
|
|
|
namespace Web {
|
|
|
|
class EventTarget {
|
|
AK_MAKE_NONCOPYABLE(EventTarget);
|
|
AK_MAKE_NONMOVABLE(EventTarget);
|
|
|
|
public:
|
|
virtual ~EventTarget();
|
|
|
|
void ref() { ref_event_target(); }
|
|
void unref() { unref_event_target(); }
|
|
|
|
void add_event_listener(String event_name, NonnullRefPtr<EventListener>);
|
|
|
|
virtual void dispatch_event(String event_name) = 0;
|
|
|
|
struct EventListenerRegistration {
|
|
String event_name;
|
|
NonnullRefPtr<EventListener> listener;
|
|
};
|
|
|
|
const Vector<EventListenerRegistration>& listeners() const { return m_listeners; }
|
|
|
|
protected:
|
|
EventTarget();
|
|
|
|
virtual void ref_event_target() = 0;
|
|
virtual void unref_event_target() = 0;
|
|
|
|
private:
|
|
Vector<EventListenerRegistration> m_listeners;
|
|
};
|
|
|
|
}
|