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   2283 use strict;
  4         13  
  4         121  
4 4     4   22 use warnings;
  4         8  
  4         97  
5              
6 4     4   22 use Moose;
  4         9  
  4         21  
7             extends 'RapidApp::Module::Tree';
8              
9 4     4   25054 use RapidApp::Util qw(:all);
  4         10  
  4         3757  
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 11 my $self = shift;
19 4         28 $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 11 my $self = shift;
25            
26 4 50       135 my $event = $self->double_click_nav ? 'dblclick' : 'click';
27            
28 4         178 $self->add_listener( $event => RapidApp::JSONFunc->new( raw => 1, func => 'Ext.ux.RapidApp.AppTab.treenav_click' ) );
29 4         133 $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 28 my $self = shift;
34 13         22 my $nodes = shift;
35            
36 13 100       47 if(ref($nodes) eq 'HASH') {
37 1 50       9 $self->apply_node_navopts_recursive($nodes->{children}) if ($nodes->{children});
38 1         6 return $self->apply_node_navopts($nodes);
39             }
40            
41 12 50       41 return undef unless (ref($nodes) eq 'ARRAY');
42            
43 12         29 foreach my $item (@$nodes) {
44            
45 11 50 33     91 if (ref($item->{children}) eq 'ARRAY' and scalar $item->{children} > 0) {
46 11 50       61 $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       44 $item->{expanded} = \1 if ($item->{expand});
55            
56 11         36 $self->apply_node_navopts($item);
57             }
58            
59 12         27 return $nodes;
60             }
61              
62             sub apply_node_navopts {
63 12     12 0 23 my $self = shift;
64 12         28 my $item = shift;
65            
66 12         26 my $autoLoad = {};
67 12 50       52 $autoLoad->{params} = $item->{params} if ($item->{params});
68 12 50       37 $autoLoad->{url} = $item->{url} if ($item->{url});
69            
70 12         29 my $module = $item->{module};
71 12 50       40 if ($module) {
72 12 50       399 $module = $self->module_scope->Module($item->{module}) unless(ref($module));
73 12         313 $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         30 my $loadCnf = {};
81 12         36 $loadCnf->{itemId} = $item->{itemId};
82 12 50       60 $loadCnf->{itemId} = $item->{id} unless ($loadCnf->{itemId});
83            
84 12   33     71 $loadCnf->{title} = $item->{title} || $item->{text};
85 12         38 $loadCnf->{iconCls} = $item->{iconCls};
86            
87 12         33 $loadCnf->{autoLoad} = $autoLoad;
88 12         68 $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     35 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   35 no Moose;
  4         18  
  4         28  
115             #__PACKAGE__->meta->make_immutable;
116             1;