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}||[]}; |
b269d2bf |
25 | confess "No skin object provided" unless $args->{skin}; |
8a293e2e |
26 | $self->_load_file($self->source_file, $args); |
aa8c0c90 |
27 | unless ($self->has_widget_class) { |
f6b79841 |
28 | $self->widget_class($args->{skin}->widget_class_for($self)); |
aa8c0c90 |
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; |
68404faa |
75 | my $skin; |
76 | if ($super_name eq 'NEXT') { |
77 | confess "No next skin and layout extends NEXT" |
78 | unless $build_args->{next_skin}; |
79 | $skin = $build_args->{next_skin}; |
80 | $super_name = $self->name; |
81 | } else { |
82 | $skin = $build_args->{skin}; |
83 | } |
84 | $self->super($skin->create_layout_set($super_name)); |
1c41e332 |
85 | } elsif ($data =~ /^widget (\S+)/) { |
86 | my $widget_type = $1; |
87 | $self->widget_type($1); |
aa8c0c90 |
88 | } elsif ($data =~ /^cut/) { |
89 | # no-op |
90 | } else { |
91 | confess "Unparseable directive ${data}"; |
92 | } |
7adfd53f |
93 | } |
7adfd53f |
94 | }; |
95 | |
1c41e332 |
96 | implements '_build_widget_type' => as { |
7adfd53f |
97 | my ($self) = @_; |
6ab43711 |
98 | my $widget = join('', map { ucfirst($_) } split('_', $self->name)); |
99 | $widget = join('::', map { ucfirst($_) } split('/', $widget)); |
100 | |
e22de101 |
101 | #print STDERR "--- ", $self->name, " maps to widget $widget \n"; |
6ab43711 |
102 | |
103 | return $widget; |
7adfd53f |
104 | }; |
7b78a39d |
105 | |
7adfd53f |
106 | }; |
107 | |
108 | 1; |