1 package SL::Presenter::MaterialComponents;
5 use SL::HTML::Restrict;
6 use SL::MoreCommon qw(listify);
7 use SL::Presenter::EscapedText qw(escape);
8 use SL::Presenter::Tag qw(html_tag);
9 use Scalar::Util qw(blessed);
10 use List::UtilsBy qw(partition_by);
12 use Exporter qw(import);
16 our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
18 use constant BUTTON => 'btn';
19 use constant BUTTON_FLAT => 'btn-flat';
20 use constant BUTTON_FLOATING => 'btn-floating';
21 use constant BUTTON_LARGE => 'btn-large';
22 use constant BUTTON_SMALL => 'btn-small';
23 use constant DISABLED => 'disabled';
24 use constant LEFT => 'left';
25 use constant MATERIAL_ICONS => 'material-icons';
26 use constant RIGHT => 'right';
27 use constant LARGE => 'large';
28 use constant MEDIUM => 'medium';
29 use constant SMALL => 'small';
30 use constant TINY => 'tiny';
31 use constant INPUT_FIELD => 'input-field';
32 use constant DATEPICKER => 'datepicker';
34 use constant WAVES_EFFECT => 'waves-effect';
35 use constant WAVES_LIGHT => 'waves-light';
38 my %optional_classes = (
42 floating => BUTTON_FLOATING,
43 large => BUTTON_LARGE,
44 small => BUTTON_SMALL,
57 (map { "s$_" } 1..12),
58 (map { "m$_" } 1..12),
59 (map { "l$_" } 1..12),
66 'if (!confirm("'. _J($_[0]) .'")) return false;'
69 sub _confirm_to_onclick {
70 my ($attributes, $onclick) = @_;
72 if ($attributes->{confirm}) {
74 $$onclick = _confirm_js(delete($attributes->{confirm})) . $attributes->{onlick};
78 # used to extract material properties that need to be translated to classes
79 # supports prefixing for delegation
80 # returns a list of classes, mutates the attributes
81 sub _extract_attribute_classes {
82 my ($attributes, $type, $prefix) = @_;
86 for my $key (keys %$attributes) {
88 next unless $key =~ /^${prefix}_(.*)/;
94 if ($optional_classes{$type}{$attr}) {
95 $attributes->{$key} = undef;
96 push @classes, $optional_classes{$type}{$attr};
100 # delete all undefined values
101 my @delete_keys = grep { !defined $attributes->{$_} } keys %$attributes;
102 delete $attributes->{$_} for @delete_keys;
107 # used to extract material classes that are passed directly as classes
108 sub _extract_classes {
109 my ($attributes, $type) = @_;
111 my @classes = map { split / / } listify($attributes->{class});
112 my %classes = partition_by { !!$optional_classes{$type}{$_} } @classes;
114 $attributes->{class} = $classes{''};
118 sub _set_id_attribute {
119 my ($attributes, $name, $unique) = @_;
121 if (!delete($attributes->{no_id}) && !$attributes->{id}) {
122 $attributes->{id} = name_to_id($name);
123 $attributes->{id} .= '_' . $attributes->{value} if $unique;
129 { # This will give you an id for identifying html tags and such.
130 # It's guaranteed to be unique unless you exceed 10 mio calls per request.
131 # Do not use these id's to store information across requests.
132 my $_id_sequence = int rand 1e7;
134 return ( $_id_sequence = ($_id_sequence + 1) % 1e7 );
142 return "id_" . _id();
145 $name =~ s/\[\+?\]/ _id() /ge; # give constructs with [] or [+] unique ids
146 $name =~ s/[^\w_]/_/g;
153 my ($onclick, $value, %attributes) = @_;
155 _set_id_attribute(\%attributes, $attributes{name}) if $attributes{name};
156 _confirm_to_onclick(\%attributes, \$onclick);
158 my @button_classes = _extract_attribute_classes(\%attributes, "button");
159 my @icon_classes = _extract_attribute_classes(\%attributes, "icon", "icon");
161 $attributes{class} = [
162 grep { $_ } $attributes{class}, WAVES_EFFECT, WAVES_LIGHT, BUTTON, @button_classes
165 if ($attributes{icon}) {
166 $value = icon(delete $attributes{icon}, class => \@icon_classes)
170 html_tag('a', $value, %attributes, onclick => $onclick);
174 my ($name, $value, %attributes) = @_;
176 _set_id_attribute(\%attributes, $attributes{name}) if $attributes{name};
177 _confirm_to_onclick(\%attributes, \($attributes{onclick} //= ''));
179 my @button_classes = _extract_attribute_classes(\%attributes, "button");
180 my @icon_classes = _extract_attribute_classes(\%attributes, "icon", "icon");
182 $attributes{class} = [
183 grep { $_ } $attributes{class}, WAVES_EFFECT, WAVES_LIGHT, BUTTON, @button_classes
186 if ($attributes{icon}) {
187 $value = icon(delete $attributes{icon}, class => \@icon_classes)
191 html_tag('button', $value, type => 'submit', %attributes);
196 my ($name, %attributes) = @_;
198 my @icon_classes = _extract_attribute_classes(\%attributes, "icon");
200 html_tag('i', $name, class => [ grep { $_ } MATERIAL_ICONS, @icon_classes, delete $attributes{class} ], %attributes);
205 my ($name, $value, %attributes) = @_;
207 _set_id_attribute(\%attributes, $attributes{name});
209 my $class = delete %attributes{class};
210 my $icon = $attributes{icon}
211 ? icon(delete $attributes{icon}, class => 'prefix')
214 my $label = $attributes{label}
215 ? html_tag('label', delete $attributes{label}, for => $attributes{id})
218 $attributes{type} //= 'text';
222 html_tag('input', undef, value => $value, %attributes, name => $name) .
224 class => [ grep $_, $class, INPUT_FIELD ],
229 my ($name, $value, %attributes) = @_;
231 _set_id_attribute(\%attributes, $name);
233 my $icon = $attributes{icon}
234 ? icon(delete $attributes{icon}, class => 'prefix')
237 my $label = $attributes{label}
238 ? html_tag('label', delete $attributes{label}, for => $attributes{id})
241 $attributes{type} = 'text'; # required for materialize
243 my @onchange = $attributes{onchange} ? (onChange => delete $attributes{onchange}) : ();
244 my @classes = (delete $attributes{class});
246 $::request->layout->add_javascripts('kivi.Validator.js');
247 $::request->presenter->need_reinit_widgets($attributes{id});
249 $attributes{'data-validate'} = join(' ', "date", grep { $_ } (delete $attributes{'data-validate'}));
254 blessed($value) ? $value->to_lxoffice : $value,
255 size => 11, type => 'text', name => $name,
257 class => DATEPICKER, @onchange,
260 class => [ grep $_, @classes, INPUT_FIELD ],
265 my ($name, $collection, %attributes) = @_;
268 _set_id_attribute(\%attributes, $name);
269 my @size_classes = _extract_classes(\%attributes, "size");
272 my $icon = $attributes{icon}
273 ? icon(delete $attributes{icon}, class => 'prefix')
276 my $label = $attributes{label}
277 ? html_tag('label', delete $attributes{label}, for => $attributes{id})
280 my $select_html = SL::Presenter::Tag::select_tag($name, $collection, %attributes);
283 $icon . $select_html . $label,
284 class => [ INPUT_FIELD, @size_classes ],
298 SL::Presenter::MaterialComponents - MaterialCSS Component wrapper
311 Sven Schöling E<lt>s.schoeling@googlemail.comE<gt>