| 1 | package Muster::Hook::Toggle; | ||||||||||
| 2 | 1 | $Muster::Hook::Toggle::VERSION = '0.96'; | |||||||||
| 3 | 1 | ▶ | use Mojo::Base 'Muster::Hook'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Muster::LeafFile; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Muster::Hooks; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use Carp 'croak'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | Muster::Hook::Toggle - Muster toggle directive. | ||||||||||
| 13 | |||||||||||
| 14 | =head1 VERSION | ||||||||||
| 15 | |||||||||||
| 16 | version 0.96 | ||||||||||
| 17 | |||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||
| 19 | |||||||||||
| 20 | L<Muster::Hook::Toggle> toggle display of sections on and off. | ||||||||||
| 21 | |||||||||||
| 22 | =head1 METHODS | ||||||||||
| 23 | |||||||||||
| 24 | L<Muster::Hook::Toggle> inherits all methods from L<Muster::Hook::Directives>. | ||||||||||
| 25 | |||||||||||
| 26 | =head2 register_filter | ||||||||||
| 27 | |||||||||||
| 28 | Do some intialization. | ||||||||||
| 29 | |||||||||||
| 30 | =cut | ||||||||||
| 31 | 0 | ▶ | sub register_filter { | ||||||||
|
Subroutine
register_filter: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 32 | 0 | my $self = shift; | |||||||||
| 33 | 0 | my $hookmaster = shift; | |||||||||
| 34 | 0 | my $conf = shift; | |||||||||
| 35 | |||||||||||
| 36 | 0 | $self->{metadb} = $hookmaster->{metadb}; | |||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | $hookmaster->add_filter('toggle' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 39 | 0 | my %args = @_; | |||||||||
| 40 | |||||||||||
| 41 | 0 | return $self->process(%args); | |||||||||
| 42 | }, | ||||||||||
| 43 | 0 | ); | |||||||||
| 44 | 0 | return $self; | |||||||||
| 45 | } # register_filter | ||||||||||
| 46 | |||||||||||
| 47 | =head2 process | ||||||||||
| 48 | |||||||||||
| 49 | Process toggling -- expects HTML input. | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | 0 | ▶ | sub process { | ||||||||
|
Subroutine
process: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 53 | 0 | my $self = shift; | |||||||||
| 54 | 0 | my %args = @_; | |||||||||
| 55 | |||||||||||
| 56 | 0 | my $c = $args{controller}; | |||||||||
| 57 | 0 | my $html = $args{html}; | |||||||||
| 58 | |||||||||||
| 59 | # Only do toggle processing if there's at least one toggle | ||||||||||
| 60 | 0 | ▶ | if ($html =~ /<!--toggle/) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | { | ||||||||||
| 62 | 0 | $html =~ s/<!--toggle-(start|end)\s+id=(\w+)\s*-->/$self->set_a_toggle($1,$2)/eg; | |||||||||
| 63 | |||||||||||
| 64 | 0 | my $css = <<EOT1; | |||||||||
| 65 | <style> | ||||||||||
| 66 | input.toggle[type="checkbox"] { | ||||||||||
| 67 | display: none; | ||||||||||
| 68 | } | ||||||||||
| 69 | label.toggle { | ||||||||||
| 70 | position: relative; | ||||||||||
| 71 | display: block; | ||||||||||
| 72 | width: 38px; | ||||||||||
| 73 | height: 38px; | ||||||||||
| 74 | cursor: pointer; | ||||||||||
| 75 | background: blue; | ||||||||||
| 76 | border-radius: 100%; | ||||||||||
| 77 | color: white; | ||||||||||
| 78 | } | ||||||||||
| 79 | label.toggle:after { | ||||||||||
| 80 | content: '+'; | ||||||||||
| 81 | position: absolute; | ||||||||||
| 82 | top: 2px; | ||||||||||
| 83 | left: 11px; | ||||||||||
| 84 | font-size: 30px; | ||||||||||
| 85 | line-height: 1em; | ||||||||||
| 86 | font-family: sans-serif; | ||||||||||
| 87 | } | ||||||||||
| 88 | .hide-show { | ||||||||||
| 89 | display: none; | ||||||||||
| 90 | padding: 10px; | ||||||||||
| 91 | border: 2px solid #999999; | ||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | input.toggle[type="checkbox"]:checked ~ label { | ||||||||||
| 95 | background: green; | ||||||||||
| 96 | } | ||||||||||
| 97 | input.toggle[type="checkbox"]:checked ~ label:after { | ||||||||||
| 98 | content: '-'; | ||||||||||
| 99 | left: 15px; | ||||||||||
| 100 | } | ||||||||||
| 101 | input.toggle[type="checkbox"]:checked ~ .hide-show { | ||||||||||
| 102 | display: block; | ||||||||||
| 103 | } | ||||||||||
| 104 | </style> | ||||||||||
| 105 | EOT1 | ||||||||||
| 106 | |||||||||||
| 107 | # This is going into the controller stash | ||||||||||
| 108 | 0 | $c->stash('head_append' => $css); | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 0 | return $html; | |||||||||
| 112 | } # process | ||||||||||
| 113 | |||||||||||
| 114 | =head2 set_a_toggle | ||||||||||
| 115 | |||||||||||
| 116 | Set one toggle (start or end) | ||||||||||
| 117 | |||||||||||
| 118 | $text = set_a_toggle($begin_or_end,$id); | ||||||||||
| 119 | |||||||||||
| 120 | =cut | ||||||||||
| 121 | 0 | ▶ | sub set_a_toggle { | ||||||||
|
Subroutine
set_a_toggle: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 122 | 0 | my $self = shift; | |||||||||
| 123 | 0 | my $begin_or_end = shift; | |||||||||
| 124 | 0 | my $id = shift; | |||||||||
| 125 | |||||||||||
| 126 | 0 | my $out = ''; | |||||||||
| 127 | 0 | ▶ | if ($begin_or_end =~ /start/i) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | { | ||||||||||
| 129 | 0 | $out = <<EOT2; | |||||||||
| 130 | <div> | ||||||||||
| 131 | <input class="toggle" type="checkbox" id="$id" /> | ||||||||||
| 132 | <label class="toggle" for="$id"></label> | ||||||||||
| 133 | <div class="hide-show"> | ||||||||||
| 134 | EOT2 | ||||||||||
| 135 | } | ||||||||||
| 136 | else | ||||||||||
| 137 | { | ||||||||||
| 138 | # put in the closing </div>s | ||||||||||
| 139 | 0 | $out = "\n</div></div>\n"; | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | 0 | return $out; | |||||||||
| 143 | } # set_a_toggle | ||||||||||
| 144 | |||||||||||
| 145 | 1 | 1; | |||||||||