Navigator.pm

filter: Statementstmt 40.0% 40 / 100 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 10 / 20 Podpod 11.1% 1 / 9 totaltotal 28.0% 51 / 182 CC 23 SCAR 32.0
CC
23
Cov
28%
call
56.0
mk_root_nodes
56.0
frameset
30.0
CRAP 24.6
« Navigator/Node/Perl/Ref.pm ↑ Summary Navigator/Node/Filesys.pm »
1 package Tree::Navigator;
2 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 extends 'Plack::Component';
7  
8 1 ▶use Alien::GvaScript;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Plack::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Tree::Navigator::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = '0.07';
16  
17 1 has root => (
18 is => 'ro',
19 isa => 'Tree::Navigator::Node',
20 handles => [qw/mount children child descendent/],
21 );
22  
23 1 has can_be_killed => (
24 is => 'ro',
25 );
26  
27 0 ▶sub BUILD {
Subroutine BUILD: not called CC=3 SCAR=24.8
Pod undocumented
28 0 my $self = shift;
29 0 my $mount_point = {navigator => $self};
30 0 weaken $mount_point->{navigator};
31  
32 0 ▶ $self->{root} ||= Tree::Navigator::Node->new(
Condition0%0/3
$self->{"root"} ||= "Tree::Navigator::Node"->new("mount_point", $mount_point, "path", "")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"root"} ||= "Tree::Navigator::Node"->new("mount_point", $mount_point, "path", "")
$self->{"root"} "Tree::Navigator::Node"->new("mount_point", $mount_point, "path", "")
Truth table0%0/3
A ||= B
A= $self->{"root"}B= "Tree::Navigator::Node"->new("mount_point", $mount_point, "path", "")
ABresult
1X1
011
000
33 mount_point => $mount_point,
34 path => '',
35 );
36  
37 0 ▶ my $gvascript_dir = Alien::GvaScript->path
Branch: 1 branch
BranchTrueFalseTotal
my $gvascript_dir = "Alien::GvaScript"->path or die "Alien::GvaScript is not installed" 0 0 0
38 or die "Alien::GvaScript is not installed";
39 0 $self->mount('_gva',
40 Filesys => {mount_point => {root => $gvascript_dir}},
41 {hidden => 1});
42 # TODO: options to specify an 'Expires' header
43 }
44  
45  
46 0 ▶sub call { # request dispatcher (see L<Plack::Component>)
Subroutine call: not called CC=7 SCAR=40.3
Pod documented
47 0 my ($self, $env) = @_;
48  
49 0 my $req = Plack::Request->new($env);
50 0 my $path = $req->path;
51 0 $path =~ s[^/][];
52  
53 # URLs for the global frameset and for the Table Of Contents
54 0 ▶ !$path and return $self->frameset($req);
Branch: 1 branch
BranchTrueFalseTotal
$path or return $self->frameset($req) 0 0 0
55 0 ▶ $path =~ s[^_toc/][] and return $self->toc($path, $req);
Branch: 1 branch
BranchTrueFalseTotal
$path =~ s[^_toc/][] and return $self->toc($path, $req) 0 0 0
56  
57 # URL to stop this server (needed by L<Tree::Navigator::App::PerlDebug>)
58 $path =~ /^_KILL/ and $self->can_be_killed
59 0 ▶ and $env->{'psgix.harakiri.commit'} = 1 # see L<HTTP::Server::PSGI> v1.004
Branch: 1 branch
BranchTrueFalseTotal
$path =~ /^_KILL/ and $self->can_be_killed and $env->{"psgix.harakiri.commit"} = 1 and return [200, ["Content-type", "text/html"], ["server killed upon user request"]] 0 0 0
Condition0%0/5
$path =~ /^_KILL/ and $self->can_be_killed and $env->{"psgix.harakiri.commit"} = 1
!ll
00
$path =~ /^_KILL/ and $self->can_be_killed
!ll&&!rl&&r
000
MC/DC0%0/2
$path =~ /^_KILL/ and $self->can_be_killed and $env->{"psgix.harakiri.commit"} = 1
$path =~ /^_KILL/ $self->can_be_killed
Truth table0%0/3
A and B
A= $path =~ /^_KILL/B= $self->can_be_killed
ABresult
0X0
100
111
60 and return [200, ['Content-type' => 'text/html'],
61 ["server killed upon user request"]];
62  
63 # otherwise, other URLs
64 0 ▶ my $node = $self->descendent($path) or die "no such node : $path";
Branch: 1 branch
BranchTrueFalseTotal
my $node = $self->descendent($path) or die "no such node : $path" 0 0 0
65 0 return $node->response($req);
66 }
67  
68  
69  
70 0 ▶sub frameset {
Subroutine frameset: not called CC=5 SCAR=34.0
Pod undocumented
71 0 my ($self, $req) = @_;
72  
73 # initial page to open
74 0 ▶ my $ini = $req->param('open') || $self->{initial_page} || '';
Condition0%0/5
$req->param('open') || $self->{'initial_page'} || ''
l!l
00
$req->param("open") or $self->{"initial_page"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$req->param('open') || $self->{'initial_page'} || ''
$req->param("open") $self->{"initial_page"}
Truth table0%0/3
A or B
A= $req->param("open")B= $self->{"initial_page"}
ABresult
1X1
011
000
75 0 ▶ my $ini_toc = $ini ? "_toc/?open=$ini" : "_toc/";
Branch: 1 branch
BranchTrueFalseTotal
$ini ? : 0 0 0
76  
77 # HTML title
78 0 ▶ my $title = escape_html($self->{title} || 'Tree Navigator');
Condition0%0/2
$self->{'title'} || 'Tree Navigator'
l!l
00
MC/DC0%0/1
$self->{'title'} || 'Tree Navigator'
$self->{'title'}
Truth table0%0/2
A
A= $self->{'title'}
Aresult
11
00
79  
80 0 my $body = <<__EOHTML__;
81 <html>
82 <head><title>$title</title></head>
83 <frameset cols="25%, 75%">
84 <frame name="tocFrame" src="$ini_toc">
85 <frame name="contentFrame" src="$ini">
86 </frameset>
87 </html>
88 __EOHTML__
89  
90 0 return [200, ['Content-type' => 'text/html'], [$body]];
91 }
92  
93  
94 0 ▶sub toc {
Subroutine toc: not called CC=2 SCAR=17.9
Pod undocumented
95 0 my ($self, $path, $req) = @_;
96  
97 0 ▶ return $path ? $self->sub_toc($path, $req)
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 0 0 0
98 : $self->main_toc($req);
99 }
100  
101 0 ▶sub main_toc {
Subroutine main_toc: not called CC=2 SCAR=17.9
Pod undocumented
102 0 my ($self, $req) = @_;
103  
104 0 my $base = $req->script_name;
105 0 my $root_nodes = $self->mk_root_nodes;
106 0 my $resp = $req->new_response(200, [ 'Content-Type' => 'text/html' ]);
107  
108 0 ▶ my $kill_serv = ! $self->can_be_killed ? ""
Branch: 1 branch
BranchTrueFalseTotal
!$self->can_be_killed ? : 0 0 0
109 : q{<a href="/_KILL" style="float:right;color:red">Stop debugging</a>};
110  
111 0 $resp->body(<<__EOHTML__);
112 <html>
113 <head>
114 <base target="contentFrame">
115 <link href="$base/_gva/GvaScript.css" rel="stylesheet" type="text/css">
116 <script src="$base/_gva/prototype.js"></script>
117 <script src="$base/_gva/GvaScript.js"></script>
118 <script>
119 var treeNavigator;
120  
121 function open_nodes(first_node, rest) {
122  
123 var node = \$(first_node);
124 if (!node || !treeNavigator) return;
125  
126 // shift to next node in sequence
127 first_node = rest.shift();
128  
129 // build a handler for "onAfterLoadContent" (closure on first_node/rest)
130 var open_or_select_next = function() {
131  
132 // delete handler that might have been placed by previous call
133 delete treeNavigator.onAfterLoadContent;
134  
135 //
136 if (rest.length > 0) {
137 open_nodes(first_node, rest)
138 }
139 else {
140 treeNavigator.openEnclosingNodes(\$(first_node));
141 treeNavigator.select(\$(first_node));
142 }
143 };
144  
145  
146 // if node is closed and currently has no content, we need to register
147 // a handler, open the node so that it gets its content by Ajax,
148 // and then execute the handler to open the rest after Ajax returns
149 if (treeNavigator.isClosed(node)
150 && !treeNavigator.content(node)) {
151 treeNavigator.onAfterLoadContent = open_or_select_next;
152 treeNavigator.open(node);
153 }
154 // otherwise just a direct call
155 else {
156 open_or_select_next();
157 }
158  
159 }
160  
161 function setup() {
162 treeNavigator
163 = new GvaScript.TreeNavigator('TN_tree', {tabIndex:-1});
164 }
165  
166 document.observe('dom:loaded', setup);
167  
168 function displayContent(event) {
169 var label = event.controller.label(event.target);
170 if (label && label.tagName == "A") {
171 label.focus();
172 return Event. stopNone;
173 }
174 }
175 </script>
176 <style>
177 BODY {margin:0px; font-size: 70%; overflow-x: hidden}
178 DIV {margin:0px; width: 100%}
179 .mount_point {color: midnightblue; font-weight: bold;}
180 </style>
181 </head>
182  
183 <body>
184 $kill_serv
185 <div id='TN_tree' onPing='displayContent'>
186 $root_nodes
187 </div>
188 </body>
189 </html>
190 __EOHTML__
191  
192 0 return $resp->finalize;
193 }
194  
195 0 ▶sub sub_toc {
Subroutine sub_toc: not called CC=2 SCAR=17.9
Pod undocumented
196 0 my ($self, $path, $req) = @_;
197  
198 0 ▶ my $node = $self->descendent($path) or die "no such node : $path";
Branch: 1 branch
BranchTrueFalseTotal
my $node = $self->descendent($path) or die "no such node : $path" 0 0 0
199 0 my $resp = $req->new_response(200);
200 0 $resp->body($self->_TOC_entry($node));
201 0 return $resp->finalize;
202 }
203  
204  
205  
206 1 my $TOC_tmpl = q{
207 [% SET full_path = node.full_path;
208 SET subnodes_and_leaves = node.subnodes_and_leaves;
209 FOREACH subnode IN subnodes_and_leaves.0;
210 SET path = "$full_path/$subnode" | url; %]
211 <div class='TN_node TN_closed' TN:contentURL='[% path %]'>
212 <a href='../[% path %]' class='TN_label'>[% subnode %]</a>
213 </div>
214 [% END; # FOREACH subnode ~%]
215  
216 [% FOREACH leaf IN subnodes_and_leaves.1;
217 SET path = "$full_path/$leaf" | url; %]
218 <div class='TN_leaf'>
219 <a href='../[% path %]' class='TN_label'>[% leaf %]</a>
220 </div>
221 [% END; # FOREACH leaf %]
222 };
223  
224 0 ▶sub _TOC_entry {
Subroutine _TOC_entry: not called CC=1 SCAR=6.9
225 0 my ($self, $node) = @_;
226  
227 0 my $view = $self->view(TT2 => \$TOC_tmpl);
228 0 my $request = undef;
229 0 my $response = $view->render($node, $request);
230 0 my $toc_html = $response->[2][0];
231 0 return $toc_html;
232 }
233  
234  
235  
236 1 my $default_tmpl = q{
237 <head>
238 <style>
239 BODY, TD {
240 font-family: Verdana, Arial, Helvetica;
241 font-size: 85%;
242 }
243 H1, H2, H3, H4, H5, H6 {
244 display:inline;
245 margin: 0;
246 }
247 .attrs TH { text-align: right; padding-right: 1ex}
248 .attrs TH, TD { font-size: 80%; }
249 .highlight { background: lightgreen }
250 </style>
251 [% SET base = request.script_name %]
252 <link href="[% base %]/_gva/GvaScript.css" rel="stylesheet" type="text/css">
253 <script src="[% base %]/_gva/prototype.js"></script>
254 <script src="[% base %]/_gva/GvaScript.js"></script>
255 <script>
256 var treeNavigator;
257 function setup() {
258 treeNavigator
259 = new GvaScript.TreeNavigator('TN_tree', {tabIndex:-1});
260 }
261 document.observe('dom:loaded', setup);
262  
263  
264 function follow_link(event) {
265 var label = event.controller.label(event.target);
266 if (label && label.tagName == "A") {
267 label.focus();
268 return Event. stopNone;
269 }
270 }
271  
272 </script>
273 </head>
274 <body>
275 <div id='TN_tree' onPing='follow_link'>
276 <div class='TN_node'>
277 <h1 class="TN_label">[% node.full_path %]</h1>
278 <div class="TN_content">
279  
280 [% IF data.attributes.size %]
281 <div class="TN_node">
282 <h2 class="TN_label">Attributes</h2>
283 <div class="TN_content">
284 <em>[% INCLUDE attrs attrs=data.attributes %]</em>
285 </div>
286 </div>
287 [% END; # IF data.attributes.size %]
288  
289 [% IF data.children.size %]
290 <div class="TN_node">
291 <h2 class="TN_label">Children</h2>
292 <div class="TN_content">
293 [% INCLUDE child FOREACH child IN data.children %]
294 </div>
295 </div>
296 [% END; # IF data.children.size %]
297  
298 [% IF data.content_text %]
299 <div class="TN_node">
300 <h2 class="TN_label">Content</h2>
301 <div class="TN_content">
302 <pre>
303 [%- data.content_text -%]
304 </pre>
305 </div>
306 </div>
307 [% END; # IF data.content %]
308  
309 </div>
310 </div>
311 </div>
312 </body>
313  
314 [%~ BLOCK child; %]
315 <div class="[% child.attributes.size ? 'TN_node TN_closed' : 'TN_leaf' %]">
316 <a class="TN_label"
317 href="[% node.last_path _ '/' _ child.name | url %]">
318 [%~ child.name ~%]
319 </a>
320 [% IF child.attributes.size %]
321 <div class="TN_content">
322 [% INCLUDE attrs attrs=child.attributes %]
323 </div>
324 [% END; # IF child.attributes.size %]
325 </div>
326 [%~ END; # BLOCK child; %]
327  
328 [%~ BLOCK attrs;
329 IF attrs.size; %]
330 <table class="attrs">
331 [% FOREACH attr IN attrs; %]
332 <tr><th>[% attr.key %]</th><td>[% attr.value %]</td></tr>
333 [% END; # FOREACH attr IN attrs; %]
334 </table>
335 [% END; # IF
336 END; # BLOCK ~%]
337 };
338  
339  
340  
341  
342  
343  
344  
345  
346 0 ▶sub mk_root_nodes {
Subroutine mk_root_nodes: not called CC=7 SCAR=40.3
Pod undocumented
347 0 my ($self) = @_;
348  
349 0 my $html = "";
350  
351 0 foreach my $path ($self->children) {
352 0 ▶ my $node = $self->child($path) or die "absent root: '$path'";
Branch: 1 branch
BranchTrueFalseTotal
my $node = $self->child($path) or die "absent root: '${path}'" 0 0 0
353 0 ▶ my $title = escape_html($node->attributes->{title} || '');
Condition0%0/2
$node->attributes->{'title'} || ''
l!l
00
MC/DC0%0/1
$node->attributes->{'title'} || ''
$node->attributes->{'title'}
Truth table0%0/2
A
A= $node->attributes->{'title'}
Aresult
11
00
354 0 ▶ $title = " title='$title'" if $title;
Branch: 1 branch
BranchTrueFalseTotal
if $title 0 0 0
355  
356 0 my $node_content = $self->_TOC_entry($node);
357 0 my $node_html = "<a href='../$path' class='TN_label mount_point'$title>$path</a>";
358 0 ▶ $node_html .= "<div class='TN_content'>$node_content</div>" if $node_content;
Branch: 1 branch
BranchTrueFalseTotal
if $node_content 0 0 0
359 0 $html .= "<div class='TN_node'>$node_html</div>";
360 }
361  
362 0 ▶ $html or die "no mounted nodes";
Branch: 1 branch
BranchTrueFalseTotal
$html or die "no mounted nodes" 0 0 0
363  
364 0 return $html;
365 }
366  
367  
368  
369 #======================================================================
370 # UTILITIES
371 #======================================================================
372  
373 1 my %escape_entity = ('&' => 'amp',
374 '<' => 'lt',
375 '>' => 'gt',
376 '"' => 'quot');
377  
378 1 my $entity_regex = "([" . join("", keys %escape_entity) . "])";
379 0 ▶sub escape_html {
Subroutine escape_html: not called CC=1 SCAR=6.9
Pod undocumented
380 0 my $html = shift;
381 0 $html =~ s/$entity_regex/&$escape_entity{$1};/g;
382 0 return $html;
383 }
384  
385 #======================================================================
386 # WORK IN PROGRESS
387 #======================================================================
388  
389 0 ▶sub view {
Subroutine view: not called CC=2 SCAR=17.9
Pod undocumented
390 0 my $self = shift;
391 0 ▶ my ($view_class, @args) = @_ ? @_ : (TT2 => \$default_tmpl);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
392  
393 0 my $class = Plack::Util::load_class($view_class, "Tree::Navigator::View");
394 0 return $class->new(@args);
395 }
396  
397  
398 1 __PACKAGE__->meta->make_immutable;
399  
400  
401  
402 1 1; # End of Tree::Navigator
403  
404 __END__
« Navigator/Node/Perl/Ref.pm ↑ Summary Navigator/Node/Filesys.pm »