Class: Dommy::IntersectionObserver
- Inherits:
-
Object
- Object
- Dommy::IntersectionObserver
- 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)`.
Instance Attribute Summary collapse
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#root_margin ⇒ Object
readonly
Returns the value of attribute root_margin.
-
#thresholds ⇒ Object
readonly
Returns the value of attribute thresholds.
Instance Method Summary collapse
- #__js_call__(method, args) ⇒ Object
- #__js_get__(key) ⇒ Object
-
#__trigger__(entries) ⇒ Object
Test seam: invoke the callback with a synthetic entries list.
- #disconnect ⇒ Object
-
#initialize(callback, options = nil) ⇒ IntersectionObserver
constructor
A new instance of IntersectionObserver.
- #observe(target) ⇒ Object
-
#observed_targets ⇒ Object
Currently-observed targets.
- #take_records ⇒ Object (also: #takeRecords)
- #unobserve(target) ⇒ Object
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, = nil) @callback = callback opts = .is_a?(Hash) ? : {} @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
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
12 13 14 |
# File 'lib/dommy/intersection_observer.rb', line 12 def callback @callback end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
12 13 14 |
# File 'lib/dommy/intersection_observer.rb', line 12 def root @root end |
#root_margin ⇒ Object (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 |
#thresholds ⇒ Object (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 |
#disconnect ⇒ Object
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_targets ⇒ Object
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_records ⇒ Object 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 |