/[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 149 - (show annotations)
Wed Jul 16 21:23:27 2008 UTC (14 years, 4 months ago) by dpavlin
File size: 2157 byte(s)
partially working label editor in ObjectDesigner -- no save yet! [0.13]
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 first_item {
37 my $self = shift;
38 $self->items->[0];
39 }
40
41 sub last_item {
42 my $self = shift;
43 my $last = $#{ $self->items };
44 $self->update_collection if $last < 0;
45 $self->items->[ $#{ $self->items } ];
46 }
47
48 =head2 update_collection
49
50 $o->update_collection;
51
52 =cut
53
54 sub update_collection {
55 my ( $self ) = @_;
56
57 return if $#{ $self->items } >= 0;
58
59 warn "## update_collection from iterator";
60
61 if ( ! $self->fey_class->can('collection') ) {
62 warn "ERROR: ", $self->fey_class, " can't do collection";
63 return;
64 }
65
66 my $i = $self->fey_class->collection( $self->pager );
67 while ( my $u = $i->next ) {
68 $self->add_item(
69 $self->item_constructor->(
70 fey => $u,
71 )
72 );
73 }
74 }
75
76 =head2 items_in_layout
77
78 my $html = $o->items_in_layout( $f );
79
80 =cut
81
82 sub items_in_layout {
83 my ( $self, $f ) = @_;
84
85 my $layout = delete( $f->{layout} );
86
87 # FIXME somehow, this seem cludgy
88 if ( $f->{page} ) {
89 $self->pager->current_page( $f->{page} );
90 $self->items( [] );
91 $self->update_collection;
92 }
93
94 my $html = join('', map {
95 $_->layout( $layout ) if $layout;
96 $_->process($f) || ''
97 } @{ $self->items } );
98
99 return $html;
100 }
101
102 =head2 render_pager
103
104 my $html = $o->render_pager;
105
106 =cut
107
108 sub render_pager {
109 my $self = shift;
110
111 my $pager = $self->pager;
112
113 return join( ' ',
114 qq|<div class="notice">|,
115 'Showing',
116 $pager->first, '-', $pager->last,
117 'of',
118 $pager->total_entries,
119 'results<br>',
120 # 'page', $pager->current_page, '/', $pager->last_page,
121 Frey::Web::Links->new(
122 name => 'page',
123 current => $pager->current_page,
124 values => [ $pager->first_page .. $pager->last_page ],
125 )->links,
126 qq|</div>|
127 );
128 }
129
130 1;

  ViewVC Help
Powered by ViewVC 1.1.26