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 #$res->content_type($self->content_type);
60 return if $res->status =~ /^3/ || length($res->body);
61 $res->body($self->view->render_window($self));
62 $res->content_type($self->content_type);
65 # required by old Renderer::XHTML
67 implements render_viewport => as {
70 return $self->view->render_viewport($self, $vp);
79 Reaction::UI::Window - Container for rendering the UI elements in
83 my $window = Reaction::UI::Window->new(
85 view_name => $view_name,
86 content_type => $content_type,
87 title => $window_title,
90 # More commonly, as Reaction::UI::Controller::Root creates one for you:
91 my $window = $ctx->stash->{window};
93 # Resolve current events and render the view of the UI
94 # elements of this Window:
95 # This is called by the end action of Reaction::UI::Controller::Root
98 # Resolve current events:
99 $window->flush_events();
101 # Render the top ViewPort in the FocusStack of this Window:
102 $window->flush_view();
104 # Render a particular ViewPort:
105 $window->render_viewport($viewport);
108 [% window.render_viewport(self.inner) %]
110 # Add a ViewPort to the UI:
111 $window->focus_stack->push_viewport('Reaction::UI::ViewPort');
115 A Window object is created and stored in the stash by
116 L<Reaction::UI::Controller::Root>, it is used to contain all the
117 elements (ViewPorts) that make up the UI. The Window is rendered in
118 the end action of the Root Controller to make up the page.
120 To add L<ViewPorts|Reaction::UI::ViewPort> to the stack, read the
121 L<Reaction::UI::FocusStack> and L<Reaction::UI::ViewPort> documentation.
123 Several Window attributes are set by
124 L<Reaction::UI::Controller::Root/begin> when a new Window is created,
125 these are as follows:
131 The current L<Catalyst> context object is set.
135 The view_name is set from the L<Reaction::UI::Controller::Root> attributes.
139 The content_type is set from the L<Reaction::UI::Controller::Root> attributes.
143 The title is set from the L<Reaction::UI::Controller::Root>
144 window_title attribute.
154 =item Arguments: $ctx?
158 Retrieve/set the current L<Catalyst> context object.
164 =item Arguments: %viewname?
168 Retrieve/set the name of the L<Catalyst::View> component used to render
169 this Window. If this has not been set, rendering the Window will fail.
175 =item Arguments: $contenttype?
179 Retrieve the content_type for the page. If this has not been set,
180 rendering the Window will fail.
186 =item Arguments: $title?
192 Retrieve/set the title of this page, if not set, it will default to
199 =item Arguments: none
203 Retrieve the L<Catalyst::View> instance, this can be set, or will be
204 instantiated using the L<view_name> class.
210 =item Arguments: none
214 $window->focus_stack->push_viewport('Reaction::UI::ViewPort');
216 Retrieve the L<stack|Reaction::UI::FocusStack> of
217 L<ViewPorts|Reaction::UI::ViewPorts> that contains all the UI elements
218 for this Window. Use L<Reaction::UI::FocusStack/push_viewport> on this
219 to create more elements. An empty FocusStack is created by the
220 Controller::Root when the Window is created.
222 =head2 render_viewport
226 =item Arguments: $viewport
230 $window->render_viewport($viewport);
232 [% window.render_viewport(self.inner) %]
234 Calls render on the L<view> object used by this Window. The following
241 The L<Catalyst> context object.
245 The ViewPort object to be rendered.
253 The string that describes the layout from L<Reaction::UI::ViewPort/layout>.
261 =item Arguments: none
265 Synchronize the current events with all the L<Reaction::UI::ViewPort>
266 objects in the UI, then render the root ViewPort. This is called for
267 you by L<Reaction::UI::Controller::Root/end>.
273 =item Arguments: none
277 Resolves all the current events, first the query parameters then the
278 body parameters, with all the L<Reaction::UI::ViewPort> objects in the
279 UI. This calls L<Reaction::UI::FocusStack/apply_events>. This method
280 is called by L<flush>.
286 =item Arguments: none
290 Renders the page into the L<Catalyst::Response> body, unless the
291 response status is already set to 3xx, or the body has already been
292 filled. This calls L<render_viewport> with the root
293 L<Reaction::UI::ViewPort> from the L<focus_stack>. This method is
298 See L<Reaction::Class> for authors.
302 See L<Reaction::Class> for the license.