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