allowing empty label in grid->header
[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(keys(%$protos));
39     }
40     return 1;
41   },
42 );
43
44 ####################################
45 sub _build_member_class { WithActions };
46
47 sub _build_field_labels {
48   my $self = shift;
49   my %labels = %{$self->_raw_field_labels};
50   for my $field ( @{$self->computed_field_order}) {
51     next if defined $labels{$field};
52     $labels{$field} = join(' ', map{ ucfirst } split('_', $field));
53   }
54   return \%labels;
55 }
56
57 sub _build_field_order { []; }
58
59 sub _build_excluded_fields { []; }
60
61 #this is a total clusterfuck and it sucks we should just eliminate it and have
62 # the grid members not render ArrayRef or Collection fields
63 sub _build_computed_field_order {
64   my ($self) = @_;
65   my %excluded = map { $_ => undef } @{ $self->excluded_fields };
66   #treat _$field_name as private and exclude fields with no reader
67   my @names = grep { $_ !~ /^_/ && !exists($excluded{$_})} map { $_->name }
68     grep {
69       !($_->has_type_constraint &&
70         ($_->type_constraint->is_a_type_of('ArrayRef') ||
71          eval {$_->type_constraint->name->isa('Reaction::InterfaceModel::Collection')} ||
72          eval { $_->_isa_metadata->isa('Reaction::InterfaceModel::Collection') }
73         )
74        )  }
75       grep { defined $_->get_read_method }
76         $self->current_collection->member_type->parameter_attributes;
77
78   return $self->sort_by_spec($self->field_order, \@names);
79 }
80
81 around _build_members => sub {
82   my $orig = shift;
83   my $self = shift;
84   $self->member_args->{computed_field_order} ||= $self->computed_field_order;
85   $self->member_args->{computed_action_order} ||= [];
86   my $members = $self->$orig(@_);
87
88   # cache everything yo
89   for my $member (@$members){
90     $member->clear_computed_action_order;
91     my $order = $member->computed_action_order;
92     @{ $self->member_args->{computed_action_order} } = @$order;
93     last;
94   }
95
96   return $members;
97 };
98
99 __PACKAGE__->meta->make_immutable;
100
101
102 1;
103
104 __END__;
105
106 =head1 NAME
107
108 Reaction::UI::ViewPort::Collection
109
110 =head1 DESCRIPTION
111
112 This subclass of L<Reaction::UI::ViewPort::Collection> allows you to display a
113 homogenous collection of Reaction::InterfaceModel::Objects as a grid.
114
115 =head1 ATTRIBUTES
116
117 =head2 field_order
118
119 =head2 excluded_fields
120
121 =head2 field_labels
122
123 =head2 _raw_field_labels
124
125 =head2 computed_field_order
126
127 =head2 member_action_count
128
129 =head1 INTERNAL METHODS
130
131 These methods, although stable, are subject to change without notice. These are meant
132 to be used only by developers. End users should refrain from using these methods to
133 avoid potential breakages.
134
135 =head1 SEE ALSO
136
137 L<Reaction::UI::ViewPort::Collection>
138
139 =head1 AUTHORS
140
141 See L<Reaction::Class> for authors.
142
143 =head1 LICENSE
144
145 See L<Reaction::Class> for the license.
146
147 =cut