| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Jifty::Plugin::Halo; | ||
| 5 | use Jifty::Plugin::Halo::Mason; | ||
| 6 | use base qw/Jifty::Plugin/; | ||
| 7 | use Time::HiRes 'time'; | ||
| 8 | use Class::Trigger; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Jifty::Plugin::Halo - Provides halos | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | This plugin provides L<http://seaside.st|Seaside>-esque halos for your | ||
| 17 | application. It's included by default when using Jifty with DevelMode | ||
| 18 | turned on. | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | =head2 init | ||
| 23 | |||
| 24 | Only enable halos in DevelMode. Add our instrumentation to | ||
| 25 | L<Template::Declare>. | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | sub init { | ||
| 30 | my $self = shift; | ||
| 31 | return if $self->_pre_init; | ||
| 32 | return unless Jifty->config->framework('DevelMode'); | ||
| 33 | |||
| 34 | Jifty->web->add_javascript( 'halo.js' ); | ||
| 35 | Jifty->web->add_css( 'halo.css' ); | ||
| 36 | |||
| 37 | warn "Overwriting an existing Template::Declare->around_template" | ||
| 38 | if Template::Declare->around_template; | ||
| 39 | Template::Declare->around_template(sub { $self->around_template(@_) }); | ||
| 40 | |||
| 41 | push @{ Jifty->config->framework('Web')->{'MasonConfig'}{plugins} }, | ||
| 42 | 'Jifty::Plugin::Halo::Mason'; | ||
| 43 | Jifty::View->add_trigger( body_start => sub { $self->body_start } ); | ||
| 44 | Jifty::View->add_trigger( body_end => sub { $self->body_end } ); | ||
| 45 | |||
| 46 | Jifty->add_trigger( post_init => sub { $self->find_halo_plugins } ); | ||
| 47 | } | ||
| 48 | |||
| 49 | =head2 find_halo_plugins | ||
| 50 | |||
| 51 | Finds plugins which have C<halo_pre_template> or C<halo_post_template> | ||
| 52 | methods, and adds them as callbacks in the requisite places. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub find_halo_plugins { | ||
| 57 | my $self = shift; | ||
| 58 | for my $plugin (Jifty->plugins) { | ||
| 59 | for my $cb (grep {$plugin->can($_)} qw/ halo_pre_template halo_post_template/ ) { | ||
| 60 | Jifty::Plugin::Halo->add_trigger( | ||
| 61 | $cb => sub { $plugin->$cb(@_) }, | ||
| 62 | ); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | =head2 around_template | ||
| 68 | |||
| 69 | This is called to instrument L<Template::Declare> templates. We also draw a halo | ||
| 70 | around the template itself. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub around_template { | ||
| 75 | my ($self, $orig, $path, $args, $code) = @_; | ||
| 76 | |||
| 77 | return $orig->() if $path eq "/__jifty/halo"; | ||
| 78 | return $orig->() unless Jifty->handler->stash->{'in_body'}; | ||
| 79 | |||
| 80 | # for now, call the last piece of the template's path the name | ||
| 81 | $path =~ m{.*/(.+)}; | ||
| 82 | my $name = $1 || $path; | ||
| 83 | |||
| 84 | my $frame = $self->push_frame( | ||
| 85 | args => [ %{ Jifty->web->request->arguments } ], | ||
| 86 | path => $path, | ||
| 87 | name => $name, | ||
| 88 | ); | ||
| 89 | |||
| 90 | $frame->{displays}->{P} = { | ||
| 91 | name => "perl", | ||
| 92 | callback => sub { | ||
| 93 | my $deparsed = eval { | ||
| 94 | require Data::Dump::Streamer; | ||
| 95 | Data::Dump::Streamer::Dump($code)->Out; | ||
| 96 | }; | ||
| 97 | Jifty->web->escape($deparsed); | ||
| 98 | }, | ||
| 99 | }; | ||
| 100 | |||
| 101 | Template::Declare->buffer->append($self->halo_header($frame)); | ||
| 102 | $orig->(); | ||
| 103 | $frame = $self->pop_frame; | ||
| 104 | Template::Declare->buffer->append($self->halo_footer($frame)); | ||
| 105 | } | ||
| 106 | |||
| 107 | =head2 halo_header frame -> string | ||
| 108 | |||
| 109 | This will give you the halo header which includes links to the various | ||
| 110 | information displays for this template. | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub halo_header { | ||
| 115 | my $self = shift; | ||
| 116 | my $frame = shift; | ||
| 117 | my $id = $frame->{id}; | ||
| 118 | my $name = Jifty->web->escape($frame->{name}); | ||
| 119 | my $displays = $frame->{displays}; | ||
| 120 | |||
| 121 | my @buttons; | ||
| 122 | for my $letter (sort keys %$displays) { | ||
| 123 | my $d = $displays->{$letter}; | ||
| 124 | my $name = Jifty->web->escape($d->{name}); | ||
| 125 | |||
| 126 | push @buttons, join "\n", grep { $_ } | ||
| 127 | qq{<a id="halo-button-$name-$id"}, | ||
| 128 | qq{ onclick="halo_render('$id', '$name'); return false"}, | ||
| 129 | $d->{default} && qq{ style="font-weight:bold"}, | ||
| 130 | qq{ href="#">$letter</a>}, | ||
| 131 | } | ||
| 132 | |||
| 133 | my $rendermode = '[ ' . join(' | ', @buttons) . ' ]'; | ||
| 134 | |||
| 135 | return << " HEADER"; | ||
| 136 | <div id="halo-$id" class="halo"> | ||
| 137 | <div class="halo-header"> | ||
| 138 | <span id="halo-rendermode-$id" class="halo-rendermode"> | ||
| 139 | $rendermode | ||
| 140 | </span> | ||
| 141 | <div class="halo-name">$name</div> | ||
| 142 | </div> | ||
| 143 | <div id="halo-inner-$id"> | ||
| 144 | <div id="halo-rendered-$id"> | ||
| 145 | HEADER | ||
| 146 | } | ||
| 147 | |||
| 148 | =head2 halo_footer frame -> string | ||
| 149 | |||
| 150 | This will give you the halo footer which includes the actual information to | ||
| 151 | be displayed when the user pokes the halo. | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub halo_footer { | ||
| 156 | my $self = shift; | ||
| 157 | my $frame = shift; | ||
| 158 | my $id = $frame->{id}; | ||
| 159 | my $displays = $frame->{displays}; | ||
| 160 | |||
| 161 | my @divs; | ||
| 162 | for (sort keys %$displays) { | ||
| 163 | my $d = $displays->{$_}; | ||
| 164 | my $name = Jifty->web->escape($d->{name}); | ||
| 165 | |||
| 166 | if ($d->{callback}) { | ||
| 167 | my $output = | ||
| 168 | qq{<div id="halo-info-$name-$id" style="display: none">}; | ||
| 169 | |||
| 170 | if (defined(my $info = $d->{callback}->($frame))) { | ||
| 171 | $output .= $info; | ||
| 172 | } | ||
| 173 | else { | ||
| 174 | # downgrade the link to plaintext so it's obvious there's no | ||
| 175 | # information available | ||
| 176 | $output .= qq{<script type="text/javascript">remove_link('$id', '$name');</script>}; | ||
| 177 | } | ||
| 178 | |||
| 179 | $output .= "</div>"; | ||
| 180 | push @divs, $output; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | my $divs = join "\n", @divs; | ||
| 185 | |||
| 186 | return << " FOOTER"; | ||
| 187 | </div> | ||
| 188 | <div id="halo-info-$id"> | ||
| 189 | $divs | ||
| 190 | </div> | ||
| 191 | </div> | ||
| 192 | </div> | ||
| 193 | FOOTER | ||
| 194 | } | ||
| 195 | |||
| 196 | =head2 new_frame -> hashref | ||
| 197 | |||
| 198 | Gives you a new frame for storing halo information. | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | sub new_frame { | ||
| 203 | my $self = shift; | ||
| 204 | |||
| 205 | my $args = { | ||
| 206 | name => "arguments", | ||
| 207 | callback => sub { | ||
| 208 | my $frame = shift; | ||
| 209 | my @out; | ||
| 210 | |||
| 211 | my @args; | ||
| 212 | while (my ($key, $value) = splice(@{$frame->{args}},0,2)) { | ||
| 213 | push @args, [$key, $value]; | ||
| 214 | } | ||
| 215 | |||
| 216 | for (sort { $a->[0] cmp $b->[0] } @args) { | ||
| 217 | my ($name, $value) = @$_; | ||
| 218 | my $ref = ref($value); | ||
| 219 | my $out = qq{<b>$name</b>: }; | ||
| 220 | |||
| 221 | if ($ref) { | ||
| 222 | my $expanded = Jifty->web->serial; | ||
| 223 | my $yaml = | ||
| 224 | eval { defined $value && fileno($value) } | ||
| 225 | ? '*GLOB*' : Jifty->web->escape( Jifty::YAML::Dump($value) ); | ||
| 226 | |||
| 227 | $out .= qq{<a href="#" onclick="jQuery(Jifty.\$('$expanded')).toggle(); return false">$ref</a><div id="$expanded" class="halo-argument" style="display: none"><pre>$yaml</pre></div>}; | ||
| 228 | } | ||
| 229 | elsif (defined $value) { | ||
| 230 | $out .= Jifty->web->escape($value); | ||
| 231 | } | ||
| 232 | else { | ||
| 233 | $out .= "undef"; | ||
| 234 | } | ||
| 235 | |||
| 236 | push @out, $out; | ||
| 237 | } | ||
| 238 | |||
| 239 | return undef if @out == 0; | ||
| 240 | |||
| 241 | return "<ul>" | ||
| 242 | . join("\n", | ||
| 243 | map { "<li>$_</li>" } | ||
| 244 | @out) | ||
| 245 | . "</ul>"; | ||
| 246 | }, | ||
| 247 | }; | ||
| 248 | |||
| 249 | return { | ||
| 250 | id => Jifty->web->serial, | ||
| 251 | start_time => time, | ||
| 252 | subcomponent => 0, | ||
| 253 | displays => { | ||
| 254 | R => { name => "render", default => 1 }, | ||
| 255 | S => { name => "source" }, | ||
| 256 | A => $args, | ||
| 257 | }, | ||
| 258 | @_, | ||
| 259 | }; | ||
| 260 | } | ||
| 261 | |||
| 262 | =head2 push_frame -> frame | ||
| 263 | |||
| 264 | Creates and pushes a frame onto the render stack. Mason's halos do not support | ||
| 265 | I<arounding> a component, so we fake it with an explicit stack. | ||
| 266 | |||
| 267 | This also triggers C<halo_pre_template> for plugins adding halo data. | ||
| 268 | |||
| 269 | =cut | ||
| 270 | |||
| 271 | sub push_frame { | ||
| 272 | my $self = shift; | ||
| 273 | |||
| 274 | my $STACK = Jifty->handler->stash->{'_halo_stack'} ||= []; | ||
| 275 | my $DEPTH = ++Jifty->handler->stash->{'_halo_depth'}; | ||
| 276 | my $INDEX_STACK = Jifty->handler->stash->{'_halo_index_stack'} ||= []; | ||
| 277 | |||
| 278 | # if this is the first frame, discard anything from the previous queries | ||
| 279 | my $previous = $STACK->[-1] || {}; | ||
| 280 | |||
| 281 | my $frame = $self->new_frame(@_, previous => $previous, depth => $DEPTH); | ||
| 282 | |||
| 283 | push @$STACK, $frame; | ||
| 284 | push @$INDEX_STACK, $#$STACK; | ||
| 285 | |||
| 286 | $self->call_trigger('halo_pre_template', frame => $frame, previous => $previous); | ||
| 287 | |||
| 288 | return $frame; | ||
| 289 | } | ||
| 290 | |||
| 291 | =head2 pop_frame -> frame | ||
| 292 | |||
| 293 | Pops a frame off the render stack. Mason's halos do not support | ||
| 294 | C<arounding> a component, so we fake it with an explicit stack. | ||
| 295 | |||
| 296 | This also triggers C<halo_post_template> for plugins adding halo data. | ||
| 297 | |||
| 298 | =cut | ||
| 299 | |||
| 300 | sub pop_frame { | ||
| 301 | my $self = shift; | ||
| 302 | |||
| 303 | my $STACK = Jifty->handler->stash->{'_halo_stack'} ||= []; | ||
| 304 | my $INDEX_STACK = Jifty->handler->stash->{'_halo_index_stack'} ||= []; | ||
| 305 | my $FRAME_ID = pop @$INDEX_STACK; | ||
| 306 | |||
| 307 | my $frame = $STACK->[$FRAME_ID]; | ||
| 308 | my $previous = $frame->{previous}; | ||
| 309 | |||
| 310 | $frame->{'end_time'} = time; | ||
| 311 | |||
| 312 | $self->call_trigger('halo_post_template', frame => $frame, previous => $previous); | ||
| 313 | |||
| 314 | --Jifty->handler->stash->{'_halo_depth'}; | ||
| 315 | |||
| 316 | return $frame; | ||
| 317 | } | ||
| 318 | |||
| 319 | =head2 body_start | ||
| 320 | |||
| 321 | Called once the C<< <body> >> element has been opened; enables halos on | ||
| 322 | templates. | ||
| 323 | |||
| 324 | =cut | ||
| 325 | |||
| 326 | sub body_start { | ||
| 327 | my $self = shift; | ||
| 328 | Jifty->handler->stash->{'in_body'} = 1; | ||
| 329 | } | ||
| 330 | |||
| 331 | =head2 body_end | ||
| 332 | |||
| 333 | Called just before the C<< <body> >> element is closed; renders the halo | ||
| 334 | component tree using L</render_component_tree>. | ||
| 335 | |||
| 336 | =cut | ||
| 337 | |||
| 338 | sub body_end { | ||
| 339 | my $self = shift; | ||
| 340 | Jifty->handler->stash->{'in_body'} = 0; | ||
| 341 | $self->render_component_tree; | ||
| 342 | } | ||
| 343 | |||
| 344 | =head2 render_component_tree | ||
| 345 | |||
| 346 | Once we're just about to finish rendering our HTML page (just before | ||
| 347 | the C<</body>> tag, we should call render_component_tree to output all | ||
| 348 | the halo data and metadata. | ||
| 349 | |||
| 350 | =cut | ||
| 351 | |||
| 352 | sub render_component_tree { | ||
| 353 | my $self = shift; | ||
| 354 | my @stack = @{ Jifty->handler->stash->{'_halo_stack'} }; | ||
| 355 | |||
| 356 | for (@stack) { | ||
| 357 | $_->{'render_time'} = int((($_->{'end_time'}||time) - $_->{'start_time'}) * 1000)/1000 | ||
| 358 | unless defined $_->{'render_time'}; | ||
| 359 | } | ||
| 360 | Jifty->web->render_template("/__jifty/halo", {stack => \@stack} ); | ||
| 361 | return 1; | ||
| 362 | } | ||
| 363 | |||
| 364 | # Back-compat with old Mason "/_elements/wrapper" files | ||
| 365 | package Jifty::View::Mason::Halo; | ||
| 366 | *render_component_tree = \&Jifty::Plugin::Halo::render_component_tree; | ||
| 367 | |||
| 368 | 1; |