X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FReaction%2FUI%2FViewPort%2FCollection.pm;h=d64ab42d45d08b1d776f95a9896ddcfb659367cc;hb=08c27c5b18df934a3f052d50a9e4ab361ddc4f3e;hp=26b75d45c67f851f5bed02a24ff3fc8684a88f55;hpb=6f39e2d485713321d06bf67088315414cefe2090;p=catagits%2FReaction.git diff --git a/lib/Reaction/UI/ViewPort/Collection.pm b/lib/Reaction/UI/ViewPort/Collection.pm index 26b75d4..d64ab42 100644 --- a/lib/Reaction/UI/ViewPort/Collection.pm +++ b/lib/Reaction/UI/ViewPort/Collection.pm @@ -5,67 +5,86 @@ use Scalar::Util qw/blessed/; use aliased 'Reaction::InterfaceModel::Collection' => 'IM_Collection'; use aliased 'Reaction::UI::ViewPort::Object'; -class Collection is 'Reaction::UI::ViewPort', which { +use MooseX::Types::Moose qw/Str HashRef/; - has members => (is => 'rw', isa => 'ArrayRef', lazy_build => 1); +use namespace::clean -except => [ qw(meta) ]; +extends 'Reaction::UI::ViewPort'; - has collection => (is => 'ro', isa => IM_Collection, required => 1); - has current_collection => (is => 'rw', isa => IM_Collection, lazy_build => 1); +with 'Reaction::UI::ViewPort::Collection::Role::Pager'; +with 'Reaction::UI::ViewPort::Role::Actions'; - has member_args => ( is => 'rw', isa => 'HashRef', lazy_build => 1); - has member_class => ( is => 'ro', isa => 'Str', lazy_build => 1); +has members => (is => 'rw', isa => 'ArrayRef', lazy_build => 1); - implements BUILD => as { - my ($self, $args) = @_; - if( my $member_args = delete $args->{Member} ){ - $self->member_args( $member_args ); - } - }; - - implements _build_member_args => as{ {} }; - - implements _build_member_class => as{ Object }; - - after clear_current_collection => sub{ - shift->clear_members; #clear the members the current collection changes, duh - }; - - implements _build_current_collection => as { - return $_[0]->collection; - }; - - #I'm not really sure why this is here all of a sudden. - implements model => as { shift->current_collection }; - - implements _build_members => as { - my ($self) = @_; - my (@members, $i); - my $args = $self->member_args; - my $builders = {}; - my $ctx = $self->ctx; - my $loc = join('-', $self->location, 'member'); - my $class = $self->member_class; - - #replace $i with a real unique identifier so that we don't run a risk of - # events being passed down to the wrong viewport. for now i disabled event - # passing until i fix this (groditi) - for my $obj ( $self->current_collection->members ) { - my $type = blessed $obj; - my $builder_cache = $builders->{$type} ||= {}; - my $member = $class->new( - ctx => $ctx, - model => $obj, - location => join('-', $loc, $i++), - builder_cache => $builder_cache, - %$args - ); - push(@members, $member); +has collection => (is => 'ro', isa => IM_Collection, required => 1); +has current_collection => (is => 'rw', isa => IM_Collection, lazy_build => 1); + +has member_args => ( is => 'rw', isa => HashRef, lazy_build => 1); +has member_class => ( is => 'ro', isa => Str, lazy_build => 1); + +sub BUILD { + my ($self, $args) = @_; + if( my $member_args = delete $args->{Member} ){ + $self->member_args( $member_args ); + } +} + +sub _build_member_args { {} } + +sub _build_member_class { Object }; + +after clear_current_collection => sub{ + shift->clear_members; #clear the members the current collection changes, duh +}; + +sub _build_current_collection { + return $_[0]->collection; +} + +#I'm not really sure why this is here all of a sudden. +sub model { shift->current_collection } + +sub _build_members { + my ($self) = @_; + my (@members, $i); + my $args = $self->member_args; + my $builders = {}; + my $field_orders = {}; + my $ctx = $self->ctx; + my $loc = join('-', $self->location, 'member'); + my $class = $self->member_class; + + #replace $i with a real unique identifier so that we don't run a risk of + # events being passed down to the wrong viewport. for now i disabled event + # passing until i fix this (groditi) + for my $obj ( $self->current_collection->members ) { + my $type = blessed $obj; + my $builder_cache = $builders->{$type} ||= {}; + my @order; + if( exists $args->{computed_field_order} ){ + @order = (computed_field_order => $args->{computed_field_order}); + } elsif( exists $field_orders->{$type} ) { + @order = (computed_field_order => $field_orders->{$type}); } - return \@members; - }; + my $member = $class->new( + ctx => $ctx, + model => $obj, + location => join('-', $loc, $i++), + builder_cache => $builder_cache, + @order, %$args, + ); + + #cache to prevent the sort function from having to be run potentially + #hundreds of times + $field_orders->{$type} ||= $member->computed_field_order unless @order; + push(@members, $member); + } + return \@members; }; +__PACKAGE__->meta->make_immutable; + + 1; __END__; @@ -76,33 +95,61 @@ Reaction::UI::ViewPort::Collection =head1 DESCRIPTION -Creates, from an InterfaceModel::Collection, a list of viewports representing each -member of the collection. +Creates, from an InterfaceModel::Collection, a list of viewports representing +each member of the collection. =head1 ATTRIBUTES =head2 collection +Required read-only L +This is the original collection. + =head2 current_collection +Read-only, lazy-building +L +This is the collection that will be used to create C and should be +altered to reflect any ordering, paging, etc. By default this is the +same thing as C. + =head2 member_args +A read-write HASH ref of additional parameters to pass to the C +constructor as items are instantiated. + =head2 member_class -=head1 +The class to use when instantiating items to represent the member items. + +See: L, +L. =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. +These methods, although stable, are subject to change without notice. +Extend at your own risk, APIs may change in the future. =head2 BUILD -=head2 get_builder_for +Intercept a parameter with the key C amd store it in C =head2 model +Returns the C + +=head2 _build_members + +Build individual viewports for each member of the collection, + +=head2 _build_member_args + +Defaults to an empty HASH ref. + +=head2 _build_member_class + +Defaults to L + =head1 AUTHORS See L for authors.