1 package Reaction::UI::Controller;
5 Catalyst::Component::ACCEPT_CONTEXT
10 use Scalar::Util 'weaken';
14 my $c = $self->context;
15 my $focus_stack = $c->stash->{focus_stack};
16 my ($class, @proto_args) = @_;
18 if (my $vp_attr = $c->stack->[-1]->attributes->{ViewPort}) {
19 if (ref($vp_attr) eq 'ARRAY') {
20 $vp_attr = $vp_attr->[0];
22 if (ref($vp_attr) eq 'HASH') {
23 if (my $conf_class = delete $vp_attr->{class}) {
26 %args = %{ $self->merge_config_hashes($vp_attr, {@proto_args}) };
37 if (exists $args{next_action} && !ref($args{next_action})) {
38 $args{next_action} = [ $self, 'redirect_to', $args{next_action} ];
40 $focus_stack->push_viewport($class, %args);
44 return shift->context->stash->{focus_stack}->pop_viewport;
47 sub pop_viewports_to {
49 return $self->context->stash->{focus_stack}->pop_viewports_to($vp);
53 my ($self, $c, $to, $cap, $args, $attrs) = @_;
55 #the confess calls could be changed later to $c->log ?
57 my $reftype = ref($to);
59 $action = $self->action_for($to);
60 confess("Failed to locate action ${to} in " . blessed($self)) unless $action;
61 } elsif($reftype eq 'ARRAY' && @$to == 2){ #is that overkill / too strict?
62 $action = $c->controller($to->[0])->action_for($to->[1]);
63 confess("Failed to locate action $to->[1] in $to->[0]" ) unless $action;
64 } elsif( blessed $to && $to->isa('Catalyst::Action') ){
67 confess("Failed to locate action from ${to}");
70 $cap ||= $c->req->captures;
71 $args ||= $c->req->args;
73 my $uri = $c->uri_for($action, $cap, @$args, $attrs);
74 $c->res->redirect($uri);
77 sub make_context_closure {
78 my($self, $closure) = @_;
79 my $ctx = $self->context;
81 return sub { $closure->($ctx, @_) };
90 Reaction::UI::Controller
94 Base Reaction Controller class. Inherits from:
98 =item L<Catalyst::Controller>
99 =item L<Catalyst::Component::ACCEPT_CONTEXT>
100 =item L<Reaction::Object>
106 =head2 push_viewport $vp_class, %args
108 Creates a new instance of the L<Reaction::UI::ViewPort> class
109 ($vp_class) using the rest of the arguments given (%args). Defaults of
110 the action can be overridden by using the C<ViewPort> key in the
111 controller configuration. For example to override the default number
112 of items in a CRUD list action:
116 list => { ViewPort => { per_page => 50 } },
120 The ViewPort is added to the L<Reaction::UI::Window>'s FocusStack in
121 the stash, and also returned to the calling code.
127 =item L<Reaction::UI::Controller::Root>
128 =item L<Reaction::UI::Window>
132 TODO: explain how next_action as a scalar gets converted to the redirect arrayref thing
136 =head2 pop_viewport_to $vp
138 Call L<Reaction::UI::FocusStack/pop_viewport> or
139 L<Reaction::UI::FocusStack/pop_viewport_to> on
140 the C<< $c->stash->{focus_stack} >>.
142 =head2 redirect_to $c, $to, $captures, $args, $attrs
144 Construct a URI and redirect to it.
150 =item The name of an action in the current controller.
152 =item A L<Catalyst::Action> instance.
154 =item An arrayref of controller name and the name of an action in that
159 $captures and $args default to the current requests $captures and
160 $args if not supplied.
164 See L<Reaction::Class> for authors.
168 See L<Reaction::Class> for the license.