mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 05:32:44 +00:00 
			
		
		
		
	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. :^)
This commit is contained in:
		
							parent
							
								
									97674da502
								
							
						
					
					
						commit
						f39e5352f0
					
				
					 17 changed files with 310 additions and 11 deletions
				
			
		
							
								
								
									
										33
									
								
								Libraries/LibWeb/Bindings/EventTargetWrapper.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								Libraries/LibWeb/Bindings/EventTargetWrapper.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,33 @@ | |||
| #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() | ||||
| { | ||||
| } | ||||
| 
 | ||||
| } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling