oopsie typo
[catagits/Reaction.git] / lib / Reaction / UI / View.pm
1 package Reaction::UI::View;
2
3 use Reaction::Class;
4
5 # declaring dependencies
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   has '_widget_class_cache' => (is => 'ro', default => sub { {} });
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
27   sub BUILD{
28     my $self = shift;
29     my $skin_name = $self->skin_name;
30     #XXX i guess we will add the path to installed reaction templates here
31     my $skin_path = $self->app->path_to('share','skin',$skin_name);
32     confess("'${skin_path}' is not a valid path for skin '${skin_name}'")
33       unless -d $skin_path;
34   }
35
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) = @_;
61     my $base = $self->blessed;
62     my $tail = $layout_set->widget_type;
63     my $lset_name = $layout_set->name;
64     # eventually more stuff will go here i guess?
65     my $app_name = ref $self->app || $self->app;
66     my $cache = $self->_widget_class_cache;
67     return $cache->{ $lset_name } if exists $cache->{ $lset_name };
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) };
73       #$@ ? next : return  $class;
74       $@ ? next : return $cache->{ $lset_name } = $class;
75     }
76     confess "Couldn't load widget '$tail': tried: @haystack";
77   };
78
79   implements 'layout_set_for' => as {
80     my ($self, $vp) = @_;
81     #print STDERR "Getting layoutset for VP ".(ref($vp) || "SC:".$vp)."\n";
82     my $lset_name = eval { $vp->layout };
83     confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@;
84     unless (length($lset_name)) {
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));
90       #print STDERR "--- $vp_class is rendered as $lset_name\n";
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) = @_;
105     my $own_class = ref($self) || $self;
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
158 1;