1 package Reaction::UI::ViewPort::Role::Actions;
4 use Reaction::UI::ViewPort::URI;
6 use namespace::clean -except => [ qw(meta) ];
19 has action_prototypes => (
26 has computed_action_order => (
32 sub _build_computed_action_order {
34 my $ordered = $self->sort_by_spec(
35 ($self->has_action_order ? $self->action_order : []),
36 [ keys %{ $self->action_prototypes } ]
45 my $loc = $self->location;
46 my $target = $self->model;
48 foreach my $proto_name ( @{ $self->computed_action_order } ) {
49 my $proto = $self->action_prototypes->{$proto_name};
50 my $uri = $proto->{uri} or confess('uri is required in prototype action');
51 my $label = exists $proto->{label} ? $proto->{label} : $proto_name;
52 my $layout = exists $proto->{layout} ? $proto->{layout} : 'uri';
54 my $action = Reaction::UI::ViewPort::URI->new(
55 location => join ('-', $loc, 'action', $i++),
56 uri => ( ref($uri) eq 'CODE' ? $uri->($target, $ctx) : $uri ),
57 display => ( ref($label) eq 'CODE' ? $label->($target, $ctx) : $label ),
58 layout => ( ref($layout) eq 'CODE' ? $layout->($target, $ctx) : $layout ),
71 Reaction::UI::ViewPort::Role::Actions
75 A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
81 Read-only, lazy-building ArrayRef of URI objects pointing to actions.
83 =head2 action_prototypes
85 A HashRef of prototypes for building the Action links. The prototypes should be
88 my %action_prototypes = (
89 example_action => { label => 'Example Action', uri => $uri_obj },
92 #or you can get fancy and do something like what is below:
94 my($im, $ctx) = @_; #InterfaceModel::Object/Collection, Catalyst Context
98 my($im, $ctx) = @_; #InterfaceModel::Object/Collection, Catalyst Context
99 return return $ctx->uri_for('some_action');
101 my %action_prototypes = (
102 example_action => { label => \&make_label, uri => \&make_uri },
107 User-provided ArrayRef with how the actions should be ordered eg
109 action_order => [qw/view edit delete/]
111 =head2 computed_action_order
113 Read-only lazy-building ARRAY ref. The final computed action order. This may
114 differ from the C<action_order> provided if you any actions were not included
119 =head2 _build_actions
121 Cycle through the C<computed_action_order> and create a new
122 L<ViewPort::URI|Reaction::UI::ViewPort::URI> object for each action using the
125 =head2 _build_computed_action_order
127 Compute the final action ordering by using the provided C<action_order> as a
128 spec to order all the present actions (the keys of C<action_prototypes>)
130 =head1 ACTION PROTOTYPES
132 Action prototypes are simply hashrefs that must contain a C<uri> key and may
133 contain a C<label> key. The label can be anything that the display attribute of
134 L<ViewPort::URI|Reaction::UI::ViewPort::URI> will accept, usually a scalar or a
135 ViewPort. The value for C<uri> may be either a scalar, a L<URI> object (or
136 anything that C<ISA URI>).
138 Additionally, both C<label> and C<uri> can be CODE refs. In this case, the code
139 will be executed at C<_build_actions> time and will recieve two arguments, the
140 value returned by C<model> and the value returned by C<ctx> in that order. Both
141 of these methods should be implemented in the consuming class. By convention,
142 model refers to the target of the action, an C<InterfaceModel::Object> in the
143 case of a member action and an C<InterfaceModel::Collection> in the case of a
144 Collection action. C<ctx> should be the current Catalyst context.
148 See L<Reaction::Class> for authors.
152 See L<Reaction::Class> for the license.