Modules/Doc: Use Icinga\Web\Menu for the toc

refs #4820
This commit is contained in:
Eric Lippmann 2014-02-05 12:35:44 +01:00
parent 88e13c378d
commit f96974fc79
10 changed files with 117 additions and 58 deletions

View File

@ -0,0 +1,19 @@
<?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
use Icinga\Web\Controller\ActionController;
use Icinga\Web\Menu;
use Icinga\Web\Url;
class LayoutController extends ActionController
{
public function menuAction()
{
$this->view->url = Url::fromRequest()->getRelativeUrl();
$this->view->items = Menu::fromConfig()->getChildren();
$this->view->sub = false;
}
}
// @codingStandardsIgnoreEnd

View File

@ -1,9 +1,12 @@
<?php echo $this->render('parts/topbar.phtml') ?> <?php echo $this->render('parts/topbar.phtml') ?>
<div class="row"> <div class="row">
<!-- Only required for left/right tabs --> <!-- Only required for left/right tabs -->
<div class="col-sm-12 col-xs-12 col-md-2 col-lg-2" id="menu"> <div class="col-sm-12 col-xs-12 col-md-2 col-lg-2">
<?php echo $this->render('parts/navigation.phtml') ?> <?= $this->partial('layout/menu.phtml', 'default', array(
'url' => \Icinga\Web\Url::fromRequest()->getRelativeUrl(),
'items' => \Icinga\Web\Menu::fromConfig()->getChildren(),
'sub' => false
)); ?>
</div> </div>
<div class="col-sm-12 col-xs-12 col-md-10 col-lg-10"> <div class="col-sm-12 col-xs-12 col-md-10 col-lg-10">

View File

@ -1,28 +0,0 @@
<ul>
<?php
foreach ($this->items as $item) {
printf(
' <li%s><a href="%s">%s%s</a>',
$this->href($this->url) === $this->href($item->getUrl()) ? ' class="active"' : '',
$item->getUrl() ? $this->href($item->getUrl()) : '#',
$item->getIcon() ? $this->img(
$item->getIcon(),
array('height' => '16px', 'width' => '16px')
) . ' ' : '',
$item->getTitle()
);
if ($item->hasChildren()) {
echo $this->partial(
'parts/menu.phtml',
array('items' => $item->getChildren(), 'url' => $this->url)
);
}
echo "</li>\n";
}
?>
</ul>

View File

@ -1,7 +1,5 @@
<?php <?php
// determine current key
$url = Icinga\Web\Url::fromRequest()->getRelativeUrl();
$menu = Icinga\Web\Menu::fromConfig();
if ($this->auth()->isAuthenticated()) { if ($this->auth()->isAuthenticated()) {
echo $this->partial( echo $this->partial(

View File

@ -0,0 +1,40 @@
<ul>
<?= $sub ? '<ul>' : '<ul class="nav nav-stacked" role="navigation" id="icinganavigation">' ?>
<?php foreach ($items as $item): ?>
<?php
$itemClass = '';
if ($sub) {
$itemClass .= 'submenu ';
}
if ($this->href($url) === $this->href($item->getUrl())) {
$itemClass .= 'active ';
}
?>
<li <?php if (!empty($itemClass)): ?>class="<?= $itemClass ?>"<?php endif ?>>
<?php if($item->getUrl()): ?>
<a href="<?= $this->href($item->getUrl()); ?>">
<?php endif; ?>
<?php
if ($icon = $item->getIcon()) {
echo $this->img($icon, array('height' => '16px', 'width' => '16px'));
}
?>
<?php if ($iconClass = $item->getIconClass()): ?>
<i class="<?= $iconClass ?>"></i>
<?php endif ?>
<?= $item->getTitle();?>
<?php if($item->getUrl()): ?>
</a>
<?php endif; ?>
<?php
if($item->hasChildren()) {
echo $this->partial(
'layout/menu.phtml',
'default',
array('items' => $item->getChildren(), 'sub' => true, 'url' => $this->url)
);
}
?>
</li>
<?php endforeach ?>
</ul>

View File

@ -3,13 +3,16 @@
<?php else: ?> <?php else: ?>
<div class="row"> <div class="row">
<div class="col-sm-12 col-xs-12 col-md-2 col-lg-2"> <div class="col-sm-12 col-xs-12 col-md-2 col-lg-2">
<ul class="nav nav-pills nav-stacked" role="navigation"> <?= $this->partial(
<?php foreach ($toc as $i): ?> 'layout/menu.phtml',
<li class="toc-h<?= $i['level'] ?>"> 'default',
<a href="#<?= $i['fragment'] ?>"><?= $this->escape($i['heading']); ?></a> array(
</li> 'items' => $toc->getChildren(),
<?php endforeach; ?> 'sub' => false,
</ul> 'url' => ''
)
);
?>
</div> </div>
<div class="col-sm-12 col-xs-12 col-md-10 col-lg-10"> <div class="col-sm-12 col-xs-12 col-md-10 col-lg-10">
<?= $html ?> <?= $html ?>

View File

@ -6,6 +6,7 @@ namespace Icinga\Module\Doc;
use Icinga\Module\Doc\DocParser; use Icinga\Module\Doc\DocParser;
use Icinga\Web\Controller\ActionController; use Icinga\Web\Controller\ActionController;
use Icinga\Web\Menu;
class Controller extends ActionController class Controller extends ActionController
{ {

View File

@ -6,10 +6,12 @@ namespace Icinga\Module\Doc;
require_once 'vendor/Parsedown/Parsedown.php'; require_once 'vendor/Parsedown/Parsedown.php';
use \Exception;
use \SplStack;
use \RecursiveIteratorIterator; use \RecursiveIteratorIterator;
use \RecursiveDirectoryIterator; use \RecursiveDirectoryIterator;
use \Exception;
use \Parsedown; use \Parsedown;
use Icinga\Web\Menu;
/** /**
* Parser for documentation written in Markdown * Parser for documentation written in Markdown
@ -33,8 +35,9 @@ class DocParser
); );
$fileInfos = iterator_to_array($iter); $fileInfos = iterator_to_array($iter);
natcasesort($fileInfos); natcasesort($fileInfos);
$cat = array(); $cat = array();
$toc = array(); $toc = new Menu('doc');
$stack = new SplStack();
foreach ($fileInfos as $fileInfo) { foreach ($fileInfos as $fileInfo) {
try { try {
$fileObject = $fileInfo->openFile(); $fileObject = $fileInfo->openFile();
@ -52,14 +55,24 @@ class DocParser
) { ) {
// Atx-style // Atx-style
$level = strlen($match[0]); $level = strlen($match[0]);
$heading = trim(strip_tags(substr($line, $level))); $heading = str_replace('.', '&#46;', trim(strip_tags(substr($line, $level))));
$fragment = urlencode($heading); $fragment = urlencode($heading);
$toc[] = array( $line = '<span id="' . $fragment . '">' . "\n" . $line;
'heading' => $heading, $stack->rewind();
'level' => $level, while ($stack->valid() && $stack->current()->level >= $level) {
'fragment' => $fragment $stack->pop();
); $stack->next();
$line = '<span id="' . $fragment . '">' . "\n" . $line; }
$parent = $stack->current();
if ($parent === null) {
$item = $toc->addChild($heading, array('url' => '#' . $fragment));
} else {
$item = $parent->item->addChild($heading, array('url' => '#' . $fragment));
}
$stack->push((object) array(
'level' => $level,
'item' => $item
));
} elseif ( } elseif (
$line && $line &&
($line[0] === '=' || $line[0] === '-') && ($line[0] === '=' || $line[0] === '-') &&
@ -70,17 +83,27 @@ class DocParser
// H1 // H1
$level = 1; $level = 1;
} else { } else {
// H2 // H
$level = 2; $level = 2;
} }
$heading = trim(strip_tags(end($cat))); $heading = trim(strip_tags(end($cat)));
$fragment = urlencode($heading); $fragment = urlencode($heading);
$toc[] = array( $line = '<span id="' . $fragment . '">' . "\n" . $line;
'heading' => $heading, $stack->rewind();
'level' => $level, while ($stack->valid() && $stack->current()->level >= $level) {
'fragment' => $fragment $stack->pop();
); $stack->next();
$line = '<span id="' . $fragment . '">' . "\n" . $line; }
$parent = $stack->current();
if ($parent === null) {
$item = $toc->addChild($heading, array('url' => '#' . $fragment));
} else {
$item = $parent->item->addChild($heading, array('url' => '#' . $fragment));
}
$stack->push((object) array(
'level' => $level,
'item' => $item
));
} }
$cat[] = $line; $cat[] = $line;
} }