Class: Dommy::History

Inherits:
Object
  • Object
show all
Includes:
Bridge::Methods
Defined in:
lib/dommy/history.rb

Overview

window.history polyfill. Stack-based; back/forward move the cursor. pushState appends; replaceState mutates the current entry. Each entry is { state:, url: }. Popstate fires when back / forward triggers a different cursor (not on pushState per spec).

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Constructor Details

#initialize(window, location) ⇒ History

Returns a new instance of History.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dommy/history.rb', line 9

def initialize(window, location)
  @window = window
  @location = location
  # Host (embedding session) seam: called with (:push | :replace |
  # :traverse, url) after each history operation, so a session can keep
  # its own navigation history and current URL in step with the page's
  # same-document entries (Turbo Drive's pushState navigations).
  # Each entry records the full href it navigated to, so back/forward can
  # restore Location to it (and fire popstate) — a restoration that
  # framework routers like Turbo's depend on to swap the cached snapshot.
  @stack = [{state: nil, url: @location.href}]
  @cursor = 0
  @scroll_restoration = "auto"
end

Instance Attribute Details

#__internal_on_change__Object

Returns the value of attribute internal_on_change.



53
54
55
# File 'lib/dommy/history.rb', line 53

def __internal_on_change__
  @__internal_on_change__
end

Instance Method Details

#__internal_go_to__(index) ⇒ Object



60
61
62
# File 'lib/dommy/history.rb', line 60

def __internal_go_to__(index)
  go(index - @cursor) unless index == @cursor
end

#__internal_index__Object

The current entry index / traversal API for the host session, used to mirror joint back/forward. go_to targets an absolute entry index (a previously observed internal_index); already there is a no-op, so no spurious popstate fires.



59
# File 'lib/dommy/history.rb', line 59

def __internal_index__ = @cursor

#__js_call__(method, args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dommy/history.rb', line 64

def __js_call__(method, args)
  case method
  when "pushState"
    push(args[0], args[2])
  when "replaceState"
    replace(args[0], args[2])
  when "back"
    go(-1)
  when "forward"
    go(1)
  when "go"
    go((args[0] || 0).to_i)
  end
end

#__js_get__(key) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dommy/history.rb', line 24

def __js_get__(key)
  case key
  when "length"
    @stack.size
  when "state"
    @stack[@cursor][:state]
  when "scrollRestoration"
    @scroll_restoration
  else
    Bridge::ABSENT
  end
end

#__js_set__(key, value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dommy/history.rb', line 37

def __js_set__(key, value)
  case key
  when "scrollRestoration"
    # Per spec, only "auto" and "manual" are accepted. Invalid
    # values silently retain the current value.
    v = value.to_s
    @scroll_restoration = v if %w[auto manual].include?(v)
  else
    return Bridge::UNHANDLED
  end

  nil
end