event-debug.js revision 3a1493afa5d204a301f770bf1e95318cf9a7bff8
0N/A * DOM event listener abstraction layer 0N/A // create custom event 0N/A ///////////////////////////////////////////////////////////// 92N/A // Internet Explorer: use the readyState of a defered script. 92N/A // This isolates what appears to be a safe moment to manipulate 92N/A // the DOM prior to when the document's readyState suggests 92N/A // it is safe to do so. 0N/A // throws an error if doc is not ready 92N/A // FireFox and Opera: These browsers provide a event for this 92N/A // moment. The latest WebKit releases now support this event. 0N/A ///////////////////////////////////////////////////////////// 92N/A * DOM event listener abstraction layer 92N/A * Executes the supplied callback when the DOM is first usable. This 92N/A * will execute immediately if called after the DOMReady event has 92N/A * fired. @todo the DOMContentReady event does not fire when the 92N/A * script is dynamically injected into the page. This means the 92N/A * DOMReady custom event will never fire in FireFox or Opera when the 154N/A * library is injected. It _will_ fire in Safari, and the IE 92N/A * implementation would allow for us to fire it if the defered script 92N/A * is not available. We want this to behave the same in all browsers. 92N/A * Is there a way to identify when the script has been injected 92N/A * instead of included inline? Is there a way to know whether the 92N/A * window onload event has fired without having had a listener attached 92N/A * to it when it did so? 92N/A * <p>The callback is a Event.Custom, so the signature is:</p> 92N/A * <p>type <string>, args <array>, customobject <object></p> 92N/A * <p>For DOMReady events, there are no fire argments, so the 92N/A * signature is:</p> 92N/A * <p>"DOMReady", [], obj</p> 154N/A * @param {function} fn what to execute when the element is found. 154N/A * @optional context execution context 154N/A * @optional args 0..n arguments to send to the listener 154N/A * Use domready event instead. @see domready 154N/A * @deprecated use 'domready' instead 0N/A // Y.log('DOMReady already fired', 'info', 'event'); 0N/A // Y.log('setting up before listener', 'info', 'event'); 0N/A * DOM event listener abstraction layer 0N/A * The event utility provides functions to add and remove event listeners, 0N/A * event cleansing. It also tries to automatically remove listeners it 154N/A * registers during the unload event. 89N/A * True after the onload event has fired 89N/A * @property _loadComplete 0N/A * The number of times to poll after window.onload. This number is 0N/A * increased if additional late-bound handlers are requested after 0N/A * @property _retryCount 0N/A * onAvailable listeners 159N/A * Custom event wrappers for DOM events. Key is 159N/A * 'event:' + Element uid stamp + event type 158N/A * Custom event wrapper map DOM events. Key is 158N/A * Element uid stamp. Each item is a hash of custom event 158N/A * wrappers as provided in the _wrappers collection. This 158N/A * provides the infrastructure for getListeners. 0N/A * The number of times we should look for elements that are not 154N/A * in the DOM at the time the event is requested after the document 0N/A * has been loaded. The default is 2000@amp;20 ms, so it will poll 154N/A * for 40 seconds or until all outstanding handlers are bound 0N/A * (whichever comes first). 0N/A * @property POLL_RETRYS * The poll interval in milliseconds * @property POLL_INTERVAL * These errors are suppressed, the method returns false, and this property * document readystate poll handle * True when the document is initially usable * Executes the supplied callback when the item with the supplied * id is found. This is meant to be used to execute behavior as * soon as possible as the page loads. If you use this after the * initial page load it will poll for a fixed time for the element. * The number of times it will poll and the frequency are * configurable. By default it will poll for 10 seconds. * <p>The callback is executed with a single parameter: * the custom object parameter, if provided.</p> * @param {string||string[]} id the id of the element, or an array * @param {function} fn what to execute when the element is found. * @param {object} p_obj an optional object to be passed back as * @param {boolean|object} p_override If set to true, fn will execute * in the context of p_obj, if set to an object it * will execute in the context of that object * @param checkContent {boolean} check child node readiness (onContentReady) * @deprecated Use Y.on("available") // Y.log('onAvailable registered for: ' + id); for (i=
0; i<a.
length; i=i+
1) {
// We want the first test to be immediate, but async return new Y.
EventHandle();
// @TODO by id needs a defered handle * Works the same way as onAvailable, but additionally checks the * state of sibling elements to determine if the content of the * available element is safe to modify. * <p>The callback is executed with a single parameter: * the custom object parameter, if provided.</p> * @param {string} id the id of the element to look for. * @param {function} fn what to execute when the element is ready. * @param {object} p_obj an optional object to be passed back as * @param {boolean|object} p_override If set to true, fn will execute * in the context of p_obj. If an object, fn will * exectute in the context of that object * @deprecated Use Y.on("contentready") * Appends an event handler * @param {String} type The type of event to append * @param {Function} fn The method the event invokes * @param {String|HTMLElement|Array|NodeList} el An id, an element * reference, or a collection of ids and/or elements to assign the * @param {Object} obj An arbitrary object that will be * passed as a parameter to the handler * @param {Boolean|object} args 0..n arguments to pass to the callback * @return {Boolean} True if the action was successful or defered, * false if one or more of the elements * could not have the listener attached, * or if the operation throws an exception. // Y.log('attach: ' + Y.Lang.dump(Y.Array(arguments, 0, true), 1)); // Y.log(Y.Array(arguments, 0, true), 1); // var a=Y.Array(arguments, 1, true), override=a[3], E=Y.Event, aa=Y.Array(arguments, 0, true); Y.
log(
'Using capture phase for: ' +
type,
'info',
'event');
// throw new TypeError(type + " attach call failed, callback undefined"); Y.
log(
type +
" attach call failed, invalid callback",
"error",
"event");
// The el argument can be an array of elements or element ids. // Y.log('collection: ' + el); // Y.log('collection: ' + el.item(0) + ', ' + el.item(1)); // If the el argument is a string, we assume it is // actually the id of the element. If the page is loaded // we convert el to the actual element, otherwise we // defer attaching the event until onload event fires // check to see if we need to delay hooking up the event // until after the page loads. // if (oEl && oEl.size && oEl.size() > 0) { // Y.log('node?: ' + (oEl instanceof Y.Node)); // Y.log('more than one: ' + size + ', ' + type); // Y.log('just one: ' + size + ', ' + type); // } else if (compat && oEl) { // Y.log('no size: ' + oEl + ', ' + type); // Not found = defer adding the event until the element is available // Y.log(el + ' not found'); // Y.log('lazy attach: ' + args); // Element should be an html element or an array if we get here. Y.
log(
"unable to attach event " +
type,
"warn",
"event");
// the custom event key is the uid for the element + type // cache the dom event details in the custom event // for later removeListener calls // window load happens once // if the load is complete, fire immediately. // all subscribers, including the current one // var capture = (Y.lang.isObject(obj) && obj.capture); // attach a listener that fires the custom event // Y.log("Attaching listener: " + [el, type, cewrapper.fn, capture]); // switched from obj to trimmedArgs[2] to deal with appened compat param // set the context as the second arg to subscribe // remove the 'obj' param // set context to the Node if not specified * Removes an event listener. Supports the signature the event was bound * with, but the preferred way to remove listeners is using the handle * that is returned when using Y.on * @param {String|HTMLElement|Array|NodeList} el An id, an element * reference, or a collection of ids and/or elements to remove * @param {String} type the type of event to remove. * @param {Function} fn the method the event invokes. If fn is * undefined, then all event handlers for the type of event are * removed. * @return {boolean} true if the unbind was successful, false * otherwise. // The el argument can be a string if (
typeof el ==
"string") {
// The el argument can be an array of elements or element ids. * Finds the event in the window object, the caller's arguments, or * in the arguments of another method in the callstack. This is * executed automatically for events registered through the event * manager, so the implementer should not normally need to execute * @param {Event} e the event parameter from the handler * @param {HTMLElement} el the element the listener was attached to * @return {Event} the event * Generates an unique ID for the element if it does not already * @param el the element to create the id for * @return {string} the resulting id of the element * We want to be able to use getElementsByTagName as a collection * to attach a group of events to. Unfortunately, different * browsers return different types of collections. This function * tests to determine if the object is array-like. It will also * fail if the object is an array, but is empty. * @method _isValidCollection * @param o the object to test * @return {boolean} true if the object is array-like and populated // Y.log('node? ' + (o instanceof Y.Node) + ', ' + ((o.size) ? o.size() : ' no size')); // if (o instanceof Y.Node) { return ( o &&
// o is something typeof o !==
"string" &&
// o is not a string // o.length && // o is indexed !o.
tagName &&
// o is not an HTML element !o.
alert &&
// o is not a window (o.
item ||
typeof o[
0] !==
"undefined") );
Y.
log(
"collection check failure",
"warn",
"event");
* hook up any deferred listeners // Y.log('Load Complete', 'info', 'event'); // Just in case DOMReady did not go off for some reason // Available elements may not have been detected before the // window load event fires. Try to find them now so that the // the user is more likely to get the onAvailable notifications // before the window load notification * Polling function that runs before the onload event fires, * attempting to attach to DOM Nodes as soon as they are * @method _tryPreloadAttach // Hold off if DOMReady has not fired and check current // readyState to protect against the IE operation aborted // Y.log.debug("tryPreloadAttach"); // keep trying until after the page is loaded. We need to // check the page load state prior to trying to bind the // elements so that we can be certain all elements have been // Y.log('avail: ' + el); // Y.log('NOT avail: ' + el); // The element is available, but not necessarily ready // @todo should we test parentNode.nextSibling? // we may need to strip the nulled out items here * Removes all listeners attached to the given element via addListener. * Optionally, the node's children can also be purged. * Optionally, you can specify a specific type of event to remove. * @param {HTMLElement} el the element to purge * @param {boolean} recurse recursively purge this element's children * as well. Use with caution. * @param {string} type optional type of listener to purge. If * left out, all listeners will be removed * Returns all listeners attached to the given element via addListener. * Optionally, you can specify a specific type of event to return. * @param el {HTMLElement|string} the element or element id to inspect * @param type {string} optional type of listener to return. If * left out, all listeners will be returned * @return {Y.Custom.Event} the custom event wrapper for the DOM event(s) * Removes all listeners registered by pe.event. Called * automatically during the unload event. * Adds a DOM event directly without the caching, cleanup, context adj, etc * @param {HTMLElement} el the element to bind the handler to * @param {string} type the type of event handler * @param {function} fn the callback to invoke * @param {boolen} capture capture or bubble phase * @param {HTMLElement} el the element to bind the handler to * @param {string} type the type of event handler * @param {function} fn the callback to invoke * @param {boolen} capture capture or bubble phase * Executes the callback as soon as the specified element * is detected in the DOM. * Executes the callback as soon as the specified element * is detected in the DOM with a nextSibling property * (indicating that the element's children are available) * Adds a DOM focus listener. Uses the focusin event in IE, * and the capture phase otherwise so that * the event propagates properly. * Adds a DOM focus listener. Uses the focusout event in IE, * and the capture phase otherwise so that * the event propagates properly. * Add a key listener. The listener will only be notified if the * keystroke detected meets the supplied specification. The * spec consists of the key event type, followed by a colon, * followed by zero or more comma separated key codes, followed * by zero or more modifiers delimited by a plus sign. Ex: * @param type {string} 'key' * @param fn {string} the function to execute * @param id {string} the element(s) to bind * @param spec {string} the keyCode and modifier specification * @param o optional context object * @param args 0..n additional arguments that should be provided * @return {Event.Handle} the detach handle Y.
log(
'Illegal key spec, creating a regular keypress listener instead.',
'info',
'event');
// key event type: 'down', 'up', or 'press' // list of key codes optionally followed by modifiers // the name of the custom event that will be created for the spec // subscribe spec validator to the DOM event // Y.log('keylistener: ' + e.keyCode); // pass this section if any supplied keyCode // Y.log('passed: ' + crit); // Y.log('failed: ' + crit); // only check modifier if no keyCode was specified // or the keyCode check was successful. pass only // if every modifier passes // Y.log(crit + ": " + passed); // fire spec custom event if spec if met // subscribe supplied listener to custom event for spec validator // remove element and spec. * Set up a delegated listener container. * @param type {string} 'delegate' * @param fn {string} the function to execute * @param el {string|node} the element that is the delegation container * @param event {string} the event type to delegate * @param spec {string} a selector that must match the target of the * @param o optional context object * @param args 0..n additional arguments that should be provided * @return {Event.Handle} the detach handle // set up the listener on the container // @TODO we need Node.some // remove element, delegation event, and delegation spec from the args // subscribe to the custom event for the delegation spec