| 1 | package Yukki::Web::View::Page; | ||
| 2 | $Yukki::Web::View::Page::VERSION = '0.991_002'; # TRIAL | ||
| 3 | |||
| 4 | $Yukki::Web::View::Page::VERSION = '0.991002';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 | # ABSTRACT: render HTML for viewing and editing wiki pages | ||
| 15 | |||
| 16 | |||
| 17 | has blank_template => ( | ||
| 18 | is => 'ro', | ||
| 19 | isa => class_type('Template::Pure'), | ||
| 20 | lazy => 1, | ||
| 21 | builder => '_build_blank_template', | ||
| 22 | ); | ||
| 23 | |||
| 24 | sub _build_blank_template { | ||
| 25 | shift->prepare_template( | ||
| 26 | template => 'page/blank.html', | ||
| 27 | directives => [ | ||
| 28 | '#yukkiname' => 'page', | ||
| 29 | '#create-page@href' => 'link', | ||
| 30 | '#file-list' => 'attachments | encoded_string', | ||
| 31 | ], | ||
| 32 | ); | ||
| 33 | } | ||
| 34 | |||
| 35 | has view_template => ( | ||
| 36 | is => 'ro', | ||
| 37 | isa => class_type('Template::Pure'), | ||
| 38 | lazy => 1, | ||
| 39 | builder => '_build_view_template', | ||
| 40 | ); | ||
| 41 | |||
| 42 | sub _build_view_template { | ||
| 43 | shift->prepare_template( | ||
| 44 | template => 'page/view.html', | ||
| 45 | directives => [ | ||
| 46 | '#yukkitext' => 'html | encoded_string', | ||
| 47 | ], | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | |||
| 51 | has history_template => ( | ||
| 52 | is => 'ro', | ||
| 53 | isa => class_type('Template::Pure'), | ||
| 54 | lazy => 1, | ||
| 55 | builder => '_build_history_template', | ||
| 56 | ); | ||
| 57 | |||
| 58 | sub _build_history_template { | ||
| 59 | shift->prepare_template( | ||
| 60 | template => 'page/history.html', | ||
| 61 | directives => [ | ||
| 62 | 'form@action' => 'form_action', | ||
| 63 | '.revision' => { | ||
| 64 | 'rev<-revisions' => [ | ||
| 65 | '.first-revision input@value' => 'rev.object_id', | ||
| 66 | '.second-revision input@value' => 'rev.object_id', | ||
| 67 | '.date' => 'rev.time_ago', | ||
| 68 | '.author' => 'rev.author_name', | ||
| 69 | '.diffstat' => '+={rev.lines_added}/-={rev.lines_removed}', | ||
| 70 | '.comment' => 'rev.comment | default("(no comment)")', | ||
| 71 | '.first-revision input' => sub { | ||
| 72 | my ($t, $input, $vars) = @_; | ||
| 73 | $input->attr(checked => 'checked') | ||
| 74 | if $vars->{index} == 2; | ||
| 75 | }, | ||
| 76 | '.second-revision input' => sub { | ||
| 77 | my ($t, $input, $vars) = @_; | ||
| 78 | $input->attr(checked => 'checked') | ||
| 79 | if $vars->{index} == 1; | ||
| 80 | }, | ||
| 81 | ], | ||
| 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 page_navigation { | ||
| 202 | my ($self, $response, $this_action, $vars) = @_; | ||
| 203 | |||
| 204 | for my $action (qw( edit history rename remove )) { | ||
| 205 | next if $action eq $this_action; | ||
| 206 | |||
| 207 | $response->add_navigation_item([ qw( page page_bottom ) ] => { | ||
| 208 | label => ucfirst $action, | ||
| 209 | href => join('/', 'page', $action, $vars->{repository}, $vars->{page}), | ||
| 210 | sort => 20, | ||
| 211 | }); | ||
| 212 | } | ||
| 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 | return $self->render_page( | ||
| 244 | template => $self->view_template, | ||
| 245 | context => $ctx, | ||
| 246 | vars => { | ||
| 247 | 'html' => $html, | ||
| 248 | }, | ||
| 249 | ); | ||
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | sub history { | ||
| 254 | my ($self, $ctx, $vars) = @_; | ||
| 255 | |||
| 256 | $ctx->response->page_title($vars->{title}); | ||
| 257 | $ctx->response->breadcrumb($vars->{breadcrumb}); | ||
| 258 | |||
| 259 | $self->page_navigation($ctx->response, 'history', $vars); | ||
| 260 | |||
| 261 | my $i = 0; | ||
| 262 | return $self->render_page( | ||
| 263 | template => $self->history_template, | ||
| 264 | context => $ctx, | ||
| 265 | vars => { | ||
| 266 | 'form_action' => join('/', '/page/diff', $vars->{repository}, $vars->{page}), | ||
| 267 | 'revisions' => $vars->{revisions}, | ||
| 268 | }, | ||
| 269 | ); | ||
| 270 | } | ||
| 271 | |||
| 272 | |||
| 273 | sub diff { | ||
| 274 | my ($self, $ctx, $vars) = @_; | ||
| 275 | my $file = $vars->{file}; | ||
| 276 | |||
| 277 | $ctx->response->page_title($vars->{title}); | ||
| 278 | $ctx->response->breadcrumb($vars->{breadcrumb}); | ||
| 279 | |||
| 280 | $self->page_navigation($ctx->response, 'diff', $vars); | ||
| 281 | |||
| 282 | my $html = $file->fetch_formatted($ctx); | ||
| 283 | |||
| 284 | return $self->render_page( | ||
| 285 | template => $self->diff_template, | ||
| 286 | context => $ctx, | ||
| 287 | vars => { | ||
| 288 | html => $html, | ||
| 289 | }, | ||
| 290 | ); | ||
| 291 | } | ||
| 292 | |||
| 293 | |||
| 294 | sub edit { | ||
| 295 | my ($self, $ctx, $vars) = @_; | ||
| 296 | my $file = $vars->{file}; | ||
| 297 | |||
| 298 | $ctx->response->page_title($vars->{title}); | ||
| 299 | $ctx->response->breadcrumb($vars->{breadcrumb}); | ||
| 300 | |||
| 301 | my $html = $file->fetch_formatted($ctx, $vars->{position}); | ||
| 302 | |||
| 303 | $self->page_navigation($ctx->response, 'edit', $vars); | ||
| 304 | |||
| 305 | return $self->render_page( | ||
| 306 | template => $self->edit_template, | ||
| 307 | context => $ctx, | ||
| 308 | vars => { | ||
| 309 | page => $vars->{page}, | ||
| 310 | source => scalar $vars->{file}->fetch // '', | ||
| 311 | position => $vars->{position}, | ||
| 312 | html => $html, | ||
| 313 | attachments => $self->attachments($ctx, $vars->{attachments}), | ||
| 314 | }, | ||
| 315 | ); | ||
| 316 | } | ||
| 317 | |||
| 318 | |||
| 319 | sub rename { | ||
| 320 | my ($self, $ctx, $vars) = @_; | ||
| 321 | my $file = $vars->{file}; | ||
| 322 | |||
| 323 | $ctx->response->page_title($vars->{title}); | ||
| 324 | $ctx->response->breadcrumb($vars->{breadcrumb}); | ||
| 325 | |||
| 326 | $self->page_navigation($ctx->response, 'rename', $vars) | ||
| 327 | unless $ctx->request->path_parameters->{file}; | ||
| 328 | |||
| 329 | return $self->render_page( | ||
| 330 | template => $self->rename_template, | ||
| 331 | context => $ctx, | ||
| 332 | vars => { | ||
| 333 | page => $vars->{page}, | ||
| 334 | }, | ||
| 335 | ); | ||
| 336 | } | ||
| 337 | |||
| 338 | |||
| 339 | sub remove { | ||
| 340 | my ($self, $ctx, $vars) = @_; | ||
| 341 | my $file = $vars->{file}; | ||
| 342 | |||
| 343 | $ctx->response->page_title($vars->{title}); | ||
| 344 | $ctx->response->breadcrumb($vars->{breadcrumb}); | ||
| 345 | |||
| 346 | $self->page_navigation($ctx->response, 'remove', $vars) | ||
| 347 | unless $ctx->request->path_parameters->{file}; | ||
| 348 | |||
| 349 | return $self->render_page( | ||
| 350 | template => $self->remove_template, | ||
| 351 | context => $ctx, | ||
| 352 | vars => { | ||
| 353 | page => $vars->{page}, | ||
| 354 | return_link => $vars->{return_link}, | ||
| 355 | }, | ||
| 356 | ); | ||
| 357 | } | ||
| 358 | |||
| 359 | |||
| 360 | sub attachments { | ||
| 361 | my ($self, $ctx, $attachments) = @_; | ||
| 362 | |||
| 363 | return $self->render( | ||
| 364 | template => $self->attachments_template, | ||
| 365 | context => $ctx, | ||
| 366 | vars => { | ||
| 367 | files => @$attachments ? [ map { | ||
| 368 | my @links = $self->attachment_links($ctx, $_); | ||
| 369 | |||
| 370 | my %primary_link = %{ $links[0] }; | ||
| 371 | $primary_link{label} = $_->file_name; | ||
| 372 | |||
| 373 | my $file_name = $self->render_links( | ||
| 374 | context => $ctx, | ||
| 375 | links => [ \%primary_link ], | ||
| 376 | ); | ||
| 377 | |||
| 378 | { | ||
| 379 | file_id => $_->file_id, | ||
| 380 | file_name => $file_name, | ||
| 381 | file_size => $_->formatted_file_size, | ||
| 382 | action => $self->render_attachment_links($ctx, \@links), | ||
| 383 | } | ||
| 384 | } @$attachments ] : undef, | ||
| 385 | }, | ||
| 386 | ); | ||
| 387 | } | ||
| 388 | |||
| 389 | |||
| 390 | sub attachment_links { | ||
| 391 | my ($self, $ctx, $attachment) = @_; | ||
| 392 | |||
| 393 | my @links; | ||
| 394 | |||
| 395 | if ($attachment->has_format) { | ||
| 396 | push @links, { | ||
| 397 | label => 'View', | ||
| 398 | href => join('/', 'page', 'view', | ||
| 399 | $attachment->repository_name, | ||
| 400 | $attachment->full_path), | ||
| 401 | }; | ||
| 402 | } | ||
| 403 | else { | ||
| 404 | push @links, { | ||
| 405 | label => 'View', | ||
| 406 | href => join('/', 'attachment', 'view', | ||
| 407 | $attachment->repository_name, | ||
| 408 | $attachment->full_path), | ||
| 409 | } if $attachment->media_type ne 'application/octet'; | ||
| 410 | |||
| 411 | push @links, { | ||
| 412 | label => 'Download', | ||
| 413 | href => join('/', 'attachment', 'download', | ||
| 414 | $attachment->repository_name, | ||
| 415 | $attachment->full_path), | ||
| 416 | }; | ||
| 417 | } | ||
| 418 | |||
| 419 | push @links, { | ||
| 420 | label => 'Rename', | ||
| 421 | href => join('/', 'attachment', 'rename', | ||
| 422 | $attachment->repository_name, | ||
| 423 | $attachment->full_path), | ||
| 424 | }; | ||
| 425 | |||
| 426 | push @links, { | ||
| 427 | label => 'Remove', | ||
| 428 | href => join('/', 'attachment', 'remove', | ||
| 429 | $attachment->repository_name, | ||
| 430 | $attachment->full_path), | ||
| 431 | }; | ||
| 432 | |||
| 433 | return @links; | ||
| 434 | } | ||
| 435 | |||
| 436 | |||
| 437 | sub render_attachment_links { | ||
| 438 | my ($self, $ctx, $links) = @_; | ||
| 439 | return $self->render_links(context => $ctx, links => $links); | ||
| 440 | } | ||
| 441 | |||
| 442 | |||
| 443 | sub preview { | ||
| 444 | my ($self, $ctx, $vars) = @_; | ||
| 445 | my $file = $vars->{file}; | ||
| 446 | |||
| 447 | my $html = $file->fetch_formatted($ctx); | ||
| 448 | |||
| 449 | return $html; | ||
| 450 | } | ||
| 451 | |||
| 452 | 1; | ||
| 453 | |||
| 454 | __END__ | ||
| 455 | |||
| 456 | =pod | ||
| 457 | |||
| 458 | =encoding UTF-8 | ||
| 459 | |||
| 460 | =head1 NAME | ||
| 461 | |||
| 462 | Yukki::Web::View::Page - render HTML for viewing and editing wiki pages | ||
| 463 | |||
| 464 | =head1 VERSION | ||
| 465 | |||
| 466 | version 0.991_002 | ||
| 467 | |||
| 468 | =head1 DESCRIPTION | ||
| 469 | |||
| 470 | Renders wiki pages. | ||
| 471 | |||
| 472 | =head1 METHODS | ||
| 473 | |||
| 474 | =head2 blank | ||
| 475 | |||
| 476 | Renders a page that links to the edit page for this location. This helps you | ||
| 477 | create the links. | ||
| 478 | |||
| 479 | =head2 page_navigation | ||
| 480 | |||
| 481 | Sets up the page navigation menu. | ||
| 482 | |||
| 483 | =head2 view | ||
| 484 | |||
| 485 | Renders a page as a view. | ||
| 486 | |||
| 487 | =head2 history | ||
| 488 | |||
| 489 | Display the history for a page. | ||
| 490 | |||
| 491 | =head2 diff | ||
| 492 | |||
| 493 | Display a diff for a file. | ||
| 494 | |||
| 495 | =head2 edit | ||
| 496 | |||
| 497 | Renders the editor for a page. | ||
| 498 | |||
| 499 | =head2 rename | ||
| 500 | |||
| 501 | Renders the rename form for a page. | ||
| 502 | |||
| 503 | =head2 remove | ||
| 504 | |||
| 505 | Renders the remove confirmation page. | ||
| 506 | |||
| 507 | =head2 attachments | ||
| 508 | |||
| 509 | Renders the attachments table. | ||
| 510 | |||
| 511 | =head2 attachment_links | ||
| 512 | |||
| 513 | =head2 render_attachment_links | ||
| 514 | |||
| 515 | Renders the links listed in the action column of the attachments table. | ||
| 516 | |||
| 517 | =head2 preview | ||
| 518 | |||
| 519 | Renders a preview of an edit in progress. | ||
| 520 | |||
| 521 | =head1 AUTHOR | ||
| 522 | |||
| 523 | Andrew Sterling Hanenkamp <hanenkamp@cpan.org> | ||
| 524 | |||
| 525 | =head1 COPYRIGHT AND LICENSE | ||
| 526 | |||
| 527 | This software is copyright (c) 2017 by Qubling Software LLC. | ||
| 528 | |||
| 529 | This is free software; you can redistribute it and/or modify it under | ||
| 530 | the same terms as the Perl 5 programming language system itself. | ||
| 531 | |||
| 532 | =cut |