/[Frey]/trunk/lib/Frey/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

Contents of /trunk/lib/Frey/Pager.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 143 - (show annotations)
Wed Jul 16 14:17:46 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 2001 byte(s)
huge refactor of code into more distinct classes

- Frey::Collection is now generally reusable from browser and designer
- Frey::Web::Item is now (bare) class instead of role
- Frey::Web::Layout now encapsulates different layouts for items
1 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 sub last_item {
37 my $self = shift;
38 warn "## last_item = ",
39 my $last = $#{ $self->items };
40 $self->update_collection if $last < 0;
41 $self->items->[ $#{ $self->items } ];
42 }
43
44 =head2 update_collection
45
46 $o->update_collection;
47
48 =cut
49
50 sub update_collection {
51 my ( $self ) = @_;
52
53 return if $#{ $self->items } >= 0;
54
55 warn "## update_collection from iterator";
56
57 my $i = $self->fey_class->collection( $self->pager );
58 while ( my $u = $i->next ) {
59 $self->add_item(
60 $self->item_constructor->(
61 fey => $u,
62 )
63 );
64 }
65 }
66
67 =head2 items_in_layout
68
69 my $html = $o->items_in_layout( $f );
70
71 =cut
72
73 sub items_in_layout {
74 my ( $self, $f ) = @_;
75
76 my $layout = delete( $f->{layout} );
77
78 # FIXME somehow, this seem cludgy
79 if ( $f->{page} ) {
80 $self->pager->current_page( $f->{page} );
81 $self->items( [] );
82 $self->update_collection;
83 }
84
85 my $html = join('', map {
86 $_->layout( $layout ) if $layout;
87 $_->process($f) || ''
88 } @{ $self->items } );
89
90 return $html;
91 }
92
93 =head2 render_pager
94
95 my $html = $o->render_pager;
96
97 =cut
98
99 sub render_pager {
100 my $self = shift;
101
102 my $pager = $self->pager;
103
104 return join( ' ',
105 qq|<div class="notice">|,
106 'Showing',
107 $pager->first, '-', $pager->last,
108 'of',
109 $pager->total_entries,
110 'results<br>',
111 # 'page', $pager->current_page, '/', $pager->last_page,
112 Frey::Web::Links->new(
113 name => 'page',
114 current => $pager->current_page,
115 values => [ $pager->first_page .. $pager->last_page ],
116 )->links,
117 qq|</div>|
118 );
119 }
120
121 1;

  ViewVC Help
Powered by ViewVC 1.1.26