blib/lib/Yukki/Web/View/Page.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 170 CC 11 SCAR 21.4
CC
11
Cov
0%
page_navigation
20.0
history
12.0
attachment_links
12.0
CRAP 8.5
1 package Yukki::Web::View::Page;
2 {
3 $Yukki::Web::View::Page::VERSION = '0.140290';
4 }
5 use 5.12.1;
6 use Moose;
7  
8 extends 'Yukki::Web::View';
9  
10 # ABSTRACT: render HTML for viewing and editing wiki pages
11  
12  
13 sub blank {
14 my ($self, $ctx, $vars) = @_;
15  
16 my $link = "/page/edit/$vars->{repository}/$vars->{page}";
17  
18 $ctx->response->page_title($vars->{title});
19 $ctx->response->breadcrumb($vars->{breadcrumb});
20  
21 return $self->render_page(
22 template => 'page/blank.html',
23 context => $ctx,
24 vars => {
25 '#yukkiname' => $vars->{page},
26 '#create-page@href' => $link,
27 '#file-list' => $self->attachments($ctx, $vars->{files}),
28 },
29 );
30 }
31  
32  
33 sub page_navigation {
34 my ($self, $response, $this_action, $vars) = @_;
35  
36 for my $action (qw( edit history rename remove )) {
37 next if $action eq $this_action;
38  
39 $response->add_navigation_item([ qw( page page_bottom ) ] => {
40 label => ucfirst $action,
41 href => join('/', 'page', $action, $vars->{repository}, $vars->{page}),
42 sort => 20,
43 });
44 }
45  
46 for my $view_name (keys %{ $self->app->settings->page_views }) {
47 my $view_info = $self->app->settings->page_views->{$view_name};
48  
49 next if $view_info->{hide};
50  
51 my $args = "?view=$view_name";
52 $args = '' if $view_name eq 'default';
53  
54 $response->add_navigation_item([ qw( page page_bottom ) ] => {
55 label => $view_info->{label},
56 href => join('/', 'page/view', $vars->{repository}, $vars->{page})
57 . $args,
58 sort => $view_info->{sort},
59 });
60 }
61 }
62  
63  
64 sub view {
65 my ($self, $ctx, $vars) = @_;
66 my $file = $vars->{file};
67  
68 $ctx->response->page_title($vars->{title});
69 $ctx->response->breadcrumb($vars->{breadcrumb});
70  
71 my $html = $file->fetch_formatted($ctx, -1);
72  
73 $self->page_navigation($ctx->response, 'view', $vars);
74  
75 return $self->render_page(
76 template => 'page/view.html',
77 context => $ctx,
78 vars => {
79 '#yukkitext' => \$html,
80 },
81 );
82 }
83  
84  
85 sub history {
86 my ($self, $ctx, $vars) = @_;
87  
88 $ctx->response->page_title($vars->{title});
89 $ctx->response->breadcrumb($vars->{breadcrumb});
90  
91 $self->page_navigation($ctx->response, 'history', $vars);
92  
93 my $i = 0;
94 return $self->render_page(
95 template => 'page/history.html',
96 context => $ctx,
97 vars => {
98 'form@action' => join('/', '/page/diff', $vars->{repository}, $vars->{page}),
99 '.revision' => [
100 map {
101 my $r = {
102 '.first-revision input@value' => $_->{object_id},
103 '.second-revision input@value' => $_->{object_id},
104 '.date' => $_->{time_ago},
105 '.author' => $_->{author_name},
106 '.diffstat' => sprintf('+%d/-%d',
107 $_->{lines_added}, $_->{lines_removed},
108 ),
109 '.comment' => $_->{comment} || '(no comment)',
110 };
111  
112 my $checked = sub { shift->setAttribute(checked => 'checked'); \$_ };
113  
114 $r->{'.first-revision input'} = $checked if $i == 1;
115 $r->{'.second-revision input'} = $checked if $i == 0;
116  
117 $i++;
118  
119 $r;
120 } @{ $vars->{revisions} }
121 ],
122 },
123 );
124 }
125  
126  
127 sub diff {
128 my ($self, $ctx, $vars) = @_;
129 my $file = $vars->{file};
130  
131 $ctx->response->page_title($vars->{title});
132 $ctx->response->breadcrumb($vars->{breadcrumb});
133  
134 $self->page_navigation($ctx->response, 'diff', $vars);
135  
136 my $html = $file->fetch_formatted($ctx);
137  
138 return $self->render_page(
139 template => 'page/diff.html',
140 context => $ctx,
141 vars => {
142 '#diff' => \$html,
143 },
144 );
145 }
146  
147  
148 sub edit {
149 my ($self, $ctx, $vars) = @_;
150 my $file = $vars->{file};
151  
152 $ctx->response->page_title($vars->{title});
153 $ctx->response->breadcrumb($vars->{breadcrumb});
154  
155 my $html = $file->fetch_formatted($ctx, $vars->{position});
156  
157 $self->page_navigation($ctx->response, 'edit', $vars);
158  
159 my %attachments;
160 if (@{ $vars->{attachments} }) {
161 %attachments = (
162 '#attachments-list@class' => 'attachment-list',
163 '#attachments-list' => $self->attachments($ctx, $vars->{attachments}),
164 );
165 }
166  
167 return $self->render_page(
168 template => 'page/edit.html',
169 context => $ctx,
170 vars => {
171 '#yukkiname' => $vars->{page},
172 '#yukkitext' => scalar $vars->{file}->fetch // '',
173 '#yukkitext_position@value' => $vars->{position},
174 '#preview-yukkitext' => \$html,
175 %attachments,
176 },
177 );
178 }
179  
180  
181 sub rename {
182 my ($self, $ctx, $vars) = @_;
183 my $file = $vars->{file};
184  
185 $ctx->response->page_title($vars->{title});
186 $ctx->response->breadcrumb($vars->{breadcrumb});
187  
188 $self->page_navigation($ctx->response, 'rename', $vars)
189 unless $ctx->request->path_parameters->{file};
190  
191 return $self->render_page(
192 template => 'page/rename.html',
193 context => $ctx,
194 vars => {
195 '#yukkiname' => $vars->{page},
196 '#yukkiname_new@value' => $vars->{page},
197 },
198 );
199 }
200  
201  
202 sub remove {
203 my ($self, $ctx, $vars) = @_;
204 my $file = $vars->{file};
205  
206 $ctx->response->page_title($vars->{title});
207 $ctx->response->breadcrumb($vars->{breadcrumb});
208  
209 $self->page_navigation($ctx->response, 'remove', $vars)
210 unless $ctx->request->path_parameters->{file};
211  
212 return $self->render_page(
213 template => 'page/remove.html',
214 context => $ctx,
215 vars => {
216 '.yukkiname' => $vars->{page},
217 '#cancel_remove@href' => $vars->{return_link},
218 },
219 );
220 }
221  
222  
223 sub attachments {
224 my ($self, $ctx, $attachments) = @_;
225  
226 my @files = map {
227 my @links = $self->attachment_links($ctx, $_);
228  
229 my %primary_link = %{ $links[0] };
230 $primary_link{label} = $_->file_name;
231  
232 my $file_name = $self->render_links(
233 context => $ctx,
234 links => [ \%primary_link ],
235 );
236  
237 +{
238 './@id' => $_->file_id,
239 '.filename' => $file_name,
240 '.size' => $_->formatted_file_size,
241 '.action' => $self->render_attachment_links($ctx, \@links),
242 };
243 } @$attachments;
244  
245 return $self->render(
246 template => 'page/attachments.html',
247 vars => {
248 '.file' => \@files,
249 },
250 );
251 }
252  
253  
254 sub attachment_links {
255 my ($self, $ctx, $attachment) = @_;
256  
257 my @links;
258  
259 if ($attachment->has_format) {
260 push @links, {
261 label => 'View',
262 href => join('/', 'page', 'view',
263 $attachment->repository_name,
264 $attachment->full_path),
265 };
266 }
267 else {
268 push @links, {
269 label => 'View',
270 href => join('/', 'attachment', 'view',
271 $attachment->repository_name,
272 $attachment->full_path),
273 } if $attachment->media_type ne 'application/octet';
274  
275 push @links, {
276 label => 'Download',
277 href => join('/', 'attachment', 'download',
278 $attachment->repository_name,
279 $attachment->full_path),
280 };
281 }
282  
283 push @links, {
284 label => 'Rename',
285 href => join('/', 'attachment', 'rename',
286 $attachment->repository_name,
287 $attachment->full_path),
288 };
289  
290 push @links, {
291 label => 'Remove',
292 href => join('/', 'attachment', 'remove',
293 $attachment->repository_name,
294 $attachment->full_path),
295 };
296  
297 return @links;
298 }
299  
300  
301 sub render_attachment_links {
302 my ($self, $ctx, $links) = @_;
303 return $self->render_links(context => $ctx, links => $links);
304 }
305  
306  
307 sub preview {
308 my ($self, $ctx, $vars) = @_;
309 my $file = $vars->{file};
310  
311 my $html = $file->fetch_formatted($ctx);
312  
313 return $html;
314 }
315  
316 1;
317  
318 __END__