| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of Config-Model-TkUI | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is Copyright (c) 2008-2021 by Dominique Dumont. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | package Config::Model::Tk::Filter 1.382; | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use 5.10.1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Log::Log4perl 1.11; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 19 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 20 | 1 | our @EXPORT_OK = qw/apply_filter/; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | my $logger = Log::Log4perl::get_logger('Tk::Filter'); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 146 | ▶ | sub _get_filter_action { | ||||||||||||||||||||||||||
|
Subroutine
_get_filter_action: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 25 | 146 | my ($elt, $elt_filter, $filtered_state, $unfiltered_state, $default_state) = @_; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 146 | my $action = $default_state; | |||||||||||||||||||||||||||
| 28 | # filter is active only when the filter string is 3 or more chars long | ||||||||||||||||||||||||||||
| 29 | 146 | ▶ | if (length($elt_filter) > 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 92 | ▶ | if ($elt =~ /$elt_filter/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 12 | $action = $filtered_state ; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | else { | ||||||||||||||||||||||||||||
| 34 | 80 | $action = $unfiltered_state; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 146 | return $action; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # scan a tree and return a hash where each found path is the key and the | ||||||||||||||||||||||||||||
| 41 | # value is either hide, show or an empty string. | ||||||||||||||||||||||||||||
| 42 | 12 | ▶ | sub apply_filter { | ||||||||||||||||||||||||||
|
Subroutine
apply_filter: called CC=7 SCAR=20.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 12 | my (%args) = @_; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # fd_path: force display path. The show action must be set in the | ||||||||||||||||||||||||||||
| 46 | # call back so that the 'show' action can be propagated from the | ||||||||||||||||||||||||||||
| 47 | # shown leaf up to the root of the tree. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 12 | ▶ | my $show_only_custom = $args{show_only_custom} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"show_only_custom"} // 0
MC/DC100%1/1
$args{"show_only_custom"} // 0
$args{"show_only_custom"}
Truth table100%2/2
A
A= $args{"show_only_custom"}
| |||||||||||||||||||||||||||||
| 50 | 12 | ▶ | my $hide_empty_values = $args{hide_empty_values} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"hide_empty_values"} // 0
MC/DC100%1/1
$args{"hide_empty_values"} // 0
$args{"hide_empty_values"}
Truth table100%2/2
A
A= $args{"hide_empty_values"}
| |||||||||||||||||||||||||||||
| 51 | 12 | ▶ | my $instance = $args{instance} // carp "missing instance" ; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{"instance"} // carp("missing instance")
MC/DC0%0/2
$args{"instance"} // carp("missing instance")
$args{"instance"}
carp("missing instance")
Truth table33%1/3
A // B
A= $args{"instance"}B= carp("missing instance")
| |||||||||||||||||||||||||||||
| 52 | 12 | ▶ | my $actions = $args{actions} // carp "missing actions ref" ; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{"actions"} // carp("missing actions ref")
MC/DC0%0/2
$args{"actions"} // carp("missing actions ref")
$args{"actions"}
carp("missing actions ref")
Truth table33%1/3
A // B
A= $args{"actions"}B= carp("missing actions ref")
| |||||||||||||||||||||||||||||
| 53 | 12 | ▶ | my $fd_path = $args{fd_path} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"fd_path"} // ""
MC/DC0%0/1
$args{"fd_path"} // ""
$args{"fd_path"}
Truth table50%1/2
A
A= $args{"fd_path"}
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # flush the hash, but keep the ref | ||||||||||||||||||||||||||||
| 56 | 12 | %$actions = (); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # actions need 3 state logic. '' means that tree node is left as | ||||||||||||||||||||||||||||
| 59 | # is, either closed or opened, depending on user's choice. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # 'show' trumps '' which trumps 'hide' | ||||||||||||||||||||||||||||
| 62 | 12 | my %combine_actions = ( | |||||||||||||||||||||||||||
| 63 | show => { show => 'show', '' => 'show', hide => 'show'}, | ||||||||||||||||||||||||||||
| 64 | '' => { show => 'show', '' => '' , hide => '' }, | ||||||||||||||||||||||||||||
| 65 | hide => { show => 'show', '' => '' , hide => 'hide'}, | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 62 | ▶ | my $leaf_cb = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 69 | 62 | my ($scanner, $data_ref, $node,$element_name,$index, $leaf_object) = @_ ; | |||||||||||||||||||||||||||
| 70 | 62 | my $loc = $leaf_object->location; | |||||||||||||||||||||||||||
| 71 | 62 | my $action = ''; | |||||||||||||||||||||||||||
| 72 | 62 | ▶ | if ($show_only_custom) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 11 | ▶ | $action = $leaf_object->has_data ? 'show' : 'hide'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 62 | ▶ | if ( $hide_empty_values ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 20 | my $v = $leaf_object->fetch(qw/check no/); | |||||||||||||||||||||||||||
| 77 | 20 | ▶ | $action = 'hide' unless (defined $v and length($v)) ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $v and length $v
MC/DC50%1/2
defined $v and length $v
defined $v
length $v
Truth table66%2/3
A and B
A= defined $vB= length $v
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 62 | ▶ | $action = 'show' if $loc eq $fd_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 62 | $logger->trace("leaf '$loc' filter is '$action'"); | |||||||||||||||||||||||||||
| 81 | 62 | $data_ref->{return} = $data_ref->{actions}{$loc} = $action ; | |||||||||||||||||||||||||||
| 82 | 12 | }; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $check_list_cb = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 85 | 0 | my ($scanner, $data_ref,$node,$element_name,undef, $obj) = @_; | |||||||||||||||||||||||||||
| 86 | 0 | my $loc = $obj->location; | |||||||||||||||||||||||||||
| 87 | 0 | my $action = ''; | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ( $hide_empty_values and not $obj->fetch()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$hide_empty_values and not $obj->fetch
MC/DC0%0/2
$hide_empty_values and not $obj->fetch
$hide_empty_values
not $obj->fetch
Truth table0%0/3
A and not(B)
A= $hide_empty_valuesB= not $obj->fetch
| |||||||||||||||||||||||||||||
| 89 | 0 | $action = 'hide'; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $action = 'show' if $loc eq $fd_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | $logger->trace("check_list '$loc' filter is '$action'"); | |||||||||||||||||||||||||||
| 93 | 0 | $data_ref->{return} = $data_ref->{actions}{$loc} = $action ; | |||||||||||||||||||||||||||
| 94 | 12 | }; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 60 | ▶ | my $hash_cb = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 97 | 60 | my ($scanner, $data_ref,$node,$element_name,@keys) = @_ ; | |||||||||||||||||||||||||||
| 98 | 60 | my $obj = $node->fetch_element($element_name); | |||||||||||||||||||||||||||
| 99 | 60 | my $loc = $obj->location; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # resume exploration | ||||||||||||||||||||||||||||
| 102 | 60 | ▶ | my $hash_action = $hide_empty_values || $show_only_custom ? 'hide' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$hide_empty_values or $show_only_custom
MC/DC100%2/2
$hide_empty_values or $show_only_custom
$hide_empty_values
$show_only_custom
Truth table100%3/3
A or B
A= $hide_empty_valuesB= $show_only_custom
| |||||||||||||||||||||||||||||
| 103 | 60 | foreach my $key (@keys) { | |||||||||||||||||||||||||||
| 104 | 13 | my $inner_ref = { actions => $data_ref->{actions}, filter => $data_ref->{filter} }; | |||||||||||||||||||||||||||
| 105 | 13 | $scanner->scan_hash($inner_ref, $node, $element_name, $key); | |||||||||||||||||||||||||||
| 106 | 13 | $hash_action = $combine_actions{$hash_action}{$inner_ref->{return}}; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 60 | ▶ | $hash_action = 'show' if $loc eq $fd_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 60 | $logger->trace("hash '$loc' filter is '$hash_action'"); | |||||||||||||||||||||||||||
| 110 | 60 | $data_ref->{return} = $data_ref->{actions}{$loc} = $hash_action; | |||||||||||||||||||||||||||
| 111 | 12 | }; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 49 | ▶ | my $node_cb = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 114 | 49 | my ($scanner, $data_ref,$node, @element_list) = @_ ; | |||||||||||||||||||||||||||
| 115 | 49 | my $node_loc = $node->location; | |||||||||||||||||||||||||||
| 116 | 49 | my $elt_filter = $data_ref->{filter}; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 49 | ▶ | my $all_elt_action = $show_only_custom ? 'hide' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 49 | ▶ | my $default_action = (length($elt_filter) > 3) ? 'hide' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 49 | foreach my $elt ( @element_list ) { | |||||||||||||||||||||||||||
| 122 | 146 | my $filter_action = _get_filter_action($elt,$elt_filter,'show','hide',''); | |||||||||||||||||||||||||||
| 123 | 146 | my $obj = $node->fetch_element($elt); | |||||||||||||||||||||||||||
| 124 | 146 | my $loc = $obj->location; | |||||||||||||||||||||||||||
| 125 | # make sure that the hash ref stays attached to $data_ref | ||||||||||||||||||||||||||||
| 126 | 146 | ▶ | $data_ref->{actions} //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$data_ref->{"actions"} //= {}
MC/DC0%0/1
$data_ref->{"actions"} //= {}
$data_ref->{"actions"}
Truth table50%1/2
A
A= $data_ref->{"actions"}
| |||||||||||||||||||||||||||||
| 127 | 146 | my $elt_action; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 146 | ▶ | if ($filter_action eq 'show') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | my $inner_ref = { | ||||||||||||||||||||||||||||
| 131 | actions => $data_ref->{actions}, | ||||||||||||||||||||||||||||
| 132 | # stop filter propagation when current element is | ||||||||||||||||||||||||||||
| 133 | # shown so all elements below can be shown or hidden | ||||||||||||||||||||||||||||
| 134 | # at user's choice | ||||||||||||||||||||||||||||
| 135 | 12 | filter => '' | |||||||||||||||||||||||||||
| 136 | }; | ||||||||||||||||||||||||||||
| 137 | 12 | $scanner->scan_element($inner_ref, $node,$elt); | |||||||||||||||||||||||||||
| 138 | # this clobbers the elt_action computed in leaf | ||||||||||||||||||||||||||||
| 139 | 12 | $elt_action = $data_ref->{actions}{$loc} = $filter_action; | |||||||||||||||||||||||||||
| 140 | 12 | $logger->trace("node '$loc' elt $elt filter is active '$elt_action'"); | |||||||||||||||||||||||||||
| 141 | } else { | ||||||||||||||||||||||||||||
| 142 | 134 | my $inner_ref = { actions => $data_ref->{actions}, filter => $data_ref->{filter} }; | |||||||||||||||||||||||||||
| 143 | 134 | $scanner->scan_element($inner_ref, $node,$elt); | |||||||||||||||||||||||||||
| 144 | 134 | ▶ | $elt_action = $data_ref->{actions}{$loc} = $inner_ref->{return} || $default_action; | ||||||||||||||||||||||||||
|
Condition100%3/3
$inner_ref->{'return'} || $default_action
MC/DC100%2/2
$inner_ref->{'return'} || $default_action
$inner_ref->{'return'}
$default_action
Truth table100%3/3
A || B
A= $inner_ref->{'return'}B= $default_action
| |||||||||||||||||||||||||||||
| 145 | 134 | $logger->trace("node '$loc' elt $elt filter is not active '$elt_action'"); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 146 | $all_elt_action = $combine_actions{$all_elt_action}{$elt_action}; | |||||||||||||||||||||||||||
| 149 | 146 | $logger->trace("node '$loc' elt $elt all_elt_action is '$all_elt_action'"); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 49 | ▶ | my $node_action = $node_loc eq $fd_path ? 'show' : $all_elt_action; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 49 | $logger->trace("node '$node_loc' filter is '$node_action'"); | |||||||||||||||||||||||||||
| 155 | 49 | $data_ref->{return} = $data_ref->{actions}{$node_loc} = $node_action; | |||||||||||||||||||||||||||
| 156 | 12 | }; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 12 | my $scan = Config::Model::ObjTreeScanner-> new ( | |||||||||||||||||||||||||||
| 159 | leaf_cb => $leaf_cb, | ||||||||||||||||||||||||||||
| 160 | hash_element_cb => $hash_cb, | ||||||||||||||||||||||||||||
| 161 | list_element_cb => $hash_cb, | ||||||||||||||||||||||||||||
| 162 | node_content_cb => $node_cb, | ||||||||||||||||||||||||||||
| 163 | ) ; | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | my $data_ref = { | ||||||||||||||||||||||||||||
| 166 | 12 | ▶ | filter => $args{elt_filter_value} // '', | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"elt_filter_value"} // ""
MC/DC100%1/1
$args{"elt_filter_value"} // ""
$args{"elt_filter_value"}
Truth table100%2/2
A
A= $args{"elt_filter_value"}
| |||||||||||||||||||||||||||||
| 167 | actions => $actions, | ||||||||||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||||||||||
| 169 | 12 | $scan->scan_node($data_ref, $instance->config_root) ; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 1 | 1; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||