line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::TagHelpersI18N; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: TagHelpers with I18N support |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
1135
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
84
|
|
6
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
86
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
18
|
use Mojolicious::Plugin::TagHelpers; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
32
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = 0.03; |
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
67
|
use Mojo::Collection; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
96
|
|
13
|
2
|
|
|
2
|
|
12
|
use Mojo::Util qw(deprecated xml_escape); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
94
|
|
14
|
2
|
|
|
2
|
|
8
|
use Scalar::Util 'blessed'; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
88
|
|
15
|
2
|
|
|
2
|
|
1320
|
use Unicode::Collate; |
|
2
|
|
|
|
|
13994
|
|
|
2
|
|
|
|
|
89
|
|
16
|
|
|
|
|
|
|
|
17
|
2
|
|
|
2
|
|
19
|
use Data::Dumper; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
142
|
|
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
2
|
|
1075
|
use parent 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
564
|
|
|
2
|
|
|
|
|
10
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub register { |
22
|
2
|
|
|
2
|
1
|
17789
|
my ($self, $app, $config) = @_; |
23
|
|
|
|
|
|
|
|
24
|
2
|
|
|
|
|
18
|
my $collate = Unicode::Collate->new; |
25
|
|
|
|
|
|
|
$app->helper( select_field => sub { |
26
|
6
|
|
|
6
|
|
175874
|
_select_field(@_, collate => $collate ) |
27
|
2
|
|
|
|
|
58701
|
} ); |
28
|
|
|
|
|
|
|
|
29
|
2
|
|
50
|
|
|
462
|
$config ||= {}; |
30
|
2
|
|
50
|
|
|
30
|
$app->attr( |
31
|
|
|
|
|
|
|
translation_method => $config->{method} || 'l' |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _prepare { |
36
|
6
|
|
|
6
|
|
18
|
my ($self, $options, %attr) = @_; |
37
|
|
|
|
|
|
|
|
38
|
6
|
|
|
|
|
10
|
my %translated; |
39
|
6
|
|
|
|
|
9
|
my $index = 0; |
40
|
6
|
|
|
|
|
9
|
for my $option ( @{$options} ) { |
|
6
|
|
|
|
|
16
|
|
41
|
12
|
50
|
|
|
|
29
|
if (ref $option eq 'HASH') { |
42
|
0
|
|
|
|
|
0
|
deprecated |
43
|
|
|
|
|
|
|
'hash references are DEPRECATED in favor of Mojo::Collection objects'; |
44
|
0
|
|
|
|
|
0
|
$option = Mojo::Collection->new(each %$option); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
12
|
50
|
|
|
|
47
|
$option = [$option => $option] unless ref $option eq 'ARRAY'; |
48
|
|
|
|
|
|
|
|
49
|
12
|
100
|
|
|
|
28
|
if ( !$attr{no_translation} ) { |
50
|
8
|
|
|
|
|
179
|
my $method = $self->app->translation_method; |
51
|
8
|
|
|
|
|
258
|
$option->[0] = $self->$method( $option->[0] ); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
12
|
|
|
|
|
1226
|
push @{ $translated{ $option->[0] } }, $index; |
|
12
|
|
|
|
|
36
|
|
55
|
|
|
|
|
|
|
|
56
|
12
|
|
|
|
|
20
|
$index++; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
6
|
100
|
|
|
|
20
|
if ( $attr{sort} ) { |
60
|
3
|
|
|
|
|
19
|
my @sorted = $attr{collate}->sort( keys %translated ); |
61
|
3
|
|
|
|
|
1065
|
my @sorted_options = map{ @{$options}[ @{ $translated{$_} } ] }@sorted; |
|
6
|
|
|
|
|
26
|
|
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
11
|
|
62
|
|
|
|
|
|
|
|
63
|
3
|
|
|
|
|
16
|
return \@sorted_options; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
3
|
|
|
|
|
14
|
return $options; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _select_field { |
70
|
6
|
|
|
6
|
|
37
|
my ($self, $name, $options, %attrs) = (shift, shift, shift, @_); |
71
|
|
|
|
|
|
|
|
72
|
6
|
50
|
|
|
|
11
|
my %values = map { $_ => 1 } @{ $self->every_param($name) || [] }; |
|
0
|
|
|
|
|
0
|
|
|
6
|
|
|
|
|
32
|
|
73
|
|
|
|
|
|
|
|
74
|
6
|
|
|
|
|
1545
|
my %opts; |
75
|
6
|
|
|
|
|
31
|
my @fields = qw(no_translation sort collate); |
76
|
6
|
|
|
|
|
34
|
@opts{@fields} = delete @attrs{@fields}; |
77
|
|
|
|
|
|
|
|
78
|
6
|
|
|
|
|
30
|
$options = _prepare( $self, $options, %opts ); |
79
|
|
|
|
|
|
|
|
80
|
6
|
|
|
|
|
16
|
my $groups = ''; |
81
|
6
|
|
|
|
|
15
|
for my $group (@$options) { |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# DEPRECATED in Top Hat! |
84
|
12
|
50
|
|
|
|
759
|
if (ref $group eq 'HASH') { |
85
|
0
|
|
|
|
|
0
|
deprecated |
86
|
|
|
|
|
|
|
'hash references are DEPRECATED in favor of Mojo::Collection objects'; |
87
|
0
|
|
|
|
|
0
|
$group = Mojo::Collection->new(each %$group); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# "optgroup" tag |
91
|
12
|
50
|
33
|
|
|
59
|
if (blessed $group && $group->isa('Mojo::Collection')) { |
92
|
0
|
|
|
|
|
0
|
my ($label, $values) = splice @$group, 0, 2; |
93
|
0
|
|
|
|
|
0
|
my $prepared = _prepare( $self, $values, %opts ); |
94
|
0
|
|
|
|
|
0
|
my $content = join '', map { Mojolicious::Plugin::TagHelpers::_option(\%values, $_) } @$prepared; |
|
0
|
|
|
|
|
0
|
|
95
|
0
|
|
|
0
|
|
0
|
$groups .= Mojolicious::Plugin::TagHelpers::_tag('optgroup', label => $label, @$group, sub {$content}); |
|
0
|
|
|
|
|
0
|
|
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# "option" tag |
99
|
12
|
|
|
|
|
47
|
else { $groups .= Mojolicious::Plugin::TagHelpers::_option(\%values, $group) } |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
6
|
|
|
6
|
|
3266
|
return Mojolicious::Plugin::TagHelpers::_validation( |
103
|
|
|
|
|
|
|
$self, $name, 'select', %attrs, name => $name, sub {$groups}, |
104
|
6
|
|
|
|
|
311
|
); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
__END__ |