Skip to content

Commit

Permalink
Updating Js::link() adding confirm option.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jul 12, 2009
1 parent 5119e58 commit 04fa46f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 33 deletions.
22 changes: 17 additions & 5 deletions cake/libs/view/helpers/js.php
Expand Up @@ -206,8 +206,12 @@ function getBuffer($clear = true) {
/** /**
* Generate an 'Ajax' link. Uses the selected JS engine to create a link * Generate an 'Ajax' link. Uses the selected JS engine to create a link
* element that is enhanced with Javascript. Options can include * element that is enhanced with Javascript. Options can include
* both those for HtmlHelper::link() and JsBaseEngine::request() * both those for HtmlHelper::link() and JsBaseEngine::request(), JsBaseEngine::event();
* *
* ### Options
*
* - confirm - Generate a confirm() dialog before sending the event.
*
* @param string $title Title for the link. * @param string $title Title for the link.
* @param mixed $url Mixed either a string URL or an cake url array. * @param mixed $url Mixed either a string URL or an cake url array.
* @param array $options Options for both the HTML element and Js::request() * @param array $options Options for both the HTML element and Js::request()
Expand All @@ -219,26 +223,34 @@ function link($title, $url = null, $options = array()) {
} }
$htmlOptions = $this->_getHtmlOptions($options); $htmlOptions = $this->_getHtmlOptions($options);
$out = $this->Html->link($title, $url, $htmlOptions); $out = $this->Html->link($title, $url, $htmlOptions);
$this->get('#' . $options['id']); $this->get('#' . $htmlOptions['id']);
$requestString = $this->request($url, $options); $requestString = '';
if (isset($options['confirm'])) {
$requestString .= 'var _confirm = ' . $this->confirm($options['confirm']);
$requestString .= "if (!_confirm) {\n\treturn false;\n}";
unset($options['confirm']);
}
$requestString .= $this->request($url, $options);
if (!empty($requestString)) { if (!empty($requestString)) {

$this->event('click', $requestString, $options);
} }
return $out; return $out;
} }
/** /**
* Parse a set of Options and extract the Html options. * Parse a set of Options and extract the Html options.
* Extracted Html Options are removed from the $options param.
* *
* @param array Options to filter. * @param array Options to filter.
* @return array Array of options for non-js. * @return array Array of options for non-js.
**/ **/
function _getHtmlOptions($options) { function _getHtmlOptions(&$options) {
$htmlKeys = array('class', 'id', 'escape', 'onblur', 'onfocus', 'rel', 'title'); $htmlKeys = array('class', 'id', 'escape', 'onblur', 'onfocus', 'rel', 'title');
$htmlOptions = array(); $htmlOptions = array();
foreach ($htmlKeys as $key) { foreach ($htmlKeys as $key) {
if (isset($options[$key])) { if (isset($options[$key])) {
$htmlOptions[$key] = $options[$key]; $htmlOptions[$key] = $options[$key];
} }
unset($options[$key]);
} }
return $htmlOptions; return $htmlOptions;
} }
Expand Down
70 changes: 42 additions & 28 deletions cake/tests/cases/libs/view/helpers/js.test.php
Expand Up @@ -83,7 +83,7 @@ class JsHelperTestCase extends CakeTestCase {
*/ */
function startTest() { function startTest() {
$this->Js =& new JsHelper('JsBase'); $this->Js =& new JsHelper('JsBase');
$this->Js->Html =& new HtmlHelper(); $this->Js->Html =& new HtmlHelper();
$this->Js->JsBaseEngine =& new JsBaseEngineHelper(); $this->Js->JsBaseEngine =& new JsBaseEngineHelper();


$view =& new JsHelperMockView(); $view =& new JsHelperMockView();
Expand All @@ -99,6 +99,16 @@ function endTest() {
ClassRegistry::removeObject('view'); ClassRegistry::removeObject('view');
unset($this->Js); unset($this->Js);
} }
/**
* Switches $this->Js to a mocked engine.
*
* @return void
**/
function _useMock() {
$this->Js =& new JsHelper(array('TestJs'));
$this->Js->TestJsEngine =& new TestJsEngineHelper();
$this->Js->Html =& new HtmlHelper();
}
/** /**
* test object construction * test object construction
* *
Expand All @@ -123,63 +133,62 @@ function testConstruction() {
* @return void * @return void
**/ **/
function testMethodDispatching() { function testMethodDispatching() {
$js =& new JsHelper(array('TestJs')); $this->_useMock();
$js->TestJsEngine =& new TestJsEngineHelper(); $this->Js->TestJsEngine->expectOnce('dispatchMethod', array('methodOne', array()));
$js->TestJsEngine->expectOnce('dispatchMethod', array('methodOne', array()));


$js->methodOne(); $this->Js->methodOne();


$js->TestEngine =& new StdClass(); $this->Js->TestEngine =& new StdClass();
$this->expectError(); $this->expectError();
$js->someMethodThatSurelyDoesntExist(); $this->Js->someMethodThatSurelyDoesntExist();
} }
/** /**
* Test that method dispatching respects buffer parameters and bufferedMethods Lists. * Test that method dispatching respects buffer parameters and bufferedMethods Lists.
* *
* @return void * @return void
**/ **/
function testMethodDispatchWithBuffering() { function testMethodDispatchWithBuffering() {
$js =& new JsHelper(array('TestJs')); $this->_useMock();
$js->TestJsEngine = new TestJsEngineHelper();
$js->TestJsEngine->bufferedMethods = array('event', 'sortables'); $this->Js->TestJsEngine->bufferedMethods = array('event', 'sortables');
$js->TestJsEngine->setReturnValue('dispatchMethod', 'This is an event call', array('event', '*')); $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'This is an event call', array('event', '*'));


$js->event('click', 'foo'); $this->Js->event('click', 'foo');
$result = $js->getBuffer(); $result = $this->Js->getBuffer();
$this->assertEqual(count($result), 1); $this->assertEqual(count($result), 1);
$this->assertEqual($result[0], 'This is an event call'); $this->assertEqual($result[0], 'This is an event call');


$result = $js->event('click', 'foo', array('buffer' => false)); $result = $this->Js->event('click', 'foo', array('buffer' => false));
$buffer = $js->getBuffer(); $buffer = $this->Js->getBuffer();
$this->assertTrue(empty($buffer)); $this->assertTrue(empty($buffer));
$this->assertEqual($result, 'This is an event call'); $this->assertEqual($result, 'This is an event call');


$result = $js->event('click', 'foo', false); $result = $this->Js->event('click', 'foo', false);
$buffer = $js->getBuffer(); $buffer = $this->Js->getBuffer();
$this->assertTrue(empty($buffer)); $this->assertTrue(empty($buffer));
$this->assertEqual($result, 'This is an event call'); $this->assertEqual($result, 'This is an event call');


$js->TestJsEngine->setReturnValue('dispatchMethod', 'I am not buffered.', array('effect', '*')); $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'I am not buffered.', array('effect', '*'));


$result = $js->effect('slideIn'); $result = $this->Js->effect('slideIn');
$buffer = $js->getBuffer(); $buffer = $this->Js->getBuffer();
$this->assertTrue(empty($buffer)); $this->assertTrue(empty($buffer));
$this->assertEqual($result, 'I am not buffered.'); $this->assertEqual($result, 'I am not buffered.');


$result = $js->effect('slideIn', true); $result = $this->Js->effect('slideIn', true);
$buffer = $js->getBuffer(); $buffer = $this->Js->getBuffer();
$this->assertNull($result); $this->assertNull($result);
$this->assertEqual(count($buffer), 1); $this->assertEqual(count($buffer), 1);
$this->assertEqual($buffer[0], 'I am not buffered.'); $this->assertEqual($buffer[0], 'I am not buffered.');


$result = $js->effect('slideIn', array('speed' => 'slow'), true); $result = $this->Js->effect('slideIn', array('speed' => 'slow'), true);
$buffer = $js->getBuffer(); $buffer = $this->Js->getBuffer();
$this->assertNull($result); $this->assertNull($result);
$this->assertEqual(count($buffer), 1); $this->assertEqual(count($buffer), 1);
$this->assertEqual($buffer[0], 'I am not buffered.'); $this->assertEqual($buffer[0], 'I am not buffered.');


$result = $js->effect('slideIn', array('speed' => 'slow', 'buffer' => true)); $result = $this->Js->effect('slideIn', array('speed' => 'slow', 'buffer' => true));
$buffer = $js->getBuffer(); $buffer = $this->Js->getBuffer();
$this->assertNull($result); $this->assertNull($result);
$this->assertEqual(count($buffer), 1); $this->assertEqual(count($buffer), 1);
$this->assertEqual($buffer[0], 'I am not buffered.'); $this->assertEqual($buffer[0], 'I am not buffered.');
Expand All @@ -190,7 +199,7 @@ function testMethodDispatchWithBuffering() {
* @return void * @return void
**/ **/
function testWriteScriptsNoFile() { function testWriteScriptsNoFile() {
$this->Js->JsBaseEngine = new TestJsEngineHelper(); $this->_useMock();
$this->Js->buffer('one = 1;'); $this->Js->buffer('one = 1;');
$this->Js->buffer('two = 2;'); $this->Js->buffer('two = 2;');
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => false)); $result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => false));
Expand All @@ -203,7 +212,7 @@ function testWriteScriptsNoFile() {
); );
$this->assertTags($result, $expected, true); $this->assertTags($result, $expected, true);


$this->Js->JsBaseEngine->expectAtLeastOnce('domReady'); $this->Js->TestJsEngine->expectAtLeastOnce('domReady');
$result = $this->Js->writeBuffer(array('onDomReady' => true, 'cache' => false)); $result = $this->Js->writeBuffer(array('onDomReady' => true, 'cache' => false));


$view =& new JsHelperMockView(); $view =& new JsHelperMockView();
Expand Down Expand Up @@ -240,6 +249,11 @@ function testWriteScriptsInFile() {
* @return void * @return void
**/ **/
function testLink() { function testLink() {
$this->_useMock();
$this->Js->TestJsEngine->setReturnValue('request', 'ajax code');
$this->Js->TestJsEngine->expectAt(0, 'request', array('/posts/view/1', '*'));
$this->Js->TestJsEngine->expectAt(0, 'event', array('click', 'ajax code'));

$result = $this->Js->link('test link', '/posts/view/1', array('update' => '#content')); $result = $this->Js->link('test link', '/posts/view/1', array('update' => '#content'));
$expected = array( $expected = array(
'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'), 'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
Expand Down

0 comments on commit 04fa46f

Please sign in to comment.