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