middleman-paginator 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +15 -2
- data/lib/middleman-paginator.rb +2 -2
- data/lib/middleman/paginator.rb +4 -63
- data/lib/middleman/paginator/core.rb +43 -0
- data/lib/middleman/paginator/link_helpers.rb +51 -0
- data/lib/middleman/paginator/pages_group.rb +47 -0
- data/middleman-paginate_collection.gemspec +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a51e3554f21fb7fd0f457ae29527eeadb8631f94
|
4
|
+
data.tar.gz: 17bd2b609b80b3c2f00f209e60fe4cb5ab7b949d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e8d446b3aecff5d826d5383d8e617660d8c56153c60c1f672f97dca6b3f1ddb8c0d22e5cb2e5b5fa3bdba504b2a0df62062da3a48b704d9b37f926e251426c6
|
7
|
+
data.tar.gz: 171b7184cee87a2be73d060b1d10f9e9e7d8b15bfcef43cb61ff25426c84d74633f5e533d39245d2a157148cad478af2d954eb2702c99f9b63342f0a6a12483d
|
data/README.md
CHANGED
@@ -48,7 +48,7 @@ paginate(
|
|
48
48
|
Inside `template`, iterate over `per_page` number of `objects`
|
49
49
|
|
50
50
|
```haml
|
51
|
-
- objects.each do |object|
|
51
|
+
- paginator[:objects].each do |object|
|
52
52
|
// render object
|
53
53
|
|
54
54
|
= previous_page_link
|
@@ -56,6 +56,19 @@ Inside `template`, iterate over `per_page` number of `objects`
|
|
56
56
|
|
57
57
|
```
|
58
58
|
|
59
|
+
As you might've noticed paginator injects local variable called `paginator`. With following helpful data: `objects`, `page`, `destination`, `last_page`
|
60
|
+
|
59
61
|
### Helpers
|
60
62
|
|
61
|
-
Paginator provides
|
63
|
+
Paginator provides a few helper methods:
|
64
|
+
* `last_page_link`
|
65
|
+
* `previous_page_link`
|
66
|
+
* `next_page_link`
|
67
|
+
* `last_page_link`
|
68
|
+
* `pages`
|
69
|
+
They work like `link_to` helper and support same the arguments.
|
70
|
+
|
71
|
+
Here is an example with bootstrap styles:
|
72
|
+
|
73
|
+
```haml
|
74
|
+
```
|
data/lib/middleman-paginator.rb
CHANGED
data/lib/middleman/paginator.rb
CHANGED
@@ -1,65 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
class Paginator < ::Middleman::ConfigExtension
|
4
|
-
self.resource_list_manipulator_priority = 0
|
5
|
-
|
6
|
-
expose_to_config :paginate
|
7
|
-
|
8
|
-
option :per_page, 20, 'Controls pagination'
|
9
|
-
|
10
|
-
CollectionProxyDescriptor = Struct.new(:descriptors) do
|
11
|
-
def execute_descriptor(app, resources)
|
12
|
-
descriptors.reduce(resources) do |resources, descriptor|
|
13
|
-
if descriptor.metadata[:ignore]
|
14
|
-
d = ::Middleman::Sitemap::Extensions::Ignores::StringIgnoreDescriptor.new(descriptor.target)
|
15
|
-
d.execute_descriptor(app, resources)
|
16
|
-
end
|
17
|
-
|
18
|
-
descriptor.execute_descriptor(app, resources)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def paginate(destination:, objects:, template:, **page_options)
|
24
|
-
descriptors = []
|
25
|
-
|
26
|
-
objects.each_slice(options.per_page).with_index(1).reverse_each do |o, i|
|
27
|
-
paginate_options = { locals: { objects: o, page: i, destination: destination } }
|
28
|
-
path = i == 1 ? 'index.html' : "pages/#{i}.html"
|
29
|
-
|
30
|
-
descriptor = Middleman::Sitemap::Extensions::ProxyDescriptor.new(
|
31
|
-
::Middleman::Util.normalize_path("#{destination}#{path}"),
|
32
|
-
::Middleman::Util.normalize_path(template),
|
33
|
-
page_options.merge(paginate_options) { |key, v1, v2| v1.merge v2 }
|
34
|
-
)
|
35
|
-
|
36
|
-
descriptors << descriptor
|
37
|
-
end
|
38
|
-
|
39
|
-
CollectionProxyDescriptor.new(descriptors)
|
40
|
-
end
|
41
|
-
|
42
|
-
helpers do
|
43
|
-
def previous_page_link(options = {})
|
44
|
-
page = @locs[:page]
|
45
|
-
return if page == 1
|
46
|
-
|
47
|
-
path = if page == 2
|
48
|
-
"#{@locs[:destination]}index.html"
|
49
|
-
else
|
50
|
-
"#{@locs[:destination]}pages/#{page - 1}.html"
|
51
|
-
end
|
52
|
-
|
53
|
-
link_to 'Previous', path, **options
|
54
|
-
end
|
55
|
-
|
56
|
-
def next_page_link(options = {})
|
57
|
-
page = @locs[:page]
|
58
|
-
path = "#{@locs[:destination]}pages/#{page + 1}.html"
|
59
|
-
return unless sitemap.find_resource_by_path(path)
|
60
|
-
|
61
|
-
link_to 'Next', path, **options
|
62
|
-
end
|
63
|
-
end
|
1
|
+
module Paginator
|
64
2
|
end
|
65
3
|
|
4
|
+
require 'middleman/paginator/link_helpers'
|
5
|
+
require 'middleman/paginator/core'
|
6
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class Paginator::Core < ::Middleman::ConfigExtension
|
2
|
+
helpers Paginator::LinkHelpers
|
3
|
+
|
4
|
+
self.resource_list_manipulator_priority = 0
|
5
|
+
|
6
|
+
expose_to_config :paginate
|
7
|
+
|
8
|
+
option :per_page, 20, 'Controls pagination'
|
9
|
+
|
10
|
+
CollectionProxyDescriptor = Struct.new(:descriptors) do
|
11
|
+
def execute_descriptor(app, resources)
|
12
|
+
descriptors.reduce(resources) do |resources, descriptor|
|
13
|
+
if descriptor.metadata[:ignore]
|
14
|
+
d = ::Middleman::Sitemap::Extensions::Ignores::StringIgnoreDescriptor.new(descriptor.target)
|
15
|
+
d.execute_descriptor(app, resources)
|
16
|
+
end
|
17
|
+
|
18
|
+
descriptor.execute_descriptor(app, resources)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def paginate(destination:, objects:, template:, **page_options)
|
24
|
+
descriptors = []
|
25
|
+
|
26
|
+
pages = objects.each_slice(options.per_page)
|
27
|
+
pages.with_index(1).reverse_each do |o, i|
|
28
|
+
paginate_options = { objects: o, page: i, destination: destination , last_page: pages.count }
|
29
|
+
path = i == 1 ? 'index.html' : "pages/#{i}.html"
|
30
|
+
|
31
|
+
descriptor = Middleman::Sitemap::Extensions::ProxyDescriptor.new(
|
32
|
+
::Middleman::Util.normalize_path("#{destination}#{path}"),
|
33
|
+
::Middleman::Util.normalize_path(template),
|
34
|
+
page_options.merge(locals: { paginator: paginate_options }) { |key, v1, v2| v1.merge v2 }
|
35
|
+
)
|
36
|
+
|
37
|
+
descriptors << descriptor
|
38
|
+
end
|
39
|
+
|
40
|
+
CollectionProxyDescriptor.new(descriptors)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'middleman/paginator/pages_group'
|
2
|
+
|
3
|
+
module Paginator::LinkHelpers
|
4
|
+
def previous_page_link(text: 'Previous', **options)
|
5
|
+
page = @locs[:paginator][:page]
|
6
|
+
return if page == 1
|
7
|
+
|
8
|
+
path = if page == 2
|
9
|
+
"#{@locs[:paginator][:destination]}index.html"
|
10
|
+
else
|
11
|
+
"#{@locs[:paginator][:destination]}pages/#{page - 1}.html"
|
12
|
+
end
|
13
|
+
|
14
|
+
link_to text, path, **options
|
15
|
+
end
|
16
|
+
|
17
|
+
def next_page_link(text: 'Next', **options)
|
18
|
+
page = @locs[:paginator][:page]
|
19
|
+
path = "#{@locs[:paginator][:destination]}pages/#{page + 1}.html"
|
20
|
+
return unless sitemap.find_resource_by_path(path)
|
21
|
+
|
22
|
+
link_to text, path, **options
|
23
|
+
end
|
24
|
+
|
25
|
+
def last_page_link(text: 'Last', **options)
|
26
|
+
page = @locs[:paginator][:last_page]
|
27
|
+
|
28
|
+
path = "#{@locs[:paginator][:destination]}pages/#{page + 1}.html"
|
29
|
+
|
30
|
+
link_to text, path, **options
|
31
|
+
end
|
32
|
+
|
33
|
+
def first_page_link(text: 'First', **options)
|
34
|
+
path = "#{@locs[:paginator][:destination]}index.html"
|
35
|
+
|
36
|
+
link_to text, path, **options
|
37
|
+
end
|
38
|
+
|
39
|
+
def pages(count: 5)
|
40
|
+
pages_group = Paginator::PagesGroup.new(
|
41
|
+
current: @locs[:paginator][:page], last: @locs[:paginator][:last_page], count: count
|
42
|
+
)
|
43
|
+
|
44
|
+
pages_group.generate do |page_number|
|
45
|
+
page_name = page_number == 1 ? 'index.html' : "pages/#{page_number}.html"
|
46
|
+
path = @locs[:paginator][:destination] + page_name
|
47
|
+
|
48
|
+
yield(page_number, path)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
class Paginator::PagesGroup
|
2
|
+
attr_reader :last, :current, :count
|
3
|
+
|
4
|
+
def initialize(current:, last:, count:)
|
5
|
+
@last = last
|
6
|
+
@current = current
|
7
|
+
@count = count
|
8
|
+
end
|
9
|
+
|
10
|
+
def generate
|
11
|
+
(first_page..last_page).each do |page_number|
|
12
|
+
yield(page_number)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def left_spread
|
19
|
+
@left_spread ||= count / 2
|
20
|
+
end
|
21
|
+
|
22
|
+
def right_spread
|
23
|
+
@right_spread ||= count.odd? ? left_spread : (left_spread - 1)
|
24
|
+
end
|
25
|
+
|
26
|
+
def first_page
|
27
|
+
@first_page ||= pagination_beginning? ? 1 : current - left_spread
|
28
|
+
end
|
29
|
+
|
30
|
+
def last_page
|
31
|
+
@last_page ||= begin
|
32
|
+
return last if pagination_ending?
|
33
|
+
right_half_count = current + right_spread
|
34
|
+
left_half_count = left_spread - current + 1
|
35
|
+
return right_half_count + left_half_count if pagination_beginning?
|
36
|
+
right_half_count
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def pagination_beginning?
|
41
|
+
current <= left_spread
|
42
|
+
end
|
43
|
+
|
44
|
+
def pagination_ending?
|
45
|
+
current >= last - right_spread
|
46
|
+
end
|
47
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: middleman-paginator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TheSmartnik
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-04-
|
11
|
+
date: 2018-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: middleman-core
|
@@ -38,6 +38,9 @@ files:
|
|
38
38
|
- features/support/env.rb
|
39
39
|
- lib/middleman-paginator.rb
|
40
40
|
- lib/middleman/paginator.rb
|
41
|
+
- lib/middleman/paginator/core.rb
|
42
|
+
- lib/middleman/paginator/link_helpers.rb
|
43
|
+
- lib/middleman/paginator/pages_group.rb
|
41
44
|
- middleman-paginate_collection.gemspec
|
42
45
|
homepage: https://github.com/TheSmartnik/middleman-paginator
|
43
46
|
licenses: []
|