| 1 | package HTML::Shakan::Widgets::Simple; | ||||||||||||||||||||||||||||
| 2 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use HTML::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 22 | ▶ | use List::MoreUtils qw/zip/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | sub render { | ||||||||||||||||||||||||||||
| 8 | 28 | ▶ | my ($self, $form, $field) = @_; | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 28 | my $type = $field->widget; | |||||||||||||||||||||||||||
| 11 | 28 | ▶ | my $code = $self->can("widget_${type}") or die "unknown widget type: $type"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 12 | 28 | $code->( | |||||||||||||||||||||||||||
| 13 | $self, $form, $field | ||||||||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub _attr { | ||||||||||||||||||||||||||||
| 18 | 47 | ▶ | my $attr = shift; | ||||||||||||||||||||||||||
|
Subroutine
_attr: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 47 | my @ret; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 47 | for my $key (sort keys %$attr) { | |||||||||||||||||||||||||||
| 23 | 107 | push @ret, sprintf(q{%s="%s"}, HTML::Escape::escape_html($key), HTML::Escape::escape_html($attr->{$key})); | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 47 | join ' ', @ret; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub widget_input { | ||||||||||||||||||||||||||||
| 30 | 16 | ▶ | my ($self, $form, $field) = @_; | ||||||||||||||||||||||||||
|
Subroutine
widget_input: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 16 | my $value = $form->fillin_param($field->{name}); | |||||||||||||||||||||||||||
| 33 | 16 | ▶ | if (defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 4 | $field->value($value); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 16 | return '<input ' . _attr($field->attr) . " />"; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub widget_textarea { | ||||||||||||||||||||||||||||
| 41 | 1 | ▶ | my ($self, $form, $field) = @_; | ||||||||||||||||||||||||||
|
Subroutine
widget_textarea: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | my $value = $form->fillin_param($field->{name}) || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$form->fillin_param($field->{'name'}) || ''
MC/DC0%0/1
$form->fillin_param($field->{'name'}) || ''
$form->fillin_param($field->{'name'})
Truth table50%1/2
A
A= $form->fillin_param($field->{'name'})
| |||||||||||||||||||||||||||||
| 44 | 1 | my $attr = {%{$field->attr}}; # shallow copy | |||||||||||||||||||||||||||
| 45 | 1 | delete $attr->{type}; # textarea tag doesn't need this | |||||||||||||||||||||||||||
| 46 | 1 | return '<textarea ' . _attr($attr) . ">${value}</textarea>"; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub widget_select { | ||||||||||||||||||||||||||||
| 50 | 12 | ▶ | my ($self, $form, $field) = @_; | ||||||||||||||||||||||||||
|
Subroutine
widget_select: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 12 | my $choices = $field->{choices}; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 12 | my $value = $form->fillin_param($field->{name}); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 12 | my @t; | |||||||||||||||||||||||||||
| 57 | 12 | push @t, sprintf(q{<select %s>}, _attr($field->attr)); | |||||||||||||||||||||||||||
| 58 | 12 | ▶ | for (my $i=0; $i<@$choices; $i+=2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 109 | my ($a, $b) = ($choices->[$i], $choices->[$i+1]); | |||||||||||||||||||||||||||
| 60 | 109 | ▶ | push @t, sprintf( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $value eq $a
MC/DC100%2/2
defined $value and $value eq $a
defined $value
$value eq $a
Truth table100%3/3
A and B
A= defined $valueB= $value eq $a
| |||||||||||||||||||||||||||||
| 61 | q{<option value="%s"%s>%s</option>}, | ||||||||||||||||||||||||||||
| 62 | HTML::Escape::escape_html($a), | ||||||||||||||||||||||||||||
| 63 | (defined $value && $value eq $a ? ' selected="selected"' : ''), | ||||||||||||||||||||||||||||
| 64 | HTML::Escape::escape_html($b)); | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 12 | push @t, q{</select>}; | |||||||||||||||||||||||||||
| 67 | 12 | return join "\n", @t; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub widget_radio { | ||||||||||||||||||||||||||||
| 71 | 5 | ▶ | my ($self, $form, $field) = @_; | ||||||||||||||||||||||||||
|
Subroutine
widget_radio: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 5 | my $choices = $field->{choices}; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 5 | my $value = $form->fillin_param($field->{name}); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 5 | my @t; | |||||||||||||||||||||||||||
| 78 | 5 | push @t, "<ul>"; | |||||||||||||||||||||||||||
| 79 | 5 | ▶ | for (my $i=0; $i<@$choices; $i+=2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 15 | my ($a, $b) = ($choices->[$i], $choices->[$i+1]); | |||||||||||||||||||||||||||
| 81 | push @t, sprintf( | ||||||||||||||||||||||||||||
| 82 | q{<li><label><input %s type="radio" value="%s"%s />%s</label></li>}, | ||||||||||||||||||||||||||||
| 83 | _attr({ | ||||||||||||||||||||||||||||
| 84 | 15 | ▶ | %{ $field->attr }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $value eq $a
MC/DC100%2/2
defined $value and $value eq $a
defined $value
$value eq $a
Truth table100%3/3
A and B
A= defined $valueB= $value eq $a
| |||||||||||||||||||||||||||||
| 85 | 15 | id => sprintf( $field->id_tmpl, $field->{name}, $i / 2 ), | |||||||||||||||||||||||||||
| 86 | }), | ||||||||||||||||||||||||||||
| 87 | HTML::Escape::escape_html($a), | ||||||||||||||||||||||||||||
| 88 | (defined $value && $value eq $a ? ' checked="checked"' : ''), | ||||||||||||||||||||||||||||
| 89 | HTML::Escape::escape_html($b) | ||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 5 | push @t, "</ul>"; | |||||||||||||||||||||||||||
| 93 | 5 | join "\n", @t; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub widget_checkbox { | ||||||||||||||||||||||||||||
| 97 | 1 | ▶ | my ($self, $form, $field) = @_; | ||||||||||||||||||||||||||
|
Subroutine
widget_checkbox: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | my $choices = $field->{choices}; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | my $values = $form->fillin_param($field->{name}); | |||||||||||||||||||||||||||
| 102 | 1 | ▶ | unless (ref $values) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 1 | ▶ | $values = defined $values ? [$values] : []; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 1 | my @t; | |||||||||||||||||||||||||||
| 106 | 1 | push @t, "<ul>"; | |||||||||||||||||||||||||||
| 107 | 1 | ▶ | for (my $i=0; $i<@$choices; $i+=2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 3 | my ($val, $label) = ($choices->[$i], $choices->[$i+1]); | |||||||||||||||||||||||||||
| 109 | 3 | my $checked = grep /^$val$/, @$values; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | push @t, sprintf( | ||||||||||||||||||||||||||||
| 112 | '<li><label><input %s type="checkbox" value="%s"%s />%s</label></li>', | ||||||||||||||||||||||||||||
| 113 | _attr({ | ||||||||||||||||||||||||||||
| 114 | 3 | ▶ | %{ $field->attr }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 3 | id => sprintf( $field->id_tmpl, $field->{name}, $i / 2 ), | |||||||||||||||||||||||||||
| 116 | }), | ||||||||||||||||||||||||||||
| 117 | HTML::Escape::escape_html($val), | ||||||||||||||||||||||||||||
| 118 | ($checked ? ' checked="checked"' : ''), | ||||||||||||||||||||||||||||
| 119 | HTML::Escape::escape_html($label), | ||||||||||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 1 | push @t, "</ul>"; | |||||||||||||||||||||||||||
| 123 | 1 | join "\n", @t; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | sub widget_date { | ||||||||||||||||||||||||||||
| 127 | 2 | ▶ | my ($self, $form, $field) = @_; | ||||||||||||||||||||||||||
|
Subroutine
widget_date: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 2 | ▶ | my $name = $field->{name} or die "missing name"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 2 | ▶ | my $years = $field->{years} or die "missing years"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | my $set = sub { | ||||||||||||||||||||||||||||
| 132 | 6 | ▶ | my ($choices, $suffix) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 133 | 6 | $self->widget_select( | |||||||||||||||||||||||||||
| 134 | $form, | ||||||||||||||||||||||||||||
| 135 | HTML::Shakan::Field::Choice->new( | ||||||||||||||||||||||||||||
| 136 | name => "${name}_${suffix}", | ||||||||||||||||||||||||||||
| 137 | choices => [zip(@$choices, @$choices)], | ||||||||||||||||||||||||||||
| 138 | ) | ||||||||||||||||||||||||||||
| 139 | ); | ||||||||||||||||||||||||||||
| 140 | 2 | }; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 2 | my @t; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 2 | push @t, '<span>'; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 2 | push @t, $set->($years, 'year'); | |||||||||||||||||||||||||||
| 147 | 2 | push @t, $set->([1..12], 'month'); | |||||||||||||||||||||||||||
| 148 | 2 | push @t, $set->([1..31], 'day'); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 2 | push @t, '</span>'; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 2 | join "\n", @t; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | sub field_filter { | ||||||||||||||||||||||||||||
| 156 | 62 | ▶ | my ($self, $form, $field, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
field_filter: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 62 | ▶ | if ($field->widget eq 'date') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 4 | my @c; | |||||||||||||||||||||||||||
| 160 | 4 | for my $k (qw/year month day/) { | |||||||||||||||||||||||||||
| 161 | 12 | my $key = $field->name . '_' . $k; | |||||||||||||||||||||||||||
| 162 | 12 | my $v = $form->request->param($key); | |||||||||||||||||||||||||||
| 163 | 12 | ▶ | if (defined $v) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 9 | push @c, $v; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 4 | ▶ | if (@c == 3) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 3 | $params->{$field->name} = join '-', @c; # http-date style | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 22 | 1; | |||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 NAME | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | HTML::Shakan::Widgets::Simple - simple default HTML widgets | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | This is basic default widgets class. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | This module generates basic XHTML. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Tokuhiro Matsuno | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | L<HTML::Shakan> | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||