1405
1420
* @submodule event-delegate
1408
var Event = Y.Event,
1414
mouseenter: "mouseover",
1415
mouseleave: "mouseout"
1418
resolveTextNode = function(n) {
1420
if (n && 3 == n.nodeType) {
1421
return n.parentNode;
1427
delegateHandler = function(delegateKey, e, el) {
1429
var target = resolveTextNode((e.target || e.srcElement)),
1430
tests = delegates[delegateKey],
1438
var getMatch = function(el, selector, container) {
1442
if (!el || el === container) {
1446
returnVal = Y.Selector.test(el, selector, container) ? el: getMatch(el.parentNode, selector, container);
1454
for (spec in tests) {
1456
if (tests.hasOwnProperty(spec)) {
1458
ename = tests[spec];
1463
if (Y.Selector.test(target, spec, el)) {
1466
else if (Y.Selector.test(target, ((spec.replace(/,/gi, " *,")) + " *"), el)) {
1468
// The target is a descendant of an element matching
1469
// the selector, so crawl up to find the ancestor that
1470
// matches the selector
1472
matched = getMatch(target, spec, el);
1480
ev = new Y.DOMEventFacade(e, el);
1481
ev.container = ev.currentTarget;
1484
ev.currentTarget = Y.one(matched);
1487
contextFn: function() {
1488
return ev.currentTarget;
1506
attach = function (type, key, element) {
1508
var focusMethods = {
1509
focus: Event._attachFocus,
1510
blur: Event._attachBlur
1513
attachFn = focusMethods[type],
1517
delegateHandler(key, (e || window.event), element);
1523
return attachFn(args, { capture: true, facade: false });
1526
return Event._attach(args, { facade: false });
1531
sanitize = Y.cached(function(str) {
1532
return str.replace(/[|,:]/g, '~');
1423
var toArray = Y.Array,
1425
isString = YLang.isString,
1426
selectorTest = Y.Selector.test,
1427
detachCategories = Y.Env.evt.handles;
1430
* <p>Sets up event delegation on a container element. The delegated event
1431
* will use a supplied selector or filtering function to test if the event
1432
* references at least one node that should trigger the subscription
1435
* <p>Selector string filters will trigger the callback if the event originated
1436
* from a node that matches it or is contained in a node that matches it.
1437
* Function filters are called for each Node up the parent axis to the
1438
* subscribing container node, and receive at each level the Node and the event
1439
* object. The function should return true (or a truthy value) if that Node
1440
* should trigger the subscription callback. Note, it is possible for filters
1441
* to match multiple Nodes for a single event. In this case, the delegate
1442
* callback will be executed for each matching Node.</p>
1444
* <p>For each matching Node, the callback will be executed with its 'this'
1445
* object set to the Node matched by the filter (unless a specific context was
1446
* provided during subscription), and the provided event's
1447
* <code>currentTarget</code> will also be set to the matching Node. The
1448
* containing Node from which the subscription was originally made can be
1449
* referenced as <code>e.container</code>.
1452
* @param type {String} the event type to delegate
1453
* @param fn {Function} the callback function to execute. This function
1454
* will be provided the event object for the delegated event.
1455
* @param el {String|node} the element that is the delegation container
1456
* @param spec {string|Function} a selector that must match the target of the
1457
* event or a function to test target and its parents for a match
1458
* @param context optional argument that specifies what 'this' refers to.
1459
* @param args* 0..n additional arguments to pass on to the callback function.
1460
* These arguments will be added after the event object.
1461
* @return {EventHandle} the detach handle
1464
function delegate(type, fn, el, filter) {
1465
var args = toArray(arguments, 0, true),
1466
query = isString(el) ? el : null,
1467
typeBits = type.split(/\|/),
1468
synth, container, categories, cat, handle;
1470
if (typeBits.length > 1) {
1471
cat = typeBits.shift();
1472
type = typeBits.shift();
1475
synth = Y.Node.DOM_EVENTS[type];
1477
if (YLang.isObject(synth) && synth.delegate) {
1478
handle = synth.delegate.apply(synth, arguments);
1482
if (!type || !fn || !el || !filter) {
1483
Y.log("delegate requires type, callback, parent, & filter", "warn");
1487
container = (query) ? Y.Selector.query(query, null, true) : el;
1489
if (!container && isString(el)) {
1490
handle = Y.on('available', function () {
1491
Y.mix(handle, Y.delegate.apply(Y, args), true);
1495
if (!handle && container) {
1496
args.splice(2, 2, container); // remove the filter
1498
if (isString(filter)) {
1499
filter = Y.delegate.compileFilter(filter);
1502
handle = Y.on.apply(Y, args);
1503
handle.sub.filter = filter;
1504
handle.sub._notify = delegate.notifySub;
1508
if (handle && cat) {
1509
categories = detachCategories[cat] || (detachCategories[cat] = {});
1510
categories = categories[type] || (categories[type] = []);
1511
categories.push(handle);
1518
* Overrides the <code>_notify</code> method on the normal DOM subscription to inject the filtering logic and only proceed in the case of a match.
1520
* @method delegate.notifySub
1521
* @param thisObj {Object} default 'this' object for the callback
1522
* @param args {Array} arguments passed to the event's <code>fire()</code>
1523
* @param ce {CustomEvent} the custom event managing the DOM subscriptions for
1524
* the subscribed event on the subscribing node.
1525
* @return {Boolean} false if the event was stopped
1530
delegate.notifySub = function (thisObj, args, ce) {
1531
// Preserve args for other subscribers
1532
args = args.slice();
1534
args.push.apply(args, this.args);
1537
// Only notify subs if the event occurred on a targeted element
1539
currentTarget = delegate._applyFilter(this.filter, args),
1540
container = e.currentTarget,
1543
if (currentTarget) {
1544
// Support multiple matches up the the container subtree
1545
currentTarget = toArray(currentTarget);
1547
for (i = currentTarget.length - 1; i >= 0; --i) {
1548
target = currentTarget[i];
1550
// New facade to avoid corrupting facade sent to direct subs
1551
args[0] = new Y.DOMEventFacade(e, target, ce);
1553
args[0].container = container;
1555
thisObj = this.context || target;
1557
ret = this.fn.apply(thisObj, args);
1559
if (ret === false) { // stop further notifications
1569
* <p>Compiles a selector string into a filter function to identify whether
1570
* Nodes along the parent axis of an event's target should trigger event
1573
* <p>This function is memoized, so previously compiled filter functions are
1574
* returned if the same selector string is provided.</p>
1576
* <p>This function may be useful when defining synthetic events for delegate
1579
* @method delegate.compileFilter
1580
* @param selector {String} the selector string to base the filtration on
1581
* @return {Function}
1585
delegate.compileFilter = Y.cached(function (selector) {
1586
return function (target, e) {
1587
return selectorTest(target._node, selector, e.currentTarget._node);
1592
* Walks up the parent axis of an event's target, and tests each element
1593
* against a supplied filter function. If any Nodes satisfy the filter, the
1594
* delegated callback will be triggered for each.
1596
* @method delegate._applyFilter
1597
* @param filter {Function} boolean function to test for inclusion in event
1599
* @param args {Array} the arguments that would be passed to subscribers
1600
* @return {Node|Node[]|undefined} The Node or Nodes that satisfy the filter
1603
delegate._applyFilter = function (filter, args) {
1605
container = e.currentTarget,
1609
// passing target as the first arg rather than leaving well enough alone
1610
// making 'this' in the filter function refer to the target. This is to
1611
// support bound filter functions.
1612
args.unshift(target);
1614
while (target && target !== container) {
1615
// filter(target, e, extra args...) - this === target
1616
if (filter.apply(target, args)) {
1619
args[0] = target = target.get('parentNode');
1622
if (match.length <= 1) {
1623
match = match[0]; // single match or undefined
1626
// remove the target
1538
1633
* Sets up event delegation on a container element. The delegated event
1539
* will use a supplied selector to test if the target or one of the
1540
* descendants of the target match it. The supplied callback function
1541
* will only be executed if a match was encountered, and, in fact,
1542
* will be executed for each element that matches if you supply an
1543
* ambiguous selector.
1634
* will use a supplied filter to test if the callback should be executed.
1635
* This filter can be either a selector string or a function that returns
1636
* a Node to use as the currentTarget for the event.
1545
1638
* The event object for the delegated event is supplied to the callback
1546
1639
* function. It is modified slightly in order to support all properties
1547
1640
* that may be needed for event delegation. 'currentTarget' is set to
1548
* the element that matched the delegation specifcation. 'container' is
1549
* set to the element that the listener is bound to (this normally would
1550
* be the 'currentTarget').
1641
* the element that matched the selector string filter or the Node returned
1642
* from the filter function. 'container' is set to the element that the
1643
* listener is delegated from (this normally would be the 'currentTarget').
1645
* Filter functions will be called with the arguments that would be passed to
1646
* the callback function, including the event object as the first parameter.
1647
* The function should return false (or a falsey value) if the success criteria
1648
* aren't met, and the Node to use as the event's currentTarget and 'this'
1649
* object if they are.
1552
1651
* @method delegate
1553
1652
* @param type {string} the event type to delegate
1554
1653
* @param fn {function} the callback function to execute. This function
1555
1654
* will be provided the event object for the delegated event.
1556
1655
* @param el {string|node} the element that is the delegation container
1557
* @param spec {string} a selector that must match the target of the
1656
* @param filter {string|function} a selector that must match the target of the
1657
* event or a function that returns a Node or false.
1559
1658
* @param context optional argument that specifies what 'this' refers to.
1560
1659
* @param args* 0..n additional arguments to pass on to the callback function.
1561
1660
* These arguments will be added after the event object.
1562
1661
* @return {EventHandle} the detach handle
1565
Event.delegate = function (type, fn, el, spec) {
1568
Y.log('delegate: no spec, nothing to do', 'warn', 'event');
1664
Y.delegate = Y.Event.delegate = delegate;
1667
}, '3.2.0' ,{requires:['node-base']});
1668
YUI.add('event-synthetic', function(Y) {
1671
* Define new DOM events that can be subscribed to from Nodes.
1674
* @submodule event-synthetic
1676
var DOMMap = Y.Env.evt.dom_map,
1679
isObject = YLang.isObject,
1680
isString = YLang.isString,
1681
query = Y.Selector.query,
1682
noop = function () {};
1685
* <p>The triggering mechanism used by SyntheticEvents.</p>
1687
* <p>Implementers should not instantiate these directly. Use the Notifier
1688
* provided to the event's implemented <code>on(node, sub, notifier)</code> or
1689
* <code>delegate(node, sub, notifier, filter)</code> methods.</p>
1691
* @class SyntheticEvent.Notifier
1693
* @param handle {EventHandle} the detach handle for the subscription to an
1694
* internal custom event used to execute the callback passed to
1695
* on(..) or delegate(..)
1696
* @param emitFacade {Boolean} take steps to ensure the first arg received by
1697
* the subscription callback is an event facade
1701
function Notifier(handle, emitFacade) {
1702
this.handle = handle;
1703
this.emitFacade = emitFacade;
1707
* <p>Executes the subscription callback, passing the firing arguments as the
1708
* first parameters to that callback. For events that are configured with
1709
* emitFacade=true, it is common practice to pass the triggering DOMEventFacade
1710
* as the first parameter. Barring a proper DOMEventFacade or EventFacade
1711
* (from a CustomEvent), a new EventFacade will be generated. In that case, if
1712
* fire() is called with a simple object, it will be mixed into the facade.
1713
* Otherwise, the facade will be prepended to the callback parameters.</p>
1715
* <p>For notifiers provided to delegate logic, the first argument should be an
1716
* object with a "currentTarget" property to identify what object to
1717
* default as 'this' in the callback. Typically this is gleaned from the
1718
* DOMEventFacade or EventFacade, but if configured with emitFacade=false, an
1719
* object must be provided. In that case, the object will be removed from the
1720
* callback parameters.</p>
1722
* <p>Additional arguments passed during event subscription will be
1723
* automatically added after those passed to fire().</p>
1726
* @param e {EventFacade|DOMEventFacade|Object|any} (see description)
1727
* @param arg* {any} additional arguments received by all subscriptions
1730
Notifier.prototype.fire = function (e) {
1731
// first arg to delegate notifier should be an object with currentTarget
1732
var args = toArray(arguments, 0, true),
1733
handle = this.handle,
1736
thisObj = sub.context,
1737
delegate = sub.filter,
1740
if (this.emitFacade) {
1741
if (!e || !e.preventDefault) {
1742
event = ce._getFacade();
1744
if (isObject(e) && !e.preventDefault) {
1745
Y.mix(event, e, true);
1748
args.unshift(event);
1752
event.type = ce.type;
1753
event.details = args.slice();
1756
event.container = ce.host;
1758
} else if (delegate && isObject(e) && e.currentTarget) {
1573
var args = Y.Array(arguments, 0, true),
1574
element = el, // HTML element serving as the delegation container
1578
if (Lang.isString(el)) {
1580
// Y.Selector.query returns an array of matches unless specified
1581
// to return just the first match. Since the primary use case for
1582
// event delegation is to use a single event handler on a container,
1583
// Y.delegate doesn't currently support being able to bind a
1584
// single listener to multiple containers.
1586
element = Y.Selector.query(el, null, true);
1588
if (!element) { // Not found, check using onAvailable
1590
availHandle = Event.onAvailable(el, function() {
1592
availHandle.handle = Event.delegate.apply(Event, args);
1594
}, Event, true, false);
1603
element = Y.Node.getDOMNode(element);
1606
var guid = Y.stamp(element),
1762
sub.context = thisObj || event.currentTarget || ce.host;
1763
ce.fire.apply(ce, args);
1764
sub.context = thisObj; // reset for future firing
1769
* <p>Wrapper class for the integration of new events into the YUI event
1770
* infrastructure. Don't instantiate this object directly, use
1771
* <code>Y.Event.define(type, config)</code>. See that method for details.</p>
1773
* <p>Properties that MAY or SHOULD be specified in the configuration are noted
1774
* below and in the description of <code>Y.Event.define</code>.</p>
1776
* @class SyntheticEvent
1778
* @param cfg {Object} Implementation pieces and configuration
1780
* @in event-synthetic
1782
function SyntheticEvent() {
1783
this._init.apply(this, arguments);
1786
Y.mix(SyntheticEvent, {
1790
* Returns the array of subscription handles for a node for the given event
1791
* type. Passing true as the third argument will create a registry entry
1792
* in the event system's DOM map to host the array if one doesn't yet exist.
1794
* @method getRegistry
1795
* @param node {Node} the node
1796
* @param type {String} the event
1797
* @param create {Boolean} create a registration entry to host a new array
1798
* if one doesn't exist.
1804
getRegistry: function (node, type, create) {
1805
var el = node._node,
1807
key = 'event:' + yuid + type + '_synth',
1808
events = DOMMap[yuid] || (DOMMap[yuid] = {});
1810
if (!events[key] && create) {
1820
detachAll : function () {
1821
var notifiers = this.notifiers,
1822
i = notifiers.length;
1825
notifiers[i].detach();
1831
return (events[key]) ? events[key].notifiers : null;
1835
* Alternate <code>_delete()</code> method for the CustomEvent object
1836
* created to manage SyntheticEvent subscriptions.
1838
* @method _deleteSub
1839
* @param sub {Subscription} the subscription to clean up
1843
_deleteSub: function (sub) {
1844
if (sub && sub.fn) {
1845
var synth = this.eventDef,
1846
method = (sub.filter) ? 'detachDelegate' : 'detach';
1848
this.subscribers = {};
1851
synth[method](sub.node, sub, this.notifier, sub.filter);
1852
synth._unregisterSub(sub);
1861
constructor: SyntheticEvent,
1864
* Construction logic for the event.
1869
_init: function () {
1870
var config = this.publishConfig || (this.publishConfig = {});
1872
// The notification mechanism handles facade creation
1873
this.emitFacade = ('emitFacade' in config) ?
1876
config.emitFacade = false;
1880
* <p>Implementers MAY provide this method definition.</p>
1882
* <p>Implement this function if the event supports a different
1883
* subscription signature. This function is used by both
1884
* <code>on()</code> and <code>delegate()</code>. The second parameter
1885
* indicates that the event is being subscribed via
1886
* <code>delegate()</code>.</p>
1888
* <p>Implementations must remove extra arguments from the args list
1889
* before returning. The required args for <code>on()</code>
1890
* subscriptions are</p>
1891
* <pre><code>[type, callback, target, context, argN...]</code></pre>
1893
* <p>The required args for <code>delegate()</code>
1894
* subscriptions are</p>
1896
* <pre><code>[type, callback, target, filter, context, argN...]</code></pre>
1898
* <p>The return value from this function will be stored on the
1899
* subscription in the '_extra' property for reference elsewhere.</p>
1901
* @method processArgs
1902
* @param args {Array} parmeters passed to Y.on(..) or Y.delegate(..)
1903
* @param delegate {Boolean} true if the subscription is from Y.delegate
1909
* <p>Implementers MAY override this property.</p>
1911
* <p>Whether to prevent multiple subscriptions to this event that are
1912
* classified as being the same. By default, this means the subscribed
1913
* callback is the same function. See the <code>subMatch</code>
1914
* method. Setting this to true will impact performance for high volume
1917
* @property preventDups
1921
//preventDups : false,
1924
* <p>Implementers SHOULD provide this method definition.</p>
1926
* Implementation logic for subscriptions done via <code>node.on(type,
1927
* fn)</code> or <code>Y.on(type, fn, target)</code>. This
1928
* function should set up the monitor(s) that will eventually fire the
1929
* event. Typically this involves subscribing to at least one DOM
1930
* event. It is recommended to store detach handles from any DOM
1931
* subscriptions to make for easy cleanup in the <code>detach</code>
1932
* method. Typically these handles are added to the <code>sub</code>
1933
* object. Also for SyntheticEvents that leverage a single DOM
1934
* subscription under the hood, it is recommended to pass the DOM event
1935
* object to <code>notifier.fire(e)</code>. (The event name on the
1936
* object will be updated).
1939
* @param node {Node} the node the subscription is being applied to
1940
* @param sub {Subscription} the object to track this subscription
1941
* @param notifier {SyntheticEvent.Notifier} call notifier.fire(..) to
1942
* trigger the execution of the subscribers
1947
* <p>Implementers SHOULD provide this method definition.</p>
1949
* <p>Implementation logic for detaching subscriptions done via
1950
* <code>node.on(type, fn)</code>. This function should clean up any
1951
* subscriptions made in the <code>on()</code> phase.</p>
1954
* @param node {Node} the node the subscription was applied to
1955
* @param sub {Subscription} the object tracking this subscription
1956
* @param notifier {SyntheticEvent.Notifier} the Notifier used to
1957
* trigger the execution of the subscribers
1962
* <p>Implementers SHOULD provide this method definition.</p>
1964
* <p>Implementation logic for subscriptions done via
1965
* <code>node.delegate(type, fn, filter)</code> or
1966
* <code>Y.delegate(type, fn, container, filter)</code>. Like with
1967
* <code>on()</code> above, this function should monitor the environment
1968
* for the event being fired, and trigger subscription execution by
1969
* calling <code>notifier.fire(e)</code>.</p>
1971
* <p>This function receives a fourth argument, which is the filter
1972
* used to identify which Node's are of interest to the subscription.
1973
* The filter will be either a boolean function that accepts a target
1974
* Node for each hierarchy level as the event bubbles, or a selector
1975
* string. To translate selector strings into filter functions, use
1976
* <code>Y.delegate.compileFilter(filter)</code>.</p>
1979
* @param node {Node} the node the subscription is being applied to
1980
* @param sub {Subscription} the object to track this subscription
1981
* @param notifier {SyntheticEvent.Notifier} call notifier.fire(..) to
1982
* trigger the execution of the subscribers
1983
* @param filter {String|Function} Selector string or function that
1984
* accepts an event object and returns null, a Node, or an
1985
* array of Nodes matching the criteria for processing.
1991
* <p>Implementers SHOULD provide this method definition.</p>
1993
* <p>Implementation logic for detaching subscriptions done via
1994
* <code>node.delegate(type, fn, filter)</code> or
1995
* <code>Y.delegate(type, fn, container, filter)</code>. This function
1996
* should clean up any subscriptions made in the
1997
* <code>delegate()</code> phase.</p>
1999
* @method detachDelegate
2000
* @param node {Node} the node the subscription was applied to
2001
* @param sub {Subscription} the object tracking this subscription
2002
* @param notifier {SyntheticEvent.Notifier} the Notifier used to
2003
* trigger the execution of the subscribers
2004
* @param filter {String|Function} Selector string or function that
2005
* accepts an event object and returns null, a Node, or an
2006
* array of Nodes matching the criteria for processing.
2009
detachDelegate : noop,
2012
* Sets up the boilerplate for detaching the event and facilitating the
2013
* execution of subscriber callbacks.
2016
* @param args {Array} array of arguments passed to
2017
* <code>Y.on(...)</code> or <code>Y.delegate(...)</code>
2018
* @param delegate {Boolean} true if called from
2019
* <code>Y.delegate(...)</code>
2020
* @return {EventHandle} the detach handle for this subscription
2024
_on: function (args, delegate) {
2027
method = delegate ? 'delegate' : 'on',
2030
// Can't just use Y.all because it doesn't support window (yet?)
2031
nodes = (isString(selector)) ? query(selector) : toArray(selector);
2033
if (!nodes.length && isString(selector)) {
2034
handle = Y.on('available', function () {
2035
Y.mix(handle, Y[method].apply(Y, args), true);
2041
Y.each(nodes, function (node) {
2042
var subArgs = args.slice(),
2048
extra = this.processArgs(subArgs, delegate);
2051
filter = subArgs.splice(3, 1)[0];
2054
// (type, fn, el, thisObj, ...) => (fn, thisObj, ...)
2055
subArgs.splice(0, 4, subArgs[1], subArgs[3]);
2057
if (!this.preventDups || !this.getSubs(node, args,null,true)) {
2058
handle = this._getNotifier(node, subArgs, extra,filter);
2060
this[method](node, handle.sub, handle.notifier, filter);
2062
handles.push(handle);
2067
return (handles.length === 1) ?
2069
new Y.EventHandle(handles);
2073
* Creates a new Notifier object for use by this event's
2074
* <code>on(...)</code> or <code>delegate(...)</code> implementation.
2076
* @method _getNotifier
2077
* @param node {Node} the Node hosting the event
2078
* @param args {Array} the subscription arguments passed to either
2079
* <code>Y.on(...)</code> or <code>Y.delegate(...)</code>
2080
* after running through <code>processArgs(args)</code> to
2081
* normalize the argument signature
2082
* @param extra {any} Extra data parsed from
2083
* <code>processArgs(args)</code>
2084
* @param filter {String|Function} the selector string or function
2085
* filter passed to <code>Y.delegate(...)</code> (not
2086
* present when called from <code>Y.on(...)</code>)
2087
* @return {SyntheticEvent.Notifier}
2091
_getNotifier: function (node, args, extra, filter) {
2092
var dispatcher = new Y.CustomEvent(this.type, this.publishConfig),
2093
handle = dispatcher.on.apply(dispatcher, args),
2094
notifier = new Notifier(handle, this.emitFacade),
2095
registry = SyntheticEvent.getRegistry(node, this.type, true),
2098
handle.notifier = notifier;
2101
sub.filter = filter;
2106
notifier : notifier,
2107
host : node, // I forget what this is for
2108
currentTarget: node, // for generating facades
2109
target : node, // for generating facades
2110
el : node._node, // For category detach
2112
_delete : SyntheticEvent._deleteSub
2115
registry.push(handle);
2121
* Removes the subscription from the Notifier registry.
2123
* @method _unregisterSub
2124
* @param sub {Subscription} the subscription
2128
_unregisterSub: function (sub) {
2129
var notifiers = SyntheticEvent.getRegistry(sub.node, this.type),
2133
for (i = notifiers.length - 1; i >= 0; --i) {
2134
if (notifiers[i].sub === sub) {
2135
notifiers.splice(i, 1);
2143
* Removes the subscription(s) from the internal subscription dispatch
2144
* mechanism. See <code>SyntheticEvent._deleteSub</code>.
2147
* @param args {Array} The arguments passed to
2148
* <code>node.detach(...)</code>
2152
_detach: function (args) {
2153
// Can't use Y.all because it doesn't support window (yet?)
2154
// TODO: Does Y.all support window now?
2155
var target = args[2],
2156
els = (isString(target)) ?
2157
query(target) : toArray(target),
2158
node, i, len, handles, j;
1608
// The Custom Event for the delegation spec
1609
ename = 'delegate:' + guid + type + sanitize(spec),
1611
// The key to the listener for the event type and container
1612
delegateKey = type + guid,
1614
delegate = delegates[delegateKey],
1627
if (specialTypes[type]) {
1629
if (!Event._fireMouseEnter) {
1630
Y.log("Delegating a " + type + " event requires the event-mouseenter submodule.", "error", "Event");
1634
type = specialTypes[type];
1635
delegate.fn = Event._fireMouseEnter;
1639
// Create the DOM Event wrapper that will fire the Custom Event
1641
domEventHandle = attach(type, delegateKey, element);
1644
// Hook into the _delete method for the Custom Event wrapper of this
1645
// DOM Event in order to clean up the 'delegates' map and unsubscribe
1646
// the associated Custom Event listeners fired by this DOM event
1647
// listener if/when the user calls "purgeElement" OR removes all
1648
// listeners of the Custom Event.
1650
Y.after(function (sub) {
1652
if (domEventHandle.sub == sub) {
1654
// Delete this event from the map of known delegates
1655
delete delegates[delegateKey];
1657
Y.log("DOM event listener associated with the " + ename + " Custom Event removed. Removing all " + ename + " listeners.", "info", "Event");
1659
// Unsubscribe all listeners of the Custom Event fired
1660
// by this DOM event.
1665
}, domEventHandle.evt, "_delete");
1667
delegate.handle = domEventHandle;
1669
delegates[delegateKey] = delegate;
1674
listeners = delegate.listeners;
1676
delegate.listeners = listeners ? (listeners + 1) : 1;
1677
delegate[spec] = ename;
1682
// Remove element, delegation spec
1686
// Subscribe to the Custom Event for the delegation spec
1688
ceHandle = Y.on.apply(Y, args);
1691
// Hook into the detach method of the handle in order to clean up the
1692
// 'delegates' map and remove the associated DOM event handler
1693
// responsible for firing this Custom Event if all listener for this
1694
// event have been removed.
1696
Y.after(function () {
1698
delegate.listeners = (delegate.listeners - 1);
1700
if (delegate.listeners === 0) {
1701
Y.log("No more listeners for the " + ename + " Custom Event. Removing its associated DOM event listener.", "info", "Event");
1702
delegate.handle.detach();
1705
}, ceHandle, "detach");
2160
// (type, fn, el, context, filter?) => (type, fn, context, filter?)
2163
for (i = 0, len = els.length; i < len; ++i) {
2164
node = Y.one(els[i]);
2167
handles = this.getSubs(node, args);
2170
for (j = handles.length - 1; j >= 0; --j) {
2171
handles[j].detach();
2179
* Returns the detach handles of subscriptions on a node that satisfy a
2180
* search/filter function. By default, the filter used is the
2181
* <code>subMatch</code> method.
2184
* @param node {Node} the node hosting the event
2185
* @param args {Array} the array of original subscription args passed
2186
* to <code>Y.on(...)</code> (before
2187
* <code>processArgs</code>
2188
* @param filter {Function} function used to identify a subscription
2189
* for inclusion in the returned array
2190
* @param first {Boolean} stop after the first match (used to check for
2191
* duplicate subscriptions)
2192
* @return {Array} detach handles for the matching subscriptions
2194
getSubs: function (node, args, filter, first) {
2195
var notifiers = SyntheticEvent.getRegistry(node, this.type),
2201
filter = this.subMatch;
2204
for (i = 0, len = notifiers.length; i < len; ++i) {
2205
handle = notifiers[i];
2206
if (filter.call(this, handle.sub, args)) {
2210
handles.push(notifiers[i]);
2216
return handles.length && handles;
2220
* <p>Implementers MAY override this to define what constitutes a
2221
* "same" subscription. Override implementations should
2222
* consider the lack of a comparator as a match, so calling
2223
* <code>getSubs()</code> with no arguments will return all subs.</p>
2225
* <p>Compares a set of subscription arguments against a Subscription
2226
* object to determine if they match. The default implementation
2227
* compares the callback function against the second argument passed to
2228
* <code>Y.on(...)</code> or <code>node.detach(...)</code> etc.</p>
2231
* @param sub {Subscription} the existing subscription
2232
* @param args {Array} the calling arguments passed to
2233
* <code>Y.on(...)</code> etc.
2234
* @return {Boolean} true if the sub can be described by the args
2238
subMatch: function (sub, args) {
2239
// Default detach cares only about the callback matching
2240
return !args[1] || sub.fn === args[1];
2245
Y.SyntheticEvent = SyntheticEvent;
2248
* <p>Defines a new event in the DOM event system. Implementers are
2249
* responsible for monitoring for a scenario whereby the event is fired. A
2250
* notifier object is provided to the functions identified below. When the
2251
* criteria defining the event are met, call notifier.fire( [args] ); to
2252
* execute event subscribers.</p>
2254
* <p>The first parameter is the name of the event. The second parameter is a
2255
* configuration object which define the behavior of the event system when the
2256
* new event is subscribed to or detached from. The methods that should be
2257
* defined in this configuration object are <code>on</code>,
2258
* <code>detach</code>, <code>delegate</code>, and <code>detachDelegate</code>.
2259
* You are free to define any other methods or properties needed to define your
2260
* event. Be aware, however, that since the object is used to subclass
2261
* SyntheticEvent, you should avoid method names used by SyntheticEvent unless
2262
* your intention is to override the default behavior.</p>
2264
* <p>This is a list of properties and methods that you can or should specify
2265
* in the configuration object:</p>
2268
* <dt><code>on</code></dt>
2269
* <dd><code>function (node, subscription, notifier)</code> The
2270
* implementation logic for subscription. Any special setup you need to
2271
* do to create the environment for the event being fired--E.g. native
2272
* DOM event subscriptions. Store subscription related objects and
2273
* state on the <code>subscription</code> object. When the
2274
* criteria have been met to fire the synthetic event, call
2275
* <code>notifier.fire(e)</code>. See Notifier's <code>fire()</code>
2276
* method for details about what to pass as parameters.</dd>
2278
* <dt><code>detach</code></dt>
2279
* <dd><code>function (node, subscription, notifier)</code> The
2280
* implementation logic for cleaning up a detached subscription. E.g.
2281
* detach any DOM subscriptions added in <code>on</code>.</dd>
2283
* <dt><code>delegate</code></dt>
2284
* <dd><code>function (node, subscription, notifier, filter)</code> The
2285
* implementation logic for subscription via <code>Y.delegate</code> or
2286
* <code>node.delegate</code>. The filter is typically either a selector
2287
* string or a function. You can use
2288
* <code>Y.delegate.compileFilter(selectorString)</code> to create a
2289
* filter function from a selector string if needed. The filter function
2290
* expects an event object as input and should output either null, a
2291
* matching Node, or an array of matching Nodes. Otherwise, this acts
2292
* like <code>on</code> DOM event subscriptions. Store subscription
2293
* related objects and information on the <code>subscription</code>
2294
* object. When the criteria have been met to fire the synthetic event,
2295
* call <code>notifier.fire(e)</code> as noted above.</dd>
2297
* <dt><code>detachDelegate</code></dt>
2298
* <dd><code>function (node, subscription, notifier)</code> The
2299
* implementation logic for cleaning up a detached delegate subscription.
2300
* E.g. detach any DOM delegate subscriptions added in
2301
* <code>delegate</code>.</dd>
2303
* <dt><code>publishConfig</code></dt>
2304
* <dd>(Object) The configuration object that will be used to instantiate
2305
* the underlying CustomEvent. See Notifier's <code>fire</code> method
2308
* <dt><code>processArgs</code></dt
2310
* <p><code>function (argArray, fromDelegate)</code> Optional method
2311
* to extract any additional arguments from the subscription
2312
* signature. Using this allows <code>on</code> or
2313
* <code>delegate</code> signatures like
2314
* <code>node.on("hover", overCallback,
2315
* outCallback)</code>.</p>
2316
* <p>When processing an atypical argument signature, make sure the
2317
* args array is returned to the normal signature before returning
2318
* from the function. For example, in the "hover" example
2319
* above, the <code>outCallback</code> needs to be <code>splice</code>d
2320
* out of the array. The expected signature of the args array for
2321
* <code>on()</code> subscriptions is:</p>
2323
* <code>[type, callback, target, contextOverride, argN...]</code>
2325
* <p>And for <code>delegate()</code>:</p>
2327
* <code>[type, callback, target, filter, contextOverride, argN...]</code>
2329
* <p>where <code>target</code> is the node the event is being
2330
* subscribed for. You can see these signatures documented for
2331
* <code>Y.on()</code> and <code>Y.delegate()</code> respectively.</p>
2332
* <p>Whatever gets returned from the function will be stored on the
2333
* <code>subscription</code> object under
2334
* <code>subscription._extra</code>.</p></dd>
2335
* <dt><code>subMatch</code></dt>
2337
* <p><code>function (sub, args)</code> Compares a set of
2338
* subscription arguments against a Subscription object to determine
2339
* if they match. The default implementation compares the callback
2340
* function against the second argument passed to
2341
* <code>Y.on(...)</code> or <code>node.detach(...)</code> etc.</p>
2345
* @method Event.define
2346
* @param type {String} the name of the event
2347
* @param config {Object} the prototype definition for the new event (see above)
2348
* @param force {Boolean} override an existing event (use with caution)
2350
* @return {SyntheticEvent} the subclass implementation instance created to
2351
* handle event subscriptions of this type
2354
* @in event-synthetic
2356
Y.Event.define = function (type, config, force) {
2361
var eventDef = (isObject(type)) ? type : Y.merge({ type: type }, config),
2364
if (force || !Y.Node.DOM_EVENTS[eventDef.type]) {
2365
Impl = function () {
2366
SyntheticEvent.apply(this, arguments);
2368
Y.extend(Impl, SyntheticEvent, eventDef);
2373
Y.Node.DOM_EVENTS[type] = Y.Env.evt.plugins[type] = {
2377
return synth._on(toArray(arguments));
2380
delegate: function () {
2381
return synth._on(toArray(arguments), true);
2384
detach: function () {
2385
return synth._detach(toArray(arguments));
1711
Y.delegate = Event.delegate;
1714
}, '3.1.2' ,{requires:['node-base']});
2395
}, '3.2.0' ,{requires:['node-base', 'event-custom']});
1715
2396
YUI.add('event-mousewheel', function(Y) {
2054
}, '3.1.2' ,{requires:['node-base']});
2611
}, '3.2.0' ,{requires:['node-base']});
2055
2612
YUI.add('event-focus', function(Y) {
2058
* Adds focus and blur event listener support. These events normally
2059
* do not bubble, so this adds support for that so these events
2060
* can be used in event delegation scenarios.
2615
* Adds bubbling and delegation support to DOM events focus and blur.
2062
2617
* @module event
2063
2618
* @submodule event-focus
2069
plugins = Y.Env.evt.plugins,
2071
bUseMutation = (UA.opera || UA.webkit),
2073
focus: (ie ? 'focusin' : (bUseMutation ? 'DOMFocusIn' : 'focus')),
2074
blur: (ie ? 'focusout' : (bUseMutation ? 'DOMFocusOut' : 'blur'))
2077
// Only need to use capture phase for Gecko since it doesn't support
2078
// focusin, focusout, DOMFocusIn, or DOMFocusOut
2079
CAPTURE_CONFIG = { capture: (UA.gecko ? true : false) },
2082
attach = function (args, config) {
2084
var a = Y.Array(args, 0, true),
2087
config.overrides = config.overrides || {};
2088
config.overrides.type = args[0];
2620
var Event = Y.Event,
2622
isString = YLang.isString,
2623
useActivate = YLang.isFunction(
2624
Y.DOM.create('<p onbeforeactivate=";">').onbeforeactivate);
2626
function define(type, proxy, directEvent) {
2627
var nodeDataKey = '_' + type + 'Notifiers';
2629
Y.Event.define(type, {
2631
_attach: function (el, notifier, delegate) {
2091
2632
if (Y.DOM.isWindow(el)) {
2092
config.capture = false;
2095
a[0] = eventNames[a[0]];
2633
return Event._attach([type, function (e) {
2637
return Event._attach(
2638
[proxy, this._proxy, el, this, notifier, delegate],
2643
_proxy: function (e, notifier, delegate) {
2644
var node = e.target,
2645
notifiers = node.getData(nodeDataKey),
2646
yuid = Y.stamp(e.currentTarget._node),
2647
defer = (useActivate || e.target !== e.currentTarget),
2648
sub = notifier.handle.sub,
2649
filterArgs = [node, e].concat(sub.args || []),
2652
notifier.currentTarget = (delegate) ? node : e.currentTarget;
2653
notifier.container = (delegate) ? e.currentTarget : null;
2655
if (!sub.filter || sub.filter.apply(node, filterArgs)) {
2656
// Maintain a list to handle subscriptions from nested
2657
// containers div#a>div#b>input #a.on(focus..) #b.on(focus..),
2658
// use one focus or blur subscription that fires notifiers from
2659
// #b then #a to emulate bubble sequence.
2662
node.setData(nodeDataKey, notifiers);
2664
// only subscribe to the element's focus if the target is
2665
// not the current target (
2667
directSub = Event._attach(
2668
[directEvent, this._notify, node._node]).sub;
2669
directSub.once = true;
2673
if (!notifiers[yuid]) {
2674
notifiers[yuid] = [];
2677
notifiers[yuid].push(notifier);
2685
_notify: function (e, container) {
2686
var node = e.currentTarget,
2687
notifiers = node.getData(nodeDataKey),
2688
// document.get('ownerDocument') returns null
2689
doc = node.get('ownerDocument') || node,
2695
// Walk up the parent axis until the origin node,
2696
while (target && target !== doc) {
2697
nots.push.apply(nots, notifiers[Y.stamp(target)] || []);
2698
target = target.get('parentNode');
2700
nots.push.apply(nots, notifiers[Y.stamp(doc)] || []);
2702
for (i = 0, len = nots.length; i < len; ++i) {
2704
e.currentTarget = nots[i].currentTarget;
2706
if (notifier.container) {
2707
e.container = notifier.container;
2715
// clear the notifications list (mainly for delegation)
2716
node.clearData(nodeDataKey);
2720
on: function (node, sub, notifier) {
2721
sub.onHandle = this._attach(node._node, notifier);
2724
detach: function (node, sub) {
2725
sub.onHandle.detach();
2728
delegate: function (node, sub, notifier, filter) {
2729
if (isString(filter)) {
2730
sub.filter = Y.delegate.compileFilter(filter);
2733
sub.delegateHandle = this._attach(node._node, notifier, true);
2736
detachDelegate: function (node, sub) {
2737
sub.delegateHandle.detach();
2099
return Event._attach(a, config);
2106
return attach(arguments, CAPTURE_CONFIG);
2112
Event._attachFocus = attach;
2113
Event._attachBlur = attach;
2116
* Adds a DOM focus listener. Uses the focusin event in IE,
2117
* DOMFocusIn for Opera and Webkit, and the capture phase for Gecko so that
2118
* the event propagates in a way that enables event delegation.
2122
* @param type {string} 'focus'
2123
* @param fn {function} the callback function to execute
2124
* @param o {string|HTMLElement|collection} the element(s) to bind
2125
* @param context optional context object
2126
* @param args 0..n additional arguments to provide to the listener.
2127
* @return {EventHandle} the detach handle
2129
plugins.focus = eventAdapter;
2132
* Adds a DOM blur listener. Uses the focusout event in IE,
2133
* DOMFocusOut for Opera and Webkit, and the capture phase for Gecko so that
2134
* the event propagates in a way that enables event delegation.
2138
* @param type {string} 'blur'
2139
* @param fn {function} the callback function to execute
2140
* @param o {string|HTMLElement|collection} the element(s) to bind
2141
* @param context optional context object
2142
* @param args 0..n additional arguments to provide to the listener.
2143
* @return {EventHandle} the detach handle
2145
plugins.blur = eventAdapter;
2150
}, '3.1.2' ,{requires:['node-base']});
2742
// For IE, we need to defer to focusin rather than focus because
2743
// `el.focus(); doSomething();` executes el.onbeforeactivate, el.onactivate,
2744
// el.onfocusin, doSomething, then el.onfocus. All others support capture
2745
// phase focus, which executes before doSomething. To guarantee consistent
2746
// behavior for this use case, IE's direct subscriptions are made against
2747
// focusin so subscribers will be notified before js following el.focus() is
2750
// name capture phase direct subscription
2751
define("focus", "beforeactivate", "focusin");
2752
define("blur", "beforedeactivate", "focusout");
2754
define("focus", "focus", "focus");
2755
define("blur", "blur", "blur");
2759
}, '3.2.0' ,{requires:['event-synthetic']});
2151
2760
YUI.add('event-resize', function(Y) {