1 package SL::Presenter::MaterialComponents;
5 use SL::HTML::Restrict;
6 use SL::Presenter::EscapedText qw(escape);
7 use SL::Presenter::Tag qw(html_tag);
8 use Scalar::Util qw(blessed);
10 use Exporter qw(import);
14 our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
16 use constant BUTTON => 'btn';
17 use constant BUTTON_FLAT => 'btn-flat';
18 use constant BUTTON_FLOATING => 'btn-floating';
19 use constant BUTTON_LARGE => 'btn-large';
20 use constant BUTTON_SMALL => 'btn-small';
21 use constant DISABLED => 'disabled';
22 use constant LEFT => 'left';
23 use constant MATERIAL_ICONS => 'material-icons';
24 use constant RIGHT => 'right';
25 use constant LARGE => 'large';
26 use constant MEDIUM => 'medium';
27 use constant SMALL => 'small';
28 use constant TINY => 'tiny';
29 use constant INPUT_FIELD => 'input-field';
31 use constant WAVES_EFFECT => 'waves-effect';
32 use constant WAVES_LIGHT => 'waves-light';
35 my %optional_classes = (
39 floating => BUTTON_FLOATING,
40 large => BUTTON_LARGE,
41 small => BUTTON_SMALL,
56 'if (!confirm("'. _J($_[0]) .'")) return false;'
59 sub _confirm_to_onclick {
60 my ($attributes, $onclick) = @_;
62 if ($attributes->{confirm}) {
64 $$onclick = _confirm_js(delete($attributes->{confirm})) . $attributes->{onlick};
68 # used to extract material properties that need to be translated to classes
69 # supports prefixing for delegation
70 # returns a list of classes, mutates the attributes
71 sub _extract_attribute_classes {
72 my ($attributes, $type, $prefix) = @_;
76 for my $key (keys %$attributes) {
78 next unless $key =~ /^${prefix}_(.*)/;
84 if ($optional_classes{$type}{$attr}) {
85 $attributes->{$key} = undef;
86 push @classes, $optional_classes{$type}{$attr};
90 # delete all undefined values
91 my @delete_keys = grep { !defined $attributes->{$_} } keys %$attributes;
92 delete $attributes->{$_} for @delete_keys;
97 sub _set_id_attribute {
98 my ($attributes, $name, $unique) = @_;
100 if (!delete($attributes->{no_id}) && !$attributes->{id}) {
101 $attributes->{id} = name_to_id($name);
102 $attributes->{id} .= '_' . $attributes->{value} if $unique;
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;
113 return ( $_id_sequence = ($_id_sequence + 1) % 1e7 );
121 return "id_" . _id();
124 $name =~ s/\[\+?\]/ _id() /ge; # give constructs with [] or [+] unique ids
125 $name =~ s/[^\w_]/_/g;
132 my ($onclick, $value, %attributes) = @_;
134 _set_id_attribute(\%attributes, $attributes{name}) if $attributes{name};
135 _confirm_to_onclick(\%attributes, \$onclick);
137 my @button_classes = _extract_attribute_classes(\%attributes, "button");
138 my @icon_classes = _extract_attribute_classes(\%attributes, "icon", "icon");
140 $attributes{class} = [
141 grep { $_ } $attributes{class}, WAVES_EFFECT, WAVES_LIGHT, BUTTON, @button_classes
144 if ($attributes{icon}) {
145 $value = icon(delete $attributes{icon}, class => \@icon_classes)
149 html_tag('a', $value, %attributes, onclick => $onclick);
153 my ($name, $value, %attributes) = @_;
155 _set_id_attribute(\%attributes, $attributes{name}) if $attributes{name};
156 _confirm_to_onclick(\%attributes, \($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('button', $value, type => 'submit', %attributes);
175 my ($name, %attributes) = @_;
177 my @icon_classes = _extract_attribute_classes(\%attributes, "icon");
179 html_tag('i', $name, class => [ grep { $_ } MATERIAL_ICONS, @icon_classes, delete $attributes{class} ], %attributes);
184 my ($name, $value, %attributes) = @_;
186 _set_id_attribute(\%attributes, $attributes{name});
188 my $class = delete %attributes{class};
189 my $icon = $attributes{icon}
190 ? icon(delete $attributes{icon}, class => 'prefix')
193 my $label = $attributes{label}
194 ? html_tag('label', delete $attributes{label}, for => $attributes{id})
197 $attributes{type} //= 'text';
201 html_tag('input', undef, value => $value, %attributes, name => $name) .
203 class => [ grep $_, $class, INPUT_FIELD ],
220 SL::Presenter::MaterialComponents - MaterialCSS Component wrapper
233 Sven Schöling E<lt>s.schoeling@googlemail.comE<gt>