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