delegate.js revision d408aa66c7199d6b6a133c20c2116414dc70fa0a
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore(function() {
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moorevar delegates = {},
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore worker = function(delegateSpec, e) {
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore var target = e.target, passed, spec, tests = delegates[delegateSpec], ename;
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore for (spec in tests) {
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore if (tests.hasOwnProperty(spec)) {
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore passed = false;
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore ename = tests[spec];
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore // @TODO we need Node.some
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore e.currentTarget.queryAll(spec).each(function (v, k) {
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore if ((!passed) && (v.compareTo(target) || v.contains(target))) {
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore e.target = v;
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore Y.fire(ename, e);
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore }
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore });
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore }
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore }
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore };
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore/**
836a3a6b58636f23a6fefabe096fe1216b644b8bTodd Kloots * Sets up a delegated listener container.
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore * @event delegate
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore * @param type {string} 'delegate'
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore * @param fn {string} the function to execute
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore * @param el {string|node} the element that is the delegation container
c8c3d38f7a6c444775b0daff51926e2e5f3b35a8Adam Moore * @param delegateType {string} the event type to delegate
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore * @param spec {string} a selector that must match the target of the
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore * event.
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore * @param o optional context object
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore * @param args 0..n additional arguments that should be provided
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore * to the listener.
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore * @return {Event.Handle} the detach handle
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore * @for YUI
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore */
9f298ce1aa6fec44a47a40d6c358950d3c26ffd0Adam MooreY.Env.evt.plugins.delegate = {
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore
c8c3d38f7a6c444775b0daff51926e2e5f3b35a8Adam Moore on: function(type, fn, el, delegateType, spec, o) {
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore
c8c3d38f7a6c444775b0daff51926e2e5f3b35a8Adam Moore var ename = 'delegate:' + (Y.Lang.isString(el) ? el : Y.stamp(el)) + delegateType + spec,
0bb2673f6a4a24e3d3f8952c58484f657a9f8de4Adam Moore a = Y.Array(arguments, 0, true);
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore if (!(delegateType in delegates)) {
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore delegates[delegateType] = {};
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore
72190d990aacbb8af3d4e584c12bfb14e2f61536Adam Moore // set up the listener on the container
c8c3d38f7a6c444775b0daff51926e2e5f3b35a8Adam Moore Y.on(delegateType, function(e) {
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore worker(delegateType, e);
72190d990aacbb8af3d4e584c12bfb14e2f61536Adam Moore }, el);
836a3a6b58636f23a6fefabe096fe1216b644b8bTodd Kloots
72190d990aacbb8af3d4e584c12bfb14e2f61536Adam Moore }
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore delegates[delegateType][spec] = ename;
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore a[0] = ename;
3a1493afa5d204a301f770bf1e95318cf9a7bff8Adam Moore
836a3a6b58636f23a6fefabe096fe1216b644b8bTodd Kloots // remove element, delegation spec and context object from the args
3a1493afa5d204a301f770bf1e95318cf9a7bff8Adam Moore a.splice(2, 3);
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore
0bb2673f6a4a24e3d3f8952c58484f657a9f8de4Adam Moore // subscribe to the custom event for the delegation spec
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore return Y.on.apply(Y, a);
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore }
aadc0b0e666b9b335884a2437510798ae8949343Adam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore};
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore
d408aa66c7199d6b6a133c20c2116414dc70fa0aAdam Moore})();