From 2548557b28f2dfd0b9e09c28d62cc47a30aea8cc Mon Sep 17 00:00:00 2001 From: Domizio Demichelis Date: Thu, 4 Apr 2019 11:40:18 +0700 Subject: [PATCH] simplified support extra (dropped pagy_serialized and pagy_apply_init_tag) --- docs/extras.md | 6 ++-- docs/extras/support.md | 38 ------------------------ lib/javascripts/pagy.js | 5 ---- lib/pagy/extras/support.rb | 26 ----------------- test/pagy/extras/support_test.rb | 50 -------------------------------- 5 files changed, 2 insertions(+), 123 deletions(-) diff --git a/docs/extras.md b/docs/extras.md index 476b46880..c69bdb030 100644 --- a/docs/extras.md +++ b/docs/extras.md @@ -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). @@ -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 diff --git a/docs/extras/support.md b/docs/extras/support.md index e59e58278..e4b1cb974 100644 --- a/docs/extras/support.md +++ b/docs/extras/support.md @@ -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. @@ -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) @@ -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. diff --git a/lib/javascripts/pagy.js b/lib/javascripts/pagy.js index d5ea05c78..1e5a8f171 100644 --- a/lib/javascripts/pagy.js +++ b/lib/javascripts/pagy.js @@ -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]) }; diff --git a/lib/pagy/extras/support.rb b/lib/pagy/extras/support.rb index 93f09b256..b5654c2c8 100644 --- a/lib/pagy/extras/support.rb +++ b/lib/pagy/extras/support.rb @@ -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) @@ -41,15 +24,6 @@ def pagy_next_link(pagy, text = pagy_t('pagy.nav.next'), link_extra = '') : %(#{text}) 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 diff --git a/test/pagy/extras/support_test.rb b/test/pagy/extras/support_test.rb index af72a1037..c32954274 100644 --- a/test/pagy/extras/support_test.rb +++ b/test/pagy/extras/support_test.rb @@ -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 "" - frontend.pagy_apply_init_tag(pagy_countless, :testFunction).must_equal "" - 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 "" - frontend.pagy_apply_init_tag(pagy_countless, :testFunction, {a: 1}).must_equal "" - end - - end - end