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