Class: Dommy::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/router.rb

Overview

‘window.location` polyfill. The Window owns one Location and one History instance, and they share the same underlying state. Hash / pushState / replaceState all flow through `set_url`.

Instance Method Summary collapse

Constructor Details

#initialize(window, origin: "http://localhost", pathname: "/", search: "", hash: "") ⇒ Location

Returns a new instance of Location.



10
11
12
13
14
15
16
# File 'lib/dommy/router.rb', line 10

def initialize(window, origin: "http://localhost", pathname: "/", search: "", hash: "")
  @window = window
  @origin = origin
  @pathname = pathname
  @search = search
  @hash = hash
end

Instance Method Details

#__js_call__(method, args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/dommy/router.rb', line 68

def __js_call__(method, args)
  case method
  when "assign", "replace"
    __set_url__(args[0].to_s)
  when "reload"
    nil
  when "toString"
    href
  end
end

#__js_get__(key) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dommy/router.rb', line 18

def __js_get__(key)
  case key
  when "origin"
    @origin
  when "pathname"
    @pathname
  when "search"
    @search
  when "hash"
    @hash
  when "href"
    href
  when "host"
    URI(@origin).host || ""
  when "hostname"
    URI(@origin).host || ""
  when "protocol"
    URI(@origin).scheme ? "#{URI(@origin).scheme}:" : ""
  when "port"
    (URI(@origin).port || 80).to_s
  end
end

#__js_set__(key, value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dommy/router.rb', line 41

def __js_set__(key, value)
  case key
  when "href"
    __set_url__(value.to_s)
  when "hash"
    new_hash = value.to_s
    new_hash = "##{new_hash}" unless new_hash.empty? || new_hash.start_with?("#")
    previous = @hash
    @hash = new_hash
    @window.fire_hashchange(previous, @hash) if previous != @hash
  when "pathname"
    @pathname = value.to_s
  when "search"
    s = value.to_s
    @search = s.empty? || s.start_with?("?") ? s : "?#{s}"
  when "host"
    # `host` is "hostname[:port]" — split and update origin.
    update_origin_host(value.to_s)
  when "hostname"
    update_origin_hostname(value.to_s)
  when "port"
    update_origin_port(value.to_s)
  when "protocol"
    update_origin_protocol(value.to_s)
  end
end

#__set_url__(raw) ⇒ Object

Internal — accepts an absolute or relative URL string and updates pathname / search / hash. Called by History pushState / replaceState and by ‘location.href = …`.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dommy/router.rb', line 86

def __set_url__(raw)
  previous_hash = @hash
  if raw.start_with?("#")
    @hash = raw
  else
    uri = URI.join(@origin + @pathname + @search + @hash, raw) rescue URI(raw)
    @pathname = uri.path.to_s == "" ? "/" : uri.path
    @search = uri.query ? "?#{uri.query}" : ""
    @hash = uri.fragment ? "##{uri.fragment}" : ""
  end

  @window.fire_hashchange(previous_hash, @hash) if previous_hash != @hash
end

#hrefObject



79
80
81
# File 'lib/dommy/router.rb', line 79

def href
  "#{@origin}#{@pathname}#{@search}#{@hash}"
end