Class: Dommy::IntersectionObserver

Inherits:
Object
  • Object
show all
Includes:
Dommy::Internal::ObservableCallback
Defined in:
lib/dommy/intersection_observer.rb

Overview

‘IntersectionObserver` — stub for the viewport-intersection API. Dommy has no layout engine, so callbacks never fire automatically. Tests can drive callbacks explicitly via `__trigger__(entries)`.

Spec: w3c.github.io/IntersectionObserver/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callback, options = nil) ⇒ IntersectionObserver

Returns a new instance of IntersectionObserver.



14
15
16
17
18
19
20
21
22
# File 'lib/dommy/intersection_observer.rb', line 14

def initialize(callback, options = nil)
  @callback = callback
  opts = options.is_a?(Hash) ? options : {}
  @root = opts["root"] || opts[:root]
  @root_margin = (opts["rootMargin"] || opts[:rootMargin] || "0px").to_s
  raw_thresholds = opts["threshold"] || opts[:threshold] || 0
  @thresholds = Array(raw_thresholds).map(&:to_f).sort.freeze
  @targets = []
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



12
13
14
# File 'lib/dommy/intersection_observer.rb', line 12

def callback
  @callback
end

#rootObject (readonly)

Returns the value of attribute root.



12
13
14
# File 'lib/dommy/intersection_observer.rb', line 12

def root
  @root
end

#root_marginObject (readonly)

Returns the value of attribute root_margin.



12
13
14
# File 'lib/dommy/intersection_observer.rb', line 12

def root_margin
  @root_margin
end

#thresholdsObject (readonly)

Returns the value of attribute thresholds.



12
13
14
# File 'lib/dommy/intersection_observer.rb', line 12

def thresholds
  @thresholds
end

Instance Method Details

#__js_call__(method, args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dommy/intersection_observer.rb', line 69

def __js_call__(method, args)
  case method
  when "observe"
    observe(args[0])
  when "unobserve"
    unobserve(args[0])
  when "disconnect"
    disconnect
  when "takeRecords"
    take_records
  end
end

#__js_get__(key) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/dommy/intersection_observer.rb', line 58

def __js_get__(key)
  case key
  when "root"
    @root
  when "rootMargin"
    @root_margin
  when "thresholds"
    @thresholds
  end
end

#__trigger__(entries) ⇒ Object

Test seam: invoke the callback with a synthetic entries list. Each entry is whatever shape the test wants (usually a Hash).



54
55
56
# File 'lib/dommy/intersection_observer.rb', line 54

def __trigger__(entries)
  invoke_callback(entries)
end

#disconnectObject



34
35
36
37
# File 'lib/dommy/intersection_observer.rb', line 34

def disconnect
  @targets.clear
  nil
end

#observe(target) ⇒ Object



24
25
26
27
# File 'lib/dommy/intersection_observer.rb', line 24

def observe(target)
  @targets << target unless @targets.include?(target)
  nil
end

#observed_targetsObject

Currently-observed targets. Useful for tests that want to assert “the controller registered the right elements”.



48
49
50
# File 'lib/dommy/intersection_observer.rb', line 48

def observed_targets
  @targets.dup
end

#take_recordsObject Also known as: takeRecords



39
40
41
42
# File 'lib/dommy/intersection_observer.rb', line 39

def take_records
  # No queued records in stub mode.
  []
end

#unobserve(target) ⇒ Object



29
30
31
32
# File 'lib/dommy/intersection_observer.rb', line 29

def unobserve(target)
  @targets.delete(target)
  nil
end