Catalyst-Controller-AutoAssets

 view release on metacpan or  search on metacpan

lib/Catalyst/Controller/AutoAssets/Handler/IconSet.pm  view on Meta::CPAN

has 'icon_manifest', is => 'rw', isa => 'HashRef', default => sub {{}};
after rebuild_manifest => sub {
  my $self = shift;
  my $manifest = $self->manifest;
  
  my %icon_map = ();
  for my $name (keys %{$self->manifest}) {
    my $icon = $self->icon_name_generator->($self,$name);
    die "Duplicate icon name '$icon' ($name)" if (exists $icon_map{$icon});
    
    # The path in the CSS can be relative because the css file itself
    # is served from the root of the asset path. That is good because
    # we would have a deep recursion problem if we wanted to get the
    # asset path here (i.e. $self->asset_path($name) ) because our caller
    # might already be: asset_path -> prepare_asset -> ... -> us
    $self->manifest->{$name}->{css_rule} = $self->css_rule_generator->($self,$icon,$name);
    $self->manifest->{$name}->{name} = $name;
    $self->manifest->{$name}->{icon_name} = $icon;
    
    $icon_map{$icon} = $self->manifest->{$name};
  }
  
  $self->icon_manifest(\%icon_map);
  $self->rebuild_css_file;
};

sub rebuild_css_file {
  my $self = shift;
  
  my $css = join("\n",map {
    $self->manifest->{$_}->{css_rule}
  } (sort keys %{$self->manifest}) );
  
  return $self->css_file->spew($css);
}

sub html_head_tags {
  my $self = shift;
  return
		"<!--   AUTO GENERATED BY " . ref($self->Controller) . " (/" .
    $self->action_namespace($self->_app) . ")   -->\r\n" .
		'<link rel="stylesheet" type="text/css" href="' . 
    join('/',$self->asset_path,$self->css_file_name) .
    '" />' .
		"\r\n<!--  ---- END AUTO GENERATED ASSETS ----  -->\r\n";
}

sub icon_class {
  my ($self, @path) = @_;
  die "icon_class_name() requires subfile path argument" 
    unless (scalar @path > 0);
  
  # Will prepare the asset if needed:
  $self->asset_path(@path);
  
  my $name = join('/',@path);
  my $data = $self->manifest->{$name} or die "No such image/icon '$name'";
  return $data->{icon_name};
}

has '_persist_attrs', is => 'ro', isa => 'ArrayRef', default => sub{[qw(
 built_mtime
 inc_mtimes
 last_fingerprint_calculated
 subfile_meta
 _excluded_paths
 manifest
 icon_manifest
)]};



1;

__END__

=pod

=head1 NAME

Catalyst::Controller::AutoAssets::Handler::IconSet - IconSet type handler

=head1 SYNOPSIS

In your controller:

  package MyApp::Controller::Assets::Icons;
  use parent 'Catalyst::Controller::AutoAssets';
  
  1;

Then, in your .conf:

  <Controller::Assets::Icons>
    include        root/static/icons/
    type           IconSet
  </Controller::Assets::Icons>

And in your .tt files:

  <head>
    <!-- include the auto generated icon CSS -->
    [% c.controller('Assets::Icons').html_head_tags %]
    
    <!-- or, in static HTML -->
    <link rel="stylesheet" type="text/css" href="/assets/icons/current/icons.css" />
  </head>
  
  ...
  
  <!-- access individual icons by name -->
  [% c.controller('Assets::Icons').img_tag('foo.png') %]
  <img src="[% c.controller('Assets::Icons').asset_path('apple.jpg') %]">
  
  ...
  
  <!-- use icon class names in markup -->
  <div class="mycls [% c.controller('Assets::Icons').icon_class('apple.jpg') %]"></div>
  
  <!-- or, in static HTML -->
  <div class="mycls icon-apple"></div>



( run in 0.377 second using v1.01-cache-2.11-cpan-39bf76dae61 )