1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-01-16 13:00:42 +01:00

Hide empty collectors until used (#672)

* Hide empty collectors until used

* Make hidden tabs configurable

* Tweak default, add test
This commit is contained in:
Barry vd. Heuvel 2024-09-10 19:28:00 +02:00 committed by GitHub
parent cc8f41e0bd
commit 63bae27159
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 4 deletions

View File

@ -62,6 +62,8 @@ class JavascriptRenderer
protected $useRequireJs = false;
protected $hideEmptyTabs = null;
protected $initialization;
protected $controls = array();
@ -157,6 +159,9 @@ class JavascriptRenderer
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);
@ -397,6 +402,29 @@ class JavascriptRenderer
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
*
@ -1036,7 +1064,7 @@ class JavascriptRenderer
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;
@ -1075,7 +1103,7 @@ class JavascriptRenderer
$nonce = $this->getNonceAttribute();
if ($nonce != '') {
if ($nonce != '') {
$js = preg_replace("/<script>/", "<script nonce='{$this->cspNonce}'>", $js);
}
@ -1100,6 +1128,11 @@ class JavascriptRenderer
$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);
}

View File

@ -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) {
@ -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();
}
}
})
}
@ -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 {
@ -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);
@ -1056,6 +1062,10 @@ if (typeof(PhpDebugBar) == 'undefined') {
}
},
setHideEmptyTabs: function(hideEmpty) {
this.hideEmptyTabs = hideEmpty;
},
/**
* Returns the handler to open past dataset
*

View File

@ -147,6 +147,13 @@ class JavascriptRendererTest extends DebugBarTestCase
$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());