# See the Pagy documentation: https://ddnexus.github.io/pagy/extras/semantic
# encoding: utf-8
# frozen_string_literal: true
require 'pagy/extras/shared'
class Pagy
module Frontend
# Pagination for semantic-ui: it returns the html with the series of links to the pages
def pagy_semantic_nav(pagy)
link, p_prev, p_next = pagy_link_proc(pagy, 'class="item"'), pagy.prev, pagy.next
html = EMPTY + (p_prev ? %(#{link.call p_prev, '', 'aria-label="previous"'})
: %(
))
pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
html << if item.is_a?(Integer); %(#{link.call item}) # page link
elsif item.is_a?(String) ; %(#{item}) # current page
elsif item == :gap ; %(...
) # page gap
end
end
html << (p_next ? %(#{link.call p_next, '', 'aria-label="next"'})
: %(
))
%()
end
# Compact pagination for semantic: it returns the html with the series of links to the pages
# we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
def pagy_semantic_compact_nav(pagy, id=pagy_id)
link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy, 'class="item"'), pagy.prev, pagy.next, pagy.page, pagy.pages
html = EMPTY + %(#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
end
# Responsive pagination for semantic: it returns the html with the series of links to the pages
# rendered by the Pagy.responsive javascript
def pagy_semantic_responsive_nav(pagy, id=pagy_id)
tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy, 'class="item"'), pagy.prev, pagy.next, pagy.responsive
tags['before'] = (p_prev ? %(#{link.call p_prev, '', 'aria-label="previous"'})
: %(
))
responsive[:items].each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
tags[item.to_s] = if item.is_a?(Integer); %(#{link.call item}) # page link
elsif item.is_a?(String) ; %(#{item}) # current page
elsif item == :gap ; %(...
) # page gap
end
end
tags['after'] = (p_next ? %(#{link.call p_next, '', 'aria-label="next"'})
: %(
))
script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
%(#{script})
end
end
end