Skip to content

Commit

Permalink
renamed items_selector > items_selector_js
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Apr 25, 2019
1 parent d4be9dd commit 25470dd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 30 deletions.
6 changes: 3 additions & 3 deletions docs/extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ A few extras require the [pagy/extras/shared](https://github.com/ddnexus/pagy/bl

## Javascript

A few helpers use javascript:
A few helpers use javascript, and they are clearly recognizable by the `js` suffix:

- `pagy_*_compact_nav_js`
- `pagy_*_nav_js`
- `pagy_items_selector`
- `pagy_*_compact_nav_js`
- `pagy_items_selector_js`

If you use any of them you should load the [pagy.js](https://github.com/ddnexus/pagy/blob/master/lib/javascripts/pagy.js) file, and run `Pagy.init()` on window load and eventually on [AJAX-load](#using-ajax-with-javascript-enabled-helpers).

Expand Down
4 changes: 2 additions & 2 deletions docs/extras/items.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Pagy::VARS[:items_param] = :custom_param # default :items
Pagy::VARS[:max_items] = 100 # default
```

Configure [javascript](../extras.md#javascript) (only if you use the `pagy_items_selector` UI)
Configure [javascript](../extras.md#javascript) (only if you use the `pagy_items_selector_js` UI)

## Files

Expand Down Expand Up @@ -77,7 +77,7 @@ This extra overrides the `pagy_countless_get_vars` method of the `Pagy::Backend`

This extra overrides also the `pagy_url_for` method of the `Pagy::Frontend` module in order to add the `:items_param` param to the url of the links.

### pagy_items_selector(pagy)
### pagy_items_selector_js(pagy)

This helper provides an items selector UI, which allows the user to select any arbitrary number of items per page (below the `:max_items` number) in a numeric input field. It looks like:

Expand Down
32 changes: 16 additions & 16 deletions lib/javascripts/pagy.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ Pagy.compact_nav = function(id, marker, page, trim){
Pagy.addInputEventListeners(input, go);
};

Pagy.items = function(id, marker, from){
var pagyEl = document.getElementById(id),
input = pagyEl.getElementsByTagName('input')[0],
current = input.value,
link = pagyEl.getElementsByTagName('a')[0],
go = function(){
var items = input.value;
if (current !== items) {
var page = Math.max(Math.ceil(from / items),1);
var href = link.getAttribute('href').replace(marker+'-page-', page).replace(marker+'-items-', items);
link.setAttribute('href', href);
link.click();
}
};
Pagy.addInputEventListeners(input, go);
};
Pagy.items_selector = function(id, marker, from){
var pagyEl = document.getElementById(id),
input = pagyEl.getElementsByTagName('input')[0],
current = input.value,
link = pagyEl.getElementsByTagName('a')[0],
go = function(){
var items = input.value;
if (current !== items) {
var page = Math.max(Math.ceil(from / items),1);
var href = link.getAttribute('href').replace(marker+'-page-', page).replace(marker+'-items-', items);
link.setAttribute('href', href);
link.click();
}
};
Pagy.addInputEventListeners(input, go);
};

Pagy.nav = function(id, marker, tags, series){
var pagyEl = document.getElementById(id),
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 @@ -46,14 +46,14 @@ def pagy_url_for_with_items(page, pagy, url=false)
alias_method :pagy_url_for, :pagy_url_for_with_items

# Return the items selector HTML. For example "Show [20] items per page"
def pagy_items_selector(pagy, id=pagy_id)
def pagy_items_selector_js(pagy, id=pagy_id)
p_vars = pagy.vars; p_items = p_vars[:items]; p_vars[:items] = "#{MARKER}-items-"

html = %(<span id="#{id}">) + %(<a href="#{pagy_url_for("#{MARKER}-page-", pagy)}"></a>)
p_vars[:items] = p_items # restore the items
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', items_input: input, count: p_items)})
html << %(</span>#{pagy_json_tag(:items, id, MARKER, pagy.from)})
html << %(</span>#{pagy_json_tag(:items_selector, id, MARKER, pagy.from)})
end

end
Expand Down
10 changes: 3 additions & 7 deletions test/pagy/extras/items_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,14 @@

end

describe '#pagy_items_selector' do
describe '#pagy_items_selector_js' do

it 'renders items selector' do
@pagy = Pagy.new count: 1000, page: 3
html = frontend.pagy_items_selector(@pagy, 'test-id')
html = frontend.pagy_items_selector_js(@pagy, 'test-id')

html.must_equal \
%(<span id="test-id">) +
%(<a href="/foo?page=#{Pagy::Frontend::MARKER}-page-&items=#{Pagy::Frontend::MARKER}-items-"></a>) +
%(Show <input type="number" min="1" max="100" value="20" style="padding: 0; text-align: center; width: 3rem;"> items per page) +
%(</span>) +
%(<script type="application/json" class="pagy-json">["items","test-id","#{Pagy::Frontend::MARKER}",41]</script>)
"<span id=\"test-id\"><a href=\"/foo?page=#{Pagy::Frontend::MARKER}-page-&items=#{Pagy::Frontend::MARKER}-items-\"></a>Show <input type=\"number\" min=\"1\" max=\"100\" value=\"20\" style=\"padding: 0; text-align: center; width: 3rem;\"> items per page</span><script type=\"application/json\" class=\"pagy-json\">[\"items_selector\",\"test-id\",\"#{Pagy::Frontend::MARKER}\",41]</script>"
end

end
Expand Down

0 comments on commit 25470dd

Please sign in to comment.