Skip to content

Commit

Permalink
Switching quote styles in mootools engine
Browse files Browse the repository at this point in the history
Adding effect() and each() to mootools
Adding tests
  • Loading branch information
markstory committed Mar 15, 2009
1 parent 51a187f commit 87d68e5
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 21 deletions.
3 changes: 2 additions & 1 deletion cake/libs/view/helpers/jquery_engine.php
Expand Up @@ -118,7 +118,8 @@ function effect($name, $options = array()) {
case 'show': case 'show':
case 'fadeIn': case 'fadeIn':
case 'fadeOut': case 'fadeOut':
case 'toggle': case 'slideIn':
case 'slideOut':
$effect = ".$name($speed);"; $effect = ".$name($speed);";
break; break;
} }
Expand Down
1 change: 0 additions & 1 deletion cake/libs/view/helpers/js.php
Expand Up @@ -538,7 +538,6 @@ function each($callback) {
* - 'hide' - hide an element. * - 'hide' - hide an element.
* - 'fadeIn' - Fade in an element. * - 'fadeIn' - Fade in an element.
* - 'fadeOut' - Fade out an element. * - 'fadeOut' - Fade out an element.
* - 'toggle' - Toggle an element's visibility.
* - 'slideIn' - Slide an element in. * - 'slideIn' - Slide an element in.
* - 'slideOut' - Slide an element out. * - 'slideOut' - Slide an element out.
* *
Expand Down
47 changes: 39 additions & 8 deletions cake/libs/view/helpers/mootools_engine.php
Expand Up @@ -4,7 +4,7 @@
* *
* Provides MooTools specific Javascript for JsHelper. * Provides MooTools specific Javascript for JsHelper.
* Assumes that you have the following MooTools packages * Assumes that you have the following MooTools packages
* *
* - Remote, Remote.HTML, Remote.JSON * - Remote, Remote.HTML, Remote.JSON
* - Fx, Fx.Tween, Fx.Morph * - Fx, Fx.Tween, Fx.Morph
* - Selectors, DomReady, * - Selectors, DomReady,
Expand Down Expand Up @@ -55,10 +55,10 @@ function get($selector) {
return $this; return $this;
} }
if (preg_match('/^#[^\s.]+$/', $selector)) { if (preg_match('/^#[^\s.]+$/', $selector)) {
$this->selection = "$('" . substr($selector, 1) . "')"; $this->selection = '$("' . substr($selector, 1) . '")';
return $this; return $this;
} }
$this->selection = "$$('" . $selector ."')"; $this->selection = '$$("' . $selector . '")';
return $this; return $this;
} }
/** /**
Expand All @@ -77,15 +77,15 @@ function get($selector) {
function event($type, $callback, $options = array()) { function event($type, $callback, $options = array()) {
$defaults = array('wrap' => true, 'stop' => true); $defaults = array('wrap' => true, 'stop' => true);
$options = array_merge($defaults, $options); $options = array_merge($defaults, $options);

$function = 'function (event) {%s}'; $function = 'function (event) {%s}';
if ($options['wrap'] && $options['stop']) { if ($options['wrap'] && $options['stop']) {
$callback .= "\nreturn false;"; $callback .= "\nreturn false;";
} }
if ($options['wrap']) { if ($options['wrap']) {
$callback = sprintf($function, $callback); $callback = sprintf($function, $callback);
} }
$out = $this->selection . ".addEvent('{$type}', $callback);"; $out = $this->selection . ".addEvent(\"{$type}\", $callback);";
return $out; return $out;
} }
/** /**
Expand All @@ -95,7 +95,8 @@ function event($type, $callback, $options = array()) {
* @return string completed domReady method * @return string completed domReady method
**/ **/
function domReady($functionBody) { function domReady($functionBody) {

$this->selection = 'window';
return $this->event('domready', $functionBody, array('stop' => false));
} }
/** /**
* Create an iteration over the current selection result. * Create an iteration over the current selection result.
Expand All @@ -105,7 +106,7 @@ function domReady($functionBody) {
* @return string completed iteration * @return string completed iteration
**/ **/
function each($callback) { function each($callback) {

return $this->selection . '.each(function (item, index) {' . $callback . '});';
} }
/** /**
* Trigger an Effect. * Trigger an Effect.
Expand All @@ -116,7 +117,37 @@ function each($callback) {
* @see JsBaseEngineHelper::effect() * @see JsBaseEngineHelper::effect()
**/ **/
function effect($name, $options = array()) { function effect($name, $options = array()) {

$speed = null;
if (isset($options['speed']) && in_array($options['speed'], array('fast', 'slow'))) {
if ($options['speed'] == 'fast') {
$speed = '"short"';
} elseif ($options['speed'] == 'slow') {
$speed = '"long"';
}
}
$effect = '';
switch ($name) {
case 'hide':
$effect = 'setStyle("display", "none")';
break;
case 'show':
$effect = 'setStyle("display", "")';
break;
case 'fadeIn':
$effect = 'fade("in")';
break;
case 'fadeOut':
case 'slideIn':
case 'slideOut':
list($effectName, $direction) = preg_split('/([A-Z][a-z]+)/', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
$direction = strtolower($direction);
if ($speed) {
$effect .= "set(\"$effectName\", {duration:$speed}).";
}
$effect .= "$effectName(\"$direction\")";
break;
}
return $this->selection . '.' . $effect . ';';
} }
/** /**
* Create an $.ajax() call. * Create an $.ajax() call.
Expand Down
8 changes: 6 additions & 2 deletions cake/tests/cases/libs/view/helpers/jquery_engine.test.php
Expand Up @@ -126,8 +126,12 @@ function testEffect() {
$expected = "\$('#foo').fadeOut();"; $expected = "\$('#foo').fadeOut();";
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);


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

$result = $this->Jquery->effect('slideOut');
$expected = "\$('#foo').slideOut();";
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
/** /**
Expand Down
53 changes: 44 additions & 9 deletions cake/tests/cases/libs/view/helpers/mootools_engine.test.php
Expand Up @@ -48,11 +48,11 @@ function endTest() {
function testSelector() { function testSelector() {
$result = $this->Moo->get('#content'); $result = $this->Moo->get('#content');
$this->assertEqual($result, $this->Moo); $this->assertEqual($result, $this->Moo);
$this->assertEqual($this->Moo->selection, "$('content')"); $this->assertEqual($this->Moo->selection, '$("content")');


$result = $this->Moo->get('a .remove'); $result = $this->Moo->get('a .remove');
$this->assertEqual($result, $this->Moo); $this->assertEqual($result, $this->Moo);
$this->assertEqual($this->Moo->selection, "$$('a .remove')"); $this->assertEqual($this->Moo->selection, '$$("a .remove")');


$result = $this->Moo->get('document'); $result = $this->Moo->get('document');
$this->assertEqual($result, $this->Moo); $this->assertEqual($result, $this->Moo);
Expand All @@ -64,11 +64,11 @@ function testSelector() {


$result = $this->Moo->get('ul'); $result = $this->Moo->get('ul');
$this->assertEqual($result, $this->Moo); $this->assertEqual($result, $this->Moo);
$this->assertEqual($this->Moo->selection, "$$('ul')"); $this->assertEqual($this->Moo->selection, '$$("ul")');


$result = $this->Moo->get('#some_long-id.class'); $result = $this->Moo->get('#some_long-id.class');
$this->assertEqual($result, $this->Moo); $this->assertEqual($result, $this->Moo);
$this->assertEqual($this->Moo->selection, "$$('#some_long-id.class')"); $this->assertEqual($this->Moo->selection, '$$("#some_long-id.class")');
} }
/** /**
* test event binding * test event binding
Expand All @@ -77,15 +77,15 @@ function testSelector() {
**/ **/
function testEvent() { function testEvent() {
$result = $this->Moo->get('#myLink')->event('click', 'doClick', array('wrap' => false)); $result = $this->Moo->get('#myLink')->event('click', 'doClick', array('wrap' => false));
$expected = "$('myLink').addEvent('click', doClick);"; $expected = '$("myLink").addEvent("click", doClick);';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);


$result = $this->Moo->get('#myLink')->event('click', 'this.setStyle("display", "");', array('stop' => false)); $result = $this->Moo->get('#myLink')->event('click', 'this.setStyle("display", "");', array('stop' => false));
$expected = "$('myLink').addEvent('click', function (event) {this.setStyle(\"display\", \"\");});"; $expected = '$("myLink").addEvent("click", function (event) {this.setStyle("display", "");});';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);


$result = $this->Moo->get('#myLink')->event('click', 'this.setStyle("display", "none");'); $result = $this->Moo->get('#myLink')->event('click', 'this.setStyle("display", "none");');
$expected = "\$('myLink').addEvent('click', function (event) {this.setStyle(\"display\", \"none\");\nreturn false;});"; $expected = "\$(\"myLink\").addEvent(\"click\", function (event) {this.setStyle(\"display\", \"none\");\nreturn false;});";
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
/** /**
Expand All @@ -94,22 +94,57 @@ function testEvent() {
* @return void * @return void
**/ **/
function testDomReady() { function testDomReady() {

$result = $this->Moo->domReady('foo.name = "bar";');
$expected = 'window.addEvent("domready", function (event) {foo.name = "bar";});';
$this->assertEqual($result, $expected);
} }
/** /**
* test Each method * test Each method
* *
* @return void * @return void
**/ **/
function testEach() { function testEach() {

$result = $this->Moo->get('#foo')->each('item.setStyle("display", "none");');
$expected = '$("foo").each(function (item, index) {item.setStyle("display", "none");});';
$this->assertEqual($result, $expected);
} }
/** /**
* test Effect generation * test Effect generation
* *
* @return void * @return void
**/ **/
function testEffect() { function testEffect() {
$result = $this->Moo->get('#foo')->effect('show');
$expected = '$("foo").setStyle("display", "");';
$this->assertEqual($result, $expected);

$result = $this->Moo->effect('hide');
$expected = '$("foo").setStyle("display", "none");';
$this->assertEqual($result, $expected);

$result = $this->Moo->effect('fadeIn');
$expected = '$("foo").fade("in");';
$this->assertEqual($result, $expected);

$result = $this->Moo->effect('fadeOut');
$expected = '$("foo").fade("out");';
$this->assertEqual($result, $expected);

$result = $this->Moo->effect('slideIn');
$expected = '$("foo").slide("in");';
$this->assertEqual($result, $expected);

$result = $this->Moo->effect('slideOut');
$expected = '$("foo").slide("out");';
$this->assertEqual($result, $expected);

$result = $this->Moo->effect('slideOut', array('speed' => 'fast'));
$expected = '$("foo").set("slide", {duration:"short"}).slide("out");';
$this->assertEqual($result, $expected);

$result = $this->Moo->effect('slideOut', array('speed' => 'slow'));
$expected = '$("foo").set("slide", {duration:"long"}).slide("out");';
$this->assertEqual($result, $expected);


} }
/** /**
Expand Down

0 comments on commit 87d68e5

Please sign in to comment.