Permalink
Browse files

Adding event() and tests to prototype library

  • Loading branch information...
1 parent da2361d commit 8cc5990e09c3b2cf46caab130e2a01dc4e5a5214 @markstory markstory committed Mar 28, 2009
@@ -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
@@ -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

0 comments on commit 8cc5990

Please sign in to comment.