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

filter: Statementstmt 0.0% 0 / 138 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 209 CC 8 SCAR 15.3
CC
8
Cov
0%
view
12.0
attachment_links
12.0
rename
6.0
CRAP 4.6
1 package Yukki::Web::View::Page;
2 $Yukki::Web::View::Page::VERSION = '0.991_007'; # TRIAL
3 $Yukki::Web::View::Page::VERSION = '0.991007';
4 use v5.24;
5 use utf8;
6 use Moo;
7  
8 use Type::Utils;
9  
10 use namespace::clean;
11  
12 extends 'Yukki::Web::View';
13  
14 with 'Yukki::Web::View::Role::Navigation';
15  
16 # ABSTRACT: render HTML for viewing and editing wiki pages
17  
18  
19 has blank_template => (
20 is => 'ro',
21 isa => class_type('Template::Pure'),
22 lazy => 1,
23 builder => '_build_blank_template',
24 );
25  
26 sub _build_blank_template {
27 shift->prepare_template(
28 template => 'page/blank.html',
29 directives => [
30 '#yukkiname' => 'page',
31 '#create-page@href' => 'link',
32 '#file-list' => 'attachments | encoded_string',
33 ],
34 );
35 }
36  
37 has view_template => (
38 is => 'ro',
39 isa => class_type('Template::Pure'),
40 lazy => 1,
41 builder => '_build_view_template',
42 );
43  
44 sub _build_view_template {
45 shift->prepare_template(
46 template => 'page/view.html',
47 directives => [
48 '#yukkitext' => 'html | encoded_string',
49 '#revision' => 'revision',
50 '#file_date' => 'file_date',
51 ],
52 );
53 }
54  
55 has history_template => (
56 is => 'ro',
57 isa => class_type('Template::Pure'),
58 lazy => 1,
59 builder => '_build_history_template',
60 );
61  
62 sub _build_history_template {
63 shift->prepare_template(
64 template => 'page/history.html',
65 directives => [
66 'form@action' => 'form_action',
67 '.revision' => {
68 'rev<-revisions' => [
69 '.first-revision input@value' => 'rev.file.object_id',
70 '.second-revision input@value' => 'rev.file.object_id',
71 '.date a' => 'rev.file.time_ago',
72 '.date a@href' => '={rev.page_url}',
73 '.author' => 'rev.file.author_name',
74 '.diffstat' => '+={rev.file.lines_added}/-={rev.file.lines_removed}',
75 '.comment' => 'rev.file.comment | default("(no comment)")',
76 ],
77 },
78 sub {
79 my ($t, $dom, $data) = @_;
80 $dom->at('.revision:nth-child(2) .first-revision input')->attr(checked => 1);
81 $dom->at('.revision:nth-child(1) .second-revision input')->attr(checked => 1);
82 },
83 ],
84 );
85 }
86  
87 has diff_template => (
88 is => 'ro',
89 isa => class_type('Template::Pure'),
90 lazy => 1,
91 builder => '_build_diff_template',
92 );
93  
94 sub _build_diff_template {
95 shift->prepare_template(
96 template => 'page/diff.html',
97 directives => [
98 '#diff' => 'html | encoded_string',
99 ],
100 );
101 }
102  
103 has edit_template => (
104 is => 'ro',
105 isa => class_type('Template::Pure'),
106 lazy => 1,
107 builder => '_build_edit_template',
108 );
109  
110 sub _build_edit_template {
111 shift->prepare_template(
112 template => 'page/edit.html',
113 directives => [
114 '#yukkiname' => 'page',
115 '#yukkitext' => 'source',
116 '#yukkitext_position@value' => 'position',
117 '#preview-yukkitext' => 'html | encoded_string',
118 '#attachments-list' => 'attachments | encoded_string',
119 ],
120 );
121 }
122  
123 has rename_template => (
124 is => 'ro',
125 isa => class_type('Template::Pure'),
126 lazy => 1,
127 builder => '_build_rename_template',
128 );
129  
130 sub _build_rename_template {
131 shift->prepare_template(
132 template => 'page/rename.html',
133 directives => [
134 '#yukkiname' => 'page',
135 '#yukkiname_new@value' => 'page',
136 ],
137 );
138 }
139  
140 has remove_template => (
141 is => 'ro',
142 isa => class_type('Template::Pure'),
143 lazy => 1,
144 builder => '_build_remove_template',
145 );
146  
147 sub _build_remove_template {
148 shift->prepare_template(
149 template => 'page/remove.html',
150 directives => [
151 '.yukkiname' => 'page',
152 '#cancel_remove@href' => 'return_link',
153 ],
154 );
155 }
156  
157 has attachments_template => (
158 is => 'ro',
159 isa => class_type('Template::Pure'),
160 lazy => 1,
161 builder => '_build_attachments_template',
162 );
163  
164 sub _build_attachments_template {
165 shift->prepare_template(
166 template => 'page/attachments.html',
167 directives => [
168 '.file' => {
169 'file<-files' => [
170 '@id' => 'file.file_id',
171 '.filename' => 'file.file_name | encoded_string',
172 '.size' => 'file.file_size',
173 '.action' => 'file.action | encoded_string',
174 ],
175 },
176 ],
177 );
178 }
179  
180  
181 sub blank {
182 my ($self, $ctx, $vars) = @_;
183  
184 my $link = "/page/edit/$vars->{repository}/$vars->{page}";
185  
186 $ctx->response->page_title($vars->{title});
187 $ctx->response->breadcrumb($vars->{breadcrumb});
188  
189 return $self->render_page(
190 template => $self->blank_template,
191 context => $ctx,
192 vars => {
193 page => $vars->{page},
194 link => $link,
195 attachments => $self->attachments($ctx, $vars->{files}),
196 },
197 );
198 }
199  
200  
201 sub standard_menu {
202 return map {
203 +{
204 action => $_,
205 href => "page/$_/%{repository}s/%{page}s",
206 },
207 } qw( edit history rename remove );
208 }
209  
210  
211 after page_navigation => sub {
212 my ($self, $response, $this_action, $vars) = @_;
213  
214 for my $view_name (keys %{ $self->app->settings->page_views }) {
215 my $view_info = $self->app->settings->page_views->{$view_name};
216  
217 next if $view_info->{hide};
218  
219 my $args = "?view=$view_name";
220 $args = '' if $view_name eq 'default';
221  
222 $response->add_navigation_item([ qw( page page_bottom ) ] => {
223 label => $view_info->{label},
224 href => join('/', 'page/view', $vars->{repository}, $vars->{page})
225 . $args,
226 sort => $view_info->{sort},
227 });
228 }
229 };
230  
231  
232 sub view {
233 my ($self, $ctx, $vars) = @_;
234 my $file = $vars->{file};
235  
236 $ctx->response->page_title($vars->{title});
237 $ctx->response->breadcrumb($vars->{breadcrumb});
238  
239 my $html = $file->fetch_formatted($ctx, -1);
240  
241 $self->page_navigation($ctx->response, 'view', $vars);
242  
243 $ctx->add_info("This is a historical version of this page")
244 if $file->revision ne 'HEAD'
245 and $file->find_path($file->full_path)
246 ne $file->object_id;
247  
248 my $rev8 = $file->revision;
249 $rev8 = substr $rev8, 0, 8 if $file->revision ne 'HEAD';
250  
251 return $self->render_page(
252 template => $self->view_template,
253 context => $ctx,
254 vars => {
255 'html' => $html,
256 'revision' => $rev8,
257 'file_date' => $file->file_date('relative'),
258 },
259 );
260 }
261  
262  
263 sub history {
264 my ($self, $ctx, $vars) = @_;
265  
266 $ctx->response->page_title($vars->{title});
267 $ctx->response->breadcrumb($vars->{breadcrumb});
268  
269 $self->page_navigation($ctx->response, 'history', $vars);
270  
271 my $i = 0;
272 return $self->render_page(
273 template => $self->history_template,
274 context => $ctx,
275 vars => {
276 'form_action' => join('/', '/page/diff', $vars->{repository}, $vars->{page}),
277 'revisions' => $vars->{revisions},
278 },
279 );
280 }
281  
282  
283 sub diff {
284 my ($self, $ctx, $vars) = @_;
285 my $file = $vars->{file};
286  
287 $ctx->response->page_title($vars->{title});
288 $ctx->response->breadcrumb($vars->{breadcrumb});
289  
290 $self->page_navigation($ctx->response, 'diff', $vars);
291  
292 my $html = $file->fetch_formatted($ctx);
293  
294 return $self->render_page(
295 template => $self->diff_template,
296 context => $ctx,
297 vars => {
298 html => $html,
299 },
300 );
301 }
302  
303  
304 sub edit {
305 my ($self, $ctx, $vars) = @_;
306 my $file = $vars->{file};
307  
308 $ctx->response->page_title($vars->{title});
309 $ctx->response->breadcrumb($vars->{breadcrumb});
310  
311 my $html = $file->fetch_formatted($ctx, $vars->{position});
312  
313 $self->page_navigation($ctx->response, 'edit', $vars);
314  
315 return $self->render_page(
316 template => $self->edit_template,
317 context => $ctx,
318 vars => {
319 page => $vars->{page},
320 source => scalar $vars->{file}->fetch // '',
321 position => $vars->{position},
322 html => $html,
323 attachments => $self->attachments($ctx, $vars->{attachments}),
324 },
325 );
326 }
327  
328  
329 sub rename {
330 my ($self, $ctx, $vars) = @_;
331 my $file = $vars->{file};
332  
333 $ctx->response->page_title($vars->{title});
334 $ctx->response->breadcrumb($vars->{breadcrumb});
335  
336 $self->page_navigation($ctx->response, 'rename', $vars)
337 unless $ctx->request->path_parameters->{file};
338  
339 return $self->render_page(
340 template => $self->rename_template,
341 context => $ctx,
342 vars => {
343 page => $vars->{page},
344 },
345 );
346 }
347  
348  
349 sub remove {
350 my ($self, $ctx, $vars) = @_;
351 my $file = $vars->{file};
352  
353 $ctx->response->page_title($vars->{title});
354 $ctx->response->breadcrumb($vars->{breadcrumb});
355  
356 $self->page_navigation($ctx->response, 'remove', $vars)
357 unless $ctx->request->path_parameters->{file};
358  
359 return $self->render_confirmation(
360 context => $ctx,
361 title => "Remove $vars->{page}?",
362 question => "Are you sure that you wish to remove $vars->{page} from the repository?",
363 yes_label => 'Remove Now',
364 no_link => $vars->{return_link},
365 );
366 }
367  
368  
369 sub attachments {
370 my ($self, $ctx, $attachments) = @_;
371  
372 return $self->render(
373 template => $self->attachments_template,
374 context => $ctx,
375 vars => {
376 files => @$attachments ? [ map {
377 my @links = $self->attachment_links($ctx, $_);
378  
379 my %primary_link = %{ $links[0] };
380 $primary_link{label} = $_->file_name;
381  
382 my $file_name = $self->render_links(
383 context => $ctx,
384 links => [ \%primary_link ],
385 );
386  
387 {
388 file_id => $_->file_id,
389 file_name => $file_name,
390 file_size => $_->formatted_file_size,
391 action => $self->render_attachment_links($ctx, \@links),
392 }
393 } @$attachments ] : undef,
394 },
395 );
396 }
397  
398  
399 sub attachment_links {
400 my ($self, $ctx, $attachment) = @_;
401  
402 my @links;
403  
404 if ($attachment->has_format) {
405 push @links, {
406 label => 'View',
407 href => join('/', 'page', 'view',
408 $attachment->repository_name,
409 $attachment->full_path),
410 };
411 }
412 else {
413 push @links, {
414 label => 'View',
415 href => join('/', 'attachment', 'view',
416 $attachment->repository_name,
417 $attachment->full_path),
418 } if $attachment->media_type ne 'application/octet';
419  
420 push @links, {
421 label => 'Download',
422 href => join('/', 'attachment', 'download',
423 $attachment->repository_name,
424 $attachment->full_path),
425 };
426 }
427  
428 push @links, {
429 label => 'Rename',
430 href => join('/', 'attachment', 'rename',
431 $attachment->repository_name,
432 $attachment->full_path),
433 };
434  
435 push @links, {
436 label => 'Remove',
437 href => join('/', 'attachment', 'remove',
438 $attachment->repository_name,
439 $attachment->full_path),
440 };
441  
442 return @links;
443 }
444  
445  
446 sub render_attachment_links {
447 my ($self, $ctx, $links) = @_;
448 return $self->render_links(context => $ctx, links => $links);
449 }
450  
451  
452 sub preview {
453 my ($self, $ctx, $vars) = @_;
454 my $file = $vars->{file};
455  
456 my $html = $file->fetch_formatted($ctx);
457  
458 return $html;
459 }
460  
461 1;
462  
463 __END__