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
|
|
|
|
|
|
2022-09-13 17:42:39 +02:00
|
|
|
|
#include <LibJS/Heap/Handle.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 {
|
2022-08-28 13:42:07 +02:00
|
|
|
|
Optional<Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Document>>> root;
|
2023-07-06 23:44:07 +01:00
|
|
|
|
String root_margin { "0px"_short_string };
|
2022-04-02 00:44:16 +03:00
|
|
|
|
Variant<double, Vector<double>> threshold { 0 };
|
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.
|
|
|
|
|
JS::Handle<IntersectionObserver> observer;
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
2021-10-13 14:20:50 -04:00
|
|
|
|
public:
|
2023-07-06 23:44:07 +01:00
|
|
|
|
static WebIDL::ExceptionOr<JS::NonnullGCPtr<IntersectionObserver>> construct_impl(JS::Realm&, JS::GCPtr<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();
|
2023-07-06 23:44:07 +01:00
|
|
|
|
Vector<JS::Handle<IntersectionObserverEntry>> take_records();
|
|
|
|
|
|
|
|
|
|
Vector<JS::NonnullGCPtr<DOM::Element>> const& observation_targets() const { return m_observation_targets; }
|
|
|
|
|
|
|
|
|
|
Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Document>, Empty> root() const;
|
|
|
|
|
Vector<double> const& thresholds() const { return m_thresholds; }
|
|
|
|
|
|
|
|
|
|
Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Document>> intersection_root() const;
|
|
|
|
|
CSSPixelRect root_intersection_rectangle() const;
|
|
|
|
|
|
|
|
|
|
void queue_entry(Badge<DOM::Document>, JS::NonnullGCPtr<IntersectionObserverEntry>);
|
|
|
|
|
|
|
|
|
|
WebIDL::CallbackType& callback() { return *m_callback; }
|
2022-09-04 14:14:22 +02:00
|
|
|
|
|
|
|
|
|
private:
|
2023-07-06 23:44:07 +01:00
|
|
|
|
explicit IntersectionObserver(JS::Realm&, JS::GCPtr<WebIDL::CallbackType> callback, Optional<Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Document>>> const& root, Vector<double>&& thresholds);
|
2023-01-10 06:28:20 -05:00
|
|
|
|
|
2023-01-28 12:33:35 -05:00
|
|
|
|
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
|
2023-07-06 23:44:07 +01:00
|
|
|
|
virtual void visit_edges(JS::Cell::Visitor&) override;
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-callback-slot
|
|
|
|
|
JS::GCPtr<WebIDL::CallbackType> m_callback;
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-root
|
|
|
|
|
Optional<Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Document>>> m_root;
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-thresholds
|
|
|
|
|
Vector<double> m_thresholds;
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-queuedentries-slot
|
|
|
|
|
Vector<JS::NonnullGCPtr<IntersectionObserverEntry>> m_queued_entries;
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-observationtargets-slot
|
|
|
|
|
Vector<JS::NonnullGCPtr<DOM::Element>> m_observation_targets;
|
2021-10-13 14:20:50 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|