branching for cleanups on collection-group
[catagits/Reaction.git] / lib / Reaction / UI / Controller / Root.pm
1 package Reaction::UI::Controller::Root;
2
3 use base qw/Reaction::UI::Controller/;
4 use Reaction::Class;
5 use Reaction::UI::Window;
6
7 __PACKAGE__->config(
8   view_name => 'XHTML',
9   content_type => 'text/html',
10 );
11
12 has 'view_name' => (isa => 'Str', is => 'rw', required => 1);
13 has 'content_type' => (isa => 'Str', is => 'rw', required => 1);
14 has 'window_title' => (
15   isa => 'Str', is => 'rw', predicate => 'has_window_title'
16 );
17
18 sub begin :Private {
19   my ($self, $ctx) = @_;
20   $ctx->stash(
21     window => Reaction::UI::Window->new(
22       ctx => $ctx,
23       view_name => $self->view_name,
24       content_type => $self->content_type,
25       ($self->has_window_title
26          ? (title => $self->window_title)
27            : ()),
28     )
29   );
30   my $focus_stack = $ctx->stash->{window}->focus_stack;
31   $focus_stack->loc_prefix('r-vp');
32   $ctx->stash(focus_stack => $focus_stack);
33 }
34
35 sub end :Private {
36   my ($self, $ctx) = @_;
37   $ctx->stash->{window}->flush;
38 }
39
40 sub error_404 :Private {
41   my ($self, $c) = @_;
42   $c->res->body("Error 404: Not Found");
43   $c->res->status(404);
44   $c->res->content_type('text/plain');
45 }
46
47 sub error_403 :Private {
48   my ($self, $c) = @_;
49   $c->res->body("Error 403: Forbidden");
50   $c->res->status(403);
51   $c->res->content_type('text/plain');
52 }
53
54 1;
55
56 =head1 NAME
57
58 Reaction::UI::Controller::Root - Base component for the Root Controller
59
60 =head1 SYNOPSIS
61
62   package MyApp::Controller::Root;
63   use base 'Reaction::UI::Controller::Root';
64
65   __PACKAGE__->config(
66     view_name => 'Site',
67     window_title => 'Reaction Test App',
68     namespace => ''
69   );
70
71   # Create UI elements:
72   $c->self->push_viewport('Reaction::UI::ViewPort', %args);
73
74   # Access the window title in a template:
75   [% window.title %]
76
77 =head1 DESCRIPTION
78
79 Using this module as a base component for your L<Catalyst> Root
80 Controller provides automatic creation of a L<Reaction::UI::Window>
81 object containing an empty L<Reaction::UI::FocusStack> for your UI
82 elements. The stack is also resolved and rendered for you in the
83 C<end> action.
84
85 At the C<begin> of each request, the Window object is
86 created using the configured L</view_name>, L</content_type> and
87 L</window_title>. These thus should be directly changed on the stashed
88 window object at runtime, if needed.
89
90 =head1 ATTRIBUTES
91
92 =head2 view_name
93
94 =over
95
96 =item Arguments: $viewname?
97
98 =back
99
100 Set or retrieve the classname of the view used to render the UI. Can
101 also be set by a call to config. Defaults to 'XHTML'.
102
103 =head2 content_type
104
105 =over
106
107 =item Arguments: $contenttype?
108
109 =back
110
111 Set or retrieve the content type of the page created. Can also be set
112 by a call to config or in a config file. Defaults to 'text/html'.
113
114 =head2 window_title
115
116 =over
117
118 =item Arguments: $windowtitle?
119
120 =back
121
122 Set or retrieve the title of the page created. Can also be set by a
123 call to config or in a config file. No default.
124
125 =head1 ACTIONS
126
127 =head2 begin
128
129 Stuffs a new L<Reaction::UI::Window> object into the stash, using the
130 L</view_name> and L</content_type> provided in the
131 L<configuration|/SYNOPSIS>.
132
133 Make sure you call this base C<begin> action if writing your own.
134
135 =head2 end
136
137 Draws the UI via the L<Reaction::UI::Window/flush> method.
138
139 =head1 METHODS
140
141 =head2 error_404
142
143 Sets $c->res (the L<Catalyst::Response>) body, status and content type
144 to output a 404 (File not found) error.
145
146 =head2 error_403
147
148 Sets $c->res (the L<Catalyst::Response>) body, status and content type
149 to output a 403 (Forbidden) error.
150
151
152 =head1 AUTHORS
153
154 See L<Reaction::Class> for authors.
155
156 =head1 LICENSE
157
158 See L<Reaction::Class> for the license.
159
160 =cut