class Ruta::History

Public Class Methods

back(by=1) click to toggle source

move browser backwards

@param [Integer] by the amount to go backwards by, defaults to 1

# File lib/ruta/history.rb, line 36
def back(by=1)
  `history.go(#{-by.to_i})`
end
current(item) click to toggle source

get current ‘item` from locaction

current items supported are:

* query
* fragment
* path
* url
* uri

@param [Symbol] item to get the current value of from the current location

# 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) click to toggle source

move browser forward

@param [Integer] by the amount to go forwards by, defaults to 1

# File lib/ruta/history.rb, line 29
def forward(by=1)
  `history.go(#{by.to_i})`
end
listen_for_on_before_load() click to toggle source

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

# 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_pop() click to toggle source

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

# File lib/ruta/history.rb, line 73
def listen_for_pop
  `window.onpopstate = function(event) {
    #{
        Router.find_and_execute(current :path)
    }
  }`
end
navigate_to_remote(path) click to toggle source

navigate browser to remote path

@param [String] path to navigate to

push(context,url,data,title="") click to toggle source

push new url to history

@param [String] url to be added to history @param [Hash] data to be added to history @param [String] title to be added to history, defaults to “”

# 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) click to toggle source

replace current url in history

@param [String] url to replace current item in history @param [Hash] data to replace current item in history @param [String] title to replace current item in history, defaults to “”

# 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_load() click to toggle source

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

# File lib/ruta/history.rb, line 87
def stop_listening_for_on_before_load
  `window.onbeforeunload = nil`
end
stop_listening_for_pop() click to toggle source

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

# File lib/ruta/history.rb, line 82
def stop_listening_for_pop
  `window.onpopstate = nil`
end