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