Sha256: 2f498d0927a01d4a6cf3a890da68c430b36090c9ef8eee2f91755a4ea0c560b3

Contents?: true

Size: 1.98 KB

Versions: 58

Compression:

Stored size: 1.98 KB

Contents

class Pager
  def self.setup
    new.setup
  end

  def initialize
    @sidebar = Element.find("#sidebar")
  end

  def setup
    on_left_right
    add_page_buttons
    on_prev_next
  end

  # When left and right clicked, go to next page based on the sidebar
  def on_left_right
    Document.on("keyup") do |e|
      case e.which
      when 37 # left
        goto_link("prev")
      when 39 # right
        goto_link("next")
      else
        # puts "something else pressed #{e.which}"
      end
    end
  end

  def add_page_buttons
    return unless Element.find("#sidebar").size > 0

    html =<<~EOL
      <div class="prev-next-buttons">
        <a id="prev" class="btn btn-basic">Back</a>
        <a id="next" class="btn btn-primary">Next Step</a>
        <p class="keyboard-tip">Pro tip: Use the <- and -> arrow keys to move back and forward.</p>
      </div>
    EOL

    fluid = Element.find(".container-fluid")
    fluid.append(html)
  end

  def on_prev_next
    next_link = Element.find("#next")
    next_link.on('click') do |e|
      goto_link("next")
    end
    prev_link = Element.find("#prev")
    prev_link.on('click') do |e|
      goto_link("prev")
    end
  end

  def goto_link(direction)
    links = sidebar_links
    current_link = find_current
    current_index = links.index(current_link)

    link = if direction == "next"
             last = current_index == links.size - 1
             i = last ? 0 : current_index + 1
             links.at(i)
           else # prev
             i = current_index-1
             links.at(i)
           end

    if link
      href = link.attr("href")
      $window.location.assign(href)
    end
  end

  @@sidebar_links = nil
  def sidebar_links
    return @@sidebar_links if @@sidebar_links
    @@sidebar_links = @sidebar.find(".content-nav a")
  end

  def find_current
    current_location = $window.location.path # `window.location.pathname`

    links = sidebar_links
    links.select do |l|
      l.attr("href") == current_location
    end.first
  end
end

Version data entries

58 entries across 58 versions & 1 rubygems

Version Path
kubes-0.9.3 docs/opal/pager.rb
kubes-0.9.2 docs/opal/pager.rb
kubes-0.9.1 docs/opal/pager.rb
kubes-0.9.0 docs/opal/pager.rb
kubes-0.8.10 docs/opal/pager.rb
kubes-0.8.9 docs/opal/pager.rb
kubes-0.8.8 docs/opal/pager.rb
kubes-0.8.7 docs/opal/pager.rb
kubes-0.8.6 docs/opal/pager.rb
kubes-0.8.5 docs/opal/pager.rb
kubes-0.8.4 docs/opal/pager.rb
kubes-0.8.3 docs/opal/pager.rb
kubes-0.8.2 docs/opal/pager.rb
kubes-0.8.1 docs/opal/pager.rb
kubes-0.8.0 docs/opal/pager.rb
kubes-0.7.10 docs/opal/pager.rb
kubes-0.7.9 docs/opal/pager.rb
kubes-0.7.8 docs/opal/pager.rb
kubes-0.7.7 docs/opal/pager.rb
kubes-0.7.6 docs/opal/pager.rb