| 
									
										
										
										
											2021-10-13 14:20:50 -04:00
										 |  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-01-31 13:07:22 -05:00
										 |  |  |  |  * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-10-13 14:20:50 -04:00
										 |  |  |  |  * | 
					
						
							|  |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #pragma once
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | #include <LibGC/Root.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-04 14:14:22 +02:00
										 |  |  |  | #include <LibWeb/Bindings/PlatformObject.h>
 | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  | #include <LibWeb/IntersectionObserver/IntersectionObserverEntry.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/PixelUnits.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-13 14:20:50 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace Web::IntersectionObserver { | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | struct IntersectionObserverInit { | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     Optional<Variant<GC::Root<DOM::Element>, GC::Root<DOM::Document>>> root; | 
					
						
							| 
									
										
										
										
											2023-08-07 22:26:17 -04:00
										 |  |  |  |     String root_margin { "0px"_string }; | 
					
						
							| 
									
										
										
										
											2024-11-22 12:01:42 +01:00
										 |  |  |  |     String scroll_margin { "0px"_string }; | 
					
						
							| 
									
										
										
										
											2022-04-02 00:44:16 +03:00
										 |  |  |  |     Variant<double, Vector<double>> threshold { 0 }; | 
					
						
							| 
									
										
										
										
											2024-11-22 12:01:42 +01:00
										 |  |  |  |     long delay = 0; | 
					
						
							|  |  |  |  |     bool track_visibility = false; | 
					
						
							| 
									
										
										
										
											2021-10-13 14:20:50 -04:00
										 |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  | // https://www.w3.org/TR/intersection-observer/#intersectionobserverregistration
 | 
					
						
							|  |  |  |  | struct IntersectionObserverRegistration { | 
					
						
							|  |  |  |  |     // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserverregistration-observer
 | 
					
						
							|  |  |  |  |     // [A]n observer property holding an IntersectionObserver.
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     GC::Ref<IntersectionObserver> observer; | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserverregistration-observer
 | 
					
						
							|  |  |  |  |     // NOTE: Optional is used in place of the spec using -1 to indicate no previous index.
 | 
					
						
							|  |  |  |  |     // [A] previousThresholdIndex property holding a number between -1 and the length of the observer’s thresholds property (inclusive).
 | 
					
						
							|  |  |  |  |     Optional<size_t> previous_threshold_index; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserverregistration-previousisintersecting
 | 
					
						
							|  |  |  |  |     // [A] previousIsIntersecting property holding a boolean.
 | 
					
						
							|  |  |  |  |     bool previous_is_intersecting { false }; | 
					
						
							|  |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-13 14:20:50 -04:00
										 |  |  |  | // https://w3c.github.io/IntersectionObserver/#intersection-observer-interface
 | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  | class IntersectionObserver final : public Bindings::PlatformObject { | 
					
						
							| 
									
										
										
										
											2022-09-04 14:14:22 +02:00
										 |  |  |  |     WEB_PLATFORM_OBJECT(IntersectionObserver, Bindings::PlatformObject); | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     GC_DECLARE_ALLOCATOR(IntersectionObserver); | 
					
						
							| 
									
										
										
										
											2022-09-04 14:14:22 +02:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-13 14:20:50 -04:00
										 |  |  |  | public: | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     static WebIDL::ExceptionOr<GC::Ref<IntersectionObserver>> construct_impl(JS::Realm&, GC::Ptr<WebIDL::CallbackType> callback, IntersectionObserverInit const& options = {}); | 
					
						
							| 
									
										
										
										
											2021-10-13 14:20:50 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-04 14:14:22 +02:00
										 |  |  |  |     virtual ~IntersectionObserver() override; | 
					
						
							| 
									
										
										
										
											2021-10-13 14:20:50 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     void observe(DOM::Element& target); | 
					
						
							|  |  |  |  |     void unobserve(DOM::Element& target); | 
					
						
							|  |  |  |  |     void disconnect(); | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     Vector<GC::Root<IntersectionObserverEntry>> take_records(); | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     Vector<GC::Ref<DOM::Element>> const& observation_targets() const { return m_observation_targets; } | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     Variant<GC::Root<DOM::Element>, GC::Root<DOM::Document>, Empty> root() const; | 
					
						
							| 
									
										
										
										
											2024-11-22 12:01:42 +01:00
										 |  |  |  |     String root_margin() const; | 
					
						
							|  |  |  |  |     String scroll_margin() const; | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  |     Vector<double> const& thresholds() const { return m_thresholds; } | 
					
						
							| 
									
										
										
										
											2024-11-22 12:01:42 +01:00
										 |  |  |  |     long delay() const { return m_delay; } | 
					
						
							|  |  |  |  |     bool track_visibility() const { return m_track_visibility; } | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     Variant<GC::Root<DOM::Element>, GC::Root<DOM::Document>> intersection_root() const; | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  |     CSSPixelRect root_intersection_rectangle() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     void queue_entry(Badge<DOM::Document>, GC::Ref<IntersectionObserverEntry>); | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     WebIDL::CallbackType& callback() { return *m_callback; } | 
					
						
							| 
									
										
										
										
											2022-09-04 14:14:22 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | private: | 
					
						
							| 
									
										
										
										
											2024-11-22 12:01:42 +01:00
										 |  |  |  |     explicit IntersectionObserver(JS::Realm&, GC::Ptr<WebIDL::CallbackType> callback, Optional<Variant<GC::Root<DOM::Element>, GC::Root<DOM::Document>>> const& root, Vector<CSS::LengthPercentage> root_margin, Vector<CSS::LengthPercentage> scroll_margin, Vector<double>&& thresholds, double debug, bool track_visibility); | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |  |     virtual void initialize(JS::Realm&) override; | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  |     virtual void visit_edges(JS::Cell::Visitor&) override; | 
					
						
							| 
									
										
										
										
											2023-08-09 21:08:52 +01:00
										 |  |  |  |     virtual void finalize() override; | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-22 12:01:42 +01:00
										 |  |  |  |     static Optional<Vector<CSS::LengthPercentage>> parse_a_margin(JS::Realm&, String); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  |     // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-callback-slot
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     GC::Ptr<WebIDL::CallbackType> m_callback; | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-root
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     GC::Ptr<DOM::Node> m_root; | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-22 12:01:42 +01:00
										 |  |  |  |     // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-rootmargin
 | 
					
						
							|  |  |  |  |     Vector<CSS::LengthPercentage> m_root_margin; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-scrollmargin
 | 
					
						
							|  |  |  |  |     Vector<CSS::LengthPercentage> m_scroll_margin; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  |     // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-thresholds
 | 
					
						
							|  |  |  |  |     Vector<double> m_thresholds; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-22 12:01:42 +01:00
										 |  |  |  |     // https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-delay
 | 
					
						
							|  |  |  |  |     long m_delay; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-trackvisibility
 | 
					
						
							|  |  |  |  |     bool m_track_visibility; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  |     // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-queuedentries-slot
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     Vector<GC::Ref<IntersectionObserverEntry>> m_queued_entries; | 
					
						
							| 
									
										
										
										
											2023-07-06 23:44:07 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-observationtargets-slot
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  |     Vector<GC::Ref<DOM::Element>> m_observation_targets; | 
					
						
							| 
									
										
										
										
											2023-11-22 23:57:34 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // AD-HOC: This is the document where we've registered the IntersectionObserver.
 | 
					
						
							|  |  |  |  |     WeakPtr<DOM::Document> m_document; | 
					
						
							| 
									
										
										
										
											2021-10-13 14:20:50 -04:00
										 |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | } |