commit changes before switch to new zoom API
[catagits/App-IdiotBox.git] / lib / App / IdiotBox.pm
CommitLineData
e30ed59d 1package App::IdiotBox;
2
3use Web::Simple __PACKAGE__;
4use Method::Signatures::Simple;
d7497a23 5use FindBin;
6use 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} }
d9702c6d 19 sub video_count {
20 exists $_[0]->{video_count}
21 ? $_[0]->{video_count}
22 : $_[0]->{videos}->count
23 }
71fd1550 24 sub videos { shift->{videos} }
25
26 package App::IdiotBox::Video;
27
28 sub slug { shift->{slug} }
29 sub name { shift->{name} }
30 sub author { shift->{author} }
31 sub details { shift->{details} }
32 sub bucket { shift->{bucket} }
d7497a23 33}
34
35default_config(
71a02d85 36 template_dir => 'share/html',
37 store => 'SQLite',
38 db_file => 'var/lib/idiotbox.db',
d7497a23 39);
e30ed59d 40
71a02d85 41sub BUILD {
42 my $self = shift;
43 my $store;
44 ($store = $self->config->{store}) =~ /^(\w+)$/
45 or die "Store config should be just a name, got ${store} instead";
46 my $store_class = "App::IdiotBox::Store::${store}";
47 eval "require ${store_class}; 1"
48 or die "Couldn't load ${store} store: $@";
49 $store_class->bind($self);
50}
51
52
e30ed59d 53dispatch {
54 sub (/) { $self->show_front_page },
55 subdispatch sub (/*/...) {
56 my $bucket = $self->buckets->get({ slug => $_[1] });
57 [
58 sub (/) {
59 $self->show_bucket($bucket)
60 },
71a02d85 61 sub (/*/) {
d7497a23 62 $self->show_video($bucket->videos->get({ slug => $_[1] }));
e30ed59d 63 }
64 ]
65 }
66};
67
d7497a23 68method recent_announcements { $self->{recent_announcements} }
69
71a02d85 70method buckets { $self->{buckets} }
71
e30ed59d 72method show_front_page {
73 my $ann = $self->recent_announcements;
74 $self->html_response(
75 front_page => [
d7497a23 76 '#announcement-list' => [
77 -repeat_content => {
78 repeat_for => $ann->map(sub { [
79 '.fill-bucket-name' => [
80 -replace_content => { replace_with => $_->bucket->name }
81 ],
82 '.fill-bucket-link' => [
83 -set_attribute => { name => 'href', value => $_->bucket->slug.'/' }
84 ],
85 '.fill-new-videos' => [
86 -replace_content => { replace_with => $_->video_count }
87 ],
88 '.fill-total-videos' => [
89 -replace_content => { replace_with => $_->bucket->video_count }
90 ],
91 ] })->as_stream
e30ed59d 92 }
d7497a23 93 ]
e30ed59d 94 ]
95 );
96}
97
d7497a23 98method show_bucket ($bucket) {
99 $self->html_response(bucket => [
100 '.fill-bucket-name' => [
101 -replace_content => { replace_with => $bucket->name }
102 ],
103 '#video-list' => [
104 -repeat_content => {
105 repeat_for => $bucket->videos->map(sub { [
106 '.fill-video-name' => [
107 -replace_content => { replace_with => $_->name }
108 ],
109 '.fill-video-author' => [
110 -replace_content => { replace_with => $_->author }
111 ],
112 '.fill-video-link' => [
113 -set_attribute => {
114 name => 'href', value => $_->slug.'/'
115 }
116 ],
117 ] })->as_stream
118 }
119 ]
120 ]);
121}
122
123method show_video ($video) {
124 $self->html_response(video => [
125 '.fill-video-name' => [
126 -replace_content => { replace_with => $video->name }
127 ],
128 '.fill-author-name' => [
129 -replace_content => { replace_with => $video->author }
130 ],
131 '.fill-bucket-link' => [
132 -set_attribute => { name => 'href', value => '../' }
133 ],
134 '.fill-bucket-name' => [
135 -replace_content => { replace_with => $video->bucket->name }
136 ],
137 '.fill-video-details' => [
138 -replace_content => { replace_with => $video->details }
139 ]
140 ]);
141}
142
e30ed59d 143method html_response ($template_name, $selectors) {
d7497a23 144 my $io = $self->_zoom_for($template_name => $selectors)->as_readable_fh;
e30ed59d 145 return [ 200, [ 'Content-Type' => 'text/html' ], $io ]
146}
147
d7497a23 148method _template_filename_for ($name) {
149 $self->{config}{template_dir}.'/'.$name.'.html';
150}
151
e30ed59d 152method _layout_zoom {
d7497a23 153 $self->{layout_zoom} ||= HTML::Zoom->from_file(
154 $self->_template_filename_for('layout')
e30ed59d 155 )
156}
157
158method _zoom_for ($template_name, $selectors) {
159 ($self->{zoom_for_template}{$template_name} ||= do {
160 my @body;
d7497a23 161 HTML::Zoom->from_file(
162 $self->_template_filename_for($template_name)
e30ed59d 163 )
164 ->with_selectors(
d7497a23 165 '#main-content' => [
166 -capture_events => { into => \@body }
167 ]
e30ed59d 168 )
d7497a23 169 ->run;
170 $self->_layout_zoom->with_selectors(
171 '#main-content' => [
172 -replace_content_events => { replace_with => \@body }
173 ]
174 )->to_zoom;
175 })->with_selectors($selectors)
e30ed59d 176}
177
1781;