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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 111 - (show annotations)
Sun Jul 13 16:17:34 2008 UTC (14 years, 4 months ago) by dpavlin
File size: 3247 byte(s)
implemented pager for collection [0.10]
1 package Frey::ObjectBrowser;
2 use Moose;
3
4 extends 'Frey';
5 with 'Frey::Web';
6
7 has 'fey_class' => (
8 is => 'rw',
9 isa => 'Str',
10 required => 1,
11 );
12
13 my @callbacks;
14
15 sub gen_link {
16 my ($text, $code) = @_;
17 push @callbacks, $code;
18 return qq{<a href="?cb=$#callbacks">$text</a>};
19 }
20
21 sub process_links {
22 my $request = shift;
23 my $cb = $request->param('cb');
24 if (exists $callbacks[$cb]) {
25 $callbacks[$cb]->($request);
26 delete $callbacks[$cb];
27 }
28 }
29
30 #use Data::Dumper;
31 #$Data::Dumper::Deparse = 1;
32 use Data::Dump qw/dump/;
33
34 use Frey::Web::Row;
35 use Frey::Web::Links;
36
37 sub html {
38 my ( $self, $req ) = @_;
39
40 my $f = { $req->params };
41 my $layout = delete( $f->{layout} ) || 'div';
42
43 my @w;
44 my $pager;
45
46 while ( 1 ) {
47
48 warn "<<< GOT REQUEST ",dump( $f );
49
50 # re-apply (changed?) layout to elements
51 if ( defined $f->{layout} ) {
52 $layout = delete( $f->{layout} );
53 warn "# new layout = $layout\n";
54 }
55
56 if ( ! @w || $f->{page} || $f->{per_page} ) {
57 my $s;
58 ( $s, $pager ) = $self->fey_class->collection( $f );
59 warn "# s ",$self->fey_class," = ",dump( $s ) if $self->debug;
60
61 @w = ();
62
63 while ( my $u = $s->next ) {
64 my $w = Frey::Web::Row->new( fey_class => $self->fey_class, fey => $u, layout => $layout );
65 warn "# w = ",dump( $w ) if $self->debug;
66 push @w, $w;
67 }
68
69 warn "# got $#w elements: ",dump( map { $_->id } @w ), " from total of ", $s->handle->rows;
70 }
71
72 my $rows_delimiter = '';
73 $rows_delimiter = '<hr/>' if $layout eq 'div';
74
75 my $form = join($rows_delimiter, map {
76 $_->layout( $layout ); # FIXME do we need this still?
77 $_->process($f) || ''
78 } @w );
79
80 if ( $w[ $#w ]->fey ) {
81 warn "+++ added new item widget\n";
82 my $w = Frey::Web::Row->new( fey_class => $self->fey_class, render_as => 'edit', layout => $layout );
83 $form .= $rows_delimiter . $w->process;
84 push @w, $w;
85 }
86
87 $form = qq|<table>$form</table>| if $layout eq 'table';
88
89 if ( $layout eq 'columns' ) {
90 # add header to table
91 my $header = $w[0]->render_iterator( sub {
92 my ( $name, $field_name, $label, $val ) = @_;
93 return if $name =~ /^_/;
94 return qq|<th>$label</th>|;
95 });
96 $form = qq|<table><tr>$header</tr>$form</table>|;
97 }
98
99 my $layout_picker =
100 qq|<div class="layout">layout: | .
101 Frey::Web::Links->new(
102 name => 'layout',
103 current => $layout,
104 values => [ 'div', 'table', 'columns' ],
105 )->links .
106 qq|</div>|
107 ;
108
109 my $pager = join( ' ',
110 qq|<div class="notice">|,
111 'Showing',
112 $pager->first, '-', $pager->last,
113 'of',
114 $pager->total_entries,
115 'results<br>',
116 # 'page', $pager->current_page, '/', $pager->last_page,
117 Frey::Web::Links->new(
118 name => 'page',
119 current => $pager->current_page,
120 values => [ $pager->first_page .. $pager->last_page ],
121 )->links,
122 qq|</div>|
123 );
124
125 my $html = dom2html(
126 html => [
127 head => [
128 link => { rel=>"stylesheet", href=>"/static/form.css", type=>"text/css" }
129 ],
130 body => [
131 h1 => [ $self->fey_class ],
132 $layout_picker,
133 $pager,
134 form => { method=>'post' } => [
135 $form,
136 ],
137 ],
138 ]
139 );
140
141 warn ">>> $layout ",length( $html ),"\n";
142
143 $req->print( $html );
144 $req->next;
145
146 $f = { $req->params };
147
148 process_links( $req );
149 }
150
151 warn "NO WAY OUT!";
152 };
153
154 1;

  ViewVC Help
Powered by ViewVC 1.1.26