From 6fe1e625553d60e8235078cc15d405e3463d073b Mon Sep 17 00:00:00 2001 From: nate Date: Wed, 12 Mar 2008 03:57:54 +0000 Subject: [PATCH] Implementing JavascriptHelper::$inBlock, docblocking JavascriptHelper properties, adding fix for AjaxHelper::sortable() 'overlap' option, fixes #4292 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6561 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/view/helpers/ajax.php | 32 ++++--- cake/libs/view/helpers/javascript.php | 86 ++++++++++++++++--- .../cases/libs/view/helpers/ajax.test.php | 12 ++- .../libs/view/helpers/javascript.test.php | 3 + 4 files changed, 104 insertions(+), 29 deletions(-) diff --git a/cake/libs/view/helpers/ajax.php b/cake/libs/view/helpers/ajax.php index 0c1791d5c..56b866820 100644 --- a/cake/libs/view/helpers/ajax.php +++ b/cake/libs/view/helpers/ajax.php @@ -178,7 +178,6 @@ class AjaxHelper extends AppHelper { if (!isset($href)) { $href = $title; } - if (!isset($options['url'])) { $options['url'] = $href; } @@ -187,20 +186,12 @@ class AjaxHelper extends AppHelper { $options['confirm'] = $confirm; unset($confirm); } - $htmlOptions = $this->__getHtmlOptions($options); if (empty($options['fallback']) || !isset($options['fallback'])) { $options['fallback'] = $href; } - - if (!isset($htmlOptions['id'])) { - $htmlOptions['id'] = 'link' . intval(rand()); - } - - if (!isset($htmlOptions['onclick'])) { - $htmlOptions['onclick'] = ''; - } + $htmlOptions = array_merge(array('id' => 'link' . intval(rand()), 'onclick' => ''), $htmlOptions); $htmlOptions['onclick'] .= ' event.returnValue = false; return false;'; $return = $this->Html->link($title, $href, $htmlOptions, null, $escapeTitle); @@ -209,7 +200,6 @@ class AjaxHelper extends AppHelper { if (is_string($script)) { $return .= $script; } - return $return; } /** @@ -246,11 +236,9 @@ class AjaxHelper extends AppHelper { if (isset($options['before'])) { $func = "{$options['before']}; $func"; } - if (isset($options['after'])) { $func = "$func; {$options['after']};"; } - if (isset($options['condition'])) { $func = "if ({$options['condition']}) { $func; }"; } @@ -647,11 +635,23 @@ class AjaxHelper extends AppHelper { } $options['onUpdate'] = 'function(sortable) {' . $this->remoteFunction($options) . '}'; } + $block = true; - $options = $this->_optionsToString($options, array('tag', 'constraint', 'only', 'handle', 'hoverclass', 'scroll', 'tree', 'treeTag', 'update')); + if (isset($options['block'])) { + $block = $options['block']; + unset($options['block']); + } + + $options = $this->_optionsToString($options, array( + 'tag', 'constraint', 'only', 'handle', 'hoverclass', 'scroll', 'tree', 'treeTag', 'update', 'overlap' + )); $options = array_merge($options, $this->_buildCallbacks($options)); $options = $this->_buildOptions($options, $this->sortOptions); - return $this->Javascript->codeBlock("Sortable.create('$id', $options);"); + $result = "Sortable.create('$id', $options);"; + if (!$block) { + return $result; + } + return $this->Javascript->codeBlock($result); } /** * Private helper function for Javascript. @@ -729,13 +729,11 @@ class AjaxHelper extends AppHelper { unset($options[$key]); } } - foreach ($extra as $key) { if (isset($options[$key])) { unset($options[$key]); } } - return $options; } /** diff --git a/cake/libs/view/helpers/javascript.php b/cake/libs/view/helpers/javascript.php index c99bbb414..a1b77b057 100644 --- a/cake/libs/view/helpers/javascript.php +++ b/cake/libs/view/helpers/javascript.php @@ -36,33 +36,98 @@ */ class JavascriptHelper extends AppHelper { - - var $__scriptBuffer = null; - var $_blockOptions = array(); - var $_cachedEvents = array(); - var $_cacheEvents = false; - var $_cacheToFile = false; - var $_cacheAll = false; - var $_rules = array(); /** * Determines whether native JSON extension is used for encoding. Set by object constructor. * * @var boolean + * @access public */ var $useNative = false; +/** + * If true, automatically writes events to the end of a script or to an external JavaScript file + * at the end of page execution + * + * @var boolean + * @access public + */ var $enabled = true; +/** + * Indicates whether ', 'javascriptstart' => '', - 'javascriptend' => '', + 'javascriptend' => '' ); +/** + * Holds options passed to codeBlock(), saved for when block is dumped to output + * + * @var array + * @access protected + * @see JavascriptHelper::codeBlock() + */ + var $_blockOptions = array(); +/** + * Caches events written by event() for output at the end of page execution + * + * @var array + * @access protected + * @see JavascriptHelper::event() + */ + var $_cachedEvents = array(); +/** + * Indicates whether generated events should be cached for later output (can be written at the end of the page, + * in the , or to an external file). + * + * @var boolean + * @access protected + * @see JavascriptHelper::event() + * @see JavascriptHelper::writeEvents() + */ + var $_cacheEvents = false; +/** + * Indicates whether cached events should be written to an external file + * + * @var boolean + * @access protected + * @see JavascriptHelper::event() + * @see JavascriptHelper::writeEvents() + */ + var $_cacheToFile = false; +/** + * Indicates whether *all* generated JavaScript should be cached for later output + * + * @var boolean + * @access protected + * @see JavascriptHelper::codeBlock() + * @see JavascriptHelper::blockEnd() + */ + var $_cacheAll = false; +/** + * Contains event rules attached with CSS selectors. Used with the event:Selectors JavaScript library. + * + * @var array + * @access protected + * @see JavascriptHelper::event() + * @link http://alternateidea.com/event-selectors/ + */ + var $_rules = array(); +/** + * @var string + * @access private + */ + var $__scriptBuffer = null; /** * Constructor. Checks for presence of native PHP JSON extension to use for object encoding * @@ -102,7 +167,6 @@ class JavascriptHelper extends AppHelper { } else if (empty($options)) { $options = array(); } - $defaultOptions = array('allowCache' => true, 'safe' => true, 'inline' => true); $options = array_merge($defaultOptions, compact('safe'), $options); @@ -120,6 +184,7 @@ class JavascriptHelper extends AppHelper { if ($script === null) { $this->__scriptBuffer = @ob_get_contents(); $this->_blockOptions = $options; + $this->inBlock = true; @ob_end_clean(); ob_start(); return null; @@ -150,6 +215,7 @@ class JavascriptHelper extends AppHelper { $this->__scriptBuffer = null; $options = $this->_blockOptions; $this->_blockOptions = array(); + $this->inBlock = false; if (isset($options['inline']) && !$options['inline']) { $view =& ClassRegistry::getObject('view'); diff --git a/cake/tests/cases/libs/view/helpers/ajax.test.php b/cake/tests/cases/libs/view/helpers/ajax.test.php index 9e6bc901f..408556f9c 100644 --- a/cake/tests/cases/libs/view/helpers/ajax.test.php +++ b/cake/tests/cases/libs/view/helpers/ajax.test.php @@ -167,11 +167,11 @@ class AjaxTest extends UnitTestCase { } function testSortable() { - $result = $this->Ajax->sortable('ull', array('constraint'=>false, 'ghosting'=>true)); + $result = $this->Ajax->sortable('ull', array('constraint' => false, 'ghosting' => true)); $expected = 'Sortable.create(\'ull\', {constraint:false, ghosting:true});'; $this->assertPattern('/^]+>\s*' . str_replace('/', '\\/', preg_quote('//')) . '\s*<\/script>$/', $result); - $result = $this->Ajax->sortable('ull', array('constraint'=>'false', 'ghosting'=>'true')); + $result = $this->Ajax->sortable('ull', array('constraint' => 'false', 'ghosting' => 'true')); $expected = 'Sortable.create(\'ull\', {constraint:false, ghosting:true});'; $this->assertPattern('/^]+>\s*' . str_replace('/', '\\/', preg_quote('//')) . '\s*<\/script>$/', $result); @@ -187,6 +187,14 @@ class AjaxTest extends UnitTestCase { 'complete' => 'Element.show(\'message\');')); $expected = 'Sortable.create(\'faqs\', {update:\'faqs\', tag:\'tbody\', handle:\'grip\', onUpdate:function(sortable) {Element.hide(\'message\'); new Ajax.Updater(\'faqs\',\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, onComplete:function(request, json) {Element.show(\'message\');}, parameters:Sortable.serialize(\'faqs\'), requestHeaders:[\'X-Update\', \'faqs\']})}});'; $this->assertPattern('/^]+>\s*' . str_replace('/', '\\/', preg_quote('//')) . '\s*<\/script>$/', $result); + + $result = $this->Ajax->sortable('div', array('overlap' => 'foo')); + $expected = "Sortable.create('div', {overlap:'foo'});"; + $this->assertPattern('/' . str_replace('/', '\\/', preg_quote($expected)) . '/', $result); + + $result = $this->Ajax->sortable('div', array('block' => false)); + $expected = "Sortable.create('div', {});"; + $this->assertEqual($result, $expected); } function testSubmitWithIndicator() { diff --git a/cake/tests/cases/libs/view/helpers/javascript.test.php b/cake/tests/cases/libs/view/helpers/javascript.test.php index 36d32df50..bec5ef201 100644 --- a/cake/tests/cases/libs/view/helpers/javascript.test.php +++ b/cake/tests/cases/libs/view/helpers/javascript.test.php @@ -175,12 +175,15 @@ class JavascriptTest extends UnitTestCase { $this->assertEqual("", $result); $this->Javascript->cacheEvents(false, true); + $this->assertFalse($this->Javascript->inBlock); $result = $this->Javascript->codeBlock(); $this->assertIdentical($result, null); + $this->assertTrue($this->Javascript->inBlock); echo 'alert("this is a buffered script");'; $result = $this->Javascript->blockEnd(); $this->assertIdentical($result, null); + $this->assertFalse($this->Javascript->inBlock); $result = $this->Javascript->getCache(); $this->assertEqual('alert("this is a buffered script");', $result);