File Coverage

blib/lib/RapidApp/Module/NavTree.pm
Criterion Covered Total %
statement 51 52 98.0
branch 13 26 50.0
condition 3 8 37.5
subroutine 10 10 100.0
pod 0 5 0.0
total 77 101 76.2


line stmt bran cond sub pod time code
1             package RapidApp::Module::NavTree;
2              
3 4     4   1870 use strict;
  4         9  
  4         105  
4 4     4   18 use warnings;
  4         8  
  4         93  
5              
6 4     4   21 use Moose;
  4         6  
  4         19  
7             extends 'RapidApp::Module::Tree';
8              
9 4     4   21328 use RapidApp::Util qw(:all);
  4         7  
  4         3225  
10              
11             has 'module_scope', is => 'ro', lazy => 1, default => sub { return shift };
12              
13             has '+fetch_nodes_deep', default => 1;
14              
15             has 'double_click_nav', is => 'ro', isa => 'Bool', default => 0;
16              
17             sub BUILD {
18 4     4 0 9 my $self = shift;
19 4         30 $self->setup_nav_listeners;
20             }
21              
22             # Add these listeners in a sub so derived classes can override/remove this:
23             sub setup_nav_listeners {
24 4     4 0 13 my $self = shift;
25            
26 4 50       114 my $event = $self->double_click_nav ? 'dblclick' : 'click';
27            
28 4         186 $self->add_listener( $event => RapidApp::JSONFunc->new( raw => 1, func => 'Ext.ux.RapidApp.AppTab.treenav_click' ) );
29 4         105 $self->add_listener( beforerender => RapidApp::JSONFunc->new( raw => 1, func => 'Ext.ux.RapidApp.AppTab.cnt_init_loadTarget' ) );
30             }
31              
32             sub apply_node_navopts_recursive {
33 13     13 0 20 my $self = shift;
34 13         17 my $nodes = shift;
35            
36 13 100       31 if(ref($nodes) eq 'HASH') {
37 1 50       8 $self->apply_node_navopts_recursive($nodes->{children}) if ($nodes->{children});
38 1         4 return $self->apply_node_navopts($nodes);
39             }
40            
41 12 50       25 return undef unless (ref($nodes) eq 'ARRAY');
42            
43 12         22 foreach my $item (@$nodes) {
44            
45 11 50 33     60 if (ref($item->{children}) eq 'ARRAY' and scalar $item->{children} > 0) {
46 11 50       47 $self->apply_node_navopts_recursive($item->{children}) if ($item->{children});
47             }
48             #else {
49             # #$item->{leaf} = \1;
50             # $item->{loaded} = \1;
51             # delete $item->{children} if ($item->{children});
52             #}
53              
54 11 50       29 $item->{expanded} = \1 if ($item->{expand});
55            
56 11         28 $self->apply_node_navopts($item);
57             }
58            
59 12         20 return $nodes;
60             }
61              
62             sub apply_node_navopts {
63 12     12 0 15 my $self = shift;
64 12         15 my $item = shift;
65            
66 12         29 my $autoLoad = {};
67 12 50       42 $autoLoad->{params} = $item->{params} if ($item->{params});
68 12 50       30 $autoLoad->{url} = $item->{url} if ($item->{url});
69            
70 12         25 my $module = $item->{module};
71 12 50       27 if ($module) {
72 12 50       318 $module = $self->module_scope->Module($item->{module}) unless(ref($module));
73 12         263 $autoLoad->{url} = $module->base_url;
74             }
75             else {
76             # Don't build a loadContentCnf if there is no module or url
77 0 0       0 return unless ($autoLoad->{url});
78             }
79            
80 12         24 my $loadCnf = {};
81 12         31 $loadCnf->{itemId} = $item->{itemId};
82 12 50       40 $loadCnf->{itemId} = $item->{id} unless ($loadCnf->{itemId});
83            
84 12   33     55 $loadCnf->{title} = $item->{title} || $item->{text};
85 12         24 $loadCnf->{iconCls} = $item->{iconCls};
86            
87 12         20 $loadCnf->{autoLoad} = $autoLoad;
88 12         39 $item->{loadContentCnf} = $loadCnf;
89             }
90              
91             # Default fetch_nodes uses an array of nodes returned from 'TreeConfig'
92             sub fetch_nodes {
93 1     1 0 3 my $self = shift;
94 1   50     27 return $self->TreeConfig || [];
95             }
96              
97              
98             # New: automatically apply navopts logic to all nodes, including for subclasses
99             # which define their own own fetch_nodes method
100             around 'prepare_node' => sub {
101             my ($orig, $self, @args) = @_;
102             my $nodes = $self->$orig(@args);
103            
104             $self->apply_node_navopts_recursive($nodes);
105            
106             $nodes
107             };
108              
109              
110              
111             #### --------------------- ####
112              
113              
114 4     4   29 no Moose;
  4         9  
  4         18  
115             #__PACKAGE__->meta->make_immutable;
116             1;