added widget_search_path option (ignored), defaults.conf support and moved file exten...
[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 use aliased 'Reaction::UI::Skin';
9 use aliased 'Path::Class::Dir';
10
11 class View which {
12
13   has '_widget_class_cache' => (is => 'ro', default => sub { {} });
14   has '_widget_cache' => (is => 'ro', default => sub { {} });
15
16   has '_layout_set_cache' => (is => 'ro', default => sub { {} });
17
18   has 'app' => (is => 'ro', required => 1);
19
20   has 'skin_name' => (is => 'ro', required => 1);
21
22   has 'skin' => (
23     is => 'ro', lazy_build => 1,
24     handles => [ qw(create_layout_set search_path_for_type) ]
25   );
26
27   has 'layout_set_class' => (is => 'ro', lazy_build => 1);
28
29   has 'rendering_context_class' => (is => 'ro', lazy_build => 1);
30
31   implements '_build_layout_set_class' => as {
32     my ($self) = @_;
33     return $self->find_related_class('LayoutSet');
34   };
35
36   implements '_build_rendering_context_class' => as {
37     my ($self) = @_;
38     return $self->find_related_class('RenderingContext');
39   };
40
41   implements '_build_skin' => as {
42     my ($self) = @_;
43     Skin->new(
44       name => $self->skin_name, view => $self,
45       skin_base_path => # returns a File, not a Dir. Thanks, Catalyst.
46         Dir->new($self->app->path_to('share', 'skin', $self->skin_name)),
47     );
48   };
49
50   implements 'COMPONENT' => as {
51     my ($class, $app, $args) = @_;
52     return $class->new(%{$args||{}}, app => $app);
53   };
54
55   implements 'render_window' => as {
56     my ($self, $window) = @_;
57     my $root_vp = $window->focus_stack->vp_head;
58     my $rctx = $self->create_rendering_context;
59     my ($widget, $args) = $self->render_viewport_args($root_vp);
60     $widget->render(widget => $rctx, $args);
61   };
62
63   implements 'render_viewport_args' => as {
64     my ($self, $vp) = @_;
65     my $layout_set = $self->layout_set_for($vp);
66     my $widget = $self->widget_for($vp, $layout_set);
67     return ($widget, { viewport => $vp });
68   };
69
70   implements 'widget_for' => as {
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
80   implements 'widget_class_for' => as {
81     my ($self, $layout_set) = @_;
82     my $base = $self->blessed;
83     my $widget_type = $layout_set->widget_type;
84     my $app_name = ref $self->app || $self->app;
85     return $self->_widget_class_cache->{$widget_type} ||= do {
86
87       my @search_path = ($base, $app_name, 'Reaction::UI');
88       my @haystack = map {join('::', $_, 'Widget', $widget_type)} @search_path;
89
90       foreach my $class (@haystack) {
91         #if the class is already loaded skip the call to Installed etc.
92         return $class if Class::MOP::is_class_loaded($class);
93         next unless Class::Inspector->installed($class);
94
95         my $ok = eval { Class::MOP::load_class($class) };
96         confess("Failed to load widget '${class}': $@") if $@;
97         return $class;
98       }
99       confess "Couldn't locate widget '${widget_type}' for layout "
100         ."'${\$layout_set->name}': tried: ".join(", ", @haystack);
101     };
102   };
103
104   implements 'layout_set_for' => as {
105     my ($self, $vp) = @_;
106     #print STDERR "Getting layoutset for VP ".(ref($vp) || "SC:".$vp)."\n";
107     my $lset_name = eval { $vp->layout };
108     confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@;
109     unless (length($lset_name)) {
110       my $vp_class = ref($vp) || $vp;
111       my ($last) = ($vp_class =~ /.*(?:::ViewPort::)(.+?)$/);
112       my @fragments = split('::', $last);
113       $_ = join("_", split(/(?=[A-Z])/, $_)) for @fragments;
114       $lset_name = lc(join('/', @fragments));
115       #print STDERR "--- $vp_class is rendered as $lset_name\n";
116     }
117     my $cache = $self->_layout_set_cache;
118     return $cache->{$lset_name} ||= $self->create_layout_set($lset_name);
119   };
120
121   implements 'layout_set_file_extension' => as {
122     confess View." is abstract, you must subclass it";
123   };
124
125   implements 'find_related_class' => as {
126     my ($self, $rel) = @_;
127     my $own_class = ref($self) || $self;
128     confess View." is abstract, you must subclass it" if $own_class eq View;
129     foreach my $super ($own_class->meta->class_precedence_list) {
130       next if $super eq View;
131       if ($super =~ /::View::/) {
132         (my $class = $super) =~ s/::View::/::${rel}::/;
133         if (eval { Class::MOP::load_class($class) }) {
134           return $class;
135         }
136       }
137     }
138     confess "Unable to find related ${rel} class for ${own_class}";
139   };
140
141   implements 'create_rendering_context' => as {
142     my ($self, @args) = @_;
143     return $self->rendering_context_class->new(
144              $self->rendering_context_args_for(@args),
145              @args,
146            );
147   };
148
149   implements 'rendering_context_args_for' => as {
150     return ();
151   };
152
153   implements 'layout_set_args_for' => as {
154     return ();
155   };
156
157 };
158
159 1;