line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer2::Plugin::MobileDevice; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
3242797
|
use strict; |
|
5
|
|
|
|
|
38
|
|
|
5
|
|
|
|
|
155
|
|
4
|
5
|
|
|
5
|
|
26
|
use warnings; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
232
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "0.000001"; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Dancer2::Plugin::MobileDevice - Make a Dancer2 app mobile-aware |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=cut |
13
|
|
|
|
|
|
|
|
14
|
5
|
|
|
5
|
|
2825
|
use Dancer2::Plugin; |
|
5
|
|
|
|
|
264463
|
|
|
5
|
|
|
|
|
52
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
plugin_keywords qw(is_mobile_device); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub is_mobile_device { |
19
|
39
|
|
|
39
|
1
|
944
|
my $self = shift; |
20
|
39
|
|
100
|
|
|
159
|
return (($self->app->request->user_agent || '') |
21
|
|
|
|
|
|
|
=~ /(?:iP(?:ad|od|hone)|Android|BlackBerry|Mobile|Palm)/) || 0 ; |
22
|
|
|
|
|
|
|
} #is_mobile_device |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub BUILD { |
25
|
4
|
|
|
4
|
1
|
8873
|
my $plugin = shift; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# Change the layout setting. Save the old to reset the setting later. |
28
|
|
|
|
|
|
|
# We check config->{mobile_layout} within the hook rather than outside |
29
|
|
|
|
|
|
|
# so that the plugin will respond correctly if mobile_layout is |
30
|
|
|
|
|
|
|
# changed between requests. |
31
|
|
|
|
|
|
|
$plugin->app->add_hook( |
32
|
|
|
|
|
|
|
Dancer2::Core::Hook->new( |
33
|
|
|
|
|
|
|
name => 'before', |
34
|
|
|
|
|
|
|
code => sub { |
35
|
17
|
100
|
|
17
|
|
265205
|
return unless $plugin->config->{mobile_layout}; |
36
|
4
|
100
|
|
|
|
99
|
return unless $plugin->is_mobile_device; |
37
|
2
|
|
|
|
|
537
|
my $mobile_layout = $plugin->config->{mobile_layout}; |
38
|
2
|
|
|
|
|
28
|
$plugin->app->log(debug => "Using mobile layout $mobile_layout"); |
39
|
|
|
|
|
|
|
|
40
|
2
|
|
|
|
|
1215
|
$plugin->app->request->var(orig_layout => $plugin->app->setting('layout')); |
41
|
2
|
|
|
|
|
190
|
$plugin->app->setting(layout => $mobile_layout); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
) |
44
|
4
|
|
|
|
|
123
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# After a request, reset the layout for the benefit of future requests. |
47
|
|
|
|
|
|
|
# Don't check the state of the request just in case the mobile_layout |
48
|
|
|
|
|
|
|
# has changed between the beginning and the end of the request (although |
49
|
|
|
|
|
|
|
# I'm not sure how that could happen!). |
50
|
|
|
|
|
|
|
$plugin->app->add_hook( |
51
|
|
|
|
|
|
|
Dancer2::Core::Hook->new( |
52
|
|
|
|
|
|
|
name => 'after', |
53
|
|
|
|
|
|
|
code => sub { |
54
|
17
|
|
|
17
|
|
36827
|
my $vars = $plugin->app->request->vars; |
55
|
17
|
100
|
|
|
|
110
|
return unless exists $vars->{orig_layout}; |
56
|
|
|
|
|
|
|
# Can't check truthiness, since undef is a valid |
57
|
|
|
|
|
|
|
# orig_layout value. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
$plugin->app->log(debug => "resetting layout to " . |
60
|
2
|
|
100
|
|
|
16
|
($vars->{orig_layout} || '')); |
61
|
2
|
|
|
|
|
1204
|
$plugin->app->setting(layout => $vars->{orig_layout}); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
) |
64
|
4
|
|
|
|
|
1898
|
); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# Make variable 'is_mobile_device' available in templates |
67
|
|
|
|
|
|
|
$plugin->app->add_hook( |
68
|
|
|
|
|
|
|
Dancer2::Core::Hook->new( |
69
|
|
|
|
|
|
|
name => 'before_template_render', |
70
|
|
|
|
|
|
|
code => sub { |
71
|
9
|
|
|
9
|
|
6910
|
my $tokens = shift; |
72
|
9
|
|
|
|
|
58
|
$plugin->app->request->var(is_mobile_device => $plugin->is_mobile_device); |
73
|
9
|
|
|
|
|
323
|
$tokens->{'is_mobile_device'} = $plugin->is_mobile_device; |
74
|
|
|
|
|
|
|
|
75
|
9
|
100
|
|
|
|
163
|
$plugin->app->log(debug => 'Requester ' . |
76
|
|
|
|
|
|
|
($plugin->is_mobile_device ? 'is' : 'is not') . |
77
|
|
|
|
|
|
|
' mobile device'); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
) |
80
|
4
|
|
|
|
|
1608
|
); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
} #BUILD |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
__END__ |