class Pagy
def self.root; Pathname.new(__FILE__).dirname end
def self.root; Pathname.new(__FILE__).dirname end
def initialize(vars)
def initialize(vars) @vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars { count:0, items:1, outset:0, page:1 }.each do |k,min| # validate instance variables (@vars[k] && instance_variable_set(:"@#{k}", @vars[k].to_i) >= min) \ or raise(ArgumentError, "expected :#{k} >= #{min}; got #{@vars[k].inspect}") end @pages = @last = [(@count.to_f / @items).ceil, 1].max # cardinal and ordinal meanings @page <= @last or raise(OverflowError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}") @offset = @items * (@page - 1) + @outset # pagination offset + outset (initial offset) @items = @count - ((@pages-1) * @items) if @page == @last && @count > 0 # adjust items for last non-empty page @from = @count == 0 ? 0 : @offset+1 - @outset # page begins from item @to = @count == 0 ? 0 : @offset + @items - @outset # page ends to item @prev = (@page-1 unless @page == 1) # nil if no prev page @next = @page == @last ? (1 if @vars[:cycle]) : @page+1 # nil if no next page, 1 if :cycle end
def overflow?; !!@overflow end
def overflow?; !!@overflow end
def responsive
:series is the hash of the series keyed by width (used by the *_responsive helpers to create the JSON string)
where :items is the unordered array union of all the page numbers for all sizes (passed to the PagyResponsive javascript function)
:widths => [550, 350, 0] }
550 =>[1, 2, 3, :gap, 16, 17, 18, 19, "20", 21, 22, 23, 24, :gap, 48, 49, 50] },
350 =>[1, 2, :gap, 17, 18, 19, "20", 21, 22, 23, :gap, 49, 50],
:series => { 0 =>[1, :gap, 18, 19, "20", 21, 22, :gap, 50],
{ :items => [1, :gap, 18, 19, "20", 21, 22, 50, 2, 17, 23, 49, 3, 16, 24, 48],
it returns something like:
Pagy.new count:1000, page: 20, breakpoints: {0 => [1,2,2,1], 350 => [2,3,3,2], 550 => [3,4,4,3]}
with an object like:
Helper for building the page_nav with javascript. For example:
def responsive @responsive ||= {items: [], series: {}, widths:[]}.tap do |r| @vars[:breakpoints].key?(0) || raise(ArgumentError, "expected :breakpoints to contain the 0 size; got #{@vars[:breakpoints].inspect}") @vars[:breakpoints].each {|width, size| r[:items] |= r[:series][width] = series(size)} r[:widths] = r[:series].keys.sort!{|a,b| b <=> a} end end
def series(size=@vars[:size])
def series(size=@vars[:size]) (series = []) and size.empty? and return series 4.times{|i| (size[i]>=0 rescue nil) or raise(ArgumentError, "expected 4 items >= 0 in :size; got #{size.inspect}")} [*0..size[0], *@page-size[1]..@page+size[2], *@last-size[3]+1..@last+1].sort!.each_cons(2) do |a, b| if a<0 || a==b || a>@last # skip out of range and duplicates elsif a+1 == b; series.push(a) # no gap -> no additions elsif a+2 == b; series.push(a, a+1) # 1 page gap -> fill with missing page else series.push(a, :gap) # n page gap -> add :gap end # skip the end boundary (last+1) end # shift the start boundary (0) and series.tap{|s| s.shift; s[s.index(@page)] = @page.to_s} # convert the current page to String end
def to_h
def to_h { count: @count, page: @page, items: @items, pages: @pages, last: @last, offset: @offset, from: @from, to: @to, prev: @prev, next: @next, vars: @vars, series: series } end