Class: URL

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

Overview

The url class handles parsing and updating the url

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ReactiveTags

included, #reactive_method_tag

Constructor Details

#initialize(router = nil) ⇒ URL

Returns a new instance of URL.



9
10
11
12
# File 'lib/volt/models/url.rb', line 9

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

Instance Attribute Details

#hostObject (readonly)

TODO: we need to make it so change events only trigger on changes



6
7
8
# File 'lib/volt/models/url.rb', line 6

def host
  @host
end

#paramsObject (readonly)

TODO: we need to make it so change events only trigger on changes



6
7
8
# File 'lib/volt/models/url.rb', line 6

def params
  @params
end

#pathObject (readonly)

TODO: we need to make it so change events only trigger on changes



6
7
8
# File 'lib/volt/models/url.rb', line 6

def path
  @path
end

#portObject (readonly)

TODO: we need to make it so change events only trigger on changes



6
7
8
# File 'lib/volt/models/url.rb', line 6

def port
  @port
end

#queryObject (readonly)

TODO: we need to make it so change events only trigger on changes



6
7
8
# File 'lib/volt/models/url.rb', line 6

def query
  @query
end

#routerObject

Returns the value of attribute router.



7
8
9
# File 'lib/volt/models/url.rb', line 7

def router
  @router
end

#schemeObject (readonly)

TODO: we need to make it so change events only trigger on changes



6
7
8
# File 'lib/volt/models/url.rb', line 6

def scheme
  @scheme
end

Instance Method Details

#full_urlObject

Full url rebuilds the url from it’s constituent parts



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/volt/models/url.rb', line 56

def full_url
  if @port
    host_with_port = "#{@host}:#{@port}"
  else
    host_with_port = @host
  end

  path, params = @router.url_for_params(@params)

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

  unless params.empty?
    new_url += '?'
    query_parts = []
    nested_params_hash(params).each_pair do |key,value|
      value = value.cur
      # remove the _ from the front
      value = `encodeURI(value)`
      query_parts << "#{key}=#{value}"
    end

    new_url += query_parts.join('&')
  end

  new_url += '#' + @fragment if @fragment

  return new_url
end

#parse(url) ⇒ Object



19
20
21
22
23
24
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
# File 'lib/volt/models/url.rb', line 19

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

    if url[0..3] != 'http'
      # Add the host for localized names
      url = "http://#{host}" + url
    else
      # Make sure its on the same host, otherwise its external.
      if url !~ /https?[:]\/\/#{host}/
        # Different host, don't process
        return false
      end
    end

    matcher = url.match(/^(https?)[:]\/\/([^\/]+)(.*)$/)
    @scheme = matcher[1]
    @host, @port = matcher[2].split(':')
    @port ||= 80

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

    assign_query_hash_to_params
  end

  scroll

  return true
end

#scrollObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/volt/models/url.rb', line 98

def scroll
  if Volt.client?
    if @fragment
      # Scroll to anchor via http://www.w3.org/html/wg/drafts/html/master/browsers.html#scroll-to-fragid
      %x{
        var anchor = $('#' + this.fragment);
        if (anchor.length == 0) {
          anchor = $('*[name="' + this.fragment + '"]:first');
        }
        console.log('found anchor: ', anchor);
        if (anchor && anchor.length > 0) {
          $(document.body).scrollTop(anchor.offset().top);
        }
      }
    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



88
89
90
91
92
93
94
95
96
# File 'lib/volt/models/url.rb', line 88

def update!
  if Volt.client?
    new_url = full_url()

    if `(document.location.href != new_url)`
      `history.pushState(null, null, new_url)`
    end
  end
end