change listview/grid so that members of a grid can have actions by default, it will...
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Collection / Grid.pm
1 package Reaction::UI::ViewPort::Collection::Grid;
2
3 use Reaction::Class;
4
5 use aliased 'Reaction::InterfaceModel::Collection' => 'IM_Collection';
6 use aliased 'Reaction::UI::ViewPort::Collection::Grid::Member::WithActions';
7
8 use namespace::clean -except => [ qw(meta) ];
9 extends 'Reaction::UI::ViewPort::Collection';
10
11 has field_order => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1);
12 has excluded_fields => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1);
13 has computed_field_order => (is => 'ro', isa => 'ArrayRef', lazy_build => 1);
14
15 has _raw_field_labels => (
16   is => 'rw',
17   isa => 'HashRef',
18   init_arg => 'field_labels',
19   default => sub { {} },
20 );
21
22 has field_labels => (
23   is => 'ro',
24   isa => 'HashRef',
25   lazy_build => 1,
26   init_arg => undef,
27 );
28
29 has member_action_count => (
30   is => 'rw',
31   isa => 'Int',
32   required => 1,
33   lazy => 1,
34   default => sub {
35     my $self = shift;
36     for (@{ $self->members }) {
37       my $protos = $_->action_prototypes;
38       return scalar(@$protos);
39       #return scalar(keys(%$protos));
40     }
41     return 1;
42   },
43 );
44
45 ####################################
46 sub _build_member_class { WithActions };
47
48 sub _build_field_labels {
49   my $self = shift;
50   my %labels = %{$self->_raw_field_labels};
51   for my $field ( @{$self->computed_field_order}) {
52     next if $labels{$field};
53     $labels{$field} = join(' ', map{ ucfirst } split('_', $field));
54   }
55   return \%labels;
56 }
57
58 sub _build_field_order { []; }
59
60 sub _build_excluded_fields { []; }
61
62 #this is a total clusterfuck and it sucks we should just eliminate it and have
63 # the grid members not render ArrayRef or Collection fields
64 sub _build_computed_field_order {
65   my ($self) = @_;
66   my %excluded = map { $_ => undef } @{ $self->excluded_fields };
67   #treat _$field_name as private and exclude fields with no reader
68   my @names = grep { $_ !~ /^_/ && !exists($excluded{$_})} map { $_->name }
69     grep {
70       !($_->has_type_constraint &&
71         ($_->type_constraint->is_a_type_of('ArrayRef') ||
72          eval {$_->type_constraint->name->isa('Reaction::InterfaceModel::Collection')} ||
73          eval { $_->_isa_metadata->isa('Reaction::InterfaceModel::Collection') }
74         )
75        )  }
76       grep { defined $_->get_read_method }
77         $self->current_collection->member_type->parameter_attributes;
78
79   return $self->sort_by_spec($self->field_order, \@names);
80 }
81
82 around _build_members => sub {
83   my $orig = shift;
84   my $self = shift;
85   $self->member_args->{computed_field_order} ||= $self->computed_field_order;
86 #  $self->member_args->{computed_action_order} ||= [];
87   my $members = $self->$orig(@_);
88
89   # cache everything yo
90 #  for my $member (@$members){
91 #    $member->clear_computed_action_order;
92 #    my $order = $member->computed_action_order;
93 #    @{ $self->member_args->{computed_action_order} } = @$order;
94 #    last;
95 #  }
96
97   return $members;
98 };
99
100 __PACKAGE__->meta->make_immutable;
101
102
103 1;
104
105 __END__;
106
107 =head1 NAME
108
109 Reaction::UI::ViewPort::Collection
110
111 =head1 DESCRIPTION
112
113 This subclass of L<Reaction::UI::ViewPort::Collection> allows you to display a
114 homogenous collection of Reaction::InterfaceModel::Objects as a grid.
115
116 =head1 ATTRIBUTES
117
118 =head2 field_order
119
120 =head2 excluded_fields
121
122 =head2 field_labels
123
124 =head2 computed_field_order
125
126 =head1 INTERNAL METHODS
127
128 These methods, although stable, are subject to change without notice. These are meant
129 to be used only by developers. End users should refrain from using these methods to
130 avoid potential breakages.
131
132 =head1 SEE ALSO
133
134 L<Reaction::UI::ViewPort::Collection>
135
136 =head1 AUTHORS
137
138 See L<Reaction::Class> for authors.
139
140 =head1 LICENSE
141
142 See L<Reaction::Class> for the license.
143
144 =cut