1 |
package Frey::ORM::Pager; |
2 |
use Moose::Role; |
3 |
use MooseX::AttributeHelpers; |
4 |
|
5 |
use Data::Page; |
6 |
use Data::Dump qw/dump/; |
7 |
|
8 |
has 'pager' => ( |
9 |
is => 'rw', |
10 |
isa => 'Data::Page', |
11 |
default => sub { Data::Page->new }, |
12 |
); |
13 |
|
14 |
has 'fey_class' => ( |
15 |
is => 'rw', |
16 |
isa => 'Str', |
17 |
required => 1, |
18 |
); |
19 |
|
20 |
has 'items' => ( |
21 |
metaclass => 'Collection::Array', |
22 |
is => 'rw', |
23 |
isa => 'ArrayRef[Frey::ORM::Item]', |
24 |
default => sub { [] }, |
25 |
provides => { |
26 |
'push' => 'add_item', |
27 |
}, |
28 |
); |
29 |
|
30 |
has 'range_around' => ( |
31 |
is => 'rw', |
32 |
isa => 'Int', |
33 |
default => 10, |
34 |
); |
35 |
|
36 |
has 'page' => ( |
37 |
is => 'rw', |
38 |
isa => 'Int', |
39 |
default => 1, |
40 |
); |
41 |
|
42 |
has 'per_page' => ( |
43 |
is => 'rw', |
44 |
isa => 'Int', |
45 |
default => 20, |
46 |
); |
47 |
|
48 |
=head1 METHODS |
49 |
|
50 |
=cut |
51 |
|
52 |
sub first_item { |
53 |
my $self = shift; |
54 |
$self->items->[0]; |
55 |
} |
56 |
|
57 |
sub last_item { |
58 |
my $self = shift; |
59 |
my $last = $#{ $self->items }; |
60 |
$self->update_collection if $last < 0; |
61 |
$self->items->[ $#{ $self->items } ]; |
62 |
} |
63 |
|
64 |
=head2 update_collection |
65 |
|
66 |
$o->update_collection; |
67 |
|
68 |
=cut |
69 |
|
70 |
sub update_collection { |
71 |
my ( $self ) = @_; |
72 |
|
73 |
return if $#{ $self->items } >= 0; |
74 |
|
75 |
warn "## update_collection from iterator"; |
76 |
|
77 |
if ( ! $self->fey_class->can('collection') ) { |
78 |
warn "ERROR: ", $self->fey_class, " can't do collection"; |
79 |
return; |
80 |
} |
81 |
|
82 |
my $i = $self->fey_class->collection( $self->pager ); |
83 |
while ( my $u = $i->next ) { |
84 |
$self->add_item( |
85 |
$self->item_constructor->( |
86 |
fey => $u, |
87 |
) |
88 |
); |
89 |
} |
90 |
} |
91 |
|
92 |
=head2 items_in_layout |
93 |
|
94 |
my $html = $o->items_in_layout; |
95 |
|
96 |
=cut |
97 |
|
98 |
sub items_in_layout { |
99 |
my ( $self ) = @_; |
100 |
|
101 |
# FIXME somehow, this seem cludgy |
102 |
$self->pager->current_page( $self->page ); |
103 |
$self->items( [] ); |
104 |
$self->update_collection; |
105 |
|
106 |
my $html = join('', map { |
107 |
$_->process |
108 |
} @{ $self->items } ); |
109 |
|
110 |
return $html; |
111 |
} |
112 |
|
113 |
=head2 render_pager |
114 |
|
115 |
my $html = $o->render_pager; |
116 |
|
117 |
=cut |
118 |
|
119 |
sub render_pager { |
120 |
my $self = shift; |
121 |
|
122 |
my $pager = $self->pager; |
123 |
|
124 |
my @show_pages; |
125 |
my $after_current = 0; |
126 |
|
127 |
if ( $pager->current_page <= $self->range_around + 2 ) { |
128 |
@show_pages = ( $pager->first_page .. $pager->current_page ); |
129 |
$after_current = $self->range_around - $pager->current_page; |
130 |
} else { |
131 |
@show_pages = ( $pager->first_page, '', $pager->current_page - $self->range_around .. $pager->current_page ); |
132 |
} |
133 |
|
134 |
if ( $pager->current_page + $self->range_around + 1 >= $pager->last_page ) { |
135 |
push @show_pages, ( $pager->current_page + 1 .. $pager->last_page ); |
136 |
} else { |
137 |
push @show_pages, ( $pager->current_page + 1 .. $pager->current_page + $after_current + $self->range_around, '', $pager->last_page ); |
138 |
} |
139 |
|
140 |
warn "## show_pages = ",dump( @show_pages ); |
141 |
|
142 |
return join( ' ', |
143 |
qq|<div class="notice">|, |
144 |
'Showing', |
145 |
$pager->first, '-', $pager->last, |
146 |
'of', |
147 |
$pager->total_entries, |
148 |
'results<br>', |
149 |
# 'page', $pager->current_page, '/', $pager->last_page, |
150 |
Frey::ORM::Links->new( |
151 |
name => 'page', |
152 |
current => $pager->current_page, |
153 |
values => \@show_pages, |
154 |
empty => '...', |
155 |
current_tag => 'b', |
156 |
)->links, |
157 |
qq|</div>| |
158 |
); |
159 |
} |
160 |
|
161 |
1; |