delegate.js revision 836a3a6b58636f23a6fefabe096fe1216b644b8b
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync/**
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync * Sets up a delegated listener container.
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync * @event delegate
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync * @param type {string} 'delegate'
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync * @param fn {string} the function to execute
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync * @param el {string|node} the element that is the delegation container
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync * @param event {string} the event type to delegate
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync * @param spec {string} a selector that must match the target of the
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync * event.
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync * @param o optional context object
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync * @param args 0..n additional arguments that should be provided
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync * to the listener.
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync * @return {Event.Handle} the detach handle
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync * @for YUI
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync */
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsyncY.Env.evt.plugins.delegate = {
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync on: function(type, fn, el, event, spec, o) {
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync var ename = 'delegate:' + (Y.Lang.isString(el) ? el : Y.stamp(el)) + event + spec,
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync a = Y.Array(arguments, 0, true);
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync
1dc25926f76c463686b66fb6adec583eef0717advboxsync if (!Y.getEvent(ename)) {
1dc25926f76c463686b66fb6adec583eef0717advboxsync
1dc25926f76c463686b66fb6adec583eef0717advboxsync // set up the listener on the container
1dc25926f76c463686b66fb6adec583eef0717advboxsync Y.on(event, function(e) {
1dc25926f76c463686b66fb6adec583eef0717advboxsync
1dc25926f76c463686b66fb6adec583eef0717advboxsync var target = e.target,
1dc25926f76c463686b66fb6adec583eef0717advboxsync passed = false;
1dc25926f76c463686b66fb6adec583eef0717advboxsync
1dc25926f76c463686b66fb6adec583eef0717advboxsync // @TODO we need Node.some
1dc25926f76c463686b66fb6adec583eef0717advboxsync e.currentTarget.queryAll(spec).each(function (v, k) {
1dc25926f76c463686b66fb6adec583eef0717advboxsync
1dc25926f76c463686b66fb6adec583eef0717advboxsync if ((!passed) && (v.compareTo(target) || v.contains(target))) {
1dc25926f76c463686b66fb6adec583eef0717advboxsync
1dc25926f76c463686b66fb6adec583eef0717advboxsync e.target = v;
1dc25926f76c463686b66fb6adec583eef0717advboxsync Y.fire(ename, e);
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync passed = true;
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync }
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync });
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync
1dc25926f76c463686b66fb6adec583eef0717advboxsync
1dc25926f76c463686b66fb6adec583eef0717advboxsync }, el);
1dc25926f76c463686b66fb6adec583eef0717advboxsync
1dc25926f76c463686b66fb6adec583eef0717advboxsync }
1dc25926f76c463686b66fb6adec583eef0717advboxsync
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync a[0] = ename;
1dc25926f76c463686b66fb6adec583eef0717advboxsync
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync // remove element, delegation spec and context object from the args
1dc25926f76c463686b66fb6adec583eef0717advboxsync a.splice(2, 3);
1dc25926f76c463686b66fb6adec583eef0717advboxsync
1dc25926f76c463686b66fb6adec583eef0717advboxsync // subscribe to the custom event for the delegation spec
1dc25926f76c463686b66fb6adec583eef0717advboxsync return Y.on.apply(Y, a);
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync }
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync
1205f5a22a14de7b9cf3055d6f914eac690c1715vboxsync};