Adding event() and tests to prototype library

This commit is contained in:
mark_story 2009-03-28 12:08:34 -04:00
parent da2361de14
commit 8cc5990e09
2 changed files with 23 additions and 1 deletions

View file

@ -2,7 +2,8 @@
/**
* Prototype Engine Helper for JsHelper
*
* Provides Prototype specific Javascript for JsHelper.
* Provides Prototype specific Javascript for JsHelper. Requires at least
* Prototype 1.6
*
* PHP versions 4 and 5
*
@ -67,7 +68,18 @@ class PrototypeEngineHelper extends JsBaseEngineHelper {
* @return string completed event handler
**/
function event($type, $callback, $options = array()) {
$defaults = array('wrap' => true, 'stop' => true);
$options = array_merge($defaults, $options);
$function = 'function (event) {%s}';
if ($options['wrap'] && $options['stop']) {
$callback = "event.stop();\n" . $callback;
}
if ($options['wrap']) {
$callback = sprintf($function, $callback);
}
$out = $this->selection . ".observe(\"{$type}\", $callback);";
return $out;
}
/**
* Create a domReady event. This is a special event in many libraries

View file

@ -76,7 +76,17 @@ class PrototypeEngineHelperTestCase extends CakeTestCase {
* @return void
**/
function testEvent() {
$result = $this->Proto->get('#myLink')->event('click', 'doClick', array('wrap' => false));
$expected = '$("myLink").observe("click", doClick);';
$this->assertEqual($result, $expected);
$result = $this->Proto->get('#myLink')->event('click', 'Element.hide(this);', array('stop' => false));
$expected = '$("myLink").observe("click", function (event) {Element.hide(this);});';
$this->assertEqual($result, $expected);
$result = $this->Proto->get('#myLink')->event('click', 'Element.hide(this);');
$expected = "\$(\"myLink\").observe(\"click\", function (event) {event.stop();\nElement.hide(this);});";
$this->assertEqual($result, $expected);
}
/**
* test dom ready event creation