diff --git a/demo/bootstrap.php b/demo/bootstrap.php index cbc9cdc..59bb30d 100644 --- a/demo/bootstrap.php +++ b/demo/bootstrap.php @@ -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 diff --git a/src/DebugBar/JavascriptRenderer.php b/src/DebugBar/JavascriptRenderer.php index 55b1dd4..1cdb2fc 100644 --- a/src/DebugBar/JavascriptRenderer.php +++ b/src/DebugBar/JavascriptRenderer.php @@ -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) { diff --git a/src/DebugBar/Resources/debugbar.js b/src/DebugBar/Resources/debugbar.js index 22a0fb9..b5ff63c 100644 --- a/src/DebugBar/Resources/debugbar.js +++ b/src/DebugBar/Resources/debugbar.js @@ -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.$); diff --git a/src/DebugBar/Resources/openhandler.js b/src/DebugBar/Resources/openhandler.js index 8d7da70..f3f6ef9 100644 --- a/src/DebugBar/Resources/openhandler.js +++ b/src/DebugBar/Resources/openhandler.js @@ -1,6 +1,7 @@ if (typeof(PhpDebugBar) == 'undefined') { // namespace var PhpDebugBar = {}; + PhpDebugBar.$ = jQuery; } (function($) { @@ -119,4 +120,4 @@ if (typeof(PhpDebugBar) == 'undefined') { }); -})(jQuery); \ No newline at end of file +})(PhpDebugBar.$); \ No newline at end of file diff --git a/src/DebugBar/Resources/widgets.js b/src/DebugBar/Resources/widgets.js index 10c41a7..56610c1 100644 --- a/src/DebugBar/Resources/widgets.js +++ b/src/DebugBar/Resources/widgets.js @@ -1,6 +1,7 @@ if (typeof(PhpDebugBar) == 'undefined') { // namespace var PhpDebugBar = {}; + PhpDebugBar.$ = jQuery; } (function($) { @@ -511,4 +512,4 @@ if (typeof(PhpDebugBar) == 'undefined') { }); -})(jQuery); +})(PhpDebugBar.$); diff --git a/tests/DebugBar/Tests/JavascriptRendererTest.php b/tests/DebugBar/Tests/JavascriptRendererTest.php index 1ee458e..2a0da1f 100644 --- a/tests/DebugBar/Tests/JavascriptRendererTest.php +++ b/tests/DebugBar/Tests/JavascriptRendererTest.php @@ -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("