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);
64 sub render_viewport_args {
66 my $layout_set = $self->layout_set_for($vp);
67 my $widget = $self->widget_for($vp, $layout_set);
68 my %layout_args = (%{ $vp->layout_args }, viewport => $vp);
69 return ($widget, \%layout_args);
72 my ($self, $vp, $layout_set) = @_;
74 $self->_widget_cache->{$layout_set->name}
75 ||= $layout_set->widget_class
77 view => $self, layout_set => $layout_set
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);
90 #XXX if it ever comes to it: this could be memoized. not bothering yet.
91 sub 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
98 join("_", split(/(?:(?<=[A-Z])(?=[A-Z][^_A-Z])|(?<=[^_A-Z])(?=[A-Z]))/, $_))
100 return lc(join('/', @fragments));
102 sub layout_set_file_extension {
103 confess View." is abstract, you must subclass it";
105 sub 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) }) {
118 confess "Unable to find related ${rel} class for ${own_class}";
120 sub create_rendering_context {
121 my ($self, @args) = @_;
122 return $self->rendering_context_class->new(
123 $self->rendering_context_args_for(@args),
127 sub rendering_context_args_for {
130 sub layout_set_args_for {
134 __PACKAGE__->meta->make_immutable;
140 Reaction::UI::View - Render the UI.
144 package MyApp::View::TT;
145 use base 'Reaction::UI::View::TT';
148 skin_name => 'MyApp',
151 ## In the Window class:
152 $res->body($self->view->render_window($self));
156 Render the viewports in the current window using the chosen skin and
157 layoutset, via the matching widgets.
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>
177 =item Arguments: $app?
181 The application L<Catalyst> class. This is set at
182 L<Catalyst/COMPONENT> time for you.
188 =item Arguments: $skinname?
192 The name of the skin to use to render the pages. This should be the
193 name of a subdirectory under the F<share/skin> in your application
194 directory. The default skin name is C<default>, the default skin is
195 provided with Reaction.
197 See also: L<Reaction::UI::Skin>
203 =item Arguments: $skin?
207 A L<Reaction::UI::Skin> object based on the L</skin_name>. It will be
208 created for you if not provided.
210 =head2 layout_set_class
212 The class of the L<Reaction::UI::LayoutSet> used to layout the
213 view. Defaults to searching down the precedence tree of the View class
214 looking for a class of the same name with C<View> replaced with
217 =head2 rendering_context_class
219 The class of the L<Reaction::UI::RenderingContext> used to layout the
220 view. Defaults to searching down the precedence tree of the View class
221 looking for a class of the same name with C<View> replaced with
228 See L<Reaction::Class> for authors.
232 See L<Reaction::Class> for the license.