Skip to content

Commit

Permalink
Added breadcrumb navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Perkins committed Nov 10, 2014
1 parent 0dc552a commit d13ea8e
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 7 deletions.
30 changes: 28 additions & 2 deletions src/Vube/VagrantCatalog/Catalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,28 @@ public function execCatalogRoute()
return $result;
}

public function computeRelativePathInfo($pathInfo)
{
// pathInfo with no leading slash (possibly empty string)
$relativePathInfo = preg_replace('%^/+%', '', $pathInfo);
return $relativePathInfo;
}

public function computeBreadcrumb($relativePathInfo)
{
$relativePathParts = array();
if($relativePathInfo != '')
$relativePathParts = explode('/', $relativePathInfo);
$backDirs = array();
for($i=0; $i<count($relativePathParts); $i++)
{
$slice = array_slice($relativePathParts, 0, $i+1);
$backdir = implode('/', $slice);
$backDirs[$backdir] = $relativePathParts[$i];
}
return $backDirs;
}

public function execIndexRoute()
{
$currentDir = $this->config['metadata-root'] . $this->pathInfo;
Expand All @@ -298,8 +320,12 @@ public function execIndexRoute()
$smarty->assign('CATALOG_URI', $this->baseUri.'/'.$this->config['catalog-uri']);

$smarty->assign('pathInfo', $this->pathInfo);
// pathInfo with no leading slash (possibly empty string)
$smarty->assign('relativePathInfo', preg_replace('%^/+%', '', $this->pathInfo));

$relativePathInfo = $this->computeRelativePathInfo($this->pathInfo);
$smarty->assign('relativePathInfo', $relativePathInfo);

$backDirs = $this->computeBreadcrumb($relativePathInfo);
$smarty->assign('relativeBackDirs', $backDirs);

$scanner = new DirectoryScan($currentDir);
$dirInfo = $scanner->scan();
Expand Down
36 changes: 31 additions & 5 deletions templates/index.tpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
<html>
<head>
<title>Vagrant Catalog</title>
<style type="text/css">
body { color: #333 }
a { color: #2a97ce }
a:visited { color: #2a97ce }
pre { background-color: #eee }
</style>
</head>
<body>
<h1>Vagrant Catalog</h1>
<h2>{$relativePathInfo|escape}</h2>
<h1>
<a href="{$BASE_URI|escape}/">Vagrant Catalog</a>
{if $relativeBackDirs|@count gt 0}:{/if}
{foreach from=$relativeBackDirs key=path item=subdir name=breadcrumb}
{if $smarty.foreach.breadcrumb.last}
{$subdir|escape}
{else}
<a href="{$BASE_URI|escape}/{$path|escape}">{$subdir|escape}</a> /
{/if}
{/foreach}
</h1>

{if $boxes|@count gt 0}
<h3>Boxes</h3>
<h2>Boxes</h2>
<ul>
{foreach $boxes as $file}
<li><a href="{$BASE_URI|escape}{$pathInfo|escape}/{$file|escape}">{$relativePathInfo|escape}{if $relativePathInfo ne ''}/{/if}{$file|escape}</a></li>
Expand All @@ -16,7 +31,7 @@
</ul>

{if $directories|@count gt 0}
<h3>Sub-directories</h3>
<h2>Sub-directories</h2>
<ul>
{foreach $directories as $dir}
<li><a href="{$BASE_URI|escape}{$pathInfo|escape}/{$dir|escape}">{$dir|escape}</a></li>
Expand All @@ -25,7 +40,18 @@
{/if}

{if $metadata !== null}
<h3><a href="{$CATALOG_URI|escape}{$pathInfo|escape}">{$CATALOG_URI|escape}{$pathInfo|escape}</a></h3>
<h2>Vagrant config</h2>
<p><pre>
Vagrant.configure(2) do |config|

config.vm.box = "{$relativePathInfo|escape}"
config.vm.box_url = '<a href="{$CATALOG_URI|escape}{$pathInfo|escape}">{$CATALOG_URI|escape}{$pathInfo|escape}</a>'

# Whatever other config stuff you want to do
end
</pre></p>

<h3>Metadata</h3>
<pre>{$metadata|escape}</pre>
{/if}

Expand Down
51 changes: 51 additions & 0 deletions test/unit/Vube/VagrantCatalog/CatalogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,56 @@ public function testExecSubdirPathInfo()

$this->assertSame($expected, $result['content']);
}

public function testComputeRelativePathInfoEmpty()
{
$tests = array(
// path_info expected_result
array('', ''),
array('/', ''),
array('/foo', 'foo'),
array('/foo/bar', 'foo/bar'),
);

$catalog = $this->constructCatalog("http://localhost/base/catalog/foo", "/base");
$catalog->init();

foreach ($tests as $test)
{
$pathInfo = $test[0];
$expected = $test[1];

$actual = $catalog->computeRelativePathInfo($pathInfo);
$this->assertSame($expected, $actual, "Path Info '$pathInfo' should return '$expected'");
}
}

public function testComputeBreadcrumb()
{
$tests = array(
array('', array()),
array('foo', array(
'foo' => 'foo'
)),
array('foo/bar', array(
'foo' => 'foo',
'foo/bar' => 'bar',
)),
);

$catalog = $this->constructCatalog("http://localhost/base/catalog/foo", "/base");
$catalog->init();

foreach ($tests as $test)
{
$relativePathInfo = $test[0];
$expected = $test[1];
$actual = $catalog->computeBreadcrumb($relativePathInfo);

$this->assertEquals($expected, $actual, "Relative Path Info '$relativePathInfo' should return '".
var_export($expected,true)."'");
}
}

}

0 comments on commit d13ea8e

Please sign in to comment.