| 1 | use strictures 1; | ||
| 2 | package Mojito::Template::Zoom; | ||
| 3 | $Mojito::Template::Zoom::VERSION = '0.25'; | ||
| 4 | use Moo; | ||
| 5 | use HTML::Zoom; | ||
| 6 | |||
| 7 | # Given a template we can zoom in on parts and manipulex 'em. | ||
| 8 | has 'template' => ( | ||
| 9 | is => 'rw', | ||
| 10 | lazy => 1, | ||
| 11 | builder => '_build_template', | ||
| 12 | ); | ||
| 13 | |||
| 14 | has 'template_z' => ( | ||
| 15 | is => 'ro', | ||
| 16 | lazy => 1, | ||
| 17 | builder => '_build_zoom', | ||
| 18 | ); | ||
| 19 | has 'edit_area_z' => ( | ||
| 20 | is => 'ro', | ||
| 21 | lazy => 1, | ||
| 22 | default => sub { $_[0]->zoom->select('#content') }, | ||
| 23 | ); | ||
| 24 | has 'view_area_z' => ( | ||
| 25 | is => 'ro', | ||
| 26 | lazy => 1, | ||
| 27 | default => sub { $_[0]->zoom->select('#view_area') }, | ||
| 28 | ); | ||
| 29 | |||
| 30 | =head1 Methods | ||
| 31 | |||
| 32 | =head2 replace_view_area | ||
| 33 | |||
| 34 | Inject rendered content into the view area. | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | sub replace_view_area { | ||
| 39 | my ($self, $new_content) = @_; | ||
| 40 | |||
| 41 | $self->view_area->replace_content(\$new_content); | ||
| 42 | } | ||
| 43 | |||
| 44 | =head2 replace_edit_area | ||
| 45 | |||
| 46 | Inject source into edit area. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub replace_edit_area{ | ||
| 51 | my ($self, $new_content) = @_; | ||
| 52 | |||
| 53 | $self->edit_area->replace_content(\$new_content); | ||
| 54 | } | ||
| 55 | |||
| 56 | =head2 replace_edit_page | ||
| 57 | |||
| 58 | Put some content into the edit page, both source and rendered. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub replace_edit_page { | ||
| 63 | my ($self, $edit, $view) = @_; | ||
| 64 | |||
| 65 | $self->template_z | ||
| 66 | ->select('#content') | ||
| 67 | ->replace_content(\$edit) | ||
| 68 | ->select('#view_area') | ||
| 69 | ->replace_content(\$view)->to_html; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _build_zoom { | ||
| 73 | my ($self) = @_; | ||
| 74 | |||
| 75 | HTML::Zoom->new->from_html($self->template); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _build_template { | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | my $edit_page = <<'END_HTML'; | ||
| 82 | <!doctype html> | ||
| 83 | <html> | ||
| 84 | <head> | ||
| 85 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
| 86 | <title>Mojito page</title> | ||
| 87 | </head> | ||
| 88 | <body> | ||
| 89 | <section id="edit_area" style="float:left;"> | ||
| 90 | <form id="editForm" action="" accept-charset="UTF-8" method="post"> | ||
| 91 | <textarea id="content" cols="72" rows="24" /></textarea><br /> | ||
| 92 | <input id="submit" type="submit" value="Submit content" /> | ||
| 93 | </form> | ||
| 94 | </section> | ||
| 95 | <section id="view_area" style="float:left; margin-left:1em;"></section> | ||
| 96 | </body> | ||
| 97 | </html> | ||
| 98 | END_HTML | ||
| 99 | |||
| 100 | return $edit_page; | ||
| 101 | |||
| 102 | } | ||
| 103 | 1; |