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
This commit is contained in:
nate 2008-03-12 03:57:54 +00:00
parent 12acf6ab9f
commit 6fe1e62555
4 changed files with 104 additions and 29 deletions

View file

@ -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;
}
/**

View file

@ -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 <script /> blocks should be written 'safely,' i.e. wrapped in CDATA blocks
*
* @var boolean
* @access public
*/
var $safe = false;
/**
* HTML tags used by this helper.
*
* @var array
* @access public
*/
var $tags = array(
'javascriptblock' => '<script type="text/javascript">%s</script>',
'javascriptstart' => '<script type="text/javascript">',
'javascriptlink' => '<script type="text/javascript" src="%s"></script>',
'javascriptend' => '</script>',
'javascriptend' => '</script>'
);
/**
* 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 <head />, 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');

View file

@ -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('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote($expected)) . '\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('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote($expected)) . '\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('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote($expected)) . '\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() {

View file

@ -175,12 +175,15 @@ class JavascriptTest extends UnitTestCase {
$this->assertEqual("</script>", $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);