Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 32260f2b

Von Sven Schöling vor mehr als 3 Jahren hinzugefügt

  • ID 32260f2b7eb83f7bbc98ff01c9267a3aa6e2a1ff
  • Vorgänger 44ab0175
  • Nachfolger fd2714b1

MaterialComponents Presenter

Erstmal für die wichtigsten Elemente:

- buttons
- submit
- input felder
- icons

Zusammen mit einem Testcontroller zum rendern

Unterschiede anzeigen:

SL/Controller/MaterializeTest.pm
1
package SL::Controller::MaterializeTest;
2

  
3
use strict;
4
use parent qw(SL::Controller::Base);
5

  
6
sub action_test {
7
  $_[0]->render("test/components");
8
}
9

  
10
1;
SL/Presenter/ALL.pm
23 23
use SL::Presenter::Text;
24 24
use SL::Presenter::Tag;
25 25
use SL::Presenter::BankAccount;
26
use SL::Presenter::MaterialComponents;
26 27

  
27 28
our %presenters = (
28 29
  chart                       => 'SL::Presenter::Chart',
......
46 47
  text                        => 'SL::Presenter::Text',
47 48
  tag                         => 'SL::Presenter::Tag',
48 49
  bank_account                => 'SL::Presenter::BankAccount',
50
  M                           => 'SL::Presenter::MaterialComponents',
49 51
);
50 52

  
51 53
sub wrap {
SL/Presenter/MaterialComponents.pm
1
package SL::Presenter::MaterialComponents;
2

  
3
use strict;
4

  
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);
9

  
10
use Exporter qw(import);
11
our @EXPORT_OK = qw(
12
  button_tag
13
);
14
our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
15

  
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

  
30
use constant WAVES_EFFECT    => 'waves-effect';
31
use constant WAVES_LIGHT     => 'waves-light';
32

  
33

  
34
my %optional_classes = (
35
  button => {
36
    disabled => DISABLED,
37
    flat     => BUTTON_FLAT,
38
    floating => BUTTON_FLOATING,
39
    large    => BUTTON_LARGE,
40
    small    => BUTTON_SMALL,
41
  },
42
  icon => {
43
    left   => LEFT,
44
    right  => RIGHT,
45
    large  => LARGE,
46
    medium => MEDIUM,
47
    small  => SMALL,
48
    tiny   => TINY,
49
  },
50
);
51

  
52
use Carp;
53

  
54
sub _confirm_js {
55
  'if (!confirm("'. _J($_[0]) .'")) return false;'
56
}
57

  
58
sub _confirm_to_onclick {
59
  my ($attributes, $onclick) = @_;
60

  
61
  if ($attributes->{confirm}) {
62
    $$onclick //= '';
63
    $$onclick = _confirm_js(delete($attributes->{confirm})) . $attributes->{onlick};
64
  }
65
}
66

  
67
# used to extract material properties that need to be translated to classes
68
# supports prefixing for delegation
69
# returns a list of classes, mutates the attributes
70
sub _extract_attribute_classes {
71
  my ($attributes, $type, $prefix) = @_;
72

  
73
  my @classes;
74
  my $attr;
75
  for my $key (keys %$attributes) {
76
    if ($prefix) {
77
      next unless $key =~ /^${prefix}_(.*)/;
78
      $attr = $1;
79
    } else {
80
      $attr = $key;
81
    }
82

  
83
    if ($optional_classes{$type}{$attr}) {
84
      $attributes->{$key} = undef;
85
      push @classes, $optional_classes{$type}{$attr};
86
    }
87
  }
88

  
89
  # delete all undefined values
90
  my @delete_keys = grep { !defined $attributes->{$_} } keys %$attributes;
91
  delete $attributes->{$_} for @delete_keys;
92

  
93
  @classes;
94
}
95

  
96
sub button_tag {
97
  my ($onclick, $value, %attributes) = @_;
98

  
99
  _set_id_attribute(\%attributes, $attributes{name}) if $attributes{name};
100
  _confirm_to_onclick(\%attributes, \$onclick);
101

  
102
  my @button_classes = _extract_attribute_classes(\%attributes, "button");
103
  my @icon_classes   = _extract_attribute_classes(\%attributes, "icon", "icon");
104

  
105
  $attributes{class} = [
106
    grep { $_ } $attributes{class}, WAVES_EFFECT, WAVES_LIGHT, BUTTON, @button_classes
107
  ];
108

  
109
  if ($attributes{icon}) {
110
    $value = icon(delete $attributes{icon}, class => \@icon_classes)
111
           . $value;
112
  }
113

  
114
  html_tag('a', $value, %attributes, onclick => $onclick);
115
}
116

  
117
sub submit_tag {
118
  my ($name, $value, %attributes) = @_;
119

  
120
  _set_id_attribute(\%attributes, $attributes{name}) if $attributes{name};
121
  _confirm_to_onclick(\%attributes, \($attributes{onclick} //= ''));
122

  
123
  my @button_classes = _extract_attribute_classes(\%attributes, "button");
124
  my @icon_classes   = _extract_attribute_classes(\%attributes, "icon", "icon");
125

  
126
  $attributes{class} = [
127
    grep { $_ } $attributes{class}, WAVES_EFFECT, WAVES_LIGHT, BUTTON, @button_classes
128
  ];
129

  
130
  if ($attributes{icon}) {
131
    $value = icon(delete $attributes{icon}, class => \@icon_classes)
132
           . $value;
133
  }
134

  
135
  html_tag('button', $value, type => 'submit',  %attributes);
136
}
137

  
138

  
139
sub icon {
140
  my ($name, %attributes) = @_;
141

  
142
  my @icon_classes = _extract_attribute_classes(\%attributes, "icon");
143

  
144
  html_tag('i', $name, class => [ grep { $_ } MATERIAL_ICONS, @icon_classes, delete $attributes{class} ], %attributes);
145
}
146

  
147

  
148
sub input_tag {
149
  my ($name, $value, %attributes) = @_;
150

  
151
  # todo icons
152
  # todo label/active
153
  # todo validate
154

  
155
  html_tag('input', $name, $value, %attributes) . html_tag('label', for => $attributes{id}, $name);
156
}
157

  
158

  
159
1;
160
__END__
161

  
162
=pod
163

  
164
=encoding utf8
165

  
166
=head1 NAME
167

  
168
SL::Presenter::MaterialComponents - MaterialCSS Component wrapper
169

  
170
=head1 SYNOPSIS
171

  
172

  
173
=head1 DESCRIPTION
174

  
175
=head1 BUGS
176

  
177
Nothing here yet.
178

  
179
=head1 AUTHOR
180

  
181
Sven Schöling E<lt>s.schoeling@googlemail.comE<gt>
182

  
183
=cut
templates/mobile_webpages/test/components.html
1
[% USE P %]
2

  
3
<h1>Material Components Tests</h1>
4

  
5

  
6
<h2>Buttons</h2>
7

  
8
[% P.M.button_tag("", "button") %]
9
[% P.M.button_tag("", P.M.icon('add'), floating=1) %]
10
[% P.M.submit_tag("", "submit") %]
11
[% P.M.submit_tag("", "disabled submit", disabled=1) %]
12
[% P.M.button_tag("", "button with left icon", icon="cloud", icon_left=1) %]
13
[% P.M.button_tag("", "button with right icon", icon="save", icon_right=1) %]
14
[% P.M.button_tag("", "flat button", flat=1) %]
15
[% P.M.button_tag("", "large button", large=1) %]
16
[% P.M.button_tag("", "small button", small=1) %]
17
[% P.M.button_tag("", "disabled small button", disabled=1, small=1) %]
18

  
19
<h2>Icons</h2>
20

  
21
[% P.M.icon("alarm") %]
22
[% P.M.icon("alarm", large=1) %]
23
[% P.M.icon("alarm", medium=1) %]
24
[% P.M.icon("alarm", small=1) %]
25
[% P.M.icon("alarm", tiny=1) %]
26

  
27
<h2>Inputs</h2>
28

  
29
[% P.M.input_tag("", "test string") %]

Auch abrufbar als: Unified diff