Commit | Line | Data |
e30ed59d |
1 | package App::IdiotBox; |
2 | |
3 | use Web::Simple __PACKAGE__; |
4 | use Method::Signatures::Simple; |
d7497a23 |
5 | use FindBin; |
6 | use HTML::Zoom; |
7 | |
8 | { |
9 | package App::IdiotBox::Announcement; |
10 | |
11 | sub made_at { shift->{made_at} } |
12 | sub bucket { shift->{bucket} } |
13 | sub video_count { shift->{video_count} } |
14 | |
15 | package App::IdiotBox::Bucket; |
16 | |
17 | sub slug { shift->{slug} } |
18 | sub name { shift->{name} } |
19 | sub video_count { shift->{video_count} } |
71fd1550 |
20 | sub videos { shift->{videos} } |
21 | |
22 | package App::IdiotBox::Video; |
23 | |
24 | sub slug { shift->{slug} } |
25 | sub name { shift->{name} } |
26 | sub author { shift->{author} } |
27 | sub details { shift->{details} } |
28 | sub bucket { shift->{bucket} } |
d7497a23 |
29 | } |
30 | |
31 | default_config( |
32 | template_dir => $FindBin::Bin.'/../share/html' |
33 | ); |
e30ed59d |
34 | |
35 | dispatch { |
36 | sub (/) { $self->show_front_page }, |
37 | subdispatch sub (/*/...) { |
38 | my $bucket = $self->buckets->get({ slug => $_[1] }); |
39 | [ |
40 | sub (/) { |
41 | $self->show_bucket($bucket) |
42 | }, |
43 | sub (/*) { |
d7497a23 |
44 | $self->show_video($bucket->videos->get({ slug => $_[1] })); |
e30ed59d |
45 | } |
46 | ] |
47 | } |
48 | }; |
49 | |
d7497a23 |
50 | method recent_announcements { $self->{recent_announcements} } |
51 | |
e30ed59d |
52 | method show_front_page { |
53 | my $ann = $self->recent_announcements; |
54 | $self->html_response( |
55 | front_page => [ |
d7497a23 |
56 | '#announcement-list' => [ |
57 | -repeat_content => { |
58 | repeat_for => $ann->map(sub { [ |
59 | '.fill-bucket-name' => [ |
60 | -replace_content => { replace_with => $_->bucket->name } |
61 | ], |
62 | '.fill-bucket-link' => [ |
63 | -set_attribute => { name => 'href', value => $_->bucket->slug.'/' } |
64 | ], |
65 | '.fill-new-videos' => [ |
66 | -replace_content => { replace_with => $_->video_count } |
67 | ], |
68 | '.fill-total-videos' => [ |
69 | -replace_content => { replace_with => $_->bucket->video_count } |
70 | ], |
71 | ] })->as_stream |
e30ed59d |
72 | } |
d7497a23 |
73 | ] |
e30ed59d |
74 | ] |
75 | ); |
76 | } |
77 | |
d7497a23 |
78 | method show_bucket ($bucket) { |
79 | $self->html_response(bucket => [ |
80 | '.fill-bucket-name' => [ |
81 | -replace_content => { replace_with => $bucket->name } |
82 | ], |
83 | '#video-list' => [ |
84 | -repeat_content => { |
85 | repeat_for => $bucket->videos->map(sub { [ |
86 | '.fill-video-name' => [ |
87 | -replace_content => { replace_with => $_->name } |
88 | ], |
89 | '.fill-video-author' => [ |
90 | -replace_content => { replace_with => $_->author } |
91 | ], |
92 | '.fill-video-link' => [ |
93 | -set_attribute => { |
94 | name => 'href', value => $_->slug.'/' |
95 | } |
96 | ], |
97 | ] })->as_stream |
98 | } |
99 | ] |
100 | ]); |
101 | } |
102 | |
103 | method show_video ($video) { |
104 | $self->html_response(video => [ |
105 | '.fill-video-name' => [ |
106 | -replace_content => { replace_with => $video->name } |
107 | ], |
108 | '.fill-author-name' => [ |
109 | -replace_content => { replace_with => $video->author } |
110 | ], |
111 | '.fill-bucket-link' => [ |
112 | -set_attribute => { name => 'href', value => '../' } |
113 | ], |
114 | '.fill-bucket-name' => [ |
115 | -replace_content => { replace_with => $video->bucket->name } |
116 | ], |
117 | '.fill-video-details' => [ |
118 | -replace_content => { replace_with => $video->details } |
119 | ] |
120 | ]); |
121 | } |
122 | |
e30ed59d |
123 | method html_response ($template_name, $selectors) { |
d7497a23 |
124 | my $io = $self->_zoom_for($template_name => $selectors)->as_readable_fh; |
e30ed59d |
125 | return [ 200, [ 'Content-Type' => 'text/html' ], $io ] |
126 | } |
127 | |
d7497a23 |
128 | method _template_filename_for ($name) { |
129 | $self->{config}{template_dir}.'/'.$name.'.html'; |
130 | } |
131 | |
e30ed59d |
132 | method _layout_zoom { |
d7497a23 |
133 | $self->{layout_zoom} ||= HTML::Zoom->from_file( |
134 | $self->_template_filename_for('layout') |
e30ed59d |
135 | ) |
136 | } |
137 | |
138 | method _zoom_for ($template_name, $selectors) { |
139 | ($self->{zoom_for_template}{$template_name} ||= do { |
140 | my @body; |
d7497a23 |
141 | HTML::Zoom->from_file( |
142 | $self->_template_filename_for($template_name) |
e30ed59d |
143 | ) |
144 | ->with_selectors( |
d7497a23 |
145 | '#main-content' => [ |
146 | -capture_events => { into => \@body } |
147 | ] |
e30ed59d |
148 | ) |
d7497a23 |
149 | ->run; |
150 | $self->_layout_zoom->with_selectors( |
151 | '#main-content' => [ |
152 | -replace_content_events => { replace_with => \@body } |
153 | ] |
154 | )->to_zoom; |
155 | })->with_selectors($selectors) |
e30ed59d |
156 | } |
157 | |
158 | 1; |