1 package Reaction::UI::Controller;
5 Catalyst::Component::ACCEPT_CONTEXT
10 use Scalar::Util 'weaken';
11 use namespace::clean -except => [ qw(meta) ];
15 my $c = $self->context;
16 my $focus_stack = $c->stash->{focus_stack};
17 my ($class, @proto_args) = @_;
19 if (my $vp_attr = $c->stack->[-1]->attributes->{ViewPort}) {
20 if (ref($vp_attr) eq 'ARRAY') {
21 $vp_attr = $vp_attr->[0];
23 if (ref($vp_attr) eq 'HASH') {
24 if (my $conf_class = delete $vp_attr->{class}) {
27 %args = %{ $self->merge_config_hashes($vp_attr, {@proto_args}) };
38 if (exists $args{next_action} && !ref($args{next_action})) {
39 $args{next_action} = [ $self, 'redirect_to', $args{next_action} ];
41 $focus_stack->push_viewport($class, %args);
45 return shift->context->stash->{focus_stack}->pop_viewport;
48 sub pop_viewports_to {
50 return $self->context->stash->{focus_stack}->pop_viewports_to($vp);
54 my ($self, $c, $to, $cap, $args, $attrs) = @_;
56 #the confess calls could be changed later to $c->log ?
58 my $reftype = ref($to);
60 $action = $self->action_for($to);
61 confess("Failed to locate action ${to} in " . blessed($self)) unless $action;
62 } elsif($reftype eq 'ARRAY' && @$to == 2){ #is that overkill / too strict?
63 $action = $c->controller($to->[0])->action_for($to->[1]);
64 confess("Failed to locate action $to->[1] in $to->[0]" ) unless $action;
65 } elsif( blessed $to && $to->isa('Catalyst::Action') ){
68 confess("Failed to locate action from ${to}");
71 $cap ||= $c->req->captures;
72 $args ||= $c->req->args;
74 my $uri = $c->uri_for($action, $cap, @$args, $attrs);
75 $c->res->redirect($uri);
78 sub make_context_closure {
79 my($self, $closure) = @_;
80 my $ctx = $self->context;
82 return sub { $closure->($ctx, @_) };
91 Reaction::UI::Controller
95 Base Reaction Controller class. Inherits from:
99 =item L<Catalyst::Controller>
100 =item L<Catalyst::Component::ACCEPT_CONTEXT>
101 =item L<Reaction::Object>
107 =head2 push_viewport $vp_class, %args
109 Creates a new instance of the L<Reaction::UI::ViewPort> class
110 ($vp_class) using the rest of the arguments given (%args). Defaults of
111 the action can be overridden by using the C<ViewPort> key in the
112 controller configuration. For example to override the default number
113 of items in a CRUD list action:
117 list => { ViewPort => { per_page => 50 } },
121 The ViewPort is added to the L<Reaction::UI::Window>'s FocusStack in
122 the stash, and also returned to the calling code.
128 =item L<Reaction::UI::Controller::Root>
129 =item L<Reaction::UI::Window>
133 TODO: explain how next_action as a scalar gets converted to the redirect arrayref thing
137 =head2 pop_viewport_to $vp
139 Call L<Reaction::UI::FocusStack/pop_viewport> or
140 L<Reaction::UI::FocusStack/pop_viewport_to> on
141 the C<< $c->stash->{focus_stack} >>.
143 =head2 redirect_to $c, $to, $captures, $args, $attrs
145 Construct a URI and redirect to it.
151 =item The name of an action in the current controller.
153 =item A L<Catalyst::Action> instance.
155 =item An arrayref of controller name and the name of an action in that
160 $captures and $args default to the current requests $captures and
161 $args if not supplied.
165 See L<Reaction::Class> for authors.
169 See L<Reaction::Class> for the license.