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