aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/Window.pm
blob: ecf1e570fa28a7bc84eba0c7235fefe5ae97ca72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package Reaction::UI::Window;

use Reaction::Class;
use Reaction::UI::FocusStack;

class Window which {

  has ctx => (isa => 'Catalyst', is => 'ro', required => 1);
  has view_name => (isa => 'Str', is => 'ro', lazy_fail => 1);
  has content_type => (isa => 'Str', is => 'ro', lazy_fail => 1);
  has title => (isa => 'Str', is => 'rw', default => sub { 'Untitled window' });
  has view => (
    # XXX compile failure because the Catalyst::View constraint would be
    # auto-generated which doesn't work with unions. ::Types::Catalyst needed.
    #isa => 'Catalyst::View|Reaction::UI::View',
    isa => 'Object', is => 'ro', lazy_build => 1
  );
  has focus_stack => (
    isa => 'Reaction::UI::FocusStack',
    is => 'ro', required => 1,
    default => sub { Reaction::UI::FocusStack->new },
  );
  
  implements build_view => as {
    my ($self) = @_;
    return $self->ctx->view($self->view_name);
  };
  
  implements flush => as {
    my ($self) = @_;
    $self->flush_events;
    $self->flush_view;
  };
  
  implements flush_events => as {
    my ($self) = @_;
    my $ctx = $self->ctx;
    foreach my $type (qw/query body/) {
      my $meth = "${type}_parameters";
      my $param_hash = $ctx->req->$meth;
      $self->focus_stack->apply_events($ctx, $param_hash);
    }
  };
  
  implements flush_view => as {
    my ($self) = @_;
    return if $self->ctx->res->status =~ /^3/ || length($self->ctx->res->body);
    $self->ctx->res->body(
      $self->view->render_window($self)
    );
    $self->ctx->res->content_type($self->content_type);
  };

  # required by old Renderer::XHTML
  
  implements render_viewport => as {
    my ($self, $vp) = @_;
    return unless $vp;
    return $self->view->render_viewport($self, $vp);
  };

};

1;

=head1 NAME

Reaction::UI::Window - Container for rendering the UI elements in

=head1 SYNOPSIS

  my $window = Reaction::UI::Window->new(
    ctx => $ctx,
    view_name => $view_name,
    content_type => $content_type,
    title => $window_title,
  );

  # More commonly, as Reaction::UI::RootController creates one for you:
  my $window = $ctx->stash->{window};

  # Resolve current events and render the view of the UI 
  #  elements of this Window:
  # This is called by the end action of Reaction::UI::RootController
  $window->flush();

  # Resolve current events:
  $window->flush_events();

  # Render the top ViewPort in the FocusStack of this Window:
  $window->flush_view();

  # Render a particular ViewPort:
  $window->render_viewport($viewport);

  # Or in a template:
  [% window.render_viewport(self.inner) %]

  # Add a ViewPort to the UI:
  $window->focus_stack->push_viewport('Reaction::UI::ViewPort');

=head1 DESCRIPTION

A Window object is created and stored in the stash by
L<Reaction::UI::RootController>, it is used to contain all the
elements (ViewPorts) that make up the UI. The Window is rendered in
the end action of the RootController to make up the page.

To add L<ViewPorts|Reaction::UI::ViewPort> to the stack, read the
L<Reaction::UI::FocusStack> and L<Reaction::UI::ViewPort> documentation.

Several Window attributes are set by
L<Reaction::UI::RootController/begin> when a new Window is created,
these are as follows:

=over

=item ctx

The current L<Catalyst> context object is set.

=item view_name

The view_name is set from the L<Reaction::UI::RootController> attributes.

=item content_type

The content_type is set from the L<Reaction::UI::RootController> attributes.

=item window_title

The window_title is set from the L<Reaction::UI::RootController> attributes.

=back

=head1 METHODS

=head2 ctx

=over

=item Arguments: none

=back

Retrieve the current L<Catalyst> context object.

=head2 view_name

=over

=item Arguments: none

=back

Retrieve the name of the L<Catalyst::View> component used to render
this Window. If this has not been set, rendering the Window will fail.

=head2 content_type

=over

=item Arguments: none

=back

Retrieve the content_type for the page. If this has not been set,
rendering the Window will fail.

=head2 title

=over

=item Arguments: $title?

=back

  [% window.title %]

Retrieve the title of this page, if not set, it will default to
"Untitled window".

=head2 view

=over

=item Arguments: none

=back

Retrieve the L<Catalyst::View> instance, this can be set, or will be
instantiated using the L<view_name> class.

=head2 focus_stack

=over

=item Arguments: none

=back

  $window->focus_stack->push_viewport('Reaction::UI::ViewPort');

Retrieve the L<stack|Reaction::UI::FocusStack> of
L<ViewPorts|Reaction::UI::ViewPorts> that contains all the UI elements
for this Window. Use L<Reaction::UI::FocusStack/push_viewport> on this
to create more elements. An empty FocusStack is created by the
RootController when the Window is created.

=head2 render_viewport

=over

=item Arguments: $viewport

=back

  $window->render_viewport($viewport);

  [% window.render_viewport(self.inner) %]

Calls render on the L<view> object used by this Window. The following
arguments are given:

=over

=item ctx

The L<Catalyst> context object.

=item self

The ViewPort object to be rendered.

=item window

The Window object.

=item type

The string that describes the layout from L<Reaction::UI::ViewPort/layout>.

=back

=head2 flush

=over

=item Arguments: none

=back

Synchronize the current events with all the L<Reaction::UI::ViewPort>
objects in the UI, then render the root ViewPort. This is called for
you by L<Reaction::UI::RootController/end>.

=head2 flush_events

=over

=item Arguments: none

=back

Resolves all the current events, first the query parameters then the
body parameters, with all the L<Reaction::UI::ViewPort> objects in the
UI. This calls L<Reaction::UI::FocusStack/apply_events>. This method
is called by L<flush>.

=head2 flush_view

=over

=item Arguments: none

=back

Renders the page into the L<Catalyst::Response> body, unless the
response status is already set to 3xx, or the body has already been
filled. This calls L<render_viewport> with the root
L<Reaction::UI::ViewPort> from the L<focus_stack>. This method is
called by L<flush>.

=head1 AUTHORS

See L<Reaction::Class> for authors.

=head1 LICENSE

See L<Reaction::Class> for the license.

=cut