line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::BootstrapAlerts; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Bootstrap alerts for your web app |
4
|
|
|
|
|
|
|
|
5
|
12
|
|
|
12
|
|
8752
|
use strict; |
|
12
|
|
|
|
|
26
|
|
|
12
|
|
|
|
|
335
|
|
6
|
12
|
|
|
12
|
|
58
|
use warnings; |
|
12
|
|
|
|
|
36
|
|
|
12
|
|
|
|
|
305
|
|
7
|
|
|
|
|
|
|
|
8
|
12
|
|
|
12
|
|
54
|
use parent 'Mojolicious::Plugin'; |
|
12
|
|
|
|
|
21
|
|
|
12
|
|
|
|
|
70
|
|
9
|
|
|
|
|
|
|
|
10
|
12
|
|
|
12
|
|
756
|
use Mojo::ByteStream; |
|
12
|
|
|
|
|
22
|
|
|
12
|
|
|
|
|
7451
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = 0.07; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub register { |
15
|
12
|
|
|
12
|
1
|
7692
|
my ($self, $app, $config) = @_; |
16
|
|
|
|
|
|
|
|
17
|
12
|
|
100
|
|
|
60
|
my $dismissable = !(exists $config->{dismissable} and $config->{dismissable} == 0 ); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$app->helper( notify => sub { |
20
|
34
|
|
|
34
|
|
174084
|
my $c = shift; |
21
|
34
|
|
|
|
|
60
|
push @{ $c->stash->{__NOTIFICATIONS__} }, [ @_ ]; |
|
34
|
|
|
|
|
94
|
|
22
|
12
|
|
|
|
|
109
|
} ); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
$app->helper( notifications => sub { |
25
|
26
|
|
|
26
|
|
49440
|
my $c = shift; |
26
|
|
|
|
|
|
|
|
27
|
26
|
|
|
|
|
55
|
my $output = ''; |
28
|
|
|
|
|
|
|
|
29
|
26
|
100
|
|
|
|
36
|
for my $notification ( @{ $c->stash('__NOTIFICATIONS__') || [] } ) { |
|
26
|
|
|
|
|
94
|
|
30
|
30
|
|
|
|
|
275
|
my ($type, $message, $config) = @{ $notification }; |
|
30
|
|
|
|
|
70
|
|
31
|
|
|
|
|
|
|
|
32
|
30
|
100
|
|
|
|
90
|
if ( 'HASH' ne ref $config ) { |
33
|
22
|
|
|
|
|
39
|
$config = {}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
30
|
|
|
|
|
72
|
my ($dismissable_class, $dismissable_button) = ("",""); |
37
|
30
|
100
|
100
|
|
|
203
|
if ( $config->{dismissable} || (!exists $config->{dismissable} && $dismissable) ) { |
|
|
|
100
|
|
|
|
|
38
|
25
|
|
|
|
|
112
|
$dismissable_class = 'alert-dismissable'; |
39
|
25
|
|
|
|
|
43
|
$dismissable_button = ''; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
30
|
100
|
100
|
|
|
125
|
if ( ref $message and ref $message eq 'ARRAY' ) { |
43
|
7
|
|
|
|
|
31
|
my $items = join '', map{ "$_" }@{ $message }; |
|
14
|
|
|
|
|
55
|
|
|
7
|
|
|
|
|
23
|
|
44
|
7
|
100
|
|
|
|
32
|
my $list_type = $config->{ordered_list} ? 'ol' : 'ul'; |
45
|
|
|
|
|
|
|
|
46
|
7
|
|
|
|
|
39
|
$message = "<$list_type>$items$list_type>"; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
30
|
100
|
|
|
|
84
|
$type = 'danger' if $type eq 'error'; |
50
|
|
|
|
|
|
|
|
51
|
30
|
|
|
|
|
192
|
$output .= qq~ |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$dismissable_button |
54
|
|
|
|
|
|
|
$message |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
~; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
26
|
|
|
|
|
245
|
return Mojo::ByteStream->new( $output ); |
60
|
12
|
|
|
|
|
1877
|
} ); |
61
|
|
|
|
|
|
|
|
62
|
12
|
|
100
|
|
|
1053
|
my $selector = $config->{before} || $config->{after}; |
63
|
12
|
100
|
100
|
|
|
73
|
if ( $config->{auto_inject} && $selector ) { |
64
|
|
|
|
|
|
|
$app->hook( after_render => sub { |
65
|
8
|
|
|
8
|
|
19924
|
my ($c, $content, $format) = @_; |
66
|
|
|
|
|
|
|
|
67
|
8
|
100
|
|
|
|
30
|
return if $format ne 'html'; |
68
|
|
|
|
|
|
|
|
69
|
7
|
|
|
|
|
42
|
my $notifications = $c->notifications->to_string; |
70
|
|
|
|
|
|
|
|
71
|
7
|
100
|
|
|
|
196
|
return if !$notifications; |
72
|
|
|
|
|
|
|
|
73
|
6
|
|
|
|
|
10
|
my $dom = Mojo::DOM->new( ${$content} ); |
|
6
|
|
|
|
|
38
|
|
74
|
6
|
|
|
|
|
1705
|
my $element = $dom->find( $selector )->first; |
75
|
|
|
|
|
|
|
|
76
|
6
|
100
|
|
|
|
2082
|
if ( !$element ) { |
77
|
2
|
|
|
|
|
5
|
$c->app->log->debug( 'no matching element found (' . $selector . ')' ); |
78
|
2
|
|
|
|
|
39
|
return; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
4
|
100
|
|
|
|
35
|
my @elems = $config->{before} ? ($notifications, "$element") : ("$element", $notifications); |
82
|
4
|
|
|
|
|
282
|
my $replacement = sprintf "%s%s", @elems; |
83
|
4
|
|
|
|
|
11
|
my $temp = "$element"; |
84
|
|
|
|
|
|
|
|
85
|
4
|
|
|
|
|
170
|
${$content} =~ s/\Q$temp\E/$replacement/; |
|
4
|
|
|
|
|
66
|
|
86
|
3
|
|
|
|
|
22
|
}); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
1; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
__END__ |