Class: Dommy::History
- Inherits:
-
Object
- Object
- Dommy::History
- Defined in:
- lib/dommy/router.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.
143 144 145 146 147 148 149 150 151 |
# File 'lib/dommy/router.rb', line 143 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
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/dommy/router.rb', line 176 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
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/dommy/router.rb', line 153 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
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/dommy/router.rb', line 164 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 |