From ae2c62fad1d748c2546495d30b23830049ae5d73 Mon Sep 17 00:00:00 2001 From: jaugustin Date: Sat, 23 Aug 2014 16:57:40 +0200 Subject: [PATCH] add show/hide button and list/expand operations buttons on sections add a new parameter default_sections_opened: false (default) To start with sections opened or closed --- DependencyInjection/Configuration.php | 1 + DependencyInjection/NelmioApiDocExtension.php | 1 + Formatter/HtmlFormatter.php | 42 ++++++++++++------- Resources/config/formatters.xml | 3 ++ Resources/doc/index.md | 1 + Resources/public/css/screen.css | 35 ++++++++++++++-- Resources/views/layout.html.twig | 35 ++++++++++++++++ Resources/views/resources.html.twig | 9 +++- 8 files changed, 107 insertions(+), 20 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 55cac15..36af70a 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -27,6 +27,7 @@ class Configuration implements ConfigurationInterface ->prototype('scalar') ->end() ->end() + ->booleanNode('default_sections_opened')->defaultValue(true)->end() ->arrayNode('motd') ->addDefaultsIfNotSet() ->children() diff --git a/DependencyInjection/NelmioApiDocExtension.php b/DependencyInjection/NelmioApiDocExtension.php index 4f07acd..87da66e 100644 --- a/DependencyInjection/NelmioApiDocExtension.php +++ b/DependencyInjection/NelmioApiDocExtension.php @@ -31,6 +31,7 @@ class NelmioApiDocExtension extends Extension $container->setParameter('nelmio_api_doc.motd.template', $config['motd']['template']); $container->setParameter('nelmio_api_doc.exclude_sections', $config['exclude_sections']); + $container->setParameter('nelmio_api_doc.default_sections_opened', $config['default_sections_opened']); $container->setParameter('nelmio_api_doc.api_name', $config['name']); $container->setParameter('nelmio_api_doc.sandbox.enabled', $config['sandbox']['enabled']); $container->setParameter('nelmio_api_doc.sandbox.endpoint', $config['sandbox']['endpoint']); diff --git a/Formatter/HtmlFormatter.php b/Formatter/HtmlFormatter.php index 3b0b00c..94ab16f 100644 --- a/Formatter/HtmlFormatter.php +++ b/Formatter/HtmlFormatter.php @@ -75,6 +75,11 @@ class HtmlFormatter extends AbstractFormatter */ private $motdTemplate; + /** + * @var boolean + */ + private $defaultSectionsOpened; + /** * @param array $authentication */ @@ -179,6 +184,14 @@ class HtmlFormatter extends AbstractFormatter return $this->motdTemplate; } + /** + * @param boolean $defaultSectionsOpened + */ + public function setDefaultSectionsOpened($defaultSectionsOpened) + { + $this->defaultSectionsOpened = $defaultSectionsOpened; + } + /** * {@inheritdoc} */ @@ -212,20 +225,21 @@ class HtmlFormatter extends AbstractFormatter private function getGlobalVars() { return array( - 'apiName' => $this->apiName, - 'authentication' => $this->authentication, - 'endpoint' => $this->endpoint, - 'enableSandbox' => $this->enableSandbox, - 'requestFormatMethod' => $this->requestFormatMethod, - 'acceptType' => $this->acceptType, - 'bodyFormats' => $this->bodyFormats, - 'defaultBodyFormat' => $this->defaultBodyFormat, - 'requestFormats' => $this->requestFormats, - 'defaultRequestFormat' => $this->defaultRequestFormat, - 'date' => date(DATE_RFC822), - 'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'), - 'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'), - 'motdTemplate' => $this->motdTemplate + 'apiName' => $this->apiName, + 'authentication' => $this->authentication, + 'endpoint' => $this->endpoint, + 'enableSandbox' => $this->enableSandbox, + 'requestFormatMethod' => $this->requestFormatMethod, + 'acceptType' => $this->acceptType, + 'bodyFormats' => $this->bodyFormats, + 'defaultBodyFormat' => $this->defaultBodyFormat, + 'requestFormats' => $this->requestFormats, + 'defaultRequestFormat' => $this->defaultRequestFormat, + 'date' => date(DATE_RFC822), + 'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'), + 'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'), + 'motdTemplate' => $this->motdTemplate, + 'defaultSectionsOpened' => $this->defaultSectionsOpened, ); } } diff --git a/Resources/config/formatters.xml b/Resources/config/formatters.xml index 832a33c..6aafe75 100644 --- a/Resources/config/formatters.xml +++ b/Resources/config/formatters.xml @@ -56,6 +56,9 @@ %nelmio_api_doc.sandbox.authentication% + + %nelmio_api_doc.default_sections_opened% + diff --git a/Resources/doc/index.md b/Resources/doc/index.md index 39ee9ce..ae551b8 100644 --- a/Resources/doc/index.md +++ b/Resources/doc/index.md @@ -439,6 +439,7 @@ Look at the built-in [Handlers](https://github.com/nelmio/NelmioApiDocBundle/tre nelmio_api_doc: name: 'API documentation' exclude_sections: [] + default_sections_opened: true motd: template: 'NelmioApiDocBundle::Components/motd.html.twig' request_listener: diff --git a/Resources/public/css/screen.css b/Resources/public/css/screen.css index 2d11e94..8056e24 100644 --- a/Resources/public/css/screen.css +++ b/Resources/public/css/screen.css @@ -172,10 +172,37 @@ table tbody tr td { } .section { - border: 1px solid #ddd; - background: #f8f8f8; - padding: 5px 20px; - margin-bottom: 15px; + padding: 5px 20px; + border-bottom: 1px solid #ddd; +} + +.section h1 { + padding: 0; +} + +.section.active { + border: 1px solid #ddd; + background: #f8f8f8; + margin: 15px 0; +} + +.section.active h1 { + padding: 10px 0; +} + +.section .actions { + text-align: right; + float: right; + margin-top: 10px; +} + +.section .actions a { + cursor: pointer; + margin-left: 10px; +} + +.section .actions a:hover { + text-decoration: underline; } li.resource { diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig index 4eb48a7..ec2dd0b 100755 --- a/Resources/views/layout.html.twig +++ b/Resources/views/layout.html.twig @@ -91,6 +91,11 @@ $('body,html').scrollTop(elem.position().top); }); elem.find('.toggler').click(); + var section = elem.parents('.section').first(); + if (section) { + section.addClass('active'); + section.find('.section-list').slideDown('fast'); + } } }); @@ -107,6 +112,36 @@ return false; }); + $('.action-show-hide, .section > h1').on('click', function(){ + var section = $(this).parents('.section').first(); + if (section.hasClass('active')) { + section.removeClass('active'); + section.find('.section-list').slideUp('fast'); + } else { + section.addClass('active'); + section.find('.section-list').slideDown('fast'); + } + + }); + + $('.action-list').on('click', function(){ + var section = $(this).parents('.section').first(); + if (!section.hasClass('active')) { + section.addClass('active'); + } + section.find('.section-list').slideDown('fast'); + section.find('.content').slideUp('fast'); + }); + + $('.action-expand').on('click', function(){ + var section = $(this).parents('.section').first(); + if (!section.hasClass('active')) { + section.addClass('active'); + } + $(section).find('ul').slideDown('fast'); + $(section).find('.content').slideDown('fast'); + }); + {% if enableSandbox %} var setParameterType = function ($context,setType) { // no 2nd argument, use default from parameters diff --git a/Resources/views/resources.html.twig b/Resources/views/resources.html.twig index 9ce33eb..c5f2f81 100644 --- a/Resources/views/resources.html.twig +++ b/Resources/views/resources.html.twig @@ -3,9 +3,14 @@ {% block content %} {% for section, sections in resources %} {% if section != '_others' %} -
  • +
  • +

    {{ section }}

    -
      +
        {% endif %} {% for resource, methods in sections %}