Commit | Line | Data |
7adfd53f |
1 | package Reaction::UI::LayoutSet; |
2 | |
3 | use Reaction::Class; |
4 | use File::Spec; |
5 | |
6 | class LayoutSet which { |
7 | |
f2fef590 |
8 | has 'layouts' => (is => 'ro', default => sub { {} }); |
7adfd53f |
9 | |
10 | has 'name' => (is => 'ro', required => 1); |
11 | |
8a293e2e |
12 | has 'source_file' => (is => 'ro', required => 1); |
e22de101 |
13 | |
aa8c0c90 |
14 | has 'widget_class' => ( |
15 | is => 'rw', lazy_fail => 1, predicate => 'has_widget_class' |
16 | ); |
17 | |
1c41e332 |
18 | has 'widget_type' => (is => 'rw', lazy_build => 1); |
19 | |
aa8c0c90 |
20 | has 'super' => (is => 'rw', predicate => 'has_super'); |
f2fef590 |
21 | |
7adfd53f |
22 | implements 'BUILD' => as { |
23 | my ($self, $args) = @_; |
24 | my @path = @{$args->{search_path}||[]}; |
aa8c0c90 |
25 | confess "No view object provided" unless $args->{view}; |
b269d2bf |
26 | confess "No skin object provided" unless $args->{skin}; |
8a293e2e |
27 | $self->_load_file($self->source_file, $args); |
aa8c0c90 |
28 | unless ($self->has_widget_class) { |
29 | $self->widget_class($args->{view}->widget_class_for($self)); |
30 | } |
f2fef590 |
31 | }; |
32 | |
33 | implements 'widget_order_for' => as { |
34 | my ($self, $name) = @_; |
aa8c0c90 |
35 | return ( |
36 | ($self->has_layout($name) |
37 | ? ([ $self->widget_class, $self ]) #; |
38 | : ()), |
39 | ($self->has_super |
40 | ? ($self->super->widget_order_for($name)) |
41 | : ()), |
42 | ); |
7adfd53f |
43 | }; |
44 | |
aa8c0c90 |
45 | implements 'layout_names' => as { |
46 | my ($self) = @_; |
47 | my %seen; |
48 | return [ |
49 | grep { !$seen{$_}++ } |
50 | keys %{shift->layouts}, |
51 | ($self->has_super |
52 | ? (@{$self->super->layout_names}) |
53 | : ()) |
54 | ]; |
55 | }; |
f2fef590 |
56 | |
57 | implements 'has_layout' => as { exists $_[0]->layouts->{$_[1]} }; |
58 | |
7adfd53f |
59 | implements '_load_file' => as { |
aa8c0c90 |
60 | my ($self, $file, $build_args) = @_; |
7adfd53f |
61 | my $data = $file->slurp; |
f2fef590 |
62 | my $layouts = $self->layouts; |
63 | # cheesy match for "=for layout name ... =something" |
7adfd53f |
64 | # final split group also handles last in file, (?==) is lookahead |
f2fef590 |
65 | # assertion for '=' so "=for layout name1 ... =for layout name2" |
66 | # doesn't have the match pos go past the latter = and lose name2 |
aa8c0c90 |
67 | while ($data =~ m/=(.*?)\n(.*?)(?:\n(?==)|$)/sg) { |
68 | my ($data, $text) = ($1, $2); |
aa8c0c90 |
69 | if ($data =~ /^for layout (\S+)/) { |
70 | my $fname = $1; |
cf272446 |
71 | $text =~ s/^(?:\s*\r?\n)+//; #remove leading empty lines |
72 | $text =~ s/[\s\r\n]+$//; #remove trailing whitespace |
73 | $layouts->{$fname} = $text; |
aa8c0c90 |
74 | } elsif ($data =~ /^extends (\S+)/) { |
75 | my $super_name = $1; |
b269d2bf |
76 | $self->super($build_args->{skin}->create_layout_set($super_name)) |
1c41e332 |
77 | } elsif ($data =~ /^widget (\S+)/) { |
78 | my $widget_type = $1; |
79 | $self->widget_type($1); |
aa8c0c90 |
80 | } elsif ($data =~ /^cut/) { |
81 | # no-op |
82 | } else { |
83 | confess "Unparseable directive ${data}"; |
84 | } |
7adfd53f |
85 | } |
7adfd53f |
86 | }; |
87 | |
1c41e332 |
88 | implements '_build_widget_type' => as { |
7adfd53f |
89 | my ($self) = @_; |
6ab43711 |
90 | my $widget = join('', map { ucfirst($_) } split('_', $self->name)); |
91 | $widget = join('::', map { ucfirst($_) } split('/', $widget)); |
92 | |
e22de101 |
93 | #print STDERR "--- ", $self->name, " maps to widget $widget \n"; |
6ab43711 |
94 | |
95 | return $widget; |
7adfd53f |
96 | }; |
7b78a39d |
97 | |
7adfd53f |
98 | }; |
99 | |
100 | 1; |