Class: Dommy::History
- Inherits:
-
Object
- Object
- Dommy::History
- 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 Method Summary collapse
- #__js_call__(method, args) ⇒ Object
- #__js_get__(key) ⇒ Object
- #__js_set__(key, value) ⇒ Object
-
#initialize(window, location) ⇒ History
constructor
A new instance of History.
Constructor Details
#initialize(window, location) ⇒ History
Returns a new instance of History.
9 10 11 12 13 14 15 16 17 |
# File 'lib/dommy/history.rb', line 9 def initialize(window, location) @window = window @location = location # Initial entry mirrors the live Location. Bookmark URL is # resynthesized lazily from Location each time we read it. @stack = [{state: nil, url: nil}] @cursor = 0 @scroll_restoration = "auto" end |
Instance Method Details
#__js_call__(method, args) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/dommy/history.rb', line 42 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
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dommy/history.rb', line 19 def __js_get__(key) case key when "length" @stack.size when "state" @stack[@cursor][:state] when "scrollRestoration" @scroll_restoration end end |
#__js_set__(key, value) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/dommy/history.rb', line 30 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) end nil end |