1 package Reaction::UI::Window;
4 use Reaction::UI::FocusStack;
8 has ctx => (isa => 'Catalyst', is => 'ro', required => 1);
9 has view_name => (isa => 'Str', is => 'ro', lazy_fail => 1);
10 has content_type => (isa => 'Str', is => 'ro', lazy_fail => 1);
11 has title => (isa => 'Str', is => 'rw', default => sub { 'Untitled window' });
13 # XXX compile failure because the Catalyst::View constraint would be
14 # auto-generated which doesn't work with unions. ::Types::Catalyst needed.
15 #isa => 'Catalyst::View|Reaction::UI::View',
16 isa => 'Object', is => 'ro', lazy_build => 1
19 isa => 'Reaction::UI::FocusStack',
20 is => 'ro', required => 1,
21 default => sub { Reaction::UI::FocusStack->new },
24 implements _build_view => as {
26 return $self->ctx->view($self->view_name);
29 implements flush => as {
35 implements flush_events => as {
39 #I really think we should make a copies of the parameter hashes here
40 #and then as we handle events, delete ethem from the event hashref, so
41 #that it thins down as it makes it down the viewport tree. which would
42 #limit the number of events that get to the children viewports. it wont
43 #save that many subcalls unless there is a lot of child_items, but it's
44 #more about doing the correct thing. It also avoids children viewports
45 #being able to see their parents' events, which leaves the door open for
46 #abuse of the system. thoughts anyone?
48 foreach my $type (qw/query body/) {
49 my $meth = "${type}_parameters";
50 my $param_hash = $ctx->req->$meth;
51 $self->focus_stack->apply_events($ctx, $param_hash)
56 implements flush_view => as {
58 my $res = $self->ctx->res;
59 if ( $res->status =~ /^3/ || length($res->body) ) {
60 $res->content_type('text/plain') unless $res->content_type;
63 $res->body($self->view->render_window($self));
64 $res->content_type($self->content_type);
67 # required by old Renderer::XHTML
69 implements render_viewport => as {
72 return $self->view->render_viewport($self, $vp);
81 Reaction::UI::Window - Container for rendering the UI elements in
85 my $window = Reaction::UI::Window->new(
87 view_name => $view_name,
88 content_type => $content_type,
89 title => $window_title,
92 # More commonly, as Reaction::UI::Controller::Root creates one for you:
93 my $window = $ctx->stash->{window};
95 # Resolve current events and render the view of the UI
96 # elements of this Window:
97 # This is called by the end action of Reaction::UI::Controller::Root
100 # Resolve current events:
101 $window->flush_events();
103 # Render the top ViewPort in the FocusStack of this Window:
104 $window->flush_view();
106 # Render a particular ViewPort:
107 $window->render_viewport($viewport);
110 [% window.render_viewport(self.inner) %]
112 # Add a ViewPort to the UI:
113 $window->focus_stack->push_viewport('Reaction::UI::ViewPort');
117 A Window object is created and stored in the stash by
118 L<Reaction::UI::Controller::Root>, it is used to contain all the
119 elements (ViewPorts) that make up the UI. The Window is rendered in
120 the end action of the Root Controller to make up the page.
122 To add L<ViewPorts|Reaction::UI::ViewPort> to the stack, read the
123 L<Reaction::UI::FocusStack> and L<Reaction::UI::ViewPort> documentation.
125 Several Window attributes are set by
126 L<Reaction::UI::Controller::Root/begin> when a new Window is created,
127 these are as follows:
133 The current L<Catalyst> context object is set.
137 The view_name is set from the L<Reaction::UI::Controller::Root> attributes.
141 The content_type is set from the L<Reaction::UI::Controller::Root> attributes.
145 The title is set from the L<Reaction::UI::Controller::Root>
146 window_title attribute.
156 =item Arguments: $ctx?
160 Retrieve/set the current L<Catalyst> context object.
166 =item Arguments: %viewname?
170 Retrieve/set the name of the L<Catalyst::View> component used to render
171 this Window. If this has not been set, rendering the Window will fail.
177 =item Arguments: $contenttype?
181 Retrieve the content_type for the page. If this has not been set,
182 rendering the Window will fail.
188 =item Arguments: $title?
194 Retrieve/set the title of this page, if not set, it will default to
201 =item Arguments: none
205 Retrieve the L<Catalyst::View> instance, this can be set, or will be
206 instantiated using the L<view_name> class.
212 =item Arguments: none
216 $window->focus_stack->push_viewport('Reaction::UI::ViewPort');
218 Retrieve the L<stack|Reaction::UI::FocusStack> of
219 L<ViewPorts|Reaction::UI::ViewPorts> that contains all the UI elements
220 for this Window. Use L<Reaction::UI::FocusStack/push_viewport> on this
221 to create more elements. An empty FocusStack is created by the
222 Controller::Root when the Window is created.
224 =head2 render_viewport
228 =item Arguments: $viewport
232 $window->render_viewport($viewport);
234 [% window.render_viewport(self.inner) %]
236 Calls render on the L<view> object used by this Window. The following
243 The L<Catalyst> context object.
247 The ViewPort object to be rendered.
255 The string that describes the layout from L<Reaction::UI::ViewPort/layout>.
263 =item Arguments: none
267 Synchronize the current events with all the L<Reaction::UI::ViewPort>
268 objects in the UI, then render the root ViewPort. This is called for
269 you by L<Reaction::UI::Controller::Root/end>.
275 =item Arguments: none
279 Resolves all the current events, first the query parameters then the
280 body parameters, with all the L<Reaction::UI::ViewPort> objects in the
281 UI. This calls L<Reaction::UI::FocusStack/apply_events>. This method
282 is called by L<flush>.
288 =item Arguments: none
292 Renders the page into the L<Catalyst::Response> body, unless the
293 response status is already set to 3xx, or the body has already been
294 filled. This calls L<render_viewport> with the root
295 L<Reaction::UI::ViewPort> from the L<focus_stack>. This method is
300 See L<Reaction::Class> for authors.
304 See L<Reaction::Class> for the license.