Skip to content

Commit

Permalink
replaced Pagy::Frontend::MARK with Pagy::PAGE_PLACEHOLDER (MARK is st…
Browse files Browse the repository at this point in the history
…ill defined for backward compatibility)
  • Loading branch information
ddnexus committed Oct 22, 2019
1 parent 439676e commit 7775155
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/pagy/extras/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def pagy_bootstrap_nav_js(pagy, id=pagy_id)
link, p_prev, p_next = pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next
tags = { 'before' => p_prev ? %(<ul class="pagination"><li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
: %(<ul class="pagination"><li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.prev')}</a></li>),
'link' => %(<li class="page-item">#{mark = link.call(MARK)}</li>),
'link' => %(<li class="page-item">#{mark = link.call(PAGE_PLACEHOLDER)}</li>),
'active' => %(<li class="page-item active">#{mark}</li>),
'gap' => %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.gap')}</a></li>),
'after' => p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li></ul>)
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/extras/bulma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def pagy_bulma_nav_js(pagy, id=pagy_id)
+ (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"')
: %(<a class="pagination-next" disabled>#{pagy_t('pagy.nav.next')}</a>)) \
+ '<ul class="pagination-list">' ),
'link' => %(<li>#{link.call(MARK, MARK, %(class="pagination-link" aria-label="goto page #{MARK}"))}</li>),
'active' => %(<li>#{link.call(MARK, MARK, %(class="pagination-link is-current" aria-current="page" aria-label="page #{MARK}"))}</li>),
'link' => %(<li>#{link.call(PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link" aria-label="goto page #{PAGE_PLACEHOLDER}"))}</li>),
'active' => %(<li>#{link.call(PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link is-current" aria-current="page" aria-label="page #{PAGE_PLACEHOLDER}"))}</li>),
'gap' => %(<li><span class="pagination-ellipsis">#{pagy_t('pagy.nav.gap')}</span></li>),
'after' => '</ul>' }
%(<nav id="#{id}" class="pagy-bulma-nav-js pagination is-centered" role="navigation" aria-label="pagination"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(TRIM) && pagy.vars[:page_param])})
Expand Down
2 changes: 1 addition & 1 deletion lib/pagy/extras/foundation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def pagy_foundation_nav_js(pagy, id=pagy_id)
tags = { 'before' => ( '<ul class="pagination">' \
+ (p_prev ? %(<li class="prev">#{link.call(p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"')}</li>)
: %(<li class="prev disabled">#{pagy_t('pagy.nav.prev')}</li>)) ),
'link' => %(<li>#{link.call(MARK)}</li>),
'link' => %(<li>#{link.call(PAGE_PLACEHOLDER)}</li>),
'active' => %(<li class="current">#{pagy.page}</li>),
'gap' => %(<li class="ellipsis gap" aria-hidden="true"></li>),
'after' => ( (p_next ? %(<li class="next">#{link.call(p_next, pagy_t('pagy.nav.next'), 'aria-label="next"')}</li>)
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/extras/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def pagy_headers(pagy)
def pagy_headers_hash(pagy)
countless = defined?(Pagy::Countless) && pagy.is_a?(Pagy::Countless)
rels = { 'first' => 1, 'prev' => pagy.prev, 'next' => pagy.next }; rels['last'] = pagy.last unless countless
url_str = pagy_url_for(Frontend::MARK, pagy, :url)
hash = { 'Link' => Hash[rels.map{|rel, n|[rel, url_str.sub(Frontend::MARK, n.to_s)] if n}.compact] }
url_str = pagy_url_for(PAGE_PLACEHOLDER, pagy, :url)
hash = { 'Link' => Hash[rels.map{|rel, n|[rel, url_str.sub(PAGE_PLACEHOLDER, n.to_s)] if n}.compact] }
headers = pagy.vars[:headers]
hash[headers[:page]] = pagy.page if headers[:page]
hash[headers[:items]] = pagy.vars[:items] if headers[:items]
Expand Down
2 changes: 1 addition & 1 deletion lib/pagy/extras/materialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def pagy_materialize_nav_js(pagy, id=pagy_id)
tags = { 'before' => ( '<ul class="pagination">' \
+ (p_prev ? %(<li class="waves-effect prev">#{link.call(p_prev, '<i class="material-icons">chevron_left</i>', 'aria-label="previous"')}</li>)
: %(<li class="prev disabled"><a href="#"><i class="material-icons">chevron_left</i></a></li>)) ),
'link' => %(<li class="waves-effect">#{mark = link.call(MARK)}</li>),
'link' => %(<li class="waves-effect">#{mark = link.call(PAGE_PLACEHOLDER)}</li>),
'active' => %(<li class="active">#{mark}</li>),
'gap' => %(<li class="gap disabled"><a href="#">#{pagy_t('pagy.nav.gap')}</a></li>),
'after' => ( (p_next ? %(<li class="waves-effect next">#{link.call(p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"')}</li>)
Expand Down
12 changes: 6 additions & 6 deletions lib/pagy/extras/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ module Backend ; private
def pagy_metadata(pagy, url=false)
names = pagy.vars[:metadata]
(unknown = names - METADATA).empty? or raise(VariableError.new(pagy), "unknown metadata #{unknown.inspect}")
scaffold_url = pagy_url_for(Frontend::MARK, pagy, url)
scaffold_url = pagy_url_for(PAGE_PLACEHOLDER, pagy, url)
metadata = {}
names.each do |key|
metadata[key] = case key
when :scaffold_url ; scaffold_url
when :first_url ; scaffold_url.sub(Frontend::MARK, 1.to_s)
when :prev_url ; scaffold_url.sub(Frontend::MARK, pagy.prev.to_s)
when :page_url ; scaffold_url.sub(Frontend::MARK, pagy.page.to_s)
when :next_url ; scaffold_url.sub(Frontend::MARK, pagy.next.to_s)
when :last_url ; scaffold_url.sub(Frontend::MARK, pagy.last.to_s)
when :first_url ; scaffold_url.sub(PAGE_PLACEHOLDER, 1.to_s)
when :prev_url ; scaffold_url.sub(PAGE_PLACEHOLDER, pagy.prev.to_s)
when :page_url ; scaffold_url.sub(PAGE_PLACEHOLDER, pagy.page.to_s)
when :next_url ; scaffold_url.sub(PAGE_PLACEHOLDER, pagy.next.to_s)
when :last_url ; scaffold_url.sub(PAGE_PLACEHOLDER, pagy.last.to_s)
else pagy.send(key)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pagy/extras/navs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def pagy_nav_js(pagy, id=pagy_id)
link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
tags = { 'before' => p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
: %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ),
'link' => %(<span class="page">#{link.call(MARK)}</span> ),
'link' => %(<span class="page">#{link.call(PAGE_PLACEHOLDER)}</span> ),
'active' => %(<span class="page active">#{pagy.page}</span> ),
'gap' => %(<span class="page gap">#{pagy_t('pagy.nav.gap')}</span> ),
'after' => p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
Expand Down
2 changes: 1 addition & 1 deletion lib/pagy/extras/semantic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def pagy_semantic_nav_js(pagy, id=pagy_id)
link, p_prev, p_next = pagy_link_proc(pagy, 'class="item"'), pagy.prev, pagy.next
tags = { 'before' => (p_prev ? %(#{link.call(p_prev, '<i class="left small chevron icon"></i>', 'aria-label="previous"')})
: %(<div class="item disabled"><i class="left small chevron icon"></i></div>)),
'link' => %(#{link.call(MARK)}),
'link' => %(#{link.call(PAGE_PLACEHOLDER)}),
'active' => %(<a class="item active">#{pagy.page}</a>),
'gap' => %(<div class="disabled item">#{pagy_t('pagy.nav.gap')}</div>),
'after' => (p_next ? %(#{link.call(p_next, '<i class="right small chevron icon"></i>', 'aria-label="next"')})
Expand Down
2 changes: 1 addition & 1 deletion lib/pagy/extras/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def pagy_id

# it returns the marked link to used by pagy.js
def pagy_marked_link(link)
link.call(MARK, '', 'style="display: none;"')
link.call(PAGE_PLACEHOLDER, '', 'style="display: none;"')
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/extras/uikit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def pagy_uikit_nav_js(pagy, id=pagy_id)
next_span = "<span uk-pagination-next>#{pagy_t('pagy.nav.next')}</span>"
tags = { 'before' => p_prev ? %(<li>#{link.call p_prev, previous_span}</li>)
: %(<li class="uk-disabled"><a href="#">#{previous_span}</a></li>),
'link' => %(<li>#{link.call(MARK)}</li>),
'active' => %(<li class="uk-active"><span>#{MARK}</span></li>),
'link' => %(<li>#{link.call(PAGE_PLACEHOLDER)}</li>),
'active' => %(<li class="uk-active"><span>#{PAGE_PLACEHOLDER}</span></li>),
'gap' => %(<li class="uk-disabled"><span>#{pagy_t('pagy.nav.gap')}</span></li>),
'after' => p_next ? %(<li>#{link.call p_next, next_span}</li>)
: %(<li class="uk-disabled"><a href="#">#{next_span}</a></li>) }
Expand Down
6 changes: 4 additions & 2 deletions lib/pagy/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Pagy

PAGE_PLACEHOLDER = '__pagy_page__' # string used for search and replace, hardcoded also in the pagy.js file

# I18n static hash loaded at startup, used as default alternative to the i18n gem.
# see https://ddnexus.github.io/pagy/api/frontend#i18n
I18n = eval(Pagy.root.join('locales', 'utils', 'i18n.rb').read) #rubocop:disable Security/Eval
Expand All @@ -28,7 +30,7 @@ module Frontend
include Helpers

EMPTY = '' # EMPTY + 'string' is almost as fast as +'string' but is also 1.9 compatible
MARK = '__pagy_page__' # string used for search and replace, hardcoded also in the pagy.js file
MARK = PAGE_PLACEHOLDER # backward compatibility in case of helper-overriding in legacy apps

# Generic pagination: it returns the html with the series of links to the pages
def pagy_nav(pagy)
Expand Down Expand Up @@ -59,7 +61,7 @@ def pagy_info(pagy)
# Benchmarked on a 20 link nav: it is ~22x faster and uses ~18x less memory than rails' link_to
def pagy_link_proc(pagy, link_extra='')
p_prev, p_next = pagy.prev, pagy.next
a, b = %(<a href="#{pagy_url_for(MARK, pagy)}" #{pagy.vars[:link_extra]} #{link_extra}).split(MARK, 2)
a, b = %(<a href="#{pagy_url_for(PAGE_PLACEHOLDER, pagy)}" #{pagy.vars[:link_extra]} #{link_extra}).split(PAGE_PLACEHOLDER, 2)
lambda {|n, text=n, extra=''| "#{a}#{n}#{b}#{ if n == p_prev ; ' rel="prev"'
elsif n == p_next ; ' rel="next"'
else '' end } #{extra}>#{text}</a>"}
Expand Down

0 comments on commit 7775155

Please sign in to comment.