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