Revision 13fddaf3
Von Sven Schöling vor mehr als 3 Jahren hinzugefügt
SL/Presenter/MaterialComponents.pm | ||
---|---|---|
26 | 26 |
use constant MEDIUM => 'medium'; |
27 | 27 |
use constant SMALL => 'small'; |
28 | 28 |
use constant TINY => 'tiny'; |
29 |
use constant INPUT_FIELD => 'input-field'; |
|
29 | 30 |
|
30 | 31 |
use constant WAVES_EFFECT => 'waves-effect'; |
31 | 32 |
use constant WAVES_LIGHT => 'waves-light'; |
... | ... | |
93 | 94 |
@classes; |
94 | 95 |
} |
95 | 96 |
|
97 |
sub _set_id_attribute { |
|
98 |
my ($attributes, $name, $unique) = @_; |
|
99 |
|
|
100 |
if (!delete($attributes->{no_id}) && !$attributes->{id}) { |
|
101 |
$attributes->{id} = name_to_id($name); |
|
102 |
$attributes->{id} .= '_' . $attributes->{value} if $unique; |
|
103 |
} |
|
104 |
|
|
105 |
%{ $attributes }; |
|
106 |
} |
|
107 |
|
|
108 |
{ # This will give you an id for identifying html tags and such. |
|
109 |
# It's guaranteed to be unique unless you exceed 10 mio calls per request. |
|
110 |
# Do not use these id's to store information across requests. |
|
111 |
my $_id_sequence = int rand 1e7; |
|
112 |
sub _id { |
|
113 |
return ( $_id_sequence = ($_id_sequence + 1) % 1e7 ); |
|
114 |
} |
|
115 |
} |
|
116 |
|
|
117 |
sub name_to_id { |
|
118 |
my ($name) = @_; |
|
119 |
|
|
120 |
if (!$name) { |
|
121 |
return "id_" . _id(); |
|
122 |
} |
|
123 |
|
|
124 |
$name =~ s/\[\+?\]/ _id() /ge; # give constructs with [] or [+] unique ids |
|
125 |
$name =~ s/[^\w_]/_/g; |
|
126 |
$name =~ s/_+/_/g; |
|
127 |
|
|
128 |
return $name; |
|
129 |
} |
|
130 |
|
|
96 | 131 |
sub button_tag { |
97 | 132 |
my ($onclick, $value, %attributes) = @_; |
98 | 133 |
|
... | ... | |
148 | 183 |
sub input_tag { |
149 | 184 |
my ($name, $value, %attributes) = @_; |
150 | 185 |
|
151 |
# todo icons |
|
152 |
# todo label/active |
|
153 |
# todo validate |
|
186 |
_set_id_attribute(\%attributes, $attributes{name}); |
|
187 |
|
|
188 |
my $class = delete %attributes{class}; |
|
189 |
my $icon = $attributes{icon} |
|
190 |
? icon(delete $attributes{icon}, class => 'prefix') |
|
191 |
: ''; |
|
192 |
|
|
193 |
my $label = $attributes{label} |
|
194 |
? html_tag('label', delete $attributes{label}, for => $attributes{id}) |
|
195 |
: ''; |
|
196 |
|
|
197 |
$attributes{type} //= 'text'; |
|
198 |
|
|
199 |
html_tag('div', |
|
200 |
$icon . |
|
201 |
html_tag('input', undef, value => $value, %attributes, name => $name) . |
|
202 |
$label, |
|
203 |
class => [ grep $_, $class, INPUT_FIELD ], |
|
204 |
); |
|
205 |
} |
|
206 |
|
|
154 | 207 |
|
155 |
html_tag('input', $name, $value, %attributes) . html_tag('label', for => $attributes{id}, $name); |
|
156 | 208 |
} |
157 | 209 |
|
158 | 210 |
|
Auch abrufbar als: Unified diff
MaterialComponents: P.M.input_tag