X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FReaction%2FUI%2FViewPort%2FCollection%2FGrid.pm;h=be24cefdd6912fb061fa36558fcef3f531d1de36;hb=642eb116a980729b37cb409094a94f9f6f39458c;hp=ab337706afc057865db6315f4edf33e9560e6e2f;hpb=cc44a3371f6bb93dfcc28961004b00064942e27b;p=catagits%2FReaction.git diff --git a/lib/Reaction/UI/ViewPort/Collection/Grid.pm b/lib/Reaction/UI/ViewPort/Collection/Grid.pm index ab33770..be24cef 100644 --- a/lib/Reaction/UI/ViewPort/Collection/Grid.pm +++ b/lib/Reaction/UI/ViewPort/Collection/Grid.pm @@ -3,58 +3,145 @@ package Reaction::UI::ViewPort::Collection::Grid; use Reaction::Class; use aliased 'Reaction::InterfaceModel::Collection' => 'IM_Collection'; -use aliased 'Reaction::UI::ViewPort::Collection::Grid::Member'; +use aliased 'Reaction::UI::ViewPort::Collection::Grid::Member::WithActions'; -class Grid is 'Reaction::UI::ViewPort::Collection', which { +use namespace::clean -except => [ qw(meta) ]; +extends 'Reaction::UI::ViewPort::Collection'; - has field_order => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1); - has excluded_fields => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1); - has field_labels => ( is => 'ro', isa => 'HashRef', lazy_build => 1); +has field_order => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1); +has excluded_fields => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1); +has computed_field_order => (is => 'ro', isa => 'ArrayRef', lazy_build => 1); - has computed_field_order => (is => 'ro', isa => 'ArrayRef', lazy_build => 1); +has _raw_field_labels => ( + is => 'rw', + isa => 'HashRef', + init_arg => 'field_labels', + default => sub { {} }, +); - #################################### - implements _build_member_class => as { }; +has field_labels => ( + is => 'ro', + isa => 'HashRef', + lazy_build => 1, + init_arg => undef, +); - implements _build_field_labels => as { +has member_action_count => ( + is => 'rw', + isa => 'Int', + required => 1, + lazy => 1, + default => sub { my $self = shift; - my %labels; - for my $field ( @{$self->field_order}){ - $labels{$field} = join(' ', map{ ucfirst } split('_', $field)); + for (@{ $self->members }) { + my $protos = $_->action_prototypes; + return scalar(keys(%$protos)); } - return \%labels; - }; - - implements _build_field_order => as { []; }; - implements _build_excluded_fields => as { []; }; - - implements _build_computed_field_order => as { - my ($self) = @_; - confess("current_collection lacks a value for 'member_type' attribute") - unless $self->current_collection->has_member_type; - my %excluded = map { $_ => undef } @{ $self->excluded_fields }; - #treat _$field_name as private and exclude fields with no reader - my @names = grep { $_ !~ /^_/ && !exists($excluded{$_})} map { $_->name } - grep { - !($_->has_type_constraint && - ($_->type_constraint->is_a_type_of('ArrayRef') || - eval {$_->type_constraint->name->isa('Reaction::InterfaceModel::Collection')} || - eval { $_->_isa_metadata->isa('Reaction::InterfaceModel::Collection') } - ) - ) } - grep { defined $_->get_read_method } - $self->current_collection->member_type->meta->parameter_attributes; - - return $self->sort_by_spec($self->field_order, \@names); - }; - - before _build_members => sub { - my ($self) = @_; - $self->member_args->{computed_field_order} ||= $self->computed_field_order; - }; + return 1; + }, +); +#################################### +sub _build_member_class { WithActions }; + +sub _build_field_labels { + my $self = shift; + my %labels = %{$self->_raw_field_labels}; + for my $field ( @{$self->computed_field_order}) { + next if defined $labels{$field}; + $labels{$field} = join(' ', map{ ucfirst } split('_', $field)); + } + return \%labels; +} + +sub _build_field_order { []; } + +sub _build_excluded_fields { []; } + +#this is a total clusterfuck and it sucks we should just eliminate it and have +# the grid members not render ArrayRef or Collection fields +sub _build_computed_field_order { + my ($self) = @_; + my %excluded = map { $_ => undef } @{ $self->excluded_fields }; + #treat _$field_name as private and exclude fields with no reader + my @names = grep { $_ !~ /^_/ && !exists($excluded{$_})} map { $_->name } + grep { + !($_->has_type_constraint && + ($_->type_constraint->is_a_type_of('ArrayRef') || + eval {$_->type_constraint->name->isa('Reaction::InterfaceModel::Collection')} || + eval { $_->_isa_metadata->isa('Reaction::InterfaceModel::Collection') } + ) + ) } + grep { defined $_->get_read_method } + $self->current_collection->member_type->parameter_attributes; + + return $self->sort_by_spec($self->field_order, \@names); +} + +around _build_members => sub { + my $orig = shift; + my $self = shift; + $self->member_args->{computed_field_order} ||= $self->computed_field_order; + $self->member_args->{computed_action_order} ||= []; + my $members = $self->$orig(@_); + + # cache everything yo + for my $member (@$members){ + $member->clear_computed_action_order; + my $order = $member->computed_action_order; + @{ $self->member_args->{computed_action_order} } = @$order; + last; + } + + return $members; }; +__PACKAGE__->meta->make_immutable; 1; + +__END__; + +=head1 NAME + +Reaction::UI::ViewPort::Collection + +=head1 DESCRIPTION + +This subclass of L allows you to display a +homogenous collection of Reaction::InterfaceModel::Objects as a grid. + +=head1 ATTRIBUTES + +=head2 field_order + +=head2 excluded_fields + +=head2 field_labels + +=head2 _raw_field_labels + +=head2 computed_field_order + +=head2 member_action_count + +=head1 INTERNAL METHODS + +These methods, although stable, are subject to change without notice. These are meant +to be used only by developers. End users should refrain from using these methods to +avoid potential breakages. + +=head1 SEE ALSO + +L + +=head1 AUTHORS + +See L for authors. + +=head1 LICENSE + +See L for the license. + +=cut