line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Notifications::Alertify; |
2
|
3
|
|
|
3
|
|
2702
|
use Mojo::Base 'Mojolicious::Plugin::Notifications::Engine'; |
|
3
|
|
|
|
|
17
|
|
|
3
|
|
|
|
|
23
|
|
3
|
3
|
|
|
3
|
|
1221
|
use Mojolicious::Plugin::Notifications::HTML qw/notify_html/; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
184
|
|
4
|
3
|
|
|
3
|
|
20
|
use Exporter 'import'; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
105
|
|
5
|
3
|
|
|
3
|
|
18
|
use Mojo::ByteStream 'b'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
142
|
|
6
|
3
|
|
|
3
|
|
19
|
use Mojo::Util qw/xml_escape quote/; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
156
|
|
7
|
3
|
|
|
3
|
|
19
|
use Mojo::JSON qw/decode_json encode_json/; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
137
|
|
8
|
3
|
|
|
3
|
|
18
|
use Scalar::Util qw/blessed/; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
125
|
|
9
|
3
|
|
|
3
|
|
20
|
use File::Spec; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
77
|
|
10
|
3
|
|
|
3
|
|
17
|
use File::Basename; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
339
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT_OK = ('notify_alertify'); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has [qw/base_class base_timeout/]; |
15
|
|
|
|
|
|
|
state $path = '/alertify/'; |
16
|
|
|
|
|
|
|
|
17
|
3
|
|
|
3
|
|
22
|
use constant DEFAULT_TIMEOUT => 5000; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
3319
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# Register plugin |
20
|
|
|
|
|
|
|
sub register { |
21
|
3
|
|
|
3
|
1
|
10
|
my ($plugin, $app, $param) = @_; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Set config |
24
|
3
|
|
100
|
|
|
22
|
$plugin->base_class( $param->{base_class} // 'default' ); |
25
|
3
|
|
50
|
|
|
85
|
$plugin->base_timeout( $param->{base_timeout} // DEFAULT_TIMEOUT ); |
26
|
|
|
|
|
|
|
|
27
|
3
|
|
|
|
|
38
|
$plugin->scripts($path . 'alertify.min.js'); |
28
|
3
|
|
|
|
|
20
|
$plugin->styles( |
29
|
|
|
|
|
|
|
$path . 'alertify.core.css', |
30
|
|
|
|
|
|
|
$path . 'alertify.' . $plugin->base_class . '.css' |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Add static path to JavaScript |
34
|
3
|
|
|
|
|
14
|
push @{$app->static->paths}, |
|
3
|
|
|
|
|
21
|
|
35
|
|
|
|
|
|
|
File::Spec->catdir( File::Basename::dirname(__FILE__), 'Alertify' ); |
36
|
|
|
|
|
|
|
}; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# Exportable function |
40
|
|
|
|
|
|
|
sub notify_alertify { |
41
|
17
|
100
|
66
|
17
|
1
|
686
|
my $c = shift if blessed $_[0] && $_[0]->isa('Mojolicious::Controller'); |
42
|
17
|
|
|
|
|
42
|
my $type = shift; |
43
|
17
|
|
|
|
|
28
|
my $param = shift; |
44
|
17
|
|
|
|
|
34
|
my $msg = pop; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
state $ajax = sub { |
47
|
8
|
|
|
8
|
|
678
|
return 'r.open("POST",' . quote($_[0]) . ');v=true'; |
48
|
17
|
|
|
|
|
37
|
}; |
49
|
|
|
|
|
|
|
|
50
|
17
|
|
|
|
|
35
|
my $js = ''; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Confirmation |
53
|
17
|
100
|
100
|
|
|
69
|
if ($param->{ok} || $param->{cancel}) { |
54
|
|
|
|
|
|
|
|
55
|
5
|
100
|
|
|
|
61
|
$js .= 'var x=' . quote($c->csrf_token) . ';' if $c; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# Set labels |
58
|
5
|
100
|
66
|
|
|
175
|
if ($param->{ok_label} || $param->{cancel_label}) { |
59
|
1
|
|
|
|
|
5
|
$js .= 'alertify.set({labels:{'; |
60
|
1
|
|
50
|
|
|
9
|
$js .= 'ok:'.quote($param->{ok_label} // 'OK') . ',' ; |
61
|
1
|
|
50
|
|
|
15
|
$js .= 'cancel:'.quote($param->{cancel_label} // 'Cancel'); |
62
|
1
|
|
|
|
|
9
|
$js .= "}});\n"; |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Create confirmation |
66
|
5
|
|
|
|
|
21
|
$js .= 'alertify.confirm(' . quote($msg); |
67
|
5
|
|
|
|
|
50
|
$js .= ',function(ok){'; |
68
|
5
|
|
|
|
|
16
|
$js .= 'var r=new XMLHttpRequest();var v;'; |
69
|
|
|
|
|
|
|
|
70
|
5
|
100
|
100
|
|
|
27
|
if ($param->{ok} && $param->{cancel}) { |
|
|
100
|
|
|
|
|
|
71
|
|
|
|
|
|
|
$js .= 'if(ok){'. $ajax->($param->{ok}) . |
72
|
3
|
|
|
|
|
34
|
'}else{' . $ajax->($param->{cancel}) . '};'; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
elsif ($param->{ok}) { |
75
|
1
|
|
|
|
|
17
|
$js .= 'if(ok){' . $ajax->($param->{ok}) . '};'; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
else { |
78
|
1
|
|
|
|
|
5
|
$js .= 'if(!ok){' . $ajax->($param->{cancel}) . '};'; |
79
|
|
|
|
|
|
|
}; |
80
|
5
|
|
|
|
|
1213
|
$js .= 'if(v){'; |
81
|
5
|
|
|
|
|
30
|
$js .= 'r.setRequestHeader("Content-type","application/x-www-form-urlencoded");'; |
82
|
5
|
100
|
|
|
|
30
|
$js .= 'r.send("csrf_token="+x);' if $c; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Alert if callback fails to respond |
85
|
5
|
|
|
|
|
18
|
$js .= 'r.onreadystatechange=function(){' . |
86
|
|
|
|
|
|
|
'if(this.readyState==4&&this.status!==200){' . |
87
|
|
|
|
|
|
|
'alertify.log(this.status?this.status+": "+this.statusText:"Connection Error",'. |
88
|
|
|
|
|
|
|
'"error")}}'; |
89
|
|
|
|
|
|
|
|
90
|
5
|
|
|
|
|
20
|
$js .= '}},' . quote('notify notify-' . $type) . ");\n"; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Normal alert |
94
|
|
|
|
|
|
|
else { |
95
|
12
|
|
|
|
|
35
|
$js .= 'alertify.log(' . quote($msg); |
96
|
12
|
|
|
|
|
109
|
$js .= ',' . quote($type) . ','; |
97
|
12
|
|
|
|
|
83
|
$js .= $param->{timeout}; |
98
|
12
|
|
|
|
|
23
|
$js .= ");\n"; |
99
|
|
|
|
|
|
|
}; |
100
|
17
|
|
|
|
|
83
|
return $js; |
101
|
|
|
|
|
|
|
}; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# Notification method |
105
|
|
|
|
|
|
|
sub notifications { |
106
|
9
|
|
|
9
|
1
|
34
|
my ($self, $c, $notify_array, $rule, @post) = @_; |
107
|
|
|
|
|
|
|
|
108
|
9
|
100
|
|
|
|
29
|
return unless $notify_array->size; |
109
|
|
|
|
|
|
|
|
110
|
8
|
|
66
|
|
|
77
|
my $theme = shift @post // $self->base_class; |
111
|
|
|
|
|
|
|
|
112
|
8
|
|
|
|
|
64
|
my $js = ''; |
113
|
8
|
100
|
|
|
|
28
|
unless ($rule->{no_include}) { |
114
|
7
|
|
|
|
|
46
|
$js .= $c->javascript( $self->scripts ); |
115
|
|
|
|
|
|
|
|
116
|
7
|
50
|
|
|
|
6206
|
unless ($rule->{no_css}) { |
117
|
7
|
|
|
|
|
36
|
$js .= $c->stylesheet( ($self->styles)[0] ); |
118
|
7
|
|
|
|
|
4974
|
$js .= $c->stylesheet( $path . 'alertify.' . $theme . '.css'); |
119
|
|
|
|
|
|
|
}; |
120
|
|
|
|
|
|
|
}; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# Start JavaScript snippet |
123
|
8
|
|
|
|
|
4851
|
$js .= qq{\n" . $noscript . ''); |
147
|
|
|
|
|
|
|
}; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
1; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
__END__ |