1 package Reaction::UI::View;
5 # declaring dependencies
6 use Reaction::UI::LayoutSet;
7 use Reaction::UI::RenderingContext;
8 use aliased 'Reaction::UI::Skin';
9 use aliased 'Path::Class::Dir';
11 use namespace::clean -except => [ qw(meta) ];
14 has '_widget_cache' => (is => 'ro', default => sub { {} });
16 has '_layout_set_cache' => (is => 'ro', default => sub { {} });
18 has 'app' => (is => 'ro', required => 1);
20 has 'skin_name' => (is => 'ro', required => 1, default => 'default');
23 is => 'ro', lazy_build => 1,
24 handles => [ qw(create_layout_set search_path_for_type) ]
27 has 'layout_set_class' => (is => 'ro', lazy_build => 1);
29 has 'rendering_context_class' => (is => 'ro', lazy_build => 1);
31 # default view doesn't localize
33 my($self, $value) = @_;
37 sub _build_layout_set_class {
39 return $self->find_related_class('LayoutSet');
41 sub _build_rendering_context_class {
43 return $self->find_related_class('RenderingContext');
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')),
54 my ($class, $app, $args) = @_;
55 return $class->new(%{$args||{}}, app => $app);
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);
65 # From 2007-07-11 @ #reaction (so don't blame me if you think this violates some stupid school of thought)
66 #15:09 <@groditi> mst: one quick thing though. do you remember what layout_args is?
67 #15:10 <@mst> isn't it crap to be passed to the top level widget render?
68 #15:11 <@groditi> that's what i thought..
69 #15:11 <@groditi> but it's kind of a no-op. there's an attr but nothing uses it
70 #15:11 <@groditi> i cant figure out if the data you give it ends up going anywhere
71 #15:12 <@groditi> although it'd be cool if anything you put there ended up being part of %_ in layout/widget land
72 #15:12 <@groditi> which would solve so fucking many of my gripes with widget/layout
73 #15:14 <@mst> I thought that was what it was supposed to do
74 #15:14 <@mst> wire it up :)
76 sub render_viewport_args {
78 my $layout_set = $self->layout_set_for($vp);
79 my $widget = $self->widget_for($vp, $layout_set);
80 my %layout_args = (%{ $vp->layout_args }, viewport => $vp);
81 return ($widget, \%layout_args);
84 my ($self, $vp, $layout_set) = @_;
86 $self->_widget_cache->{$layout_set->name}
87 ||= $layout_set->widget_class
89 view => $self, layout_set => $layout_set
94 my $lset_name = eval { $vp->layout };
95 confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@;
96 $lset_name = $self->layout_set_name_from_viewport( blessed($vp) )
97 unless (length($lset_name));
98 my $cache = $self->_layout_set_cache;
99 return $cache->{$lset_name} ||= $self->create_layout_set($lset_name);
102 #XXX if it ever comes to it: this could be memoized. not bothering yet.
103 sub layout_set_name_from_viewport {
104 my ($self, $class) = @_;
105 my ($last) = ($class =~ /.*(?:::ViewPort::)(.+?)$/);
106 #split when a non-uppercase letter meets an uppercase or when an
107 #uppercase letter is followed by another uppercase and then a non-uppercase
108 #FooBar = foo_bar; Foo_Bar = foo_bar; FOOBar = foo_bar; FooBAR = foo_bar
109 my @fragments = map {
110 join("_", split(/(?:(?<=[A-Z])(?=[A-Z][^_A-Z])|(?<=[^_A-Z])(?=[A-Z]))/, $_))
111 } split('::', $last);
112 return lc(join('/', @fragments));
114 sub layout_set_file_extension {
115 confess View." is abstract, you must subclass it";
117 sub find_related_class {
118 my ($self, $rel) = @_;
119 my $own_class = ref($self) || $self;
120 confess View." is abstract, you must subclass it" if $own_class eq View;
121 foreach my $super ($own_class->meta->class_precedence_list) {
122 next if $super eq View;
123 if ($super =~ /::View::/) {
124 (my $class = $super) =~ s/::View::/::${rel}::/;
125 if (eval { Class::MOP::load_class($class) }) {
130 confess "Unable to find related ${rel} class for ${own_class}";
132 sub create_rendering_context {
133 my ($self, @args) = @_;
134 return $self->rendering_context_class->new(
135 $self->rendering_context_args_for(@args),
139 sub rendering_context_args_for {
142 sub layout_set_args_for {
146 __PACKAGE__->meta->make_immutable;
152 Reaction::UI::View - Render the UI.
156 package MyApp::View::TT;
157 use base 'Reaction::UI::View::TT';
160 skin_name => 'MyApp',
163 ## In the Window class:
164 $res->body($self->view->render_window($self));
168 Render the viewports in the current window using the chosen skin and
169 layoutset, via the matching widgets.
175 =item L<Reaction::UI::Controller::Root>
176 =item L<Reaction::UI::ViewPort>
177 =item L<Reaction::UI::Window>
178 =item L<Reaction::UI::LayoutSet>
179 =item L<Reaction::UI::Widget>
189 =item Arguments: $app?
193 The application L<Catalyst> class. This is set at
194 L<Catalyst/COMPONENT> time for you.
200 =item Arguments: $skinname?
204 The name of the skin to use to render the pages. This should be the
205 name of a subdirectory under the F<share/skin> in your application
206 directory. The default skin name is C<default>, the default skin is
207 provided with Reaction.
209 See also: L<Reaction::UI::Skin>
215 =item Arguments: $skin?
219 A L<Reaction::UI::Skin> object based on the L</skin_name>. It will be
220 created for you if not provided.
222 =head2 layout_set_class
224 The class of the L<Reaction::UI::LayoutSet> used to layout the
225 view. Defaults to searching down the precedence tree of the View class
226 looking for a class of the same name with C<View> replaced with
229 =head2 rendering_context_class
231 The class of the L<Reaction::UI::RenderingContext> used to layout the
232 view. Defaults to searching down the precedence tree of the View class
233 looking for a class of the same name with C<View> replaced with
240 See L<Reaction::Class> for authors.
244 See L<Reaction::Class> for the license.