IntersectionObserver.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Heap/Handle.h>
  8. #include <LibWeb/Bindings/PlatformObject.h>
  9. #include <LibWeb/IntersectionObserver/IntersectionObserverEntry.h>
  10. #include <LibWeb/PixelUnits.h>
  11. namespace Web::IntersectionObserver {
  12. struct IntersectionObserverInit {
  13. Optional<Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Document>>> root;
  14. String root_margin { "0px"_string };
  15. Variant<double, Vector<double>> threshold { 0 };
  16. };
  17. // https://www.w3.org/TR/intersection-observer/#intersectionobserverregistration
  18. struct IntersectionObserverRegistration {
  19. // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserverregistration-observer
  20. // [A]n observer property holding an IntersectionObserver.
  21. JS::NonnullGCPtr<IntersectionObserver> observer;
  22. // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserverregistration-observer
  23. // NOTE: Optional is used in place of the spec using -1 to indicate no previous index.
  24. // [A] previousThresholdIndex property holding a number between -1 and the length of the observer’s thresholds property (inclusive).
  25. Optional<size_t> previous_threshold_index;
  26. // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserverregistration-previousisintersecting
  27. // [A] previousIsIntersecting property holding a boolean.
  28. bool previous_is_intersecting { false };
  29. };
  30. // https://w3c.github.io/IntersectionObserver/#intersection-observer-interface
  31. class IntersectionObserver final : public Bindings::PlatformObject {
  32. WEB_PLATFORM_OBJECT(IntersectionObserver, Bindings::PlatformObject);
  33. JS_DECLARE_ALLOCATOR(IntersectionObserver);
  34. public:
  35. static WebIDL::ExceptionOr<JS::NonnullGCPtr<IntersectionObserver>> construct_impl(JS::Realm&, JS::GCPtr<WebIDL::CallbackType> callback, IntersectionObserverInit const& options = {});
  36. virtual ~IntersectionObserver() override;
  37. void observe(DOM::Element& target);
  38. void unobserve(DOM::Element& target);
  39. void disconnect();
  40. Vector<JS::Handle<IntersectionObserverEntry>> take_records();
  41. Vector<JS::NonnullGCPtr<DOM::Element>> const& observation_targets() const { return m_observation_targets; }
  42. Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Document>, Empty> root() const;
  43. Vector<double> const& thresholds() const { return m_thresholds; }
  44. Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Document>> intersection_root() const;
  45. CSSPixelRect root_intersection_rectangle() const;
  46. void queue_entry(Badge<DOM::Document>, JS::NonnullGCPtr<IntersectionObserverEntry>);
  47. WebIDL::CallbackType& callback() { return *m_callback; }
  48. private:
  49. explicit IntersectionObserver(JS::Realm&, JS::GCPtr<WebIDL::CallbackType> callback, Optional<Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Document>>> const& root, Vector<double>&& thresholds);
  50. virtual void initialize(JS::Realm&) override;
  51. virtual void visit_edges(JS::Cell::Visitor&) override;
  52. virtual void finalize() override;
  53. // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-callback-slot
  54. JS::GCPtr<WebIDL::CallbackType> m_callback;
  55. // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-root
  56. Optional<Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Document>>> m_root;
  57. // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-thresholds
  58. Vector<double> m_thresholds;
  59. // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-queuedentries-slot
  60. Vector<JS::NonnullGCPtr<IntersectionObserverEntry>> m_queued_entries;
  61. // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-observationtargets-slot
  62. Vector<JS::NonnullGCPtr<DOM::Element>> m_observation_targets;
  63. // AD-HOC: This is the document where we've registered the IntersectionObserver.
  64. WeakPtr<DOM::Document> m_document;
  65. };
  66. }