summaryrefslogtreecommitdiff
path: root/Libraries/LibWeb/DOM/EventTarget.h
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-03-18 15:22:31 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-18 17:13:22 +0100
commitf39e5352f05c86dfd499b18952692d0f97c36aac (patch)
treee3f9e0dade0d88cfd7c5172d3d2be98b728ca916 /Libraries/LibWeb/DOM/EventTarget.h
parent97674da502903a19dd0cabaabf39b0191f978a03 (diff)
downloadserenity-f39e5352f05c86dfd499b18952692d0f97c36aac.zip
LibWeb: Start working on DOM event support
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. :^)
Diffstat (limited to 'Libraries/LibWeb/DOM/EventTarget.h')
-rw-r--r--Libraries/LibWeb/DOM/EventTarget.h41
1 files changed, 41 insertions, 0 deletions
diff --git a/Libraries/LibWeb/DOM/EventTarget.h b/Libraries/LibWeb/DOM/EventTarget.h
new file mode 100644
index 0000000000..09d874621d
--- /dev/null
+++ b/Libraries/LibWeb/DOM/EventTarget.h
@@ -0,0 +1,41 @@
+#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;
+};
+
+}