undo r1236
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Role / Actions.pm
CommitLineData
ddccc6a2 1package Reaction::UI::ViewPort::Role::Actions;
b8faba69 2
3use Reaction::Role;
37728bba 4use Reaction::UI::ViewPort::URI;
b8faba69 5
81393881 6use namespace::clean -except => [ qw(meta) ];
7
37728bba 8has actions => (
9 is => 'ro',
10 isa => 'ArrayRef',
11 lazy_build => 1
12);
13
14has action_order => (
15 is => 'ro',
16 isa => 'ArrayRef'
17);
18
565a1fc7 19has action_filter => (
20 isa => 'CodeRef', is => 'ro',
565a1fc7 21);
22
37728bba 23has action_prototypes => (
24 is => 'ro',
25 isa => 'HashRef',
26 required => 1,
27 default => sub{ {} }
28);
29
30has computed_action_order => (
31 is => 'ro',
32 isa => 'ArrayRef',
33 lazy_build => 1
34);
35
0caf173e 36sub _filter_action_list {
37 my $self = shift;
38 my $actions = [keys %{$self->action_prototypes}];
39 return $self->has_action_filter ?
40 $self->action_filter->($actions, $self->model)
41 : $actions;
42}
43
37728bba 44sub _build_computed_action_order {
45 my $self = shift;
46 my $ordered = $self->sort_by_spec(
47 ($self->has_action_order ? $self->action_order : []),
0caf173e 48 $self->_filter_action_list
37728bba 49 );
0caf173e 50 return $ordered;
37728bba 51}
81393881 52
81393881 53sub _build_actions {
54 my ($self) = @_;
55 my (@act, $i);
56 my $ctx = $self->ctx;
57 my $loc = $self->location;
37728bba 58 my $target = $self->model;
59
60 foreach my $proto_name ( @{ $self->computed_action_order } ) {
61 my $proto = $self->action_prototypes->{$proto_name};
62 my $uri = $proto->{uri} or confess('uri is required in prototype action');
63 my $label = exists $proto->{label} ? $proto->{label} : $proto_name;
a33275e9 64 my $layout = exists $proto->{layout} ? $proto->{layout} : 'uri';
37728bba 65
66 my $action = Reaction::UI::ViewPort::URI->new(
67 location => join ('-', $loc, 'action', $i++),
68 uri => ( ref($uri) eq 'CODE' ? $uri->($target, $ctx) : $uri ),
69 display => ( ref($label) eq 'CODE' ? $label->($target, $ctx) : $label ),
08c27c5b 70 layout => $layout,
37728bba 71 );
81393881 72 push(@act, $action);
73 }
74 return \@act;
37728bba 75}
81393881 76
b8faba69 771;
2dba7201 78
79__END__;
80
81=head1 NAME
82
83Reaction::UI::ViewPort::Role::Actions
84
85=head1 DESCRIPTION
86
87A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
88
89=head1 ATTRIBUTES
90
91=head2 actions
92
91140599 93Read-only, lazy-building ArrayRef of URI objects pointing to actions.
3be50b19 94
2dba7201 95=head2 action_prototypes
96
3be50b19 97A HashRef of prototypes for building the Action links. The prototypes should be
98composed like these:
99
100 my %action_prototypes = (
101 example_action => { label => 'Example Action', uri => $uri_obj },
102 );
103
104 #or you can get fancy and do something like what is below:
105 sub make_label{
106 my($im, $ctx) = @_; #InterfaceModel::Object/Collection, Catalyst Context
107 return 'label_text';
108 }
109 sub make_uri{
110 my($im, $ctx) = @_; #InterfaceModel::Object/Collection, Catalyst Context
111 return return $ctx->uri_for('some_action');
112 }
113 my %action_prototypes = (
114 example_action => { label => \&make_label, uri => \&make_uri },
115 );
116
117=head2 action_order
118
119User-provided ArrayRef with how the actions should be ordered eg
120
121 action_order => [qw/view edit delete/]
122
123=head2 computed_action_order
124
91140599 125Read-only lazy-building ARRAY ref. The final computed action order. This may
126differ from the C<action_order> provided if you any actions were not included
127in that list.
128
129=head1 METHODS
130
131=head2 _build_actions
132
133Cycle through the C<computed_action_order> and create a new
134L<ViewPort::URI|Reaction::UI::ViewPort::URI> object for each action using the
135provided prototypes.
136
137=head2 _build_computed_action_order
138
139Compute the final action ordering by using the provided C<action_order> as a
140spec to order all the present actions (the keys of C<action_prototypes>)
141
142=head1 ACTION PROTOTYPES
143
144Action prototypes are simply hashrefs that must contain a C<uri> key and may
145contain a C<label> key. The label can be anything that the display attribute of
146L<ViewPort::URI|Reaction::UI::ViewPort::URI> will accept, usually a scalar or a
147ViewPort. The value for C<uri> may be either a scalar, a L<URI> object (or
148anything that C<ISA URI>).
149
150Additionally, both C<label> and C<uri> can be CODE refs. In this case, the code
151will be executed at C<_build_actions> time and will recieve two arguments, the
152value returned by C<model> and the value returned by C<ctx> in that order. Both
153of these methods should be implemented in the consuming class. By convention,
154model refers to the target of the action, an C<InterfaceModel::Object> in the
155case of a member action and an C<InterfaceModel::Collection> in the case of a
156Collection action. C<ctx> should be the current Catalyst context.
3be50b19 157
2dba7201 158=head1 AUTHORS
159
160See L<Reaction::Class> for authors.
161
162=head1 LICENSE
163
164See L<Reaction::Class> for the license.
165
166=cut