summaryrefslogtreecommitdiff
path: root/lib/DeWeave/Collection/Bookmarks.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/DeWeave/Collection/Bookmarks.pm')
-rw-r--r--lib/DeWeave/Collection/Bookmarks.pm76
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/DeWeave/Collection/Bookmarks.pm b/lib/DeWeave/Collection/Bookmarks.pm
new file mode 100644
index 0000000..af1cc3e
--- /dev/null
+++ b/lib/DeWeave/Collection/Bookmarks.pm
@@ -0,0 +1,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;