volt-pagination 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/README.md +40 -0
- data/Rakefile +1 -0
- data/VERSION +1 -0
- data/app/pagination/config/dependencies.rb +1 -0
- data/app/pagination/config/routes.rb +1 -0
- data/app/pagination/controllers/main_controller.rb +104 -0
- data/app/pagination/views/main/index.html +18 -0
- data/lib/volt/pagination.rb +7 -0
- data/volt-pagination.gemspec +24 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3e65eb7e52ea0fb7c9a1a9ee3a4aa11ef971f0e2
|
4
|
+
data.tar.gz: fbb222ee9d8ff831dc8988a2ff2c5e2873acb832
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 186400610afcc52340bfe7e0206ad5fd31d024f5300620360f7002417950876fda89cb0f8a23a9ef8093eb548db5fd180916b2ad0d2799e00563a5403353d041
|
7
|
+
data.tar.gz: 8f7783af49ef7aa712292328dc2223b04df0a47c879f7f5889dd2206fafb443165fb27f3fbb53cd7a16508be631bba678dfd563ec360141562d80f84596b9644
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Volt::Pagination
|
2
|
+
|
3
|
+
Provides a pagination component to easily show the paging.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'volt-pagination'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Then include it as a component in your main component's ```dependencies.rb```
|
16
|
+
|
17
|
+
component "pagination"
|
18
|
+
|
19
|
+
To page a collection or query, simply use ```.skip``` and ```.limit```
|
20
|
+
Assuming a page size of 10:
|
21
|
+
|
22
|
+
def items_on_page
|
23
|
+
store._items.find({active: true}).skip((params._page.or(1).to_i - 1) * 10).limit(10)
|
24
|
+
end
|
25
|
+
|
26
|
+
Then in your view you can add:
|
27
|
+
|
28
|
+
```html
|
29
|
+
<:pagination total="{{ store._items.size }}" per_page="10" page="{{ params._page }}" />
|
30
|
+
```
|
31
|
+
|
32
|
+
```per_page``` defaults to 10 and ```page``` defaults to params._page
|
33
|
+
|
34
|
+
The pagination tag supports the following options:
|
35
|
+
|
36
|
+
- total (required): the total number of items.
|
37
|
+
- per_page (default: 10): how many items per page.
|
38
|
+
- page (default: ```params._page```): the number of the current page (starting with 1)
|
39
|
+
- window (default: 5): the number of pages to show around the current page
|
40
|
+
- outer_window: (default: 0): the number of pages to show at the start and end of the pager.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1 @@
|
|
1
|
+
# Component dependencies
|
@@ -0,0 +1 @@
|
|
1
|
+
# Component routes
|
@@ -0,0 +1,104 @@
|
|
1
|
+
module Pagination
|
2
|
+
class MainController < Volt::ModelController
|
3
|
+
|
4
|
+
def middle_window(cpage, total)
|
5
|
+
side_size = ((window - 1) / 2).ceil
|
6
|
+
|
7
|
+
start_pos = cpage - side_size
|
8
|
+
end_pos = cpage + side_size
|
9
|
+
|
10
|
+
if start_pos <= 0
|
11
|
+
end_pos += (0 - start_pos) + 1
|
12
|
+
start_pos = 1
|
13
|
+
end
|
14
|
+
|
15
|
+
if end_pos > total
|
16
|
+
start_pos = [start_pos - (end_pos - total), 1].max
|
17
|
+
end_pos = total
|
18
|
+
end
|
19
|
+
|
20
|
+
end_pos = [end_pos, 1].max
|
21
|
+
|
22
|
+
return start_pos, end_pos
|
23
|
+
end
|
24
|
+
|
25
|
+
def page_numbers
|
26
|
+
total = total_pages()
|
27
|
+
cpage = current_page()
|
28
|
+
|
29
|
+
start_pos, end_pos = middle_window(cpage, total)
|
30
|
+
middle_window = (start_pos..end_pos).to_a
|
31
|
+
|
32
|
+
if outer_window == 0
|
33
|
+
pages = middle_window
|
34
|
+
else
|
35
|
+
side_size = ((outer_window - 1) / 2).ceil
|
36
|
+
|
37
|
+
start_outer_pos = [1 + side_size, start_pos-1].min
|
38
|
+
end_outer_pos = [total - side_size, end_pos + 1].max
|
39
|
+
|
40
|
+
start_window = (1..start_outer_pos).to_a
|
41
|
+
end_window = (end_outer_pos..total).to_a
|
42
|
+
|
43
|
+
pages = start_window
|
44
|
+
pages << nil unless start_outer_pos == (middle_window[0] - 1)
|
45
|
+
pages += middle_window
|
46
|
+
pages << nil unless end_outer_pos == middle_window[-1] + 1
|
47
|
+
pages += end_window
|
48
|
+
end
|
49
|
+
|
50
|
+
return pages
|
51
|
+
end
|
52
|
+
|
53
|
+
def window
|
54
|
+
attrs.window.or(5)
|
55
|
+
end
|
56
|
+
|
57
|
+
def outer_window
|
58
|
+
attrs.outer_window.or(0)
|
59
|
+
end
|
60
|
+
|
61
|
+
def per_page
|
62
|
+
attrs.per_page.or(10)
|
63
|
+
end
|
64
|
+
|
65
|
+
def current_page
|
66
|
+
(attrs.page || params._page.or(1)).to_i
|
67
|
+
end
|
68
|
+
|
69
|
+
def total_pages
|
70
|
+
(attrs.total.to_i / per_page.to_f).ceil
|
71
|
+
end
|
72
|
+
|
73
|
+
def last_page
|
74
|
+
current_page == page_numbers.last
|
75
|
+
end
|
76
|
+
|
77
|
+
def first_page
|
78
|
+
current_page == 1
|
79
|
+
end
|
80
|
+
|
81
|
+
def next_page
|
82
|
+
new_page = current_page + 1
|
83
|
+
if new_page <= total_pages
|
84
|
+
set_page(new_page)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def previous_page
|
89
|
+
new_page = current_page - 1
|
90
|
+
if new_page >= 1
|
91
|
+
set_page(new_page)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def set_page(page_number)
|
96
|
+
page_number = page_number.to_i
|
97
|
+
if attrs.respond_to?(:page=)
|
98
|
+
attrs.page = page_number
|
99
|
+
else
|
100
|
+
params._page = page_number
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
<:Body>
|
2
|
+
<ul class="pagination">
|
3
|
+
<li class="{{ if first_page }}disabled{{ end }}">
|
4
|
+
<a e-click="previous_page">«</a>
|
5
|
+
</li>
|
6
|
+
{{ page_numbers.each do |page_number| }}
|
7
|
+
{{ if page_number.nil? }}
|
8
|
+
<li><a>...</a></li>
|
9
|
+
{{ else }}
|
10
|
+
<li class="{{ if current_page == page_number }}active{{ end }}">
|
11
|
+
<a e-click="set_page(page_number)">{{ page_number }}</a>
|
12
|
+
</li>
|
13
|
+
{{ end }}
|
14
|
+
{{ end }}
|
15
|
+
<li class="{{ if last_page }}disabled{{ end }}">
|
16
|
+
<a e-click="next_page">»</a>
|
17
|
+
</li>
|
18
|
+
</ul>
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
version = File.read(File.expand_path('../VERSION', __FILE__)).strip
|
6
|
+
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = "volt-pagination"
|
10
|
+
spec.version = version
|
11
|
+
spec.authors = ["Ryan Stout"]
|
12
|
+
spec.email = ["ryanstout@gmail.com"]
|
13
|
+
spec.summary = %q{A Volt component that provides a :pagination tag}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "volt", "~> 0.8.0"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: volt-pagination
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Stout
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: volt
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.8.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- ryanstout@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- VERSION
|
53
|
+
- app/pagination/config/dependencies.rb
|
54
|
+
- app/pagination/config/routes.rb
|
55
|
+
- app/pagination/controllers/main_controller.rb
|
56
|
+
- app/pagination/views/main/index.html
|
57
|
+
- lib/volt/pagination.rb
|
58
|
+
- volt-pagination.gemspec
|
59
|
+
homepage: ''
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.2.2
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: A Volt component that provides a :pagination tag
|
83
|
+
test_files: []
|