Class Wee::Pager
In: lib/wee/components/pager.rb
Parent: Wee::Component

Methods

Attributes

current_page  [R] 
entries_per_page  [RW] 
num_entries  [RW] 

Public Class methods

[Source]

# File lib/wee/components/pager.rb, line 5
  def initialize(num_entries=0)
    super()
    @num_entries = num_entries
    @current_page = 0
    @entries_per_page = 20
    yield self if block_given?
  end

Public Instance methods

Returns the index of the first entry on the current page

[Source]

# File lib/wee/components/pager.rb, line 22
  def current_start_index
    @current_page * @entries_per_page 
  end

Go to first page

[Source]

# File lib/wee/components/pager.rb, line 34
  def first
    goto(0)
  end

Go to page with index page Note that page-indices start with zero!

[Source]

# File lib/wee/components/pager.rb, line 59
  def goto(page)
    @current_page = page
    validate
  end

Go to last page

[Source]

# File lib/wee/components/pager.rb, line 40
  def last
    goto(last_page_index())
  end

Returns the index of the last page

[Source]

# File lib/wee/components/pager.rb, line 28
  def last_page_index
    num_pages() - 1
  end

Go to next page

[Source]

# File lib/wee/components/pager.rb, line 52
  def next
    goto(@current_page + 1)
  end

Returns the number of pages

[Source]

# File lib/wee/components/pager.rb, line 15
  def num_pages
    n, rest = @num_entries.divmod(@entries_per_page)
    if rest > 0 then n + 1 else n end
  end

Go to previous page

[Source]

# File lib/wee/components/pager.rb, line 46
  def prev
    goto(@current_page - 1)
  end

[Source]

# File lib/wee/components/pager.rb, line 64
  def render(r)
    return if num_pages() <= 0
    render_arrow(r, :first, "<<", "Go to first page"); r.space(2)
    render_arrow(r, :prev, "<", "Go to previous page"); r.space(2)
    render_index(r); r.space(2)
    render_arrow(r, :next, ">", "Go to next page"); r.space(2)
    render_arrow(r, :last, ">>", "Go to last page")
  end

Private Instance methods

[Source]

# File lib/wee/components/pager.rb, line 75
  def render_arrow(r, sym, text, tooltip=text)
    r.anchor.callback(&method(sym)).tooltip(tooltip).with { r.encode_text(text) }
  end

[Source]

# File lib/wee/components/pager.rb, line 79
  def render_index(r)
    last = last_page_index()
    (0 .. last).each do |i|
      if i == @current_page
        render_page_num(r, i, true)
      else
        render_page_num(r, i, false)
      end
      r.space if i < last
    end
  end

[Source]

# File lib/wee/components/pager.rb, line 91
  def render_page_num(r, num, current)
    if current
      r.bold(num+1)
    else
      r.anchor.callback{ goto(num) }.with(num+1)
    end
  end

[Source]

# File lib/wee/components/pager.rb, line 99
  def validate
    @current_page = [[0, @current_page].max, last_page_index()].min
  end

[Validate]