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