/[Frey]/trunk/lib/Frey/ORM/Pager.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/Frey/ORM/Pager.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 207 - (hide annotations)
Fri Oct 31 15:33:15 2008 UTC (15 years, 6 months ago) by dpavlin
Original Path: trunk/lib/Frey/Pager.pm
File size: 2941 byte(s)
implement slinding pager which show context of 10 pages
or 20 pages total if there is enough pages to display
1 dpavlin 143 package Frey::Pager;
2     use Moose;
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 'item_constructor' => (
21     is => 'rw',
22     isa => 'CodeRef',
23     required => 1,
24     );
25    
26     has 'items' => (
27     metaclass => 'Collection::Array',
28     is => 'rw',
29     isa => 'ArrayRef[Frey::Web::Item]',
30     default => sub { [] },
31     provides => {
32     'push' => 'add_item',
33     },
34     );
35    
36 dpavlin 207 has 'range_around' => (
37     is => 'rw',
38     isa => 'Int',
39     default => 10,
40     );
41    
42 dpavlin 149 sub first_item {
43     my $self = shift;
44     $self->items->[0];
45     }
46    
47 dpavlin 143 sub last_item {
48     my $self = shift;
49     my $last = $#{ $self->items };
50     $self->update_collection if $last < 0;
51     $self->items->[ $#{ $self->items } ];
52     }
53    
54     =head2 update_collection
55    
56     $o->update_collection;
57    
58     =cut
59    
60     sub update_collection {
61     my ( $self ) = @_;
62    
63     return if $#{ $self->items } >= 0;
64    
65     warn "## update_collection from iterator";
66    
67 dpavlin 149 if ( ! $self->fey_class->can('collection') ) {
68     warn "ERROR: ", $self->fey_class, " can't do collection";
69     return;
70     }
71    
72 dpavlin 143 my $i = $self->fey_class->collection( $self->pager );
73     while ( my $u = $i->next ) {
74     $self->add_item(
75     $self->item_constructor->(
76     fey => $u,
77     )
78     );
79     }
80     }
81    
82     =head2 items_in_layout
83    
84     my $html = $o->items_in_layout( $f );
85    
86     =cut
87    
88     sub items_in_layout {
89     my ( $self, $f ) = @_;
90    
91     my $layout = delete( $f->{layout} );
92    
93     # FIXME somehow, this seem cludgy
94     if ( $f->{page} ) {
95     $self->pager->current_page( $f->{page} );
96     $self->items( [] );
97     $self->update_collection;
98     }
99    
100     my $html = join('', map {
101     $_->layout( $layout ) if $layout;
102     $_->process($f) || ''
103     } @{ $self->items } );
104    
105     return $html;
106     }
107    
108     =head2 render_pager
109    
110     my $html = $o->render_pager;
111    
112     =cut
113    
114     sub render_pager {
115     my $self = shift;
116    
117     my $pager = $self->pager;
118    
119 dpavlin 207 my @show_pages;
120     my $after_current = 0;
121    
122     if ( $pager->current_page <= $self->range_around + 2 ) {
123     @show_pages = ( $pager->first_page .. $pager->current_page );
124     $after_current = $self->range_around - $pager->current_page;
125     } else {
126     @show_pages = ( $pager->first_page, '', $pager->current_page - $self->range_around .. $pager->current_page );
127     }
128    
129     if ( $pager->current_page + $self->range_around + 1 >= $pager->last_page ) {
130     push @show_pages, ( $pager->current_page + 1 .. $pager->last_page );
131     } else {
132     push @show_pages, ( $pager->current_page + 1 .. $pager->current_page + $after_current + $self->range_around, '', $pager->last_page );
133     }
134    
135     warn "## show_pages = ",dump( @show_pages );
136    
137 dpavlin 143 return join( ' ',
138     qq|<div class="notice">|,
139     'Showing',
140     $pager->first, '-', $pager->last,
141     'of',
142     $pager->total_entries,
143     'results<br>',
144     # 'page', $pager->current_page, '/', $pager->last_page,
145     Frey::Web::Links->new(
146     name => 'page',
147     current => $pager->current_page,
148 dpavlin 207 values => \@show_pages,
149     empty => '...',
150     current_tag => 'b',
151 dpavlin 143 )->links,
152     qq|</div>|
153     );
154     }
155    
156     1;

  ViewVC Help
Powered by ViewVC 1.1.26