Sha256: f0595ee0254a680b911fa9faa005228f859b5f5fa8f54786943e3f859cc0d50a

Contents?: true

Size: 784 Bytes

Versions: 1

Compression:

Stored size: 784 Bytes

Contents

require 'opal-jquery'

module Vienna
  class HistoryRouter
    attr_reader :path, :routes

    def initialize(&block)
      @routes = []
      @location = $global.location

      Window.on(:popstate) { update }

      instance_eval(&block) if block
    end

    def route(path, &handler)
      route = Router::Route.new(path, &handler)
      @routes << route
      route
    end

    def update
      path = if @location.pathname.empty?
                '/'
              else
                @location.pathname
              end

      unless @path == path
        @path = path
        match @path
      end
    end

    def match(path)
      @routes.find { |r| r.match path }
    end

    def navigate(path)
      `history.pushState(null, null, path)`
      update
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
opal-vienna-0.7.0 opal/vienna/history_router.rb