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

jQuery.noConflict() is now called by default (fixed #29)

Will not be called if the js vendors are not included.
Can be enabled/disabled using JavascriptRenderer::setEnableJqueryNoConflict()
This commit is contained in:
maximebf 2013-09-19 16:31:50 -04:00
parent 7e81754ef6
commit 1b7e7fdbc8
7 changed files with 87 additions and 15 deletions

View File

@ -8,7 +8,9 @@ session_start();
use DebugBar\StandardDebugBar;
$debugbar = new StandardDebugBar();
$debugbarRenderer = $debugbar->getJavascriptRenderer()->setBaseUrl('../src/DebugBar/Resources');
$debugbarRenderer = $debugbar->getJavascriptRenderer()
->setBaseUrl('../src/DebugBar/Resources')
->setEnableJqueryNoConflict(false);
//
// create a writable profiles folder in the demo directory to uncomment the following lines

View File

@ -43,6 +43,8 @@ class JavascriptRenderer
protected $variableName = 'phpdebugbar';
protected $enableJqueryNoConflict = true;
protected $initialization;
protected $controls = array();
@ -115,6 +117,9 @@ class JavascriptRenderer
if (array_key_exists('initialization', $options)) {
$this->setInitialization($options['initialization']);
}
if (array_key_exists('enable_jquery_noconflict', $options)) {
$this->setEnableJqueryNoConflict($options['enable_jquery_noconflict']);
}
if (array_key_exists('controls', $options)) {
foreach ($options['controls'] as $name => $control) {
$this->addControl($name, $control);
@ -130,6 +135,12 @@ class JavascriptRenderer
$this->ignoreCollector($name);
}
}
if (array_key_exists('ajax_handler_classname', $options)) {
$this->setAjaxHandlerClass($options['ajax_handler_classname']);
}
if (array_key_exists('ajax_handler_bind_to_jquery', $options)) {
$this->setBindAjaxHandlerToJquery($options['ajax_handler_bind_to_jquery']);
}
if (array_key_exists('open_handler_classname', $options)) {
$this->setOpenHandlerClass($options['open_handler_classname']);
}
@ -194,6 +205,12 @@ class JavascriptRenderer
$enabled = array($enabled);
}
$this->includeVendors = $enabled;
if (!$enabled || (is_array($enabled) && !in_array('js', $enabled))) {
// no need to call jQuery.noConflict() if we do not include our own version
$this->enableJqueryNoConflict = false;
}
return $this;
}
@ -274,6 +291,27 @@ class JavascriptRenderer
return $this->initialization;
}
/**
* Sets whether to call jQuery.noConflict()
*
* @param boolean $enabled
*/
public function setEnableJqueryNoConflict($enabled = true)
{
$this->enableJqueryNoConflict = $enabled;
return $this;
}
/**
* Checks if jQuery.noConflict() will be called
*
* @return boolean
*/
public function isJqueryNoConflictEnabled()
{
return $this->enableJqueryNoConflict;
}
/**
* Adds a control to initialize
*
@ -642,8 +680,12 @@ class JavascriptRenderer
{
$js = '';
if ($this->enableJqueryNoConflict) {
$js .= "jQuery.noConflict(true);\n";
}
if (($this->initialization & self::INITIALIZE_CONSTRUCTOR) === self::INITIALIZE_CONSTRUCTOR) {
$js = sprintf("var %s = new %s();\n", $this->variableName, $this->javascriptClass);
$js .= sprintf("var %s = new %s();\n", $this->variableName, $this->javascriptClass);
}
if (($this->initialization & self::INITIALIZE_CONTROLS) === self::INITIALIZE_CONTROLS) {

View File

@ -1,18 +1,19 @@
if (typeof(PhpDebugBar) == 'undefined') {
// namespace
var PhpDebugBar = {};
}
if (typeof(localStorage) == 'undefined') {
// provide mock localStorage object for dumb browsers
localStorage = {
setItem: function(key, value) {},
getItem: function(key) { return null; }
};
PhpDebugBar.$ = jQuery;
}
(function($) {
if (typeof(localStorage) == 'undefined') {
// provide mock localStorage object for dumb browsers
localStorage = {
setItem: function(key, value) {},
getItem: function(key) { return null; }
};
}
/**
* Returns the value from an object property.
* Using dots in the key, it is possible to retrieve nested property values
@ -816,4 +817,4 @@ if (typeof(localStorage) == 'undefined') {
});
})(jQuery);
})(PhpDebugBar.$);

View File

@ -1,6 +1,7 @@
if (typeof(PhpDebugBar) == 'undefined') {
// namespace
var PhpDebugBar = {};
PhpDebugBar.$ = jQuery;
}
(function($) {
@ -119,4 +120,4 @@ if (typeof(PhpDebugBar) == 'undefined') {
});
})(jQuery);
})(PhpDebugBar.$);

View File

@ -1,6 +1,7 @@
if (typeof(PhpDebugBar) == 'undefined') {
// namespace
var PhpDebugBar = {};
PhpDebugBar.$ = jQuery;
}
(function($) {
@ -511,4 +512,4 @@ if (typeof(PhpDebugBar) == 'undefined') {
});
})(jQuery);
})(PhpDebugBar.$);

View File

@ -23,6 +23,7 @@ class JavascriptRendererTest extends DebugBarTestCase
'javascript_class' => 'Foobar',
'variable_name' => 'foovar',
'initialization' => JavascriptRenderer::INITIALIZE_CONTROLS,
'enable_jquery_noconflict' => true,
'controls' => array(
'memory' => array(
"icon" => "cogs",
@ -31,7 +32,11 @@ class JavascriptRendererTest extends DebugBarTestCase
)
),
'disable_controls' => array('messages'),
'ignore_collectors' => 'config'
'ignore_collectors' => 'config',
'ajax_handler_classname' => 'AjaxFoo',
'ajax_handler_bind_to_jquery' => false,
'open_handler_classname' => 'OpenFoo',
'open_handler_url' => 'open.php'
));
$this->assertEquals('/foo', $this->r->getBasePath());
@ -40,12 +45,17 @@ class JavascriptRendererTest extends DebugBarTestCase
$this->assertEquals('Foobar', $this->r->getJavascriptClass());
$this->assertEquals('foovar', $this->r->getVariableName());
$this->assertEquals(JavascriptRenderer::INITIALIZE_CONTROLS, $this->r->getInitialization());
$this->assertTrue($this->r->isJqueryNoConflictEnabled());
$controls = $this->r->getControls();
$this->assertCount(2, $controls);
$this->assertArrayHasKey('memory', $controls);
$this->assertArrayHasKey('messages', $controls);
$this->assertNull($controls['messages']);
$this->assertContains('config', $this->r->getIgnoredCollectors());
$this->assertEquals('AjaxFoo', $this->r->getAjaxHandlerClass());
$this->assertFalse($this->r->isAjaxHandlerBoundToJquery());
$this->assertEquals('OpenFoo', $this->r->getOpenHandlerClass());
$this->assertEquals('open.php', $this->r->getOpenHandlerUrl());
}
public function testGetAssets()
@ -83,6 +93,20 @@ class JavascriptRendererTest extends DebugBarTestCase
$this->r->setJavascriptClass('Foobar');
$this->r->setVariableName('foovar');
$this->r->setAjaxHandlerClass(false);
$this->assertStringStartsWith("<script type=\"text/javascript\">\nvar foovar = new Foobar();\nfoovar.addDataSet(", $this->r->render());
$this->r->setEnableJqueryNoConflict(true);
$this->assertStringStartsWith("<script type=\"text/javascript\">\njQuery.noConflict(true);\nvar foovar = new Foobar();\nfoovar.addDataSet(", $this->r->render());
}
public function testJQueryNoConflictAutoDisabling()
{
$this->assertTrue($this->r->isJqueryNoConflictEnabled());
$this->r->setIncludeVendors(false);
$this->assertFalse($this->r->isJqueryNoConflictEnabled());
$this->r->setEnableJqueryNoConflict(true);
$this->r->setIncludeVendors('css');
$this->assertFalse($this->r->isJqueryNoConflictEnabled());
$this->r->setEnableJqueryNoConflict(true);
$this->r->setIncludeVendors(array('css', 'js'));
$this->assertTrue($this->r->isJqueryNoConflictEnabled());
}
}

View File

@ -1,4 +1,5 @@
<script type="text/javascript">
jQuery.noConflict(true);
var phpdebugbar = new PhpDebugBar.DebugBar();
phpdebugbar.addTab("messages", new PhpDebugBar.DebugBar.Tab({"title":"Messages", "widget": new PhpDebugBar.Widgets.MessagesWidget()}));
phpdebugbar.addIndicator("time", new PhpDebugBar.DebugBar.Indicator({"icon":"time"}));