Skip to content

Commit

Permalink
Hide empty collectors until used (#672)
Browse files Browse the repository at this point in the history
* Hide empty collectors until used

* Make hidden tabs configurable

* Tweak default, add test
  • Loading branch information
barryvdh authored Sep 10, 2024
1 parent cc8f41e commit 63bae27
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
37 changes: 35 additions & 2 deletions src/DebugBar/JavascriptRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class JavascriptRenderer

protected $useRequireJs = false;

protected $hideEmptyTabs = null;

protected $initialization;

protected $controls = array();
Expand Down Expand Up @@ -157,6 +159,9 @@ public function setOptions(array $options)
if (array_key_exists('use_requirejs', $options)) {
$this->setUseRequireJs($options['use_requirejs']);
}
if (array_key_exists('hide_empty_tabs', $options)) {
$this->setHideEmptyTabs($options['hide_empty_tabs']);
}
if (array_key_exists('controls', $options)) {
foreach ($options['controls'] as $name => $control) {
$this->addControl($name, $control);
Expand Down Expand Up @@ -397,6 +402,29 @@ public function isRequireJsUsed()
return $this->useRequireJs;
}


/**
* Sets whether to hide empty tabs or not
*
* @param boolean $hide
* @return $this
*/
public function setHideEmptyTabs($hide = true)
{
$this->hideEmptyTabs = $hide;
return $this;
}

/**
* Checks if empty tabs are hidden or not
*
* @return boolean
*/
public function areEmptyTabsHidden()
{
return $this->hideEmptyTabs;
}

/**
* Adds a control to initialize
*
Expand Down Expand Up @@ -1036,7 +1064,7 @@ public function renderOnShutdownWithHead($here = true, $initialize = true, $rend
public function replaceTagInBuffer($here = true, $initialize = true, $renderStackedData = true, $head = false)
{
$render = ($head ? $this->renderHead() : "")
. $this->render($initialize, $renderStackedData);
. $this->render($initialize, $renderStackedData);

$current = ($here && ob_get_level() > 0) ? ob_get_clean() : self::REPLACEABLE_TAG;

Expand Down Expand Up @@ -1075,7 +1103,7 @@ public function render($initialize = true, $renderStackedData = true)

$nonce = $this->getNonceAttribute();

if ($nonce != '') {
if ($nonce != '') {
$js = preg_replace("/<script>/", "<script nonce='{$this->cspNonce}'>", $js);
}

Expand All @@ -1100,6 +1128,11 @@ protected function getJsInitializationCode()
$js .= sprintf("var %s = new %s();\n", $this->variableName, $this->javascriptClass);
}

if ($this->hideEmptyTabs !== null) {
$js .= sprintf("%s.setHideEmptyTabs(%s);\n", $this->variableName,
json_encode($this->hideEmptyTabs));
}

if (($this->initialization & self::INITIALIZE_CONTROLS) === self::INITIALIZE_CONTROLS) {
$js .= $this->getJsControlsDefinitionCode($this->variableName);
}
Expand Down
14 changes: 12 additions & 2 deletions src/DebugBar/Resources/debugbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ if (typeof(PhpDebugBar) == 'undefined') {

render: function() {
this.$tab = $('<a />').addClass(csscls('tab'));

this.$icon = $('<i />').appendTo(this.$tab);
this.bindAttr('icon', function(icon) {
if (icon) {
Expand Down Expand Up @@ -285,6 +284,9 @@ if (typeof(PhpDebugBar) == 'undefined') {
this.bindAttr('data', function(data) {
if (this.has('widget')) {
this.get('widget').set('data', data);
if (!$.isEmptyObject(data)) {
this.$tab.show();
}
}
})
}
Expand Down Expand Up @@ -423,6 +425,7 @@ if (typeof(PhpDebugBar) == 'undefined') {
this.firstTabName = null;
this.activePanelName = null;
this.activeDatasetId = null;
this.hideEmptyTabs = false;
this.datesetTitleFormater = new DatasetTitleFormater(this);
this.options.bodyMarginBottomHeight = parseInt($('body').css('margin-bottom'));
try {
Expand Down Expand Up @@ -641,7 +644,10 @@ if (typeof(PhpDebugBar) == 'undefined') {
} else {
self.showTab(name);
}
});
})
if (this.hideEmptyTabs) {
tab.$tab.hide();
}
tab.$tab.attr('data-collector', name);
tab.$el.attr('data-collector', name);
tab.$el.appendTo(this.$body);
Expand Down Expand Up @@ -1056,6 +1062,10 @@ if (typeof(PhpDebugBar) == 'undefined') {
}
},

setHideEmptyTabs: function(hideEmpty) {
this.hideEmptyTabs = hideEmpty;
},

/**
* Returns the handler to open past dataset
*
Expand Down
7 changes: 7 additions & 0 deletions tests/DebugBar/Tests/JavascriptRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ public function testRenderConstructorWithNonce()
$this->assertStringStartsWith("<script type=\"text/javascript\" nonce=\"mynonce\">\nvar phpdebugbar = new PhpDebugBar.DebugBar();", $this->r->render());
}

public function testRenderConstructorWithEmptyTabsHidden()
{
$this->r->setInitialization(JavascriptRenderer::INITIALIZE_CONSTRUCTOR);
$this->r->setHideEmptyTabs(true);
$this->assertStringStartsWith("<script type=\"text/javascript\">\nvar phpdebugbar = new PhpDebugBar.DebugBar();\nphpdebugbar.setHideEmptyTabs(true);", $this->r->render());
}

public function testJQueryNoConflictAutoDisabling()
{
$this->assertTrue($this->r->isJqueryNoConflictEnabled());
Expand Down

0 comments on commit 63bae27

Please sign in to comment.