Sha256: b22680245ea39f0774328364f4df2808c03b9705513b01d5ca5d2fef1ff0c064

Contents?: true

Size: 1.85 KB

Versions: 6

Compression:

Stored size: 1.85 KB

Contents

require 'browser/location'

module Browser

# {History} allows manipulation of the session history.
#
# @see https://developer.mozilla.org/en-US/docs/Web/API/History
class History
  # Check if HTML5 history is supported.
  def self.supported?
    Browser.supports? 'History'
  end

  include Browser::NativeCachedWrapper

  # @!attribute [r] length
  # @return [Integer] how many items are in the history
  alias_native :length

  # Go back in the history.
  #
  # @param number [Integer] how many items to go back
  def back(number = 1)
    `#@native.go(-number)`
  end

  # Go forward in the history.
  #
  # @param number [Integer] how many items to go forward
  def forward(number = 1)
    `#@native.go(number)`
  end

  # Push an item in the history.
  #
  # @param item [String] the item to push in the history
  # @param data [Object] additional state to push
  def push(item, data = nil)
    `#@native.pushState(#{data.to_n}, null, item)`
  end

  # Replace the current history item with another.
  #
  # @param item [String] the item to replace with
  # @param data [Object] additional state to replace
  def replace(item, data = nil)
    `#@native.replaceState(#{data.to_n}, null, item)`
  end

  # @!attribute [r] current
  # @return [String] the current item
  def current
    $window.location.full_path
  end

  # @!attribute [r] state
  # @return [Object] the current state
  if Browser.supports? 'History.state'
    def state
      %x{
        var state = #@native.state;

        if (state == null) {
          return nil;
        }
        else {
          return state;
        }
      }
    end
  else
    def state
      raise NotImplementedError, 'history state unsupported'
    end
  end
end

class Window
  # @!attribute [r] history
  # @return [History] the history for this window
  def history
    History.new(`#@native.history`) if `#@native.history`
  end
end

end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
opal-browser-0.3.4 opal/browser/history.rb
atome-opal-browser-0.3.9.5 opal/browser/history.rb
opal-browser-0.3.3 opal/browser/history.rb
opal-browser-0.3.2 opal/browser/history.rb
opal-browser-0.3.1 opal/browser/history.rb
opal-browser-0.3.0 opal/browser/history.rb