customevent.html revision 0d1c0eaa8a51a181149d802dacbe840f9990e7f5
3952N/A<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
3952N/A<html>
3952N/A<head>
3952N/A<title>YUI3 Custom Event Tests</title>
3952N/A<script type="text/javascript" src="/build/yui/yui-debug.js"></script>
3952N/A</head>
3952N/A
3952N/A<body class="yui-skin-sam">
3952N/A<h1>Event Tests</h1>
3952N/A<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
3952N/A<div id="adiv">a div</div>
3952N/A
3952N/A
3952N/A<script type="text/javascript">
3952N/A
3952N/A(function() {
3952N/A
3952N/A var global_notified;
3952N/A
3952N/A //YUI.add("selector-native", function(){});
3952N/A YUI({
3952N/A filter: "debug",
4305N/A // combine: false,
3952N/A useConsole: true,
3952N/A onCSS: function(Y) {
3952N/A Y.log('CSS is done loading', 'info', 'testcase');
3952N/A },
3952N/A // logInclude: ['event', 'test']
3952N/A logExclude: {
3952N/A 'deprecated': 1,
3952N/A get: true,
3952N/A Dom: true,
3952N/A Selector: true,
3952N/A Node: true,
3952N/A yui: true,
3952N/A attribute: true,
3952N/A event: true,
3952N/A // deprecated: true,
3952N/A base: true,
3952N/A loader: true,
3952N/A widget: true
3952N/A },
3952N/A
3952N/A filters: {
3952N/A base: 'raw',
3952N/A // dom: null,
3952N/A attribute: 'min'
3952N/A }
3952N/A }).use("dump", "test", "console", function(Y) {
3952N/A
3952N/A
4115N/A // Y.Global.on('yui:log', function(e) {
3952N/A // console.log('GLOBAL LOG: ' + e.msg);
3952N/A // });
3952N/A
3952N/A var button = Y.get('#btnRun');
3952N/A
3952N/A // Set up the page
3952N/A button.set("disabled", false);
3952N/A Y.on("click", function() {
3952N/A Y.Test.Runner.run();
3952N/A }, button);
3952N/A
3952N/A
3952N/A var myConsole = new Y.Console().render();
4115N/A Y.log('{}');
3952N/A
3952N/A var testEventTarget = new Y.Test.Case({
3952N/A name: "Custom event tests",
3952N/A
3952N/A testAugment: function() {
3952N/A
3952N/A var fired = false;
3952N/A
3952N/A var O = function(id) {
3952N/A this.id = id;
3952N/A Y.log('O constructor executed ' + id);
3952N/A }
3952N/A
3952N/A O.prototype = {
4305N/A oOo: function(ok) {
3952N/A Y.log('oOo');
3952N/A }
3952N/A }
3952N/A
3952N/A // pass configuration info into EventTarget with the following
3952N/A // construct
3952N/A Y.augment(O, Y.EventTarget, null, null, {
3952N/A emitFacade: true
3952N/A });
3952N/A
3952N/A var o = new O(),
4305N/A handle = o.on('testAugment', function(e, arg1, arg2) {
4305N/A Y.Assert.isTrue(this instanceof O);
3952N/A Y.Assert.isTrue(e instanceof Y.EventFacade);
3952N/A Y.Assert.isTrue(e.foo === 'afoo');
3952N/A Y.Assert.isTrue(e.details[1] === 1);
3952N/A Y.Assert.isTrue(arg1 === 1);
3952N/A Y.Assert.isTrue(arg2 === 2);
3952N/A fired = true;
3952N/A });
4115N/A
3952N/A o.fire('testAugment', { foo: 'afoo' }, 1, 2);
3952N/A
3952N/A Y.Assert.isTrue(fired);
3952N/A
3952N/A handle.detach();
3952N/A
3952N/A // if the first argument is not an object, the
3952N/A // event facade is moved in front of the args rather
3952N/A // than overwriting existing object.
3952N/A o.on('testAugment', function(e, arg1, arg2) {
3952N/A Y.Assert.areEqual(1, arg1);
3952N/A Y.Assert.areEqual(2, arg2);
3952N/A });
3952N/A
3952N/A o.fire('testAugment', 1, 2);
3952N/A
3952N/A },
3952N/A
3952N/A testExtend: function() {
3952N/A
3952N/A var fired = false;
3952N/A
3952N/A var Base = function() {
3952N/A Y.log('Base constructor executed');
3952N/A arguments.callee.superclass.constructor.apply(this, arguments);
3952N/A }
3952N/A
3952N/A Y.extend(Base, Y.EventTarget, {
3952N/A base: function() {
3952N/A Y.log('all your base...');
3952N/A }
3952N/A });
3952N/A
3952N/A var b = new Base();
3952N/A b.on('testExtend', function(arg1, arg2) {
4305N/A Y.Assert.isTrue(this instanceof Base);
3952N/A Y.Assert.isTrue(arg1 === 1);
3952N/A Y.Assert.isTrue(arg2 === 2);
3952N/A fired = true;
4115N/A });
3952N/A
3952N/A b.fire('testExtend', 1, 2);
3952N/A
3952N/A Y.Assert.isTrue(fired);
3952N/A },
3952N/A
3952N/A testPrefix: function() {
3952N/A
3952N/A var fired1 = false,
3952N/A fired2 = false;
3952N/A
3952N/A var O = function(id) {
3952N/A this.id = id;
3952N/A Y.log('O constructor executed ' + id);
3952N/A }
3952N/A
3952N/A O.prototype = {
3952N/A oOo: function(ok) {
3952N/A Y.log('oOo');
3952N/A }
3952N/A }
3952N/A
3952N/A // pass configuration info into EventTarget with the following
3952N/A // construct
3952N/A Y.augment(O, Y.EventTarget, null, null, {
3952N/A emitFacade: true,
3952N/A prefix: 'prefix'
3952N/A });
3952N/A
3952N/A var o = new O();
3952N/A o.on('testPrefix', function(e, arg1, arg2) {
3952N/A Y.Assert.isTrue(this instanceof O);
3952N/A fired1 = true;
3952N/A });
3952N/A
4115N/A o.on('prefix:testPrefix', function(e, arg1, arg2) {
3952N/A Y.Assert.isTrue(this instanceof O);
3952N/A fired2 = true;
3952N/A });
3952N/A
3952N/A o.fire('testPrefix', { foo: 'afoo' }, 1, 2);
3952N/A
3952N/A Y.Assert.isTrue(fired1);
3952N/A // Y.Assert.isTrue(fired2);
3952N/A
3952N/A fired1 = false;
4305N/A fired2 = false;
3952N/A
3952N/A o.fire('prefix:testPrefix', { foo: 'afoo' }, 1, 2);
3952N/A Y.Assert.isTrue(fired1);
3952N/A Y.Assert.isTrue(fired2);
3952N/A },
3952N/A
3952N/A testDetachKey: function() {
3952N/A
3952N/A var fired1 = false,
3952N/A fired2 = false;
4305N/A
4115N/A Y.on('handle|test:event', function() {
4115N/A fired1 = true;
3952N/A });
3952N/A
3952N/A // one listener
3952N/A Y.fire('test:event');
3952N/A Y.Assert.isTrue(fired1);
3952N/A Y.Assert.isFalse(fired2);
3952N/A
3952N/A Y.detach('handle|test:event');
3952N/A
3952N/A fired1 = false;
3952N/A fired2 = false;
3952N/A
3952N/A Y.on('handle|test:event', function() {
3952N/A fired2 = true;
3952N/A });
3952N/A
3952N/A // first lisener detached, added a new listener
3952N/A Y.fire('test:event');
3952N/A Y.Assert.isFalse(fired1);
3952N/A Y.Assert.isTrue(fired2);
3952N/A
3952N/A Y.detach('handle|test:event');
3952N/A fired1 = false;
3952N/A fired2 = false;
3952N/A
3952N/A Y.after('handle|test:event', function(arg1) {
3952N/A Y.Assert.areEqual('orange', arg1);
4305N/A Y.Assert.isTrue(fired1);
3952N/A fired2 = true;
3952N/A });
3952N/A
3952N/A // comma or pipe
3952N/A Y.on('handle|test:event', function(arg1) {
3952N/A Y.Assert.areEqual('orange', arg1);
3952N/A Y.Assert.isFalse(fired2);
3952N/A fired1 = true;
3952N/A });
3952N/A
3952N/A // testing on and after order
3952N/A Y.fire('test:event', 'orange');
3952N/A
3952N/A fired1 = false;
3952N/A fired2 = false;
3952N/A
3952N/A // spaces after the comma or lack thereof should have
3952N/A // no effect on the addition or removal of listeners
3952N/A var ret = Y.detach('handle|test:event');
3952N/A
3952N/A Y.Assert.areEqual(Y, ret);
3952N/A
3952N/A // added both an on listener and an after listener,
3952N/A // then detached both
3952N/A Y.fire('test:event', 'orange');
3952N/A Y.Assert.isFalse(fired1);
3952N/A Y.Assert.isFalse(fired2);
4305N/A
3952N/A },
3952N/A
3952N/A testDetachAllByKey: function() {
3952N/A
3952N/A var fired1 = false,
3952N/A fired2 = false;
3952N/A
3952N/A Y.after('handle|event2', function() {
3952N/A fired2 = true;
3952N/A });
3952N/A
3952N/A Y.on('handle|event2', function() {
3952N/A fired1 = true;
3952N/A });
3952N/A
3952N/A // detachAll
3952N/A Y.detach('handle|*');
3952N/A
3952N/A Y.fire('event2');
3952N/A
3952N/A Y.Assert.isFalse(fired1, 'fired1, the after listener should not have fired.');
3952N/A Y.Assert.isFalse(fired2, 'fired2, the on listener should not have fired.');
3952N/A
3952N/A },
3952N/A
3952N/A testChain: function() {
3952N/A
3952N/A var fired1 = false,
3952N/A fired2 = false,
3952N/A fired3 = false,
3952N/A fired4 = false,
4115N/A fired5 = false;
3952N/A
3952N/A // should be executed once, after f2
3952N/A var f1 = function() {
3952N/A Y.Assert.isTrue(fired2);
3952N/A fired1 = true;
3952N/A };
3952N/A
3952N/A // should be executed once, before f1
3952N/A var f2 = function() {
3952N/A Y.Assert.isFalse(fired1);
3952N/A fired2 = true;
3952N/A };
3952N/A
3952N/A // should be executed once, different event from f1 and f2
3952N/A var f3 = function() {
3952N/A fired3 = true;
3952N/A };
3952N/A
3952N/A // detached before fired, should not executed
3952N/A var f4 = function() {
3952N/A fired4 = true;
4305N/A };
4305N/A
3952N/A // should fire once, preserving the custom prefix rather
3952N/A // than using the configured event target prefix
3952N/A var f5 = function() {
3952N/A fired5 = true;
3952N/A };
3952N/A
3952N/A // configure chaining via global default or on the event target
3952N/A YUI({ /* chain: true */
3952N/A base:'/build/',
3952N/A logInclude: {
3952N/A test: true
3952N/A }
3952N/A }).use('*', function(Y2) {
3952N/A
3952N/A var o = new Y2.EventTarget({
3952N/A prefix: 'foo',
3952N/A chain : true
3952N/A });
3952N/A
3952N/A // without event target prefix manipulation (incomplete now)
3952N/A // @TODO an error here is throwing an uncaught exception rather than failing the test
3952N/A // Y2.after('p:e', f1).on('p:e', f2).on('p:e2', f3).on('detach, p:e', f4).detach('detach, p:e').fire('p:e').fire('p:e2');
3952N/A
4305N/A // with event target prefix manipulation ('e' is the same event as 'foo:e',
3952N/A // but 'pre:e' is a different event only accessible by using that exact name)
3952N/Ao.after('e', f1).on('foo:e', f2).on('foo:e2', f3).on('detach, e', f4).detach('detach,e').fire('foo:e').fire('e2').on('pre:e', f5).fire('pre:e');
3952N/A
3952N/A Y.Assert.isTrue(fired1); // verifies chaining, on/after order, and adding the event target prefix
3952N/A Y.Assert.isTrue(fired2); // verifies chaining, on/after order, and accepting the prefix in the event name
3952N/A Y.Assert.isTrue(fired3); // verifies no interaction between events, and prefix manipulation
3952N/A Y.Assert.isFalse(fired4); // verifies detach works (regardless of spaces after comma)
3952N/A Y.Assert.isTrue(fired5); // verifies custom prefix
3952N/A
3952N/A });
3952N/A
3952N/A },
3952N/A
3952N/A testObjType: function() {
3952N/A var f1, f2;
3952N/A Y.on({
3952N/A 'y:click': function() {f1 = true},
3952N/A 'y:clack': function() {f2 = true}
3952N/A });
3952N/A
3952N/A Y.fire('y:click');
3952N/A Y.fire('y:clack');
3952N/A
3952N/A Y.Assert.isTrue(f1);
3952N/A Y.Assert.isTrue(f2);
3952N/A },
3952N/A
3952N/A testBubble: function() {
3952N/A var count = 0,
3952N/A ret,
3952N/A config = {
3952N/A emitFacade: true,
3952N/A bubbles: true
3952N/A },
3952N/A a = new Y.EventTarget(config),
3952N/A b = new Y.EventTarget(config);
3952N/A
3952N/A b.addTarget(a);
3952N/A
3952N/A // this should not be necessary // fixed
3952N/A // b.publish('test:foo');
3952N/A
3952N/A a.on('test:foo', function(e) {
3952N/A count++;
3952N/A // we will fire this on the parent, so that should be the target
3952N/A Y.Assert.areEqual(b, e.target);
3952N/A Y.Assert.areEqual(a, e.currentTarget);
3952N/A });
3952N/A
3952N/A ret = b.fire('test:foo', {}, b);
3952N/A
3952N/A Y.Assert.areEqual(1, count);
3952N/A Y.Assert.isTrue(ret);
3952N/A
3952N/A b.on('test:foo', function(e) {
3952N/A e.stopPropagation();
3952N/A });
3952N/A
3952N/A ret = b.fire('test:foo', {}, b);
3952N/A
3952N/A Y.Assert.areEqual(1, count);
3952N/A Y.Assert.isFalse(ret);
3952N/A },
3952N/A
3952N/A
3952N/A testPreventFnOnce: function() {
3952N/A var count = 0;
3952N/A Y.publish('y:foo1', {
3952N/A emitFacade: true,
3952N/A preventedFn: function() {
4305N/A count++;
4305N/A Y.Assert.isTrue(this instanceof YUI);
4305N/A }
4305N/A });
3952N/A
3952N/A Y.on('y:foo1', function(e) {
3952N/A e.preventDefault();
3952N/A });
3952N/A
3952N/A Y.on('y:foo1', function(e) {
3952N/A e.preventDefault();
3952N/A });
3952N/A
3952N/A Y.fire('y:foo1');
3952N/A
3952N/A Y.Assert.areEqual(1, count);
3952N/A },
3952N/A
3952N/A testDetachHandle: function() {
4115N/A var count = 0, handle, handle2;
3952N/A Y.publish('y:foo', {
3952N/A emitFacade: true
3952N/A });
3952N/A
3952N/A Y.on('y:foo', function(e) {
3952N/A count++;
3952N/A handle2.detach();
3952N/A });
3952N/A
3952N/A handle = Y.on('y:foo', function(e) {
3952N/A count += 100;
3952N/A });
3952N/A
3952N/A handle2 = Y.on('y:foo', function(e) {
3952N/A count += 1000;
3952N/A });
3952N/A
3952N/A Y.detach(handle);
3952N/A
4115N/A Y.fire('y:foo');
3952N/A
3952N/A Y.Assert.areEqual(1, count);
3952N/A
3952N/A count = 0;
3952N/A
3952N/A var handle3 = Y.on('y:click', function() {
3952N/A count++;
3952N/A handle3.detach();
3952N/A });
3952N/A
3952N/A Y.fire('y:click');
3952N/A Y.fire('y:click');
4115N/A
3952N/A var o = new Y.EventTarget();
3952N/A
3952N/A count = 0;
3952N/A
3952N/A o.on('foo', function(e) {
3952N/A count++;
3952N/A });
3952N/A
3952N/A o.on('foo', function(e) {
3952N/A count++;
3952N/A });
3952N/A
3952N/A o.detachAll();
3952N/A
3952N/A o.fire('foo');
3952N/A
3952N/A Y.Assert.areEqual(0, count);
3952N/A
3952N/A var handle3 = Y.on('y:click', function() {
3952N/A count++;
3952N/A });
3952N/A
3952N/A // detachAll can't be allowed to work on the YUI instance.
3952N/A Y.detachAll();
3952N/A
3952N/A Y.fire('y:click');
3952N/A
3952N/A Y.Assert.areEqual(1, count);
4305N/A },
3952N/A
3952N/A testBroadcast: function() {
3952N/A var o = new Y.EventTarget(), s1, s2, s3, s4;
3952N/A
3952N/A o.publish('y:foo2', {
3952N/A emitFacade: true,
3952N/A broadcast: 1
3952N/A });
3952N/A
3952N/A Y.on('y:foo2', function() {
3952N/A Y.log('Y foo2 executed');
3952N/A s1 = 1;
3952N/A });
3952N/A
3952N/A Y.Global.on('y:foo2', function() {
3952N/A Y.log('GLOBAL foo2 executed');
3952N/A s2 = 1;
3952N/A });
3952N/A
3952N/A o.fire('y:foo2');
3952N/A
3952N/A Y.Assert.areEqual(1, s1);
3952N/A Y.Assert.areNotEqual(1, s2);
3952N/A
3952N/A s1 = 0;
3952N/A s2 = 0;
3952N/A
3952N/A o.publish('y:bar', {
3952N/A emitFacade: true,
3952N/A broadcast: 2
3952N/A });
3952N/A
3952N/A Y.on('y:bar', function() {
3952N/A Y.log('Y bar executed');
3952N/A s3 = 1;
3952N/A });
3952N/A
3952N/A Y.Global.on('y:bar', function() {
3952N/A Y.log('GLOBAL bar executed');
3952N/A s4 = 1;
3952N/A });
3952N/A
3952N/A o.fire('y:bar');
3952N/A
3952N/A Y.Assert.areEqual(1, s3);
3952N/A Y.Assert.areEqual(1, s4);
3952N/A
3952N/A
3952N/A Y.Global.on('y:bar', function(e) {
3952N/A Y.Assert.areEqual(0, e.stopped);
3952N/A // Y.Assert.areEqual(0, e._event.stopped);
3952N/A Y.log('GLOBAL bar executed');
3952N/A e.stopPropagation();
3952N/A });
3952N/A
3952N/A o.fire('y:bar');
3952N/A o.fire('y:bar');
3952N/A
3952N/A },
3952N/A
3952N/A test_fire_once: function() {
3952N/A
3952N/A var notified = 0;
3952N/A
3952N/A Y.publish('fireonce', {
4305N/A fireOnce: true
4305N/A });
3952N/A
3952N/A Y.fire('fireonce', 'foo', 'bar');
3952N/A
4115N/A Y.on('fireonce', function(arg1, arg2) {
3952N/A notified++;
4115N/A Y.Assert.areEqual('foo', arg1, 'arg1 not correct for lazy fireOnce listener')
3952N/A Y.Assert.areEqual('bar', arg2, 'arg2 not correct for lazy fireOnce listener')
3952N/A });
3952N/A
3952N/A Y.fire('fireonce', 'foo2', 'bar2');
3952N/A Y.fire('fireonce', 'foo3', 'bar3');
3952N/A
3952N/A global_notified = false;
4305N/A
3952N/A Y.on('fireonce', function(arg1, arg2) {
3952N/A Y.log('the notification is asynchronous, so I need to wait for this test');
3952N/A Y.Assert.areEqual(1, notified, 'listener notified more than once.');
3952N/A global_notified = true;
3952N/A });
3952N/A
3952N/A Y.Assert.isFalse(global_notified, 'notification was not asynchronous');
3952N/A
3952N/A },
3952N/A
3952N/A test_async_fireonce: function() {
3952N/A Y.Assert.isTrue(global_notified, 'asynchronous notification did not seem to work.');
3952N/A },
3952N/A
3952N/A test_node_publish: function() {
3952N/A var node = Y.one('#adiv');
3952N/A
3952N/A var preventCount = 0, heard = 0;
3952N/A node.publish('foo1', {
3952N/A emitFacade: true,
3952N/A // should only be called once
3952N/A preventedFn: function() {
3952N/A preventCount++;
3952N/A Y.Assert.isTrue(this instanceof Y.Node);
3952N/A }
3952N/A });
3952N/A
3952N/A node.on('foo1', function(e) {
3952N/A Y.Assert.areEqual('faking foo', e.type);
3952N/A Y.Assert.areEqual('foo1', e._type);
3952N/A heard++;
3952N/A e.preventDefault();
3952N/A });
3952N/A
3952N/A node.on('foo1', function(e) {
3952N/A heard++;
3952N/A e.preventDefault();
3952N/A });
3952N/A
3952N/A node.fire('foo1', {
3952N/A type: 'faking foo'
3952N/A });
4115N/A
3952N/A Y.Assert.areEqual(1, preventCount);
3952N/A Y.Assert.areEqual(2, heard);
4305N/A },
4305N/A
3952N/A // SRC, ON
3952N/A // BUBBLE, ON
3952N/A // BUBBLE, DEFAULT BEHAVIOR
3952N/A // BUBBLE, AFTER
3952N/A // SRC, DEFAULT BEHAVIOR
3952N/A // SRC, AFTER
3952N/A __testBubbleSequence300GA: function() {
3952N/A var count = 0,
3952N/A called = null,
3952N/A fn = function() {
3952N/A called = this.name;
3952N/A },
3952N/A config = {
3952N/A emitFacade: true,
3952N/A bubbles: true
3952N/A },
3952N/A leaf = new Y.EventTarget(config),
3952N/A branch = new Y.EventTarget(config),
3952N/A root = new Y.EventTarget(config);
4115N/A
3952N/A leaf.name = 'leaf';
3952N/A branch.name = 'branch';
3952N/A root.name = 'root';
3952N/A
3952N/A leaf.addTarget(branch);
3952N/A branch.addTarget(root);
3952N/A
3952N/A leaf.publish('test:foo', { defaultFn: fn});
3952N/A branch.publish('test:foo', { defaultFn: fn});
3952N/A root.publish('test:foo', { defaultFn: fn});
3952N/A
3952N/A leaf.on('test:foo', function(e) {
3952N/A Y.Assert.areEqual(0, count, 'leaf.on should be first');
3952N/A Y.Assert.isNull(called, 'leaf.on should be executed before any default function');
3952N/A count++;
3952N/A });
3952N/A
3952N/A branch.on('test:foo', function() {
3952N/A Y.Assert.areEqual(1, count, 'branch.on should be second');
3952N/A Y.Assert.isNull(called, 'branch.on should be executed before any default function');
3952N/A count++;
3952N/A });
3952N/A
3952N/A root.on('test:foo', function() {
3952N/A Y.Assert.areEqual(2, count, 'root.on should be third');
3952N/A Y.Assert.isNull(called, 'root.on should be executed before any default function');
3952N/A count++;
3952N/A });
3952N/A
3952N/A root.after('test:foo', function() {
3952N/A Y.Assert.areEqual(3, count, 'root.after should be fourth');
3952N/A Y.Assert.areEqual('root', called, 'root.after should be executed after the root default function');
3952N/A count++;
3952N/A });
3952N/A
3952N/A branch.after('test:foo', function() {
3952N/A Y.Assert.areEqual(4, count, 'branch.after should be fifth');
3952N/A Y.Assert.areEqual('branch', called, 'branch.after should be executed after the branch default function');
3952N/A count++;
3952N/A });
3952N/A
3952N/A leaf.after('test:foo', function(e) {
3952N/A Y.Assert.areEqual(5, count, 'leaf.after should be sixth and last');
4305N/A Y.Assert.areEqual('leaf', called, 'leaf.after should be executed after the leaf default function');
4305N/A count++;
3952N/A });
3952N/A
3952N/A leaf.fire('test:foo', {}, leaf);
3952N/A Y.Assert.areEqual(6, count);
3952N/A
3952N/A },
3952N/A
3952N/A // Ideally it should be this, but the defaultFn order is the least important bit
3952N/A // and there are issues changing the order.
3952N/A // SRC, ON
3952N/A // BUBBLE, ON
3952N/A // SRC, DEFAULT BEHAVIOR
3952N/A // BUBBLE, DEFAULT BEHAVIOR (unless configured to only execute the default function on the target)
3952N/A // SRC, AFTER
3952N/A // BUBBLE, AFTER
3952N/A
3952N/A // The actual order is this:
3952N/A // SRC, ON
3952N/A // BUBBLE, ON
3952N/A // BUBBLE, DEFAULT BEHAVIOR
3952N/A // SRC, DEFAULT BEHAVIOR (unless configured to only execute the default function on the target)
3952N/A // SRC, AFTER
3952N/A // BUBBLE, AFTER
3952N/A testAlternativeSequencePost300GA: function() {
3952N/A var count = 0,
3952N/A called = null,
3952N/A fn = function() {
3952N/A called = this.name;
3952N/A },
3952N/A config = {
3952N/A emitFacade: true,
3952N/A bubbles: true
3952N/A },
3952N/A leaf = new Y.EventTarget(config),
3952N/A branch = new Y.EventTarget(config),
4305N/A root = new Y.EventTarget(config);
3952N/A
4305N/A leaf.name = 'leaf';
3952N/A branch.name = 'branch';
4305N/A root.name = 'root';
3952N/A
4305N/A leaf.addTarget(branch);
4115N/A branch.addTarget(root);
3952N/A
3952N/A leaf.publish('test:foo', { defaultFn: fn});
3952N/A branch.publish('test:foo', { defaultFn: fn});
3952N/A root.publish('test:foo', { defaultFn: fn});
3952N/A
3952N/A leaf.on('test:foo', function(e) {
3952N/A Y.Assert.areEqual(0, count, 'leaf.on should be first');
3952N/A Y.Assert.isNull(called, 'leaf.on should be executed before any default function');
3952N/A count++;
3952N/A });
3952N/A
3952N/A branch.on('test:foo', function() {
3952N/A Y.Assert.areEqual(1, count, 'branch.on should be second');
3952N/A Y.Assert.isNull(called, 'branch.on should be executed before any default function');
3952N/A count++;
3952N/A });
3952N/A
3952N/A root.on('test:foo', function() {
3952N/A Y.Assert.areEqual(2, count, 'root.on should be third');
3952N/A Y.Assert.isNull(called, 'root.on should be executed before any default function');
3952N/A count++;
3952N/A });
3952N/A
3952N/A leaf.after('test:foo', function(e) {
3952N/A Y.Assert.areEqual(3, count, 'leaf.after should be fourth');
3952N/A // Y.Assert.areEqual('root', called, 'leaf.after should be executed after the root default function');
3952N/A Y.Assert.areEqual('leaf', called, 'leaf.after should be executed after the root default function');
3952N/A count++;
3952N/A });
3952N/A
3952N/A branch.after('test:foo', function() {
3952N/A Y.Assert.areEqual(4, count, 'branch.after should be fifth');
3952N/A // Y.Assert.areEqual('root', called, 'leaf.after should be executed after the root default function');
3952N/A Y.Assert.areEqual('leaf', called, 'leaf.after should be executed after the root default function');
3952N/A count++;
3952N/A });
3952N/A
3952N/A root.after('test:foo', function() {
3952N/A Y.Assert.areEqual(5, count, 'root.after should be sixth and last');
3952N/A // Y.Assert.areEqual('root', called, 'leaf.after should be executed after the root default function');
3952N/A Y.Assert.areEqual('leaf', called, 'leaf.after should be executed after the root default function');
3952N/A count++;
3952N/A });
3952N/A
3952N/A leaf.fire('test:foo', {}, leaf);
4115N/A Y.Assert.areEqual(6, count, 'total subscriber count');
3952N/A
3952N/A },
3952N/A
3952N/A testStarSubscriber: function() {
3952N/A var count = 0,
3952N/A ret,
3952N/A config = {
3952N/A emitFacade: true,
3952N/A bubbles: true,
3952N/A prefix: 'stars'
3952N/A },
3952N/A z = new Y.EventTarget(config),
3952N/A a = new Y.EventTarget(config),
3952N/A b = new Y.EventTarget(config);
3952N/A
3952N/A b.addTarget(a);
3952N/A a.addTarget(z);
3952N/A
3952N/A z.on('*:foo', function(e) {
3952N/A count++;
3952N/A // b -> a -> z -- the parent's parent should be the target
3952N/A Y.Assert.areEqual(b, e.target);
3952N/A Y.Assert.areEqual(z, e.currentTarget);
3952N/A switch (count) {
3952N/A case 1:
3952N/A Y.Assert.areEqual('a:foo', e.type);
3952N/A break;
3952N/A case 2:
3952N/A Y.Assert.areEqual('b:foo', e.type);
3952N/A break;
3952N/A case 3:
3952N/A Y.Assert.areEqual('stars:foo', e.type);
3952N/A break;
3952N/A }
3952N/A });
4305N/A
3952N/A ret = b.fire('a:foo', {}, b);
3952N/A
3952N/A Y.Assert.areEqual(1, count);
3952N/A Y.Assert.isTrue(ret);
3952N/A
3952N/A ret = b.fire('b:foo', {}, b);
3952N/A
3952N/A Y.Assert.areEqual(2, count);
3952N/A Y.Assert.isTrue(ret);
3952N/A
4305N/A // if the event target is not configured with a prefix, this won't work by design.
3952N/A ret = b.fire('foo', {}, b);
3952N/A Y.Assert.areEqual(3, count);
3952N/A
3952N/A Y.Assert.isTrue(ret);
3952N/A },
3952N/A
3952N/A testPreventBubble: function() {
3952N/A var count = 0,
3952N/A ret,
3952N/A config = {
3952N/A emitFacade: true,
3952N/A bubbles: true,
3952N/A prefix: 'stars'
3952N/A },
3952N/A z = new Y.EventTarget(config),
3952N/A a = new Y.EventTarget(config),
3952N/A b = new Y.EventTarget(config);
3952N/A
3952N/A b.addTarget(a);
3952N/A a.addTarget(z);
3952N/A
3952N/A z.after('*:foo', function(e) {
3952N/A
3952N/A // e.preventDefault();
3952N/A Y.Assert.areEqual(b, e.target);
3952N/A
3952N/A });
3952N/A
3952N/A ret = b.fire('a:foo', {}, b);
3952N/A
3952N/A Y.Assert.areEqual(0, count);
3952N/A },
3952N/A
3952N/A test_listen_once: function() {
3952N/A
3952N/A var count = 0;
3952N/A
3952N/A Y.once('foo', function(e) {
3952N/A count++;
3952N/A });
3952N/A
3952N/A Y.fire('foo', 'bar');
3952N/A
3952N/A Y.Assert.areEqual(1, count);
3952N/A
3952N/A Y.fire('foo', 'bar');
4305N/A
4305N/A Y.Assert.areEqual(1, count);
4305N/A
4305N/A }
4305N/A });
4305N/A
4305N/A Y.Test.Runner.add(testEventTarget);
4305N/A Y.Test.Runner.run();
4305N/A });
4305N/A
3952N/A // YUI({
3952N/A // base: "/build/",
3952N/A // filter: "debug",
3952N/A // combine: false,
3952N/A // useConsole: true,
3952N/A // logExclude: {Dom: true, Selector: true, Node: true, attribute: true, base: true, loader: true, get: true, widget: true}
4305N/A // }).use("datasource", function(Y) {
// Y.log('loaded datasource: ' + Y.DataSource);
// });
})();
</script>
</body>
</html>