Commit | Line | Data |
89b70ba7 |
1 | package Reaction::UI::Controller::Root; |
7adfd53f |
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 | |
8996aa3f |
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 | ); |
7adfd53f |
17 | |
18 | sub 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 | |
35 | sub end :Private { |
1810d302 |
36 | my ($self, $ctx) = @_; |
37 | $ctx->stash->{window}->flush; |
7adfd53f |
38 | } |
39 | |
cb92a3a3 |
40 | sub 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 | |
47 | sub 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 |
54 | 1; |
55 | |
56 | =head1 NAME |
57 | |
68f88d4e |
58 | Reaction::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 | |
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 | |
f1cd5548 |
85 | At the C<begin> of each request, the Window object is |
bdc404da |
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 | |
f1cd5548 |
90 | =head1 ATTRIBUTES |
7adfd53f |
91 | |
92 | =head2 view_name |
93 | |
94 | =over |
95 | |
96 | =item Arguments: $viewname? |
97 | |
98 | =back |
99 | |
c2bd9d41 |
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'. |
7adfd53f |
102 | |
103 | =head2 content_type |
104 | |
105 | =over |
106 | |
107 | =item Arguments: $contenttype? |
108 | |
109 | =back |
110 | |
c2bd9d41 |
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'. |
7adfd53f |
113 | |
114 | =head2 window_title |
115 | |
116 | =over |
117 | |
118 | =item Arguments: $windowtitle? |
119 | |
120 | =back |
121 | |
c2bd9d41 |
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. |
7adfd53f |
124 | |
f1cd5548 |
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 | |
7adfd53f |
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 |