Class: Volt::URL

Inherits:
Object show all
Includes:
ReactiveAccessors
Defined in:
lib/volt/models/url.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ReactiveAccessors

#__reactive_dependency_get, included

Constructor Details

#initialize(router = nil) ⇒ URL

Returns a new instance of URL.



14
15
16
17
# File 'lib/volt/models/url.rb', line 14

def initialize(router = nil)
  @router = router
  @location = Location.new
end

Instance Attribute Details

#routerObject

Returns the value of attribute router.



12
13
14
# File 'lib/volt/models/url.rb', line 12

def router
  @router
end

Instance Method Details

#paramsObject



19
20
21
# File 'lib/volt/models/url.rb', line 19

def params
  @params ||= Model.new({}, persistor: Persistors::Params)
end

#parse(url) ⇒ Object

Parse takes in a url and extracts each sections. It also assigns and changes to the params.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
# File 'lib/volt/models/url.rb', line 25

def parse(url)
  if url[0] == '#'
    # url only updates fragment
    self.fragment = url[1..-1]
    update!
  else
    host = location.host
    protocol = location.protocol

    if url !~ /[:]\/\//
      # Add the host for local urls
      url = protocol + "//#{host}" + url
    else
      # Make sure its on the same protocol and host, otherwise its external.
      if url !~ /#{protocol}\/\/#{host}/
        # Different host, don't process
        return false
      end
    end

    matcher         = url.match(/^(#{protocol[0..-2]})[:]\/\/([^\/]+)(.*)$/)
    self.scheme     = matcher[1]
    self.host, port = matcher[2].split(':')

    self.port = (port || 80).to_i

    path           = matcher[3]
    path, fragment = path.split('#', 2)
    path, query    = path.split('?', 2)

    self.path     = path
    self.fragment = fragment
    self.query    = query

    result = assign_query_hash_to_params
  end

  scroll

  result
end

#scrollObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/volt/models/url.rb', line 125

def scroll
  if Volt.in_browser?
    frag = fragment
    if frag.present?
      # Scroll to anchor via http://www.w3.org/html/wg/drafts/html/master/browsers.html#scroll-to-fragid
      # Sometimes the fragment will cause a jquery parsing error, so we
      # catch any exceptions.
      `
      try {
        var anchor = $('#' + frag);
        if (anchor.length == 0) {
          anchor = $('*[name="' + frag + '"]:first');
        }
        if (anchor && anchor.length > 0) {
          console.log('scroll to: ', anchor.offset().top);
          $(document.body).scrollTop(anchor.offset().top);
        }
      }
      catch(e) {}
    `
    else
      # Scroll to the top by default
      `$(document.body).scrollTop(0);`
    end
  end
end

#update!Object

Called when the state has changed and the url in the browser should be updated Called when an attribute changes to update the url



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/volt/models/url.rb', line 111

def update!
  if Volt.in_browser?
    new_url = url_for(params.to_h)

    # Push the new url if pushState is supported
    # TODO: add fragment fallback
    `
    if (document.location.href != new_url && history && history.pushState) {
      history.pushState(null, null, new_url);
    }
  `
  end
end

#url_for(params) ⇒ Object

Full url rebuilds the url from it’s constituent parts. The params passed in are used to generate the urls.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/volt/models/url.rb', line 69

def url_for(params)
  host_with_port = host
  host_with_port += ":#{port}" if port && port != 80

  path, params = @router.params_to_url(params)

  if path == nil
    raise "No route matched, make sure you have the base route defined last: `client '/', {}`"
  end

  new_url    = "#{scheme}://#{host_with_port}#{path.chomp('/')}"

  # Add query params
  params_str = ''
  unless params.empty?
    query_parts = []
    nested_params_hash(params).each_pair do |key, value|
      # remove the _ from the front
      value = Volt::Parsing.encodeURI(value)
      query_parts << "#{key}=#{value}"
    end

    if query_parts.size > 0
      query = query_parts.join('&')
      new_url += "?#{query}"
    end
  end

  # Add fragment
  frag    = fragment
  new_url += '#' + frag if frag.present?

  new_url
end

#url_with(passed_params) ⇒ Object



104
105
106
# File 'lib/volt/models/url.rb', line 104

def url_with(passed_params)
  url_for(params.to_h.merge(passed_params))
end