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; |
8 | |
9 | class View which { |
10 | |
11 | has '_layout_set_cache' => (is => 'ro', default => sub { {} }); |
12 | |
13 | has 'app' => (is => 'ro', required => 1); |
14 | |
15 | has 'skin_name' => (is => 'ro', required => 1); |
16 | |
17 | has 'layout_set_class' => (is => 'ro', lazy_build => 1); |
18 | |
19 | has 'rendering_context_class' => (is => 'ro', lazy_build => 1); |
20 | |
21 | implements 'COMPONENT' => as { |
22 | my ($class, $app, $args) = @_; |
23 | return $class->new(%{$args||{}}, app => $app); |
24 | }; |
25 | |
7b78a39d |
26 | sub BUILD{ |
27 | my $self = shift; |
28 | my $skin_name = $self->skin_name; |
e22de101 |
29 | #XXX i guess we will add the path to installed reaction templates here |
87018d74 |
30 | my $skin_path = $self->app->path_to('share','skin',$skin_name); |
7b78a39d |
31 | confess("'${skin_path}' is not a valid path for skin '${skin_name}'") |
32 | unless -d $skin_path; |
33 | } |
34 | |
7adfd53f |
35 | implements 'render_window' => as { |
36 | my ($self, $window) = @_; |
37 | my $root_vp = $window->focus_stack->vp_head; |
38 | $self->render_viewport(undef, $root_vp); |
39 | }; |
40 | |
41 | implements 'render_viewport' => as { |
42 | my ($self, $outer_rctx, $vp) = @_; |
43 | my $layout_set = $self->layout_set_for($vp); |
44 | my $rctx = $self->create_rendering_context( |
45 | layouts => $layout_set, |
46 | outer => $outer_rctx, |
47 | ); |
48 | my $widget = $self->widget_for($vp, $layout_set); |
49 | $widget->render($rctx); |
50 | }; |
51 | |
52 | implements 'widget_for' => as { |
53 | my ($self, $vp, $layout_set) = @_; |
54 | return $self->widget_class_for($layout_set) |
55 | ->new(view => $self, viewport => $vp); |
56 | }; |
57 | |
58 | implements 'widget_class_for' => as { |
59 | my ($self, $layout_set) = @_; |
5a1a893e |
60 | my $base = $self->blessed; |
7adfd53f |
61 | my $tail = $layout_set->widget_type; |
e22de101 |
62 | # eventually more stuff will go here i guess? |
63 | my $app_name = ref $self->app || $self->app; |
64 | |
65 | my @search_path = ($base, $app_name, 'Reaction::UI'); |
66 | my @haystack = map { join '::', $_, 'Widget', $tail } @search_path; |
67 | for my $class (@haystack){ |
68 | eval { Class::MOP::load_class($class) }; |
69 | $@ ? next : return $class; |
70 | } |
71 | confess "Couldn't load widget '$tail': tried: @haystack"; |
7adfd53f |
72 | }; |
73 | |
74 | implements 'layout_set_for' => as { |
75 | my ($self, $vp) = @_; |
e22de101 |
76 | #print STDERR "Getting layoutset for VP ".(ref($vp) || "SC:".$vp)."\n"; |
7adfd53f |
77 | my $lset_name = eval { $vp->layout }; |
78 | confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@; |
79 | unless (length($lset_name)) { |
6ab43711 |
80 | my $vp_class = ref($vp) || $vp; |
81 | my ($last) = ($vp_class =~ /.*(?:::ViewPort::)(.+?)$/); |
82 | my @fragments = split('::', $last); |
83 | $_ = join("_", split(/(?=[A-Z])/, $_)) for @fragments; |
84 | $lset_name = lc(join('/', @fragments)); |
e22de101 |
85 | #print STDERR "--- $vp_class is rendered as $lset_name\n"; |
7adfd53f |
86 | } |
87 | my $cache = $self->_layout_set_cache; |
88 | return $cache->{$lset_name} ||= $self->create_layout_set($lset_name); |
89 | }; |
90 | |
91 | implements 'create_layout_set' => as { |
92 | my ($self, $name) = @_; |
93 | return $self->layout_set_class->new( |
94 | $self->layout_set_args_for($name), |
95 | ); |
96 | }; |
97 | |
98 | implements 'find_related_class' => as { |
99 | my ($self, $rel) = @_; |
6ab43711 |
100 | my $own_class = ref($self) || $self; |
7adfd53f |
101 | confess View." is abstract, you must subclass it" if $own_class eq View; |
102 | foreach my $super ($own_class->meta->class_precedence_list) { |
103 | next if $super eq View; |
104 | if ($super =~ /::View::/) { |
105 | (my $class = $super) =~ s/::View::/::${rel}::/; |
106 | if (eval { Class::MOP::load_class($class) }) { |
107 | return $class; |
108 | } |
109 | } |
110 | } |
111 | confess "Unable to find related ${rel} class for ${own_class}"; |
112 | }; |
113 | |
114 | implements 'build_layout_set_class' => as { |
115 | my ($self) = @_; |
116 | return $self->find_related_class('LayoutSet'); |
117 | }; |
118 | |
119 | implements 'layout_set_args_for' => as { |
120 | my ($self, $name) = @_; |
121 | return (name => $name, search_path => $self->layout_search_path); |
122 | }; |
123 | |
124 | implements 'layout_search_path' => as { |
125 | my ($self) = @_; |
126 | return $self->search_path_for_type('layout'); |
127 | }; |
128 | |
129 | implements 'search_path_for_type' => as { |
130 | my ($self, $type) = @_; |
131 | return [ $self->app->path_to('share','skin',$self->skin_name,$type) ]; |
132 | }; |
133 | |
134 | implements 'create_rendering_context' => as { |
135 | my ($self, @args) = @_; |
136 | return $self->rendering_context_class->new( |
137 | $self->rendering_context_args_for(@args), |
138 | @args, |
139 | ); |
140 | }; |
141 | |
142 | implements 'build_rendering_context_class' => as { |
143 | my ($self) = @_; |
144 | return $self->find_related_class('RenderingContext'); |
145 | }; |
146 | |
147 | implements 'rendering_context_args_for' => as { |
148 | return (); |
149 | }; |
150 | |
151 | }; |
152 | |
153 | 1; |