1 |
package Frey::Class::Icon; |
2 |
use Moose::Role; |
3 |
|
4 |
has 'icon_html' => ( |
5 |
is => 'rw', |
6 |
# isa => 'HTML', # FIXME |
7 |
default => '<!-- no icon -->', |
8 |
); |
9 |
|
10 |
=head2 add_icon |
11 |
|
12 |
Frey::Foo->add_icon; # /static/icons/Frey/Foo.png |
13 |
Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png |
14 |
|
15 |
=cut |
16 |
|
17 |
sub icon_path { |
18 |
my ($self,$class,$variant) = @_; |
19 |
|
20 |
sub icon_exists { |
21 |
my $class = shift; |
22 |
$class =~ s{::}{/}g; |
23 |
$class .= "/$variant" if $variant; |
24 |
my $icon_path = 'static/icons/' . $class . '.png'; |
25 |
return $icon_path if -e $icon_path; |
26 |
return; |
27 |
} |
28 |
|
29 |
my $path = icon_exists( $class ); |
30 |
if ( ! $path ) { |
31 |
my $super_class = $class; |
32 |
while ( $super_class =~ s{::[^:]+$}{} && ! $path ) { |
33 |
$path = icon_exists( $super_class ) unless $super_class eq 'Frey'; # don't default on Frey icon |
34 |
} |
35 |
} |
36 |
|
37 |
if ( ! $path ) { |
38 |
$self->TODO( "add icon for $class" . ( $variant ? " variant $variant" : '' ) ); |
39 |
return undef; |
40 |
} |
41 |
|
42 |
warn "# $class from $self icon_path $path" if $self->debug; |
43 |
return $path; |
44 |
} |
45 |
|
46 |
our $icon_html; |
47 |
sub icon_html { $icon_html }; |
48 |
|
49 |
sub add_icon { |
50 |
my ($self,$variant) = @_; |
51 |
|
52 |
my $class = $self->class if $self->can('class'); |
53 |
#$class ||= $self->title; |
54 |
$class ||= ref($self); |
55 |
my $icon_path = $self->icon_path( $class, $variant ) || return; |
56 |
|
57 |
$icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|; |
58 |
warn "# using icon $icon_path for $class $variant "; |
59 |
|
60 |
=for later |
61 |
|
62 |
# FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work! |
63 |
my $ico_path = $icon_path; |
64 |
$ico_path =~ s{png$}{ico}; |
65 |
if ( ! -e $ico_path ) { |
66 |
system "convert $icon_path $ico_path"; |
67 |
warn "# convert $icon_path $ico_path : $@"; |
68 |
} |
69 |
$icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path; |
70 |
|
71 |
=cut |
72 |
|
73 |
} |
74 |
|
75 |
1; |