1 |
package Frey::IconBrowser; |
2 |
use Moose; |
3 |
|
4 |
extends 'Frey'; |
5 |
with 'Frey::Web', 'Frey::Web::CombineImages', 'Frey::File'; |
6 |
|
7 |
use Data::Dump qw/dump/; |
8 |
|
9 |
has path => ( |
10 |
is => 'rw', |
11 |
isa => 'Str', |
12 |
required => 1, |
13 |
default => 'static/icons/fugue/', |
14 |
); |
15 |
|
16 |
has show_names => ( |
17 |
is => 'rw', |
18 |
isa => 'Bool', |
19 |
required => 1, |
20 |
default => 1, |
21 |
); |
22 |
|
23 |
sub as_markup { |
24 |
my ($self) = @_; |
25 |
|
26 |
$self->title( 'icons - ' . $self->path ); |
27 |
|
28 |
my $extension = '\.(?:png)$'; |
29 |
my @icons = $self->dir_extension( $self->path, qr{$extension} ); |
30 |
|
31 |
my ( $combined_path, $styles ) = $self->combine_images( @icons ); |
32 |
|
33 |
my $width = 0; |
34 |
foreach ( @icons ) { |
35 |
my $l = length($_); |
36 |
$width = $l if $l > $width; |
37 |
} |
38 |
|
39 |
$width = 2 if ! $self->show_names; |
40 |
|
41 |
$self->add_css(qq| |
42 |
h1 { font-size: 20px } |
43 |
/* group */ |
44 |
div.g { |
45 |
float: left; |
46 |
clear: both; |
47 |
} |
48 |
/* individual icon */ |
49 |
div.i { |
50 |
color: #888; |
51 |
width: ${width}ex; |
52 |
float: left; |
53 |
} |
54 |
|); |
55 |
|
56 |
my $html = '<div class="g">'; |
57 |
my $base; |
58 |
|
59 |
foreach my $icon ( @icons ) { |
60 |
my $name = $icon; |
61 |
$name =~ s{^[\w/]*/([^/]+)$extension}{$1}; |
62 |
|
63 |
if ( ! $base ) { |
64 |
$base = $name; |
65 |
$html .= qq|<h1>$name</h1>|; |
66 |
} |
67 |
|
68 |
my $desc = ''; |
69 |
|
70 |
my $bl = length $base; |
71 |
|
72 |
if ( $name =~ m{_} && substr($name, 0, $bl) eq $base ) { |
73 |
($name,$desc) = ( |
74 |
substr($name,0,$bl), |
75 |
substr($name,$bl) |
76 |
); |
77 |
} else { |
78 |
$base = $name; |
79 |
warn "# new base $base" if $self->debug; |
80 |
$html .= qq| |
81 |
</div> |
82 |
<div class="g"> |
83 |
<h1>$name</h1> |
84 |
|; |
85 |
} |
86 |
|
87 |
# $html .= qq|<img src="/| . $self->path . qq|/$icon" alt="$icon"> <b>$name</b>$desc<br/>\n|; |
88 |
# $html .= qq|<b>$name</b>$desc<br/>\n|; |
89 |
|
90 |
my $path = $icon; |
91 |
$path = $self->path . '/' . $icon unless $icon =~ m{[\w/]+/[^/]+$}; |
92 |
|
93 |
my $pic = $styles->{$path} || die "can't find pic for $path in ",dump( $styles ); |
94 |
$html .= '' |
95 |
. qq|<div title="$icon" class="i">$pic| |
96 |
. ( $self->show_names ? qq|<b>$name</b>$desc| : '' ) |
97 |
. qq|</div>\n| |
98 |
; |
99 |
} |
100 |
|
101 |
|
102 |
$html .= '</div>'; |
103 |
# $html .= qq|<img src="$combined_path">|; |
104 |
return $html; |
105 |
} |
106 |
|
107 |
__PACKAGE__->meta->make_immutable; |
108 |
no Moose; |
109 |
|
110 |
1; |