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}; |
8a293e2e |
26 | $self->_load_file($self->source_file, $args); |
aa8c0c90 |
27 | unless ($self->has_widget_class) { |
28 | $self->widget_class($args->{view}->widget_class_for($self)); |
29 | } |
f2fef590 |
30 | }; |
31 | |
32 | implements 'widget_order_for' => as { |
33 | my ($self, $name) = @_; |
aa8c0c90 |
34 | return ( |
35 | ($self->has_layout($name) |
36 | ? ([ $self->widget_class, $self ]) #; |
37 | : ()), |
38 | ($self->has_super |
39 | ? ($self->super->widget_order_for($name)) |
40 | : ()), |
41 | ); |
7adfd53f |
42 | }; |
43 | |
aa8c0c90 |
44 | implements 'layout_names' => as { |
45 | my ($self) = @_; |
46 | my %seen; |
47 | return [ |
48 | grep { !$seen{$_}++ } |
49 | keys %{shift->layouts}, |
50 | ($self->has_super |
51 | ? (@{$self->super->layout_names}) |
52 | : ()) |
53 | ]; |
54 | }; |
f2fef590 |
55 | |
56 | implements 'has_layout' => as { exists $_[0]->layouts->{$_[1]} }; |
57 | |
7adfd53f |
58 | implements '_load_file' => as { |
aa8c0c90 |
59 | my ($self, $file, $build_args) = @_; |
7adfd53f |
60 | my $data = $file->slurp; |
f2fef590 |
61 | my $layouts = $self->layouts; |
62 | # cheesy match for "=for layout name ... =something" |
7adfd53f |
63 | # final split group also handles last in file, (?==) is lookahead |
f2fef590 |
64 | # assertion for '=' so "=for layout name1 ... =for layout name2" |
65 | # doesn't have the match pos go past the latter = and lose name2 |
aa8c0c90 |
66 | while ($data =~ m/=(.*?)\n(.*?)(?:\n(?==)|$)/sg) { |
67 | my ($data, $text) = ($1, $2); |
aa8c0c90 |
68 | if ($data =~ /^for layout (\S+)/) { |
69 | my $fname = $1; |
cf272446 |
70 | $text =~ s/^(?:\s*\r?\n)+//; #remove leading empty lines |
71 | $text =~ s/[\s\r\n]+$//; #remove trailing whitespace |
72 | $layouts->{$fname} = $text; |
aa8c0c90 |
73 | } elsif ($data =~ /^extends (\S+)/) { |
74 | my $super_name = $1; |
75 | $self->super($build_args->{view}->create_layout_set($super_name)) |
1c41e332 |
76 | } elsif ($data =~ /^widget (\S+)/) { |
77 | my $widget_type = $1; |
78 | $self->widget_type($1); |
aa8c0c90 |
79 | } elsif ($data =~ /^cut/) { |
80 | # no-op |
81 | } else { |
82 | confess "Unparseable directive ${data}"; |
83 | } |
7adfd53f |
84 | } |
7adfd53f |
85 | }; |
86 | |
1c41e332 |
87 | implements '_build_widget_type' => as { |
7adfd53f |
88 | my ($self) = @_; |
6ab43711 |
89 | my $widget = join('', map { ucfirst($_) } split('_', $self->name)); |
90 | $widget = join('::', map { ucfirst($_) } split('/', $widget)); |
91 | |
e22de101 |
92 | #print STDERR "--- ", $self->name, " maps to widget $widget \n"; |
6ab43711 |
93 | |
94 | return $widget; |
7adfd53f |
95 | }; |
7b78a39d |
96 | |
7adfd53f |
97 | }; |
98 | |
99 | 1; |