1 package Reaction::UI::ViewPort::Collection;
4 use Scalar::Util qw/blessed/;
5 use aliased 'Reaction::InterfaceModel::Collection' => 'IM_Collection';
6 use aliased 'Reaction::UI::ViewPort::Object';
8 use MooseX::Types::Moose qw/Str HashRef/;
10 use namespace::clean -except => [ qw(meta) ];
11 extends 'Reaction::UI::ViewPort';
13 with 'Reaction::UI::ViewPort::Collection::Role::Pager';
14 with 'Reaction::UI::ViewPort::Role::Actions';
16 has members => (is => 'rw', isa => 'ArrayRef', lazy_build => 1);
18 has collection => (is => 'ro', isa => IM_Collection, required => 1);
19 has current_collection => (is => 'rw', isa => IM_Collection, lazy_build => 1);
21 has member_args => ( is => 'rw', isa => HashRef, lazy_build => 1);
22 has member_class => ( is => 'ro', isa => Str, lazy_build => 1);
25 my ($self, $args) = @_;
26 if( my $member_args = delete $args->{Member} ){
27 $self->member_args( $member_args );
31 sub _build_member_args { {} }
33 sub _build_member_class { Object };
35 after clear_current_collection => sub{
36 shift->clear_members; #clear the members the current collection changes, duh
39 sub _build_current_collection {
40 return $_[0]->collection;
43 #I'm not really sure why this is here all of a sudden.
44 sub model { shift->current_collection }
49 my $args = $self->member_args;
51 my $field_orders = {};
53 my $loc = join('-', $self->location, 'member');
54 my $class = $self->member_class;
56 #replace $i with a real unique identifier so that we don't run a risk of
57 # events being passed down to the wrong viewport. for now i disabled event
58 # passing until i fix this (groditi)
59 for my $obj ( $self->current_collection->members ) {
60 my $type = blessed $obj;
61 my $builder_cache = $builders->{$type} ||= {};
63 if( exists $args->{computed_field_order} ){
64 @order = (computed_field_order => $args->{computed_field_order});
65 } elsif( exists $field_orders->{$type} ) {
66 @order = (computed_field_order => $field_orders->{$type});
69 my $member = $class->new(
72 location => join('-', $loc, $i++),
73 builder_cache => $builder_cache,
77 #cache to prevent the sort function from having to be run potentially
79 $field_orders->{$type} ||= $member->computed_field_order unless @order;
80 push(@members, $member);
85 __PACKAGE__->meta->make_immutable;
94 Reaction::UI::ViewPort::Collection
98 Creates, from an InterfaceModel::Collection, a list of viewports representing
99 each member of the collection.
105 Required read-only L<InterfaceModel::Collection|Reaction::InterfaceModel::Collection>
106 This is the original collection.
108 =head2 current_collection
110 Read-only, lazy-building
111 L<InterfaceModel::Collection|Reaction::InterfaceModel::Collection>
112 This is the collection that will be used to create C<members> and should be
113 altered to reflect any ordering, paging, etc. By default this is the
114 same thing as C<collection>.
118 A read-write HASH ref of additional parameters to pass to the C<member_class>
119 constructor as items are instantiated.
123 The class to use when instantiating items to represent the member items.
125 See: L<Object|Reaction::UI::ViewPort::Object>,
126 L<Member|Reaction::UI::ViewPort::Collection::Grid::Member>.
128 =head1 INTERNAL METHODS
130 These methods, although stable, are subject to change without notice.
131 Extend at your own risk, APIs may change in the future.
135 Intercept a parameter with the key C<Member> amd store it in C<member_args>
139 Returns the C<current_collection>
141 =head2 _build_members
143 Build individual viewports for each member of the collection,
145 =head2 _build_member_args
147 Defaults to an empty HASH ref.
149 =head2 _build_member_class
151 Defaults to L<Reaction::UI::ViewPort::Object>
155 See L<Reaction::Class> for authors.
159 See L<Reaction::Class> for the license.