| 1 | package WWW::MenuGrinder::Plugin::ActivePath; | ||
| 2 | BEGIN { | ||
| 3 | $WWW::MenuGrinder::Plugin::ActivePath::VERSION = '0.06'; | ||
| 4 | } | ||
| 5 | |||
| 6 | # ABSTRACT: WWW::MenuGrinder plugin that finds a path to the currently active page. | ||
| 7 | |||
| 8 | use Moose; | ||
| 9 | use List::Util; | ||
| 10 | |||
| 11 | with 'WWW::MenuGrinder::Role::ItemMogrifier'; | ||
| 12 | with 'WWW::MenuGrinder::Role::BeforeMogrify'; | ||
| 13 | |||
| 14 | has path => ( is => 'rw' ); | ||
| 15 | |||
| 16 | has active_child_ref => ( | ||
| 17 | is => 'rw', | ||
| 18 | default => 0, | ||
| 19 | ); | ||
| 20 | |||
| 21 | sub plugin_required_grinder_methods { qw(path) } | ||
| 22 | |||
| 23 | sub before_mogrify { | ||
| 24 | my ($self) = @_; | ||
| 25 | |||
| 26 | $self->path( $self->grinder->path ); | ||
| 27 | } | ||
| 28 | |||
| 29 | has 'longest' => ( | ||
| 30 | is => 'rw', | ||
| 31 | default => 0 | ||
| 32 | ); | ||
| 33 | |||
| 34 | sub item_mogrify_methods { | ||
| 35 | qw(find_longest_match mark_active_path) | ||
| 36 | }; | ||
| 37 | |||
| 38 | sub find_longest_match { | ||
| 39 | my ( $self, $item ) = @_; | ||
| 40 | |||
| 41 | if (exists $item->{location}) { | ||
| 42 | my @loc = ref($item->{location}) eq 'ARRAY' ? | ||
| 43 | @{ $item->{location} } : $item->{location}; | ||
| 44 | |||
| 45 | for my $location ( @loc ) { | ||
| 46 | |||
| 47 | my $active; | ||
| 48 | # XML::Simple is stupid | ||
| 49 | if ($location eq '' or (ref($location) && ref($location) eq 'HASH') ) { | ||
| 50 | $active = 0.01; # more than 0, less than 1 | ||
| 51 | } elsif ( $self->path =~ m#^\Q$location\E(/|$)# ) { | ||
| 52 | $active = length($location); | ||
| 53 | } | ||
| 54 | |||
| 55 | if (defined $active && $active > $self->longest) { | ||
| 56 | $self->longest( $active ); | ||
| 57 | # This one might be the longest, so we might use it later. | ||
| 58 | # If not, we'll delete it. | ||
| 59 | $item->{active} = $active; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | return $item; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub mark_active_path { | ||
| 68 | my ( $self, $item ) = @_; | ||
| 69 | |||
| 70 | # If we were the longest match, set active="yes". | ||
| 71 | # If one of our children is active (of either type) set active="child". | ||
| 72 | |||
| 73 | my $max = $self->longest; | ||
| 74 | |||
| 75 | if (defined $item->{active} and $item->{active} == $max) { | ||
| 76 | $item->{active} = "yes"; | ||
| 77 | } else { | ||
| 78 | delete $item->{active}; | ||
| 79 | if (ref ($item->{item})) { | ||
| 80 | for my $child ( @{ $item->{item} } ) { | ||
| 81 | if (defined $child->{active}) { | ||
| 82 | $item->{active} = "child"; | ||
| 83 | $item->{active_child} = $child if $self->active_child_ref; | ||
| 84 | last; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | return $item; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub cleanup { | ||
| 94 | my ($self) = @_; | ||
| 95 | $self->longest(0); | ||
| 96 | } | ||
| 97 | |||
| 98 | __PACKAGE__->meta->make_immutable; | ||
| 99 | |||
| 100 | no Moose; | ||
| 101 | 1; | ||
| 102 | |||
| 103 | |||
| 104 | __END__ |