fixup widgets to use fragment
[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       #here we should throw if exits and error instead of eating the error
73       #only next when !exists
74       eval { Class::MOP::load_class($class) };
75       #$@ ? next : return  $class;
76       $@ ? next : return $cache->{ $lset_name } = $class;
77     }
78     confess "Couldn't load widget '$tail': tried: @haystack";
79   };
80
81   implements 'layout_set_for' => as {
82     my ($self, $vp) = @_;
83     #print STDERR "Getting layoutset for VP ".(ref($vp) || "SC:".$vp)."\n";
84     my $lset_name = eval { $vp->layout };
85     confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@;
86     unless (length($lset_name)) {
87       my $vp_class = ref($vp) || $vp;
88       my ($last) = ($vp_class =~ /.*(?:::ViewPort::)(.+?)$/);
89       my @fragments = split('::', $last);
90       $_ = join("_", split(/(?=[A-Z])/, $_)) for @fragments;
91       $lset_name = lc(join('/', @fragments));
92       #print STDERR "--- $vp_class is rendered as $lset_name\n";
93     }
94     my $cache = $self->_layout_set_cache;
95     return $cache->{$lset_name} ||= $self->create_layout_set($lset_name);
96   };
97
98   implements 'create_layout_set' => as {
99     my ($self, $name) = @_;
100     return $self->layout_set_class->new(
101              $self->layout_set_args_for($name),
102            );
103   };
104
105   implements 'find_related_class' => as {
106     my ($self, $rel) = @_;
107     my $own_class = ref($self) || $self;
108     confess View." is abstract, you must subclass it" if $own_class eq View;
109     foreach my $super ($own_class->meta->class_precedence_list) {
110       next if $super eq View;
111       if ($super =~ /::View::/) {
112         (my $class = $super) =~ s/::View::/::${rel}::/;
113         if (eval { Class::MOP::load_class($class) }) {
114           return $class;
115         }
116       }
117     }
118     confess "Unable to find related ${rel} class for ${own_class}";
119   };
120
121   implements 'build_layout_set_class' => as {
122     my ($self) = @_;
123     return $self->find_related_class('LayoutSet');
124   };
125
126   implements 'layout_set_args_for' => as {
127     my ($self, $name) = @_;
128     return (name => $name, search_path => $self->layout_search_path);
129   };
130
131   implements 'layout_search_path' => as {
132     my ($self) = @_;
133     return $self->search_path_for_type('layout');
134   };
135
136   implements 'search_path_for_type' => as {
137     my ($self, $type) = @_;
138     return [ $self->app->path_to('share','skin',$self->skin_name,$type) ];
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 'build_rendering_context_class' => as {
150     my ($self) = @_;
151     return $self->find_related_class('RenderingContext');
152   };
153
154   implements 'rendering_context_args_for' => as {
155     return ();
156   };
157
158 };
159
160 1;