wire layout_args, which was somehow never connected to the mix
[catagits/Reaction.git] / lib / Reaction / UI / View.pm
CommitLineData
7adfd53f 1package Reaction::UI::View;
2
3use Reaction::Class;
4
5# declaring dependencies
7adfd53f 6use Reaction::UI::LayoutSet;
7use Reaction::UI::RenderingContext;
8a293e2e 8use aliased 'Reaction::UI::Skin';
9use aliased 'Path::Class::Dir';
7adfd53f 10
81393881 11use namespace::clean -except => [ qw(meta) ];
7adfd53f 12
7adfd53f 13
81393881 14has '_widget_cache' => (is => 'ro', default => sub { {} });
8a293e2e 15
81393881 16has '_layout_set_cache' => (is => 'ro', default => sub { {} });
7adfd53f 17
81393881 18has 'app' => (is => 'ro', required => 1);
7adfd53f 19
f94f51bf 20has 'skin_name' => (is => 'ro', required => 1, default => 'default');
7adfd53f 21
81393881 22has 'skin' => (
23 is => 'ro', lazy_build => 1,
24 handles => [ qw(create_layout_set search_path_for_type) ]
25);
7adfd53f 26
81393881 27has 'layout_set_class' => (is => 'ro', lazy_build => 1);
7adfd53f 28
81393881 29has 'rendering_context_class' => (is => 'ro', lazy_build => 1);
00b55ddd 30
31# default view doesn't localize
32sub localize {
33 my($self, $value) = @_;
34 return $value;
35}
36
81393881 37sub _build_layout_set_class {
38 my ($self) = @_;
39 return $self->find_related_class('LayoutSet');
40};
41sub _build_rendering_context_class {
42 my ($self) = @_;
43 return $self->find_related_class('RenderingContext');
44};
45sub _build_skin {
46 my ($self) = @_;
47 Skin->new(
48 name => $self->skin_name, view => $self,
49 # path_to returns a File, not a Dir. Thanks, Catalyst.
50 skin_base_dir => Dir->new($self->app->path_to('share', 'skin')),
51 );
52};
53sub COMPONENT {
54 my ($class, $app, $args) = @_;
55 return $class->new(%{$args||{}}, app => $app);
56};
57sub render_window {
58 my ($self, $window) = @_;
59 my $root_vp = $window->focus_stack->vp_head;
60 my $rctx = $self->create_rendering_context;
61 my ($widget, $args) = $self->render_viewport_args($root_vp);
62 $widget->render(widget => $rctx, $args);
63};
64sub render_viewport_args {
65 my ($self, $vp) = @_;
66 my $layout_set = $self->layout_set_for($vp);
67 my $widget = $self->widget_for($vp, $layout_set);
4ad1eed3 68 my %layout_args = (%{ $vp->layout_args }, viewport => $vp);
69 return ($widget, \%layout_args);
81393881 70};
71sub widget_for {
72 my ($self, $vp, $layout_set) = @_;
73 return
74 $self->_widget_cache->{$layout_set->name}
75 ||= $layout_set->widget_class
76 ->new(
77 view => $self, layout_set => $layout_set
78 );
79};
80sub layout_set_for {
81 my ($self, $vp) = @_;
82 my $lset_name = eval { $vp->layout };
83 confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@;
84 $lset_name = $self->layout_set_name_from_viewport( blessed($vp) )
85 unless (length($lset_name));
86 my $cache = $self->_layout_set_cache;
87 return $cache->{$lset_name} ||= $self->create_layout_set($lset_name);
88};
349a57a4 89
81393881 90#XXX if it ever comes to it: this could be memoized. not bothering yet.
91sub layout_set_name_from_viewport {
92 my ($self, $class) = @_;
93 my ($last) = ($class =~ /.*(?:::ViewPort::)(.+?)$/);
94 #split when a non-uppercase letter meets an uppercase or when an
95 #uppercase letter is followed by another uppercase and then a non-uppercase
96 #FooBar = foo_bar; Foo_Bar = foo_bar; FOOBar = foo_bar; FooBAR = foo_bar
97 my @fragments = map {
98 join("_", split(/(?:(?<=[A-Z])(?=[A-Z][^_A-Z])|(?<=[^_A-Z])(?=[A-Z]))/, $_))
99 } split('::', $last);
100 return lc(join('/', @fragments));
101};
102sub layout_set_file_extension {
103 confess View." is abstract, you must subclass it";
104};
105sub find_related_class {
106 my ($self, $rel) = @_;
107 my $own_class = ref($self) || $self;
108 confess View." is abstract, you must subclass it" if $own_class eq View;
109 foreach my $super ($own_class->meta->class_precedence_list) {
110 next if $super eq View;
111 if ($super =~ /::View::/) {
112 (my $class = $super) =~ s/::View::/::${rel}::/;
113 if (eval { Class::MOP::load_class($class) }) {
114 return $class;
7adfd53f 115 }
116 }
81393881 117 }
118 confess "Unable to find related ${rel} class for ${own_class}";
119};
120sub create_rendering_context {
121 my ($self, @args) = @_;
122 return $self->rendering_context_class->new(
123 $self->rendering_context_args_for(@args),
124 @args,
125 );
126};
127sub rendering_context_args_for {
128 return ();
129};
130sub layout_set_args_for {
131 return ();
132};
7adfd53f 133
81393881 134__PACKAGE__->meta->make_immutable;
8a293e2e 135
f1cd5548 136=pod
137
138=head1 NAME
139
140Reaction::UI::View - Render the UI.
141
142=head1 SYNOPSIS
143
144 package MyApp::View::TT;
145 use base 'Reaction::UI::View::TT';
146
147 __PACKAGE__->config(
148 skin_name => 'MyApp',
149 );
150
151 ## In the Window class:
152 $res->body($self->view->render_window($self));
153
154=head1 DESCRIPTION
155
156Render the viewports in the current window using the chosen skin and
157layoutset, via the matching widgets.
158
159See also:
160
161=over
162
163=item L<Reaction::UI::Controller::Root>
164=item L<Reaction::UI::ViewPort>
165=item L<Reaction::UI::Window>
166=item L<Reaction::UI::LayoutSet>
167=item L<Reaction::UI::Widget>
168
169=back
170
171=head1 ATTRIBUTES
172
173=head2 app
174
175=over
176
177=item Arguments: $app?
178
179=back
180
181The application L<Catalyst> class. This is set at
182L<Catalyst/COMPONENT> time for you.
183
184=head2 skin_name
185
186=over
187
188=item Arguments: $skinname?
189
190=back
191
192The name of the skin to use to render the pages. This should be the
193name of a subdirectory under the F<share/skin> in your application
194directory. The default skin name is C<default>, the default skin is
195provided with Reaction.
196
197See also: L<Reaction::UI::Skin>
198
199=head2 skin
200
201=over
202
203=item Arguments: $skin?
204
205=back
206
207A L<Reaction::UI::Skin> object based on the L</skin_name>. It will be
208created for you if not provided.
209
210=head2 layout_set_class
211
212The class of the L<Reaction::UI::LayoutSet> used to layout the
213view. Defaults to searching down the precedence tree of the View class
214looking for a class of the same name with C<View> replaced with
215C<LayoutSet>.
216
217=head2 rendering_context_class
218
219The class of the L<Reaction::UI::RenderingContext> used to layout the
220view. Defaults to searching down the precedence tree of the View class
221looking for a class of the same name with C<View> replaced with
222C<RenderingContext>.
223
224=head1 METHODS
225
226=head1 AUTHORS
227
228See L<Reaction::Class> for authors.
229
230=head1 LICENSE
231
232See L<Reaction::Class> for the license.
233
234=cut
7adfd53f 235
2361;