Class: Ruta::History

Inherits:
Object
  • Object
show all
Defined in:
lib/ruta/history.rb

Class Method Summary collapse

Class Method Details

.back(by = 1) ⇒ Object

move browser backwards

Parameters:

  • by (Integer) (defaults to: 1)

    the amount to go backwards by, defaults to 1



36
37
38
# File 'lib/ruta/history.rb', line 36

def back(by=1)
  `history.go(#{-by.to_i})`
end

.current(item) ⇒ Object

get current ‘item` from locaction

current items supported are:

* query
* fragment
* path
* url
* uri

Parameters:

  • item (Symbol)

    to get the current value of from the current location



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ruta/history.rb', line 56

def current item
  case item
  when :query
    `location.query`
  when :fragment
  `  location.fragment`
  when :path
    `location.pathname`
  when :url
    `location.href`
  when :uri
    `location.uri`
  end

end

.forward(by = 1) ⇒ Object

move browser forward

Parameters:

  • by (Integer) (defaults to: 1)

    the amount to go forwards by, defaults to 1



29
30
31
# File 'lib/ruta/history.rb', line 29

def forward(by=1)
  `history.go(#{by.to_i})`
end

.listen_for_on_before_loadObject

will turn on a listener for when the page move away from the current page(refresh or back), will present user with dialogue box



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ruta/history.rb', line 93

def listen_for_on_before_load
  `window.onbeforeunload = function (evt) {
    var message = 'Are you sure you want to leave?';
    if (typeof evt == 'undefined') {
     evt = window.event;
    }
    if (evt) {
     evt.returnValue = message;
    }
    return message;
  }`
end

.listen_for_popObject

will turn on a listener for when the forward and backward buttons on the browser are pressed



73
74
75
76
77
78
79
# File 'lib/ruta/history.rb', line 73

def listen_for_pop
  `window.onpopstate = function(event) {
    #{
        Router.find_and_execute(current :path)
    }
  }`
end

navigate browser to remote path

Parameters:

  • path (String)

    to navigate to



43
44
45
# File 'lib/ruta/history.rb', line 43

def navigate_to_remote path
  `location.href = #{path}`
end

.push(context, url, data, title = "") ⇒ Object

push new url to history

Parameters:

  • url (String)

    to be added to history

  • data (Hash)

    to be added to history

  • title (String) (defaults to: "")

    to be added to history, defaults to “”



12
13
14
15
# File 'lib/ruta/history.rb', line 12

def push(context,url,data,title="")
  url = Ruta.config.context_prefix ?  "/#{context}#{url}" : url
  `history.pushState(#{data.to_n}, #{title}, #{url})`
end

.replace(url, data, title = nil) ⇒ Object

replace current url in history

Parameters:

  • url (String)

    to replace current item in history

  • data (Hash)

    to replace current item in history

  • title (String) (defaults to: nil)

    to replace current item in history, defaults to “”



22
23
24
# File 'lib/ruta/history.rb', line 22

def replace(url,data,title=nil)
  `history.replaceState(#{data.to_n}, #{title}, #{url})`
end

.stop_listening_for_on_before_loadObject

will stop listening for when the page move away from the current page(refresh or back), will present user with dialogue box



87
88
89
# File 'lib/ruta/history.rb', line 87

def stop_listening_for_on_before_load
  `window.onbeforeunload = nil`
end

.stop_listening_for_popObject

will stop listening for when forward and backward buttons on the browser are pressed



82
83
84
# File 'lib/ruta/history.rb', line 82

def stop_listening_for_pop
  `window.onpopstate = nil`
end