Commit | Line | Data |
ddccc6a2 |
1 | package Reaction::UI::ViewPort::Collection; |
2 | |
3 | use Reaction::Class; |
4 | use Scalar::Util qw/blessed/; |
5 | use aliased 'Reaction::InterfaceModel::Collection' => 'IM_Collection'; |
6 | use aliased 'Reaction::UI::ViewPort::Object'; |
7 | |
81393881 |
8 | use namespace::clean -except => [ qw(meta) ]; |
9 | extends 'Reaction::UI::ViewPort'; |
ddccc6a2 |
10 | |
81393881 |
11 | |
12 | |
13 | has members => (is => 'rw', isa => 'ArrayRef', lazy_build => 1); |
14 | |
15 | has collection => (is => 'ro', isa => IM_Collection, required => 1); |
16 | has current_collection => (is => 'rw', isa => IM_Collection, lazy_build => 1); |
17 | |
18 | has member_args => ( is => 'rw', isa => 'HashRef', lazy_build => 1); |
19 | has member_class => ( is => 'ro', isa => 'Str', lazy_build => 1); |
20 | sub BUILD { |
21 | my ($self, $args) = @_; |
22 | if( my $member_args = delete $args->{Member} ){ |
23 | $self->member_args( $member_args ); |
24 | } |
25 | }; |
26 | sub _build_member_args { {} }; |
27 | sub _build_member_class { Object }; |
28 | |
29 | after clear_current_collection => sub{ |
30 | shift->clear_members; #clear the members the current collection changes, duh |
ddccc6a2 |
31 | }; |
81393881 |
32 | sub _build_current_collection { |
33 | return $_[0]->collection; |
34 | }; |
35 | |
36 | #I'm not really sure why this is here all of a sudden. |
37 | sub model { shift->current_collection }; |
38 | sub _build_members { |
39 | my ($self) = @_; |
40 | my (@members, $i); |
41 | my $args = $self->member_args; |
42 | my $builders = {}; |
43 | my $ctx = $self->ctx; |
44 | my $loc = join('-', $self->location, 'member'); |
45 | my $class = $self->member_class; |
46 | |
47 | #replace $i with a real unique identifier so that we don't run a risk of |
48 | # events being passed down to the wrong viewport. for now i disabled event |
49 | # passing until i fix this (groditi) |
50 | for my $obj ( $self->current_collection->members ) { |
51 | my $type = blessed $obj; |
52 | my $builder_cache = $builders->{$type} ||= {}; |
53 | my $member = $class->new( |
54 | ctx => $ctx, |
55 | model => $obj, |
56 | location => join('-', $loc, $i++), |
57 | builder_cache => $builder_cache, |
58 | %$args |
59 | ); |
60 | push(@members, $member); |
61 | } |
62 | return \@members; |
63 | }; |
64 | |
65 | __PACKAGE__->meta->make_immutable; |
66 | |
ddccc6a2 |
67 | |
2dba7201 |
68 | 1; |
ddccc6a2 |
69 | |
2dba7201 |
70 | __END__; |
ddccc6a2 |
71 | |
2dba7201 |
72 | =head1 NAME |
73 | |
74 | Reaction::UI::ViewPort::Collection |
75 | |
76 | =head1 DESCRIPTION |
77 | |
78 | Creates, from an InterfaceModel::Collection, a list of viewports representing each |
79 | member of the collection. |
80 | |
81 | =head1 ATTRIBUTES |
82 | |
83 | =head2 collection |
84 | |
85 | =head2 current_collection |
86 | |
87 | =head2 member_args |
88 | |
89 | =head2 member_class |
90 | |
2dba7201 |
91 | =head1 INTERNAL METHODS |
92 | |
93 | These methods, although stable, are subject to change without notice. These are meant |
94 | to be used only by developers. End users should refrain from using these methods to |
95 | avoid potential breakages. |
96 | |
97 | =head2 BUILD |
98 | |
99 | =head2 get_builder_for |
100 | |
101 | =head2 model |
102 | |
103 | =head1 AUTHORS |
104 | |
105 | See L<Reaction::Class> for authors. |
106 | |
107 | =head1 LICENSE |
108 | |
109 | See L<Reaction::Class> for the license. |
110 | |
111 | =cut |