mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-06 19:42:41 +00:00
Updating Js::link() adding confirm option.
This commit is contained in:
parent
5119e58e3b
commit
04fa46f228
2 changed files with 59 additions and 33 deletions
|
@ -206,8 +206,12 @@ class JsHelper extends AppHelper {
|
|||
/**
|
||||
* Generate an 'Ajax' link. Uses the selected JS engine to create a link
|
||||
* 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 mixed $url Mixed either a string URL or an cake url array.
|
||||
* @param array $options Options for both the HTML element and Js::request()
|
||||
|
@ -219,26 +223,34 @@ class JsHelper extends AppHelper {
|
|||
}
|
||||
$htmlOptions = $this->_getHtmlOptions($options);
|
||||
$out = $this->Html->link($title, $url, $htmlOptions);
|
||||
$this->get('#' . $options['id']);
|
||||
$requestString = $this->request($url, $options);
|
||||
$this->get('#' . $htmlOptions['id']);
|
||||
$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)) {
|
||||
|
||||
$this->event('click', $requestString, $options);
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
/**
|
||||
* Parse a set of Options and extract the Html options.
|
||||
* Extracted Html Options are removed from the $options param.
|
||||
*
|
||||
* @param array Options to filter.
|
||||
* @return array Array of options for non-js.
|
||||
**/
|
||||
function _getHtmlOptions($options) {
|
||||
function _getHtmlOptions(&$options) {
|
||||
$htmlKeys = array('class', 'id', 'escape', 'onblur', 'onfocus', 'rel', 'title');
|
||||
$htmlOptions = array();
|
||||
foreach ($htmlKeys as $key) {
|
||||
if (isset($options[$key])) {
|
||||
$htmlOptions[$key] = $options[$key];
|
||||
}
|
||||
unset($options[$key]);
|
||||
}
|
||||
return $htmlOptions;
|
||||
}
|
||||
|
|
|
@ -83,7 +83,7 @@ class JsHelperTestCase extends CakeTestCase {
|
|||
*/
|
||||
function startTest() {
|
||||
$this->Js =& new JsHelper('JsBase');
|
||||
$this->Js->Html =& new HtmlHelper();
|
||||
$this->Js->Html =& new HtmlHelper();
|
||||
$this->Js->JsBaseEngine =& new JsBaseEngineHelper();
|
||||
|
||||
$view =& new JsHelperMockView();
|
||||
|
@ -99,6 +99,16 @@ class JsHelperTestCase extends CakeTestCase {
|
|||
ClassRegistry::removeObject('view');
|
||||
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
|
||||
*
|
||||
|
@ -123,15 +133,14 @@ class JsHelperTestCase extends CakeTestCase {
|
|||
* @return void
|
||||
**/
|
||||
function testMethodDispatching() {
|
||||
$js =& new JsHelper(array('TestJs'));
|
||||
$js->TestJsEngine =& new TestJsEngineHelper();
|
||||
$js->TestJsEngine->expectOnce('dispatchMethod', array('methodOne', array()));
|
||||
$this->_useMock();
|
||||
$this->Js->TestJsEngine->expectOnce('dispatchMethod', array('methodOne', array()));
|
||||
|
||||
$js->methodOne();
|
||||
$this->Js->methodOne();
|
||||
|
||||
$js->TestEngine =& new StdClass();
|
||||
$this->Js->TestEngine =& new StdClass();
|
||||
$this->expectError();
|
||||
$js->someMethodThatSurelyDoesntExist();
|
||||
$this->Js->someMethodThatSurelyDoesntExist();
|
||||
}
|
||||
/**
|
||||
* Test that method dispatching respects buffer parameters and bufferedMethods Lists.
|
||||
|
@ -139,47 +148,47 @@ class JsHelperTestCase extends CakeTestCase {
|
|||
* @return void
|
||||
**/
|
||||
function testMethodDispatchWithBuffering() {
|
||||
$js =& new JsHelper(array('TestJs'));
|
||||
$js->TestJsEngine = new TestJsEngineHelper();
|
||||
$js->TestJsEngine->bufferedMethods = array('event', 'sortables');
|
||||
$js->TestJsEngine->setReturnValue('dispatchMethod', 'This is an event call', array('event', '*'));
|
||||
$this->_useMock();
|
||||
|
||||
$js->event('click', 'foo');
|
||||
$result = $js->getBuffer();
|
||||
$this->Js->TestJsEngine->bufferedMethods = array('event', 'sortables');
|
||||
$this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'This is an event call', array('event', '*'));
|
||||
|
||||
$this->Js->event('click', 'foo');
|
||||
$result = $this->Js->getBuffer();
|
||||
$this->assertEqual(count($result), 1);
|
||||
$this->assertEqual($result[0], 'This is an event call');
|
||||
|
||||
$result = $js->event('click', 'foo', array('buffer' => false));
|
||||
$buffer = $js->getBuffer();
|
||||
$result = $this->Js->event('click', 'foo', array('buffer' => false));
|
||||
$buffer = $this->Js->getBuffer();
|
||||
$this->assertTrue(empty($buffer));
|
||||
$this->assertEqual($result, 'This is an event call');
|
||||
|
||||
$result = $js->event('click', 'foo', false);
|
||||
$buffer = $js->getBuffer();
|
||||
$result = $this->Js->event('click', 'foo', false);
|
||||
$buffer = $this->Js->getBuffer();
|
||||
$this->assertTrue(empty($buffer));
|
||||
$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');
|
||||
$buffer = $js->getBuffer();
|
||||
$result = $this->Js->effect('slideIn');
|
||||
$buffer = $this->Js->getBuffer();
|
||||
$this->assertTrue(empty($buffer));
|
||||
$this->assertEqual($result, 'I am not buffered.');
|
||||
|
||||
$result = $js->effect('slideIn', true);
|
||||
$buffer = $js->getBuffer();
|
||||
$result = $this->Js->effect('slideIn', true);
|
||||
$buffer = $this->Js->getBuffer();
|
||||
$this->assertNull($result);
|
||||
$this->assertEqual(count($buffer), 1);
|
||||
$this->assertEqual($buffer[0], 'I am not buffered.');
|
||||
|
||||
$result = $js->effect('slideIn', array('speed' => 'slow'), true);
|
||||
$buffer = $js->getBuffer();
|
||||
$result = $this->Js->effect('slideIn', array('speed' => 'slow'), true);
|
||||
$buffer = $this->Js->getBuffer();
|
||||
$this->assertNull($result);
|
||||
$this->assertEqual(count($buffer), 1);
|
||||
$this->assertEqual($buffer[0], 'I am not buffered.');
|
||||
|
||||
$result = $js->effect('slideIn', array('speed' => 'slow', 'buffer' => true));
|
||||
$buffer = $js->getBuffer();
|
||||
$result = $this->Js->effect('slideIn', array('speed' => 'slow', 'buffer' => true));
|
||||
$buffer = $this->Js->getBuffer();
|
||||
$this->assertNull($result);
|
||||
$this->assertEqual(count($buffer), 1);
|
||||
$this->assertEqual($buffer[0], 'I am not buffered.');
|
||||
|
@ -190,7 +199,7 @@ class JsHelperTestCase extends CakeTestCase {
|
|||
* @return void
|
||||
**/
|
||||
function testWriteScriptsNoFile() {
|
||||
$this->Js->JsBaseEngine = new TestJsEngineHelper();
|
||||
$this->_useMock();
|
||||
$this->Js->buffer('one = 1;');
|
||||
$this->Js->buffer('two = 2;');
|
||||
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => false));
|
||||
|
@ -203,7 +212,7 @@ class JsHelperTestCase extends CakeTestCase {
|
|||
);
|
||||
$this->assertTags($result, $expected, true);
|
||||
|
||||
$this->Js->JsBaseEngine->expectAtLeastOnce('domReady');
|
||||
$this->Js->TestJsEngine->expectAtLeastOnce('domReady');
|
||||
$result = $this->Js->writeBuffer(array('onDomReady' => true, 'cache' => false));
|
||||
|
||||
$view =& new JsHelperMockView();
|
||||
|
@ -240,6 +249,11 @@ class JsHelperTestCase extends CakeTestCase {
|
|||
* @return void
|
||||
**/
|
||||
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'));
|
||||
$expected = array(
|
||||
'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue