| 1 | package Dancer::Plugin::Ajax; | ||||||||||||
| 2 | 3 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||
| 3 | #ABSTRACT: a plugin for adding Ajax route handlers | ||||||||||||
| 4 | 3 | $Dancer::Plugin::Ajax::VERSION = '1.3520'; | |||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 3 | ▶ | use Dancer ':syntax'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 3 | ▶ | use Dancer::Exception ':all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 3 | ▶ | use Dancer::Plugin; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 3 | register 'ajax' => \&ajax; | |||||||||||
| 13 | |||||||||||||
| 14 | hook before => sub { | ||||||||||||
| 15 | 0 | ▶ | if (request->is_ajax) { | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
| |||||||||||||
| 16 | 0 | ▶ | content_type( plugin_setting->{content_type} || 'text/xml' ); | ||||||||||
|
Condition0%0/2
plugin_setting()->{'content_type'} || 'text/xml'
MC/DC0%0/1
plugin_setting()->{'content_type'} || 'text/xml'
plugin_setting()->{'content_type'}
Truth table0%0/2
A
A= plugin_setting()->{'content_type'}
| |||||||||||||
| 17 | } | ||||||||||||
| 18 | 3 | }; | |||||||||||
| 19 | |||||||||||||
| 20 | sub ajax { | ||||||||||||
| 21 | 1 | ▶ | my ($pattern, @rest) = @_; | ||||||||||
|
Subroutine
ajax: called CC=5 SCAR=23.4
| |||||||||||||
| 22 | |||||||||||||
| 23 | 1 | my $code; | |||||||||||
| 24 | 1 | ▶ | for my $e (@rest) { $code = $e if (ref($e) eq 'CODE') } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | |||||||||||||
| 26 | my $ajax_route = sub { | ||||||||||||
| 27 | # must be an XMLHttpRequest | ||||||||||||
| 28 | 0 | ▶ | if (not request->is_ajax) { | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
| |||||||||||||
| 29 | 0 | ▶ | pass and return 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | } | ||||||||||||
| 31 | |||||||||||||
| 32 | # disable layout | ||||||||||||
| 33 | 0 | my $layout = setting('layout'); | |||||||||||
| 34 | 0 | setting('layout' => undef); | |||||||||||
| 35 | my $response = try { | ||||||||||||
| 36 | 0 | ▶ | $code->(); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 37 | } catch { | ||||||||||||
| 38 | 0 | ▶ | my $e = $_; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 39 | 0 | setting('layout' => $layout); | |||||||||||
| 40 | 0 | die $e; | |||||||||||
| 41 | 0 | }; | |||||||||||
| 42 | 0 | setting('layout' => $layout); | |||||||||||
| 43 | 0 | return $response; | |||||||||||
| 44 | 1 | }; | |||||||||||
| 45 | |||||||||||||
| 46 | # rebuild the @rest array with the compiled route handler | ||||||||||||
| 47 | 1 | my @compiled_rest; | |||||||||||
| 48 | 1 | for my $e (@rest) { | |||||||||||
| 49 | 1 | ▶ | if (ref($e) eq 'CODE') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | 1 | push @compiled_rest, {ajax => 1}, $ajax_route; | |||||||||||
| 51 | } | ||||||||||||
| 52 | else { | ||||||||||||
| 53 | 0 | push @compiled_rest, {ajax => 1}, $e; | |||||||||||
| 54 | } | ||||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | 1 | any ['get', 'post'] => $pattern, @compiled_rest; | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | 3 | register_plugin; | |||||||||||
| 61 | 3 | 1; | |||||||||||
| 62 | |||||||||||||
| 63 | __END__ | ||||||||||||