Skip to content

Commit

Permalink
simplified support extra (dropped pagy_serialized and pagy_apply_init…
Browse files Browse the repository at this point in the history
…_tag)
  • Loading branch information
ddnexus committed Apr 25, 2019
1 parent f82b2e7 commit 2548557
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 123 deletions.
6 changes: 2 additions & 4 deletions docs/extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ A few helpers use javascript:
- `pagy_*_compact_nav`
- `pagy_*_responsive_nav`
- `pagy_items_selector`
- `pagy_apply_init_tag`

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 Expand Up @@ -103,11 +102,10 @@ import '../src/javascripts/pagy.js.erb'
**Notice**:

- You may want to use `turbolinks:load` if your app uses turbolinks despite webpacker
- or you may want just `export { Pagy, PagyInit }` from the `pagy.js.erb` file and import and use it somewhere else.
- You may want to expose the `Pagy` and `PagyInit` namespaces, if you need them available elsewhere (e.g. in js.erb templates):
- or you may want just `export { Pagy }` from the `pagy.js.erb` file and import and use it somewhere else.
- You may want to expose the `Pagy` namespace, if you need it available elsewhere (e.g. in js.erb templates):
```js
global.Pagy = Pagy
global.PagyInit = PagyInit
```

### In non-rails apps
Expand Down
38 changes: 0 additions & 38 deletions docs/extras/support.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ title: Support

This extra adds support for features like countless or navless pagination, where you don't need the full link bar but only a simple `next` or `prev` link or no link at all (as for [auto-incremental](#auto-incremental)).

It also provides a couple of helpers to setup you own custom javascript components.

## Synopsis

See [extras](../extras.md) for general usage info.
Expand Down Expand Up @@ -138,10 +136,6 @@ You may want to combine it with something like:
<%== pagy_next_link(@pagy, 'More...') %>
```

### Custom UIs

You can use the `pagy_prev_url` and `pagy_next_url` to build your own links or buttons. Besides, with the `pagy_apply_init_tag` and `pagy_serialized` helpers you can also setup you own custom javascript components.

## Methods

### pagy_prev_url(pagy)
Expand All @@ -163,35 +157,3 @@ Useful to build minimalistic helpers UIs that don't use nav bar links (e.g. `cou
Returns the link for the next page. It is the same next link string which is part of the `pagy_nav` helper.

Useful to build minimalistic helpers UIs that don't use nav bar links (e.g. `countless` extra).

### pagy_serialized(pagy)

Returns a hash with all the instance variables, series, prev_url and next_url of the `pagy` argument. Useful to use in some client-side javascript. It's the default payload of `pagy_apply_init_tag`.

### pagy_apply_init_tag(pagy, function, payload=...)

This is a multi-purpose helper that generates a JSON tag that will be loaded and exececuted by the `Pagy.init` javascript function at document load (see [Javascript](../extras.md#javascript)).

**Notice**: This method is useful to create your custom reusable component. For simpler need you can just use some generic javascript.

The method requires a pagy object, a javascript function name and accepts an optional payload (default to the hash returned by `pagy_serialized` method) that will get passed to the function. For example:

```ryby
# this uses the serialized pagy object as the default payload
pagy_apply_init_tag(@pagy, :myInitFunction)
# this uses a custom payload
pagy_apply_init_tag(@pagy, :myOtherFunction, {a: 1, b: 2})
```

You should define your functions in the `PagyInit` namespace, as follow, using the payload as needed:

```javascript
PagyInit.myInitFunction = function(payload){
console.log(payload);
doSomethingWith(payload);
...
}
```

You can use it to initialize your custom pagination elements. For auto-incremental pagination, you may want to update some client side variable with the `pagy_next_url` at each page load.
5 changes: 0 additions & 5 deletions lib/javascripts/pagy.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,3 @@ Pagy.init = function(arg){
Pagy[args.shift()].apply(null, args);
}
};

// namespace for custom init functions
function PagyInit(){}

Pagy.applyInit = function(name, payload){ PagyInit[name].apply(null, [payload]) };
26 changes: 0 additions & 26 deletions lib/pagy/extras/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,8 @@
# encoding: utf-8
# frozen_string_literal: true

require 'pagy/extras/shared'

class Pagy

def to_h
{ count: defined?(@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

module Frontend

def pagy_prev_url(pagy)
Expand All @@ -41,15 +24,6 @@ def pagy_next_link(pagy, text = pagy_t('pagy.nav.next'), link_extra = '')
: %(<span class="page next disabled">#{text}</span>)
end

def pagy_serialized(pagy)
pagy.to_h.merge(prev_url: pagy_prev_url(pagy), next_url: pagy_next_url(pagy))
end

# Multi purpose JSON tag for custom javascript initialization
def pagy_apply_init_tag(pagy, function, payload=pagy_serialized(pagy))
pagy_json_tag(:applyInit, function, payload)
end

end

end
50 changes: 0 additions & 50 deletions test/pagy/extras/support_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,55 +139,5 @@

end

describe "#pagy_serialized" do

it 'returns the serialized object for page 1' do
pagy = Pagy.new count: 1000, page: 1
pagy_countless = Pagy::Countless.new(page: 1).finalize(21)
frontend.pagy_serialized(pagy).must_equal({:count=>1000, :page=>1, :items=>20, :pages=>50, :last=>50, :offset=>0, :from=>1, :to=>20, :prev=>nil, :next=>2, :vars=>{:page=>1, :items=>20, :outset=>0, :size=>[1, 4, 4, 1], :page_param=>:page, :params=>{}, :anchor=>"", :link_extra=>"", :item_path=>"pagy.info.item_name", :cycle=>false, :breakpoints=>{0=>[1, 4, 4, 1]}, :count=>1000}, :series=>["1", 2, 3, 4, 5, :gap, 50], :prev_url=>nil, :next_url=>"/foo?page=2"})
frontend.pagy_serialized(pagy_countless).must_equal({:count=>nil, :page=>1, :items=>20, :pages=>2, :last=>2, :offset=>0, :from=>1, :to=>20, :prev=>nil, :next=>2, :vars=>{:page=>1, :items=>20, :outset=>0, :size=>[1, 4, 4, 1], :page_param=>:page, :params=>{}, :anchor=>"", :link_extra=>"", :item_path=>"pagy.info.item_name", :cycle=>false, :breakpoints=>{0=>[1, 4, 4, 1]}}, :series=>["1", 2], :prev_url=>nil, :next_url=>"/foo?page=2"})
end

it 'returns the serialized object for page 3' do
pagy = Pagy.new count: 1000, page: 3
pagy_countless = Pagy::Countless.new(page: 3).finalize(21)
frontend.pagy_serialized(pagy).must_equal({:count=>1000, :page=>3, :items=>20, :pages=>50, :last=>50, :offset=>40, :from=>41, :to=>60, :prev=>2, :next=>4, :vars=>{:page=>3, :items=>20, :outset=>0, :size=>[1, 4, 4, 1], :page_param=>:page, :params=>{}, :anchor=>"", :link_extra=>"", :item_path=>"pagy.info.item_name", :cycle=>false, :breakpoints=>{0=>[1, 4, 4, 1]}, :count=>1000}, :series=>[1, 2, "3", 4, 5, 6, 7, :gap, 50], :prev_url=>"/foo?page=2", :next_url=>"/foo?page=4"})
frontend.pagy_serialized(pagy_countless).must_equal({:count=>nil, :page=>3, :items=>20, :pages=>4, :last=>4, :offset=>40, :from=>41, :to=>60, :prev=>2, :next=>4, :vars=>{:page=>3, :items=>20, :outset=>0, :size=>[1, 4, 4, 1], :page_param=>:page, :params=>{}, :anchor=>"", :link_extra=>"", :item_path=>"pagy.info.item_name", :cycle=>false, :breakpoints=>{0=>[1, 4, 4, 1]}}, :series=>[1, 2, "3", 4], :prev_url=>"/foo?page=2", :next_url=>"/foo?page=4"})
end

it 'returns the serialized object for page 6' do
pagy = Pagy.new count: 1000, page: 6
pagy_countless = Pagy::Countless.new(page: 6).finalize(21)
frontend.pagy_serialized(pagy).must_equal({:count=>1000, :page=>6, :items=>20, :pages=>50, :last=>50, :offset=>100, :from=>101, :to=>120, :prev=>5, :next=>7, :vars=>{:page=>6, :items=>20, :outset=>0, :size=>[1, 4, 4, 1], :page_param=>:page, :params=>{}, :anchor=>"", :link_extra=>"", :item_path=>"pagy.info.item_name", :cycle=>false, :breakpoints=>{0=>[1, 4, 4, 1]}, :count=>1000}, :series=>[1, 2, 3, 4, 5, "6", 7, 8, 9, 10, :gap, 50], :prev_url=>"/foo?page=5", :next_url=>"/foo?page=7"})
frontend.pagy_serialized(pagy_countless).must_equal({:count=>nil, :page=>6, :items=>20, :pages=>7, :last=>7, :offset=>100, :from=>101, :to=>120, :prev=>5, :next=>7, :vars=>{:page=>6, :items=>20, :outset=>0, :size=>[1, 4, 4, 1], :page_param=>:page, :params=>{}, :anchor=>"", :link_extra=>"", :item_path=>"pagy.info.item_name", :cycle=>false, :breakpoints=>{0=>[1, 4, 4, 1]}}, :series=>[1, 2, 3, 4, 5, "6", 7], :prev_url=>"/foo?page=5", :next_url=>"/foo?page=7"})
end

it 'returns the serialized object for last page' do
pagy = Pagy.new count: 1000, page: 50
pagy_countless = Pagy::Countless.new(page: 50).finalize(20)
frontend.pagy_serialized(pagy).must_equal({:count=>1000, :page=>50, :items=>20, :pages=>50, :last=>50, :offset=>980, :from=>981, :to=>1000, :prev=>49, :next=>nil, :vars=>{:page=>50, :items=>20, :outset=>0, :size=>[1, 4, 4, 1], :page_param=>:page, :params=>{}, :anchor=>"", :link_extra=>"", :item_path=>"pagy.info.item_name", :cycle=>false, :breakpoints=>{0=>[1, 4, 4, 1]}, :count=>1000}, :series=>[1, :gap, 46, 47, 48, 49, "50"], :prev_url=>"/foo?page=49", :next_url=>nil})
frontend.pagy_serialized(pagy_countless).must_equal({:count=>nil, :page=>50, :items=>20, :pages=>50, :last=>50, :offset=>980, :from=>981, :to=>1000, :prev=>49, :next=>nil, :vars=>{:page=>50, :items=>20, :outset=>0, :size=>[1, 4, 4, 1], :page_param=>:page, :params=>{}, :anchor=>"", :link_extra=>"", :item_path=>"pagy.info.item_name", :cycle=>false, :breakpoints=>{0=>[1, 4, 4, 1]}}, :series=>[1, :gap, 46, 47, 48, 49, "50"], :prev_url=>"/foo?page=49", :next_url=>nil})
end

end

describe "#pagy_apply_init_tag" do

it 'renders the default apply-init tag for page 3' do
pagy = Pagy.new count: 1000, page: 3
pagy_countless = Pagy::Countless.new(page: 3).finalize(21)
frontend.pagy_apply_init_tag(pagy, :testFunction).must_equal "<script type=\"application/json\" class=\"pagy-json\">[\"applyInit\",\"testFunction\",{\"count\":1000,\"page\":3,\"items\":20,\"pages\":50,\"last\":50,\"offset\":40,\"from\":41,\"to\":60,\"prev\":2,\"next\":4,\"vars\":{\"page\":3,\"items\":20,\"outset\":0,\"size\":[1,4,4,1],\"page_param\":\"page\",\"params\":{},\"anchor\":\"\",\"link_extra\":\"\",\"item_path\":\"pagy.info.item_name\",\"cycle\":false,\"breakpoints\":{\"0\":[1,4,4,1]},\"count\":1000},\"series\":[1,2,\"3\",4,5,6,7,\"gap\",50],\"prev_url\":\"/foo?page=2\",\"next_url\":\"/foo?page=4\"}]</script>"
frontend.pagy_apply_init_tag(pagy_countless, :testFunction).must_equal "<script type=\"application/json\" class=\"pagy-json\">[\"applyInit\",\"testFunction\",{\"count\":null,\"page\":3,\"items\":20,\"pages\":4,\"last\":4,\"offset\":40,\"from\":41,\"to\":60,\"prev\":2,\"next\":4,\"vars\":{\"page\":3,\"items\":20,\"outset\":0,\"size\":[1,4,4,1],\"page_param\":\"page\",\"params\":{},\"anchor\":\"\",\"link_extra\":\"\",\"item_path\":\"pagy.info.item_name\",\"cycle\":false,\"breakpoints\":{\"0\":[1,4,4,1]}},\"series\":[1,2,\"3\",4],\"prev_url\":\"/foo?page=2\",\"next_url\":\"/foo?page=4\"}]</script>"
end

it 'renders the apply-init tag for page 3' do
pagy = Pagy.new count: 1000, page: 3
pagy_countless = Pagy::Countless.new(page: 3).finalize(21)
frontend.pagy_apply_init_tag(pagy, :testFunction, {a: 1}).must_equal "<script type=\"application/json\" class=\"pagy-json\">[\"applyInit\",\"testFunction\",{\"a\":1}]</script>"
frontend.pagy_apply_init_tag(pagy_countless, :testFunction, {a: 1}).must_equal "<script type=\"application/json\" class=\"pagy-json\">[\"applyInit\",\"testFunction\",{\"a\":1}]</script>"
end

end

end

0 comments on commit 2548557

Please sign in to comment.