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