/[Frey]/trunk/lib/Frey/Web/Row.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/Web/Row.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: 6351 byte(s)
implemented pager for collection [0.10]
1 package Frey::Web::Row;
2
3 use Data::Dump qw/dump/;
4 use Carp qw/carp/;
5
6 use Moose;
7
8 extends 'Frey::Web::Button';
9 #with 'BonusTypes';
10
11 use Moose::Util::TypeConstraints;
12
13 enum 'Render_as' => qw( view edit none );
14 has render_as => ( is => 'rw', isa => 'Render_as', required => 1, default => 'view' );
15
16 has fey => (
17 is => 'rw',
18 isa => 'Object', # FIXME Strix::User?
19 # required => 1, # XXX if we require it we can't have Add form
20 );
21
22 has fey_class => (
23 is => 'rw',
24 isa => 'Str',
25 required => 1,
26 );
27
28 enum 'Layouts' => qw( div table columns );
29 has layout => (
30 is => 'rw',
31 isa => 'Layouts',
32 default => 'div',
33 required => 1,
34 );
35
36 has 'display_columns' => (
37 is => 'rw',
38 isa => 'ArrayRef[Str]',
39 lazy_build => 1,
40 );
41
42 sub id {
43 my $self = shift;
44 carp "LEGACY: called ->id";
45 $self->fey->id if $self->fey;
46 }
47
48 sub set_from_hash {
49 my ($self, $f) = @_;
50 my $attrmap = $self->fey->meta->get_attribute_map if $self->fey;
51 my $hash;
52 foreach my $name ( @{ $self->display_columns } ) {
53 my $field_name = $self->field_name($name);
54 if(defined $f->{$field_name}) {
55 $hash->{ $name } = $f->{$field_name};
56
57 if ( defined $attrmap->{$name} ) {
58 my $writer = $attrmap->{$name}->get_write_method;
59 $self->fey->$writer($f->{$field_name});
60 } else {
61 warn "can't store value back into fey->$name";
62 }
63 }
64 }
65 warn "# set_from_hash ", $self->uuid," produced hash = ",dump( $hash ) if $hash;
66 return $hash;
67 }
68
69 sub render_iterator {
70 confess "BACKWARD INCOMATIBLE CHANGE: render_iterator works ONLY with 2 params!" unless $#_ == 1;
71 my ($self, $iterator) = @_;
72 my $out;
73 # my %attrmap = %{ $self->meta->get_attribute_map };
74 # while( my ($name, $attr) = each %attrmap ) {
75 # my $reader = $attr->get_read_method;
76 # my $val = $self->$reader || '';
77 foreach my $name ( @{ $self->display_columns } ) {
78 my $field_name = $self->field_name($name);
79 my $val;
80 $val = $self->fey->$name if $self->fey && $self->fey->can($name);
81 $out .= $iterator->( $name, $field_name, ucfirst($name), $val ) || ''; # || '' to shut warnings
82 }
83 return $out;
84 }
85
86 sub edit_delete_buttons {
87 my $self = shift;
88
89 return unless $self->fey;
90
91 $self->add_button( 'Edit' => sub {
92 $self->remove_button( 'Delete' );
93 $self->rename_button( 'Edit' => 'Save' );
94 my $out = $self->render_edit;
95 my $f = $self->next($out);
96 my $hash = $self->set_from_hash($f);
97 warn "# Edit/Save hash = ",dump( $hash );
98 $self->fey->update( %$hash ) if $hash;
99 $self->rename_button( 'Save' => 'Edit' );
100 $self->delete_button;
101 });
102 $self->delete_button;
103 }
104
105 sub delete_button {
106 my $self = shift;
107 $self->add_button('Delete' => sub {
108 $self->fey->delete;
109 $self->next( qq|<div class="notice">Deleted id @{[$self->id]}!</div>|);
110 $self->render_as( 'none' );
111 # Strix::Schema->ClearObjectCaches(); # XXX important!
112 });
113 }
114
115 sub main {
116 my ( $self ) = @_;
117
118 if ( ! $self->fey ) {
119 $self->add_button( 'Add' => sub {
120 my $f = shift;
121 my $hash = $self->set_from_hash($f);
122 warn "## Add hash ", $self->uuid, " => ",dump( $hash, $f );
123 delete( $hash->{id} ); # FIXME clear primary key
124 my $u = $self->fey_class->insert( %$hash );
125 warn "Inserted ",$u->id;
126 # XXX store object for later
127 $self->fey( $u );
128 # put ID in widget, so that it know it's not new
129 $self->id( $u->id );
130 $self->render_as( 'view' );
131 $self->edit_delete_buttons;
132 $self->remove_button( 'Add' );
133 });
134 }
135
136 $self->edit_delete_buttons;
137
138 warn "# ",$self->uuid, " [", $self->id ,"] fey = ",dump( $self->fey );
139
140 while(1) {
141 my $out;
142 if ( $self->render_as eq 'edit' ) {
143 $out .= $self->render_edit;
144 } elsif ( $self->render_as eq 'view' ) {
145 $out .= $self->render_view;
146 } else {
147 warn "no renderer ",dump( $self->render_as ), " skipping...";
148 }
149
150 warn $@ if $@;
151 warn ">>> ",length($out),"\n";
152 my $f = $self->next($out);
153 $self->set_from_hash($f);
154 $self->exec_buttons($f);
155 }
156 };
157
158 sub render_edit {
159 my $self = shift;
160 warn "# render_edit ",$self->id," ",$self->uuid, " ", $self->layout, "\n";
161 my $out = $self->render_iterator( sub {
162 #warn "# edit render_iterator ",dump( @_ );
163 my ( $name, $field_name, $label, $val ) = @_;
164 return if $name =~ /^_/;
165 $val ||= '';
166 return qq|
167 <tr class="editform">
168 <td class="label">$label</td>
169 <td class="field">
170 <input type=text id="$field_name" name="$field_name" value="$val">
171 </td>
172 </tr>
173 | if $self->layout eq 'table';
174 return qq|
175 <td class="field">
176 <input type=text id="$field_name" name="$field_name" value="$val">
177 </td>
178 | if $self->layout eq 'columns';
179 return qq|
180 <div class=fieldholder>
181 <label for="$field_name">$label</label>
182 <div class=field>
183 <input type=text id="$field_name" name="$field_name" value="$val">
184 </div>
185 </div>
186 |;
187 });
188
189 return qq|<tr class="editform">$out<td>| . $self->render_buttons . qq|</td></tr>| if $self->layout eq 'columns';
190
191 $self->render_wrapper_class( $out, 'editform' );
192 }
193
194 sub render_view {
195 my $self = shift;
196 warn "# render_view ",$self->id," ",$self->uuid," ", $self->layout,"\n";
197 my $out = $self->render_iterator( sub {
198 #warn "# view render_iterator ",dump( @_ );
199 my ( $name, $field_name, $label, $val ) = @_;
200 return if $name =~ /^_/;
201 $val ||= '';
202 return qq|<tr><td>$label</td><td>$val</td></tr>| if $self->layout eq 'table';
203 return qq|<td>$val</td>| if $self->layout eq 'columns';
204 return qq|
205 <div class=fieldholder>
206 <div class=label>$label</div>
207 <div class=field>$val</div>
208 </div>
209 </div>
210 |;
211 });
212 return qq|<tr>$out<td>| . $self->render_buttons . qq|</td></tr>| if $self->layout eq 'columns';
213 $self->render_wrapper_class( $out, 'view' );
214 }
215
216 sub render_wrapper_class {
217 my ( $self, $out, $class ) = @_;
218 if ( length($out) == 0 ) {
219 carp "no output, skipping";
220 return '<!-- no output -->';
221 }
222 $out =~ s/^\t+//mg; # XXX compress output
223 return $out . qq|<tr><td colspan=2>| . $self->render_buttons . qq|</td></tr>| if $self->layout eq 'table';
224 return qq|<div class="$class">| . $out . $self->render_buttons . qq|</div>|;
225 }
226
227 sub _build_display_columns {
228 my $self = shift;
229
230 my $m = $self->fey_class->meta;
231
232 my @cols;
233
234 foreach ( $m->get_attribute_list ) {
235 my $attr = $m->get_attribute( $_ );
236 warn "_build_display_columns $_\n" if $self->debug;
237 # FIXME primary key would have to be read-only!
238 push @cols, $_;
239 }
240
241 warn "## display_columns ",dump( @cols ) if $self->debug;
242
243 return \@cols;
244 }
245
246 1;

  ViewVC Help
Powered by ViewVC 1.1.26