From 8cc5990e09c3b2cf46caab130e2a01dc4e5a5214 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 28 Mar 2009 12:08:34 -0400 Subject: [PATCH] Adding event() and tests to prototype library --- cake/libs/view/helpers/prototype_engine.php | 14 +++++++++++++- .../libs/view/helpers/prototype_engine.test.php | 10 ++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/cake/libs/view/helpers/prototype_engine.php b/cake/libs/view/helpers/prototype_engine.php index 5d595a4c47f..755a69bd4a8 100644 --- a/cake/libs/view/helpers/prototype_engine.php +++ b/cake/libs/view/helpers/prototype_engine.php @@ -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 @@ function get($selector) { * @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 diff --git a/cake/tests/cases/libs/view/helpers/prototype_engine.test.php b/cake/tests/cases/libs/view/helpers/prototype_engine.test.php index c80a96013dd..82f4c81303f 100644 --- a/cake/tests/cases/libs/view/helpers/prototype_engine.test.php +++ b/cake/tests/cases/libs/view/helpers/prototype_engine.test.php @@ -76,7 +76,17 @@ function testSelector() { * @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