Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adding effects and effects tests to JqueryEngine
  • Loading branch information
markstory committed Mar 14, 2009
1 parent 94c4e18 commit 72e1c73
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
29 changes: 28 additions & 1 deletion cake/libs/view/helpers/jquery_engine.php
Expand Up @@ -24,7 +24,9 @@
* @lastmodified
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class jqueryEngineHelper extends AppHelper {
App::import('Helper', 'Js');

class jqueryEngineHelper extends JsBaseEngineHelper {
/**
* Create javascript selector for a CSS rule
*
Expand Down Expand Up @@ -74,5 +76,30 @@ function domReady($functionBody) {
function each($callback) {
return $this->selection . '.each(function () {' . $callback . '});';
}
/**
* Trigger an Effect.
*
* @param string $name The name of the effect to trigger.
* @param array $options Array of options for the effect.
* @return string completed string with effect.
* @see JsBaseEngineHelper::effect()
**/
function effect($name, $options = array()) {
$speed = null;
if (isset($options['speed']) && in_array($options['speed'], array('fast', 'slow'))) {
$speed = $this->value($options['speed']);
}
$effect = '';
switch ($name) {
case 'hide':
case 'show':
case 'fadeIn':
case 'fadeOut':
case 'toggle':
$effect = ".$name($speed);";
break;
}
return $this->selection . $effect;
}
}
?>
37 changes: 36 additions & 1 deletion cake/libs/view/helpers/js.php
Expand Up @@ -467,7 +467,7 @@ function writeCache($script) {
/**
* Get all the cached scripts
*
* @param boolean $clear Whether or not to clear the script cache.s
* @param boolean $clear Whether or not to clear the script caches
* @return array Array of scripts added to the request.
**/
function getCache($clear = true) {
Expand Down Expand Up @@ -517,6 +517,41 @@ function domReady($functionBody) {
function each($callback) {
trigger_error(sprintf(__('%s does not have each() implemented', true), get_class($this)), E_USER_WARNING);
}
/**
* Trigger an Effect.
*
* #### Supported Effects
*
* The following effects are supported by all JsEngines
*
* - 'show' - reveal an element.
* - 'hide' - hide an element.
* - 'fadeIn' - Fade in an element.
* - 'fadeOut' - Fade out an element.
* - 'toggle' - Toggle an element's visibility.
* - 'slideIn' - Slide an element in.
* - 'slideOut' - Slide an element out.
*
* #### Options
*
* - 'speed' - Speed at which the animation should occur. Accepted values are 'slow', 'fast'. Not all effects use
* the speed option.
*
* @param string $name The name of the effect to trigger.
* @param array $options Array of options for the effect.
* @return string completed string with effect.
**/
function effect($name, $options) {
trigger_error(sprintf(__('%s does not have effect() implemented', true), get_class($this)), E_USER_WARNING);
}
/**
* Make an XHR request
*
* @return string XHR request.
**/
function request() {
trigger_error(sprintf(__('%s does not have request() implemented', true), get_class($this)), E_USER_WARNING);
}
/**
* Parse an options assoc array into an Javascript object literal.
* Similar to object() but treats any non-integer value as a string,
Expand Down
29 changes: 29 additions & 0 deletions cake/tests/cases/libs/view/helpers/jquery_engine.test.php
Expand Up @@ -96,6 +96,35 @@ function testEach() {
$expected = "\$('#foo').each(function () {\$(this).hide();});";
$this->assertEqual($result, $expected);
}
/**
* test Effect generation
*
* @return void
**/
function testEffect() {
$result = $this->Jquery->get('#foo')->effect('show');
$expected = "\$('#foo').show();";
$this->assertEqual($result, $expected);

$result = $this->Jquery->effect('hide');
$expected = "\$('#foo').hide();";
$this->assertEqual($result, $expected);

$result = $this->Jquery->effect('hide', array('speed' => 'fast'));
$expected = "\$('#foo').hide(\"fast\");";
$this->assertEqual($result, $expected);

$result = $this->Jquery->effect('fadeIn');
$expected = "\$('#foo').fadeIn();";
$this->assertEqual($result, $expected);

$result = $this->Jquery->effect('fadeOut');
$expected = "\$('#foo').fadeOut();";
$this->assertEqual($result, $expected);

$result = $this->Jquery->effect('toggle');
$expected = "\$('#foo').toggle();";
$this->assertEqual($result, $expected);
}
}
?>

0 comments on commit 72e1c73

Please sign in to comment.