moved shit to trunk
[catagits/Reaction.git] / lib / Reaction / UI / Controller.pm
1 package Reaction::UI::Controller;
2
3 use base qw/Catalyst::Controller::BindLex Reaction::Object/;
4 use Reaction::Class;
5
6 sub push_viewport {
7   my $self = shift;
8   my $focus_stack :Stashed;
9   my ($class, @proto_args) = @_;
10   my %args;
11   my $c = Catalyst::Controller::BindLex::_get_c_obj(4);
12   if (my $vp_attr = $c->stack->[-1]->attributes->{ViewPort}) {
13     if (ref($vp_attr) eq 'ARRAY') {
14       $vp_attr = $vp_attr->[0];
15     }
16     if (ref($vp_attr) eq 'HASH') {
17       if (my $conf_class = delete $vp_attr->{class}) {
18         $class = $conf_class;
19       }
20       %args = (%$vp_attr, @proto_args);
21     } else {
22       $class = $vp_attr;
23       %args = @proto_args;
24     }
25   } else {
26     %args = @proto_args;
27   }
28
29   $args{ctx} = $c;
30
31   if (exists $args{next_action} && !ref($args{next_action})) {
32     $args{next_action} = [ $self, 'redirect_to', $args{next_action} ];
33   }
34   $focus_stack->push_viewport($class, %args);
35 }
36
37 sub pop_viewport {
38   my $focus_stack :Stashed;
39   return $focus_stack->pop_viewport;
40 }
41
42 sub pop_viewports_to {
43   my ($self, $vp) = @_;
44   my $focus_stack :Stashed;
45   return $focus_stack->pop_viewports_to($vp);
46 }
47
48 sub redirect_to {
49   my ($self, $c, $to, $cap, $args, $attrs) = @_;
50
51   #the confess calls could be changed later to $c->log ?
52   my $action;
53   if(!ref $to){
54       $action = $self->action_for($to);
55       confess("Failed to locate action ${to} in " . $self->blessed) unless $action;
56   }
57   elsif( blessed $to && $to->isa('Catalyst::Action') ){
58       $action = $to;
59   } elsif(ref $action eq 'ARRAY' && @$action == 2){ #is that overkill / too strict?
60       $action = $c->controller($to->[0])->action_for($to->[1]);
61       confess("Failed to locate action $to->[1] in $to->[0]" ) unless $action;
62   } else{
63       confess("Failed to locate action from ${to}");
64   }
65
66   $cap ||= $c->req->captures;
67   $args ||= $c->req->args;
68   $attrs ||= {};
69   my $uri = $c->uri_for($action, $cap, @$args, $attrs);
70   $c->res->redirect($uri);
71 }
72
73 1;