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 {
38 foreach my $type (qw/query body/) {
39 my $meth = "${type}_parameters";
40 my $param_hash = $ctx->req->$meth;
41 $self->focus_stack->apply_events($ctx, $param_hash);
45 implements flush_view => as {
47 return if $self->ctx->res->status =~ /^3/ || length($self->ctx->res->body);
48 $self->ctx->res->body(
49 $self->view->render_window($self)
51 $self->ctx->res->content_type($self->content_type);
54 # required by old Renderer::XHTML
56 implements render_viewport => as {
59 return $self->view->render_viewport($self, $vp);
68 Reaction::UI::Window - Container for rendering the UI elements in
72 my $window = Reaction::UI::Window->new(
74 view_name => $view_name,
75 content_type => $content_type,
76 title => $window_title,
79 # More commonly, as Reaction::UI::Controller::Root creates one for you:
80 my $window = $ctx->stash->{window};
82 # Resolve current events and render the view of the UI
83 # elements of this Window:
84 # This is called by the end action of Reaction::UI::Controller::Root
87 # Resolve current events:
88 $window->flush_events();
90 # Render the top ViewPort in the FocusStack of this Window:
91 $window->flush_view();
93 # Render a particular ViewPort:
94 $window->render_viewport($viewport);
97 [% window.render_viewport(self.inner) %]
99 # Add a ViewPort to the UI:
100 $window->focus_stack->push_viewport('Reaction::UI::ViewPort');
104 A Window object is created and stored in the stash by
105 L<Reaction::UI::Controller::Root>, it is used to contain all the
106 elements (ViewPorts) that make up the UI. The Window is rendered in
107 the end action of the Root Controller to make up the page.
109 To add L<ViewPorts|Reaction::UI::ViewPort> to the stack, read the
110 L<Reaction::UI::FocusStack> and L<Reaction::UI::ViewPort> documentation.
112 Several Window attributes are set by
113 L<Reaction::UI::Controller::Root/begin> when a new Window is created,
114 these are as follows:
120 The current L<Catalyst> context object is set.
124 The view_name is set from the L<Reaction::UI::Controller::Root> attributes.
128 The content_type is set from the L<Reaction::UI::Controller::Root> attributes.
132 The title is set from the L<Reaction::UI::Controller::Root>
133 window_title attribute.
143 =item Arguments: $ctx?
147 Retrieve/set the current L<Catalyst> context object.
153 =item Arguments: %viewname?
157 Retrieve/set the name of the L<Catalyst::View> component used to render
158 this Window. If this has not been set, rendering the Window will fail.
164 =item Arguments: $contenttype?
168 Retrieve the content_type for the page. If this has not been set,
169 rendering the Window will fail.
175 =item Arguments: $title?
181 Retrieve/set the title of this page, if not set, it will default to
188 =item Arguments: none
192 Retrieve the L<Catalyst::View> instance, this can be set, or will be
193 instantiated using the L<view_name> class.
199 =item Arguments: none
203 $window->focus_stack->push_viewport('Reaction::UI::ViewPort');
205 Retrieve the L<stack|Reaction::UI::FocusStack> of
206 L<ViewPorts|Reaction::UI::ViewPorts> that contains all the UI elements
207 for this Window. Use L<Reaction::UI::FocusStack/push_viewport> on this
208 to create more elements. An empty FocusStack is created by the
209 Controller::Root when the Window is created.
211 =head2 render_viewport
215 =item Arguments: $viewport
219 $window->render_viewport($viewport);
221 [% window.render_viewport(self.inner) %]
223 Calls render on the L<view> object used by this Window. The following
230 The L<Catalyst> context object.
234 The ViewPort object to be rendered.
242 The string that describes the layout from L<Reaction::UI::ViewPort/layout>.
250 =item Arguments: none
254 Synchronize the current events with all the L<Reaction::UI::ViewPort>
255 objects in the UI, then render the root ViewPort. This is called for
256 you by L<Reaction::UI::Controller::Root/end>.
262 =item Arguments: none
266 Resolves all the current events, first the query parameters then the
267 body parameters, with all the L<Reaction::UI::ViewPort> objects in the
268 UI. This calls L<Reaction::UI::FocusStack/apply_events>. This method
269 is called by L<flush>.
275 =item Arguments: none
279 Renders the page into the L<Catalyst::Response> body, unless the
280 response status is already set to 3xx, or the body has already been
281 filled. This calls L<render_viewport> with the root
282 L<Reaction::UI::ViewPort> from the L<focus_stack>. This method is
287 See L<Reaction::Class> for authors.
291 See L<Reaction::Class> for the license.