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