Skip to content

Commit

Permalink
refactoring of pagy_json_tag to pagy_json_attr: from script tag to da…
Browse files Browse the repository at this point in the history
…ta attribute
  • Loading branch information
ddnexus committed May 12, 2021
1 parent 57e6f91 commit d217965
Show file tree
Hide file tree
Showing 30 changed files with 1,476 additions and 346 deletions.
8 changes: 4 additions & 4 deletions lib/javascripts/pagy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ Pagy.delay = 100
Pagy.init =
function(arg) {
var target = arg instanceof Event || arg === undefined ? document : arg,
jsonTags = target.getElementsByClassName('pagy-json')
for (var i = 0, len = jsonTags.length; i < len; i++) {
var args = JSON.parse(jsonTags[i].innerHTML),
elements = target.querySelectorAll('[data-pagy-json]')
for (var i = 0, len = elements.length; i < len; i++) {
var args = JSON.parse(elements[i].getAttribute('data-pagy-json')),
fname = args.shift()
args.unshift(jsonTags[i].previousSibling)
args.unshift(elements[i])
Pagy[fname].apply(null, args)
}
}
Expand Down
11 changes: 5 additions & 6 deletions lib/pagy/extras/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def pagy_bootstrap_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '',
'gap' => %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.gap'}</a></li>),
'after' => %(#{pagy_bootstrap_next_html pagy, link}</ul>) }

html = %(<nav#{p_id} class="pagy-njs pagy-bootstrap-nav-js" aria-label="pager"></nav>)
html << pagy_json_tag(pagy, :nav, tags, pagy.sequels(steps))
%(<nav#{p_id} class="pagy-njs pagy-bootstrap-nav-js" aria-label="pager" #{pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></nav>)
end

# Javascript combo pagination for bootstrap: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
Expand All @@ -48,7 +47,9 @@ def pagy_bootstrap_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extr
p_pages = pagy.pages
input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" class="text-primary" style="padding: 0; border: none; text-align: center; width: #{p_pages.to_s.length+1}rem;">)

%(<nav#{p_id} class="pagy-bootstrap-combo-nav-js pagination" aria-label="pager"><div class="btn-group" role="group">#{
%(<nav#{p_id} class="pagy-bootstrap-combo-nav-js pagination" aria-label="pager"><div class="btn-group" role="group" #{
pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)
}>#{
if (p_prev = pagy.prev)
link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous" class="prev btn btn-primary"'
else
Expand All @@ -60,9 +61,7 @@ def pagy_bootstrap_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extr
else
%(<a class="next btn btn-primary disabled" href="#">#{pagy_t 'pagy.nav.next' }</a>)
end
}</div></nav>#{
pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
})
}</div></nav>)
end

private
Expand Down
11 changes: 5 additions & 6 deletions lib/pagy/extras/bulma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def pagy_bulma_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '', ste
'gap' => %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.nav.gap' }</span></li>),
'after' => '</ul>' }

html = %(<nav#{p_id} class="pagy-njs pagy-bulma-nav-js pagination is-centered" aria-label="pagination"></nav>)
html << pagy_json_tag(pagy, :nav, tags, pagy.sequels(steps))
%(<nav#{p_id} class="pagy-njs pagy-bulma-nav-js pagination is-centered" aria-label="pagination" #{pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></nav>)
end

# Javascript combo pagination for Bulma: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
Expand All @@ -47,7 +46,9 @@ def pagy_bulma_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '
p_pages = pagy.pages
input = %(<input class="input" type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem; margin:0 0.3rem;">)

%(<nav#{p_id} class="pagy-bulma-combo-nav-js" aria-label="pagination"><div class="field is-grouped is-grouped-centered" role="group">#{
%(<nav#{p_id} class="pagy-bulma-combo-nav-js" aria-label="pagination"><div class="field is-grouped is-grouped-centered" role="group" #{
pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)
}>#{
if (p_prev = pagy.prev)
%(<p class="control">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'class="button" aria-label="previous page"'}</p>)
else
Expand All @@ -59,9 +60,7 @@ def pagy_bulma_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '
else
%(<p class="control"><a class="button" disabled>#{pagy_t 'pagy.nav.next'}</a></p>)
end
}</div></nav>#{
pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
})
}</div></nav>)
end

private
Expand Down
11 changes: 5 additions & 6 deletions lib/pagy/extras/foundation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def pagy_foundation_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: ''
'gap' => %(<li class="ellipsis gap" aria-hidden="true"></li>),
'after' => %(#{pagy_foundation_next_html pagy, link}</ul>) }

html = %(<nav#{p_id} class="pagy-njs pagy-foundation-nav-js" aria-label="Pagination"></nav>)
html << pagy_json_tag(pagy, :nav, tags, pagy.sequels(steps))
%(<nav#{p_id} class="pagy-njs pagy-foundation-nav-js" aria-label="Pagination" #{pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></nav>)
end

# Javascript combo pagination for Foundation: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
Expand All @@ -48,7 +47,9 @@ def pagy_foundation_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_ext
p_pages = pagy.pages
input = %(<input class="input-group-field cell shrink" type="number" min="1" max="#{p_pages}" value="#{p_page}" style="width: #{p_pages.to_s.length+1}rem; padding: 0 0.3rem; margin: 0 0.3rem;">)

%(<nav#{p_id} class="pagy-foundation-combo-nav-js" aria-label="Pagination"><div class="input-group">#{
%(<nav#{p_id} class="pagy-foundation-combo-nav-js" aria-label="Pagination"><div class="input-group" #{
pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)
}>#{
if (p_prev = pagy.prev)
link.call p_prev, pagy_t('pagy.nav.prev'), 'style="margin-bottom: 0" aria-label="previous" class="prev button primary"'
else
Expand All @@ -60,9 +61,7 @@ def pagy_foundation_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_ext
else
%(<a style="margin-bottom: 0" class="next button primary disabled" href="#">#{pagy_t 'pagy.nav.next'}</a>)
end
}</div></nav>#{
pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
})
}</div></nav>)
end

private
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/extras/items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ def pagy_items_selector_js(pagy, deprecated_id=nil, pagy_id: nil, item_name: nil
link = pagy_marked_link(pagy_link_proc(pagy, link_extra: link_extra))
p_vars[:items] = p_items # restore the items

html = %(<span#{p_id} class="pagy-items-selector-js">)
html = %(<span#{p_id} class="pagy-items-selector-js" #{pagy_json_attr pagy, :items_selector, pagy.from, link}>)
input = %(<input type="number" min="1" max="#{p_vars[:max_items]}" value="#{p_items}" style="padding: 0; text-align: center; width: #{p_items.to_s.length+1}rem;">)
html << pagy_t('pagy.items_selector_js', item_name: item_name || pagy_t(i18n_key || p_vars[:i18n_key], count: p_items),
items_input: input,
count: p_items)
html << %(</span>#{pagy_json_tag pagy, :items_selector, pagy.from, link})
html << %(</span>)
end

end
Expand Down
11 changes: 5 additions & 6 deletions lib/pagy/extras/materialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ def pagy_materialize_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '
'gap' => %(<li class="gap disabled"><a href="#">#{pagy_t 'pagy.nav.gap'}</a></li>),
'after' => %(#{pagy_materialize_next_html pagy, link}</ul>) }

html = %(<div#{p_id} class="pagy-njs pagy-materialize-nav-js" role="navigation" aria-label="pager"></div>)
html << pagy_json_tag(pagy, :nav, tags, pagy.sequels(steps))
%(<div#{p_id} class="pagy-njs pagy-materialize-nav-js" role="navigation" aria-label="pager" #{pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></div>)
end

# Javascript combo pagination for materialize: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
Expand All @@ -50,15 +49,15 @@ def pagy_materialize_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_ex
style = ' style="vertical-align: middle;"'
input = %(<input type="number" class="browser-default" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 2px; border: none; border-radius: 2px; text-align: center; width: #{p_pages.to_s.length+1}rem;">)

%(<div#{p_id} class="pagy-materialize-combo-nav-js pagination" role="navigation" aria-label="pager"><div class="pagy-compact-chip" role="group" style="height: 35px; border-radius: 18px; background: #e4e4e4; display: inline-block;"><ul class="pagination" style="margin: 0">#{
%(<div#{p_id} class="pagy-materialize-combo-nav-js pagination" role="navigation" aria-label="pager"><div class="pagy-compact-chip" role="group" style="height: 35px; border-radius: 18px; background: #e4e4e4; display: inline-block;"><ul class="pagination" style="margin: 0" #{
pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)
}>#{
pagy_materialize_prev_html pagy, link, style
}<div class="pagy-combo-input btn-flat" style="cursor: default; padding: 0px">#{
pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
}</div>#{
pagy_materialize_next_html pagy, link, style
}</ul></div>#{
pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
})
}</ul></div>)
end

private
Expand Down
11 changes: 5 additions & 6 deletions lib/pagy/extras/navs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def pagy_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '', steps: ni
'gap' => %(<span class="page gap">#{pagy_t 'pagy.nav.gap'}</span> ),
'after' => pagy_nav_next_html(pagy, link) }

html = %(<nav#{p_id} class="pagy-njs pagy-nav-js pagination" aria-label="pager"></nav>)
html << pagy_json_tag(pagy, :nav, tags, pagy.sequels(steps))
%(<nav#{p_id} class="pagy-njs pagy-nav-js pagination" aria-label="pager" #{pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></nav>)
end

# Javascript combo pagination: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
Expand All @@ -30,15 +29,15 @@ def pagy_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '')
p_pages = pagy.pages
input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem;">)

%(<nav#{p_id} class="pagy-combo-nav-js pagination" aria-label="pager">#{
%(<nav#{p_id} class="pagy-combo-nav-js pagination" aria-label="pager" #{
pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)
}>#{
pagy_nav_prev_html pagy, link
}<span class="pagy-combo-input" style="margin: 0 0.6rem;">#{
pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
}</span> #{
pagy_nav_next_html pagy, link
}</nav>#{
pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
})
}</nav>)
end

private
Expand Down
11 changes: 5 additions & 6 deletions lib/pagy/extras/semantic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def pagy_semantic_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '',
'gap' => %(<div class="disabled item">#{pagy_t('pagy.nav.gap')}</div>),
'after' => pagy_semantic_next_html(pagy, link) }

html = %(<div#{p_id} class="pagy-njs pagy-semantic-nav-js ui pagination menu" role="navigation"></div>)
html << pagy_json_tag(pagy, :nav, tags, pagy.sequels(steps))
%(<div#{p_id} class="pagy-njs pagy-semantic-nav-js ui pagination menu" role="navigation" #{pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></div>)
end

# Combo pagination for semantic: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
Expand All @@ -48,15 +47,15 @@ def pagy_semantic_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra
p_pages = pagy.pages
input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem; margin: 0 0.3rem">)

%(<div#{p_id} class="pagy-semantic-combo-nav-js ui compact menu" role="navigation">#{
%(<div#{p_id} class="pagy-semantic-combo-nav-js ui compact menu" role="navigation" #{
pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)
}>#{
pagy_semantic_prev_html pagy, link
}<div class="pagy-combo-input item">#{
pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
}</div> #{
pagy_semantic_next_html pagy, link
}</div>#{
pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
})
}</div>)
end

private
Expand Down
8 changes: 4 additions & 4 deletions lib/pagy/extras/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ module Frontend

if defined?(Oj)
# it returns a script tag with the JSON-serialized args generated with the faster oj gem
def pagy_json_tag(pagy, *args)
def pagy_json_attr(pagy, *args)
args << pagy.vars[:page_param] if pagy.vars[:enable_trim_extra]
%(<script type="application/json" class="pagy-json">#{Oj.dump(args, mode: :strict)}</script>)
%(data-pagy-json="#{Oj.dump(args, mode: :strict).gsub('"', '&quot;')}")
end
else
require 'json'
# it returns a script tag with the JSON-serialized args generated with the slower to_json
def pagy_json_tag(pagy, *args)
def pagy_json_attr(pagy, *args)
args << pagy.vars[:page_param] if pagy.vars[:enable_trim_extra]
%(<script type="application/json" class="pagy-json">#{args.to_json}</script>)
%(data-pagy-json="#{args.to_json.gsub('"', '&quot;')}")
end
end

Expand Down
11 changes: 5 additions & 6 deletions lib/pagy/extras/uikit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def pagy_uikit_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '', ste
'gap' => %(<li class="uk-disabled"><span>#{pagy_t 'pagy.nav.gap'}</span></li>),
'after' => pagy_uikit_next_html(pagy, link) }

html = %(<ul#{p_id} class="pagy-njs pagy-uikit-nav-js uk-pagination uk-flex-center"></ul>)
html << pagy_json_tag(pagy, :nav, tags, pagy.sequels(steps))
%(<ul#{p_id} class="pagy-njs pagy-uikit-nav-js uk-pagination uk-flex-center" #{pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></ul>)
end

# Javascript combo pagination for uikit: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
Expand All @@ -47,7 +46,9 @@ def pagy_uikit_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '
p_pages = pagy.pages
input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" class="uk-input" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem;">)

%(<div#{p_id} class="pagy-uikit-combo-nav-js uk-button-group">#{
%(<div#{p_id} class="pagy-uikit-combo-nav-js uk-button-group" #{
pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)
}>#{
if (p_prev = pagy.prev)
link.call p_prev, pagy_t('pagy.nav.prev'), 'class="uk-button uk-button-default"'
else
Expand All @@ -61,9 +62,7 @@ def pagy_uikit_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '
else
%(<button class="uk-button uk-button-default" disabled>#{pagy_t 'pagy.nav.next'}</button>)
end
}</div>#{
pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
})
}</div>)
end


Expand Down
Loading

0 comments on commit d217965

Please sign in to comment.