summaryrefslogtreecommitdiff
path: root/lib/DeWeave/Collection/Bookmarks.pm
blob: af1cc3ec5de46eaa460f43cb92d1f75a5bf9aafd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package DeWeave::Collection::Bookmarks; 
use Moose;
use namespace::autoclean;
use DeWeave::BO::Bookmark::Base;
use DeWeave::BO::Bookmark::Bookmark;
use DeWeave::BO::Bookmark::Microsummary;
use DeWeave::BO::Bookmark::Folder;
use DeWeave::BO::Bookmark::Livemark;
use DeWeave::BO::Bookmark::Separator;
use DeWeave::BO::Bookmark::Query;
use MooseX::Types::Moose qw(HashRef);
 
extends 'DeWeave::Collection';
 
has _item_cache => (
    isa => HashRef['DeWeave::BO::Bookmark::Base'],
    lazy_build => 1,
    traits => ['Hash'],
    handles => {
        item_by_id => 'get',
        item_known => 'exists',
    },
);
 
sub _build__item_cache {
    my ($self) = @_;
 
    my %ret;
 
    for my $item (@{$self->items}) {
        $ret{$item->id}=$item;
    }
 
    return \%ret;
}
 
sub item_class {
    my ($class,$input_item) = @_;
 
    my $item_class = 'Base';
    if (exists $input_item->{type} && defined $input_item->{type}) {
        $item_class = ucfirst($input_item->{type});
    }
 
    return "DeWeave::BO::Bookmark::$item_class";
}
 
sub as_tree {
    my ($self,$start_id) = @_;
 
    $start_id ||= 'menu';
    return $self->_as_tree_rec($start_id,0);
}
 
sub _as_tree_rec {
    my ($self,$start_id,$depth) = @_;
 
    if (!$self->item_known($start_id)) {
        return (('  ' x $depth)." $start_id is unknown\n");
    }
 
    my $item = $self->item_by_id($start_id);
 
    my $padding = '  ' x $depth;
    my $out = $item->as_string;
    $out =~ s/^/$padding/smg;
 
    if ($item->can('children')) {
        for my $child_id (@{$item->children}) {
            $out .= $self->_as_tree_rec($child_id,$depth+1);
        }
    }
    return $out;
}
 
1;