event-dom.js revision 5cbcc8e7f5c3e4ad283e5cb76520840300f81a0a
145N/A * DOM event listener abstraction layer 145N/A * @submodule event-base 145N/A * The event utility provides functions to add and remove event listeners, 145N/A * event cleansing. It also tries to automatically remove listeners it 145N/A * registers during the unload event. 187N/A Y.
log(
"collection check failure",
"warn",
"event");
145N/A * True after the onload event has fired 145N/A * @property _loadComplete 191N/A * The number of times to poll after window.onload. This number is 145N/A * increased if additional late-bound handlers are requested after 187N/A * @property _retryCount 197N/A * Custom event wrappers for DOM events. Key is 197N/A * 'event:' + Element uid stamp + event type * Custom event wrapper map DOM events. Key is * Element uid stamp. Each item is a hash of custom event * wrappers as provided in the _wrappers collection. This * provides the infrastructure for getListeners. * The number of times we should look for elements that are not * in the DOM at the time the event is requested after the document * has been loaded. The default is 1000@amp;40 ms, so it will poll * for 40 seconds or until all outstanding handlers are bound * (whichever comes first). * 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 // set by the event system for lazy DOM listeners // otherwise try to remove the onAvailable listener(s) for (i =
0; i < a.
length; i++) {
* 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") * @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} context optional context object * @param {Boolean|object} args 0..n arguments to pass to the callback * @return {EventHandle} an object to that can be used to detach the listener // for later removeListener calls // window load happens once // 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. // return (handles.length === 1) ? handles[0] : handles; // 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 the element is // oEl = (compat) ? Y.DOM.byId(el) : Y.Selector.query(el); // 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 node Y.
log(
"unable to attach event " +
type,
"warn",
"event");
// if the load is complete, fire immediately. // all subscribers, including the current one // set context to the Node if not specified // ret = cewrapper.on.apply(cewrapper, trimmedArgs); * 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} 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 * @param {String|HTMLElement|Array|NodeList|EventHandle} el An * event handle, an id, an element reference, or a collection * of ids and/or elements to remove the listener from. * @return {boolean} true if the unbind was successful, false otherwise. // The el argument can be a string if (
typeof el ==
"string") {
// el = (compat) ? Y.DOM.byId(el) : Y.all(el); // return Y.Event.detach.apply(Y.Event, args); // 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 * @deprecated was not meant to be used directly * 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 // Hold off if DOMReady has not fired and check current // readyState to protect against the IE operation aborted // 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 // el = (item.compat) ? Y.DOM.byId(item.id) : Y.one(item.id); // Y.log('avail: ' + el); // Y.log('NOT avail: ' + el); // el = (item.compat) ? Y.DOM.byId(item.id) : Y.one(item.id); // 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 // var oEl = (Y.Lang.isString(el)) ? Y.one(el) : el, // purgeElement: function(el, recurse, type) { // // var oEl = (Y.Lang.isString(el)) ? Y.one(el) : el, // var oEl = (Y.Lang.isString(el)) ? Y.Selector.query(el, null, true) : el, // lis = this.getListeners(oEl, type), i, len, props, children, child; // // children = Y.Selector.query('*', oEl); // children = oEl.getElementsByTagName('*'); // len = children.length; // for (; i < len; ++i) { // child = this.getListeners(children[i], type); // for (; i < len; ++i) { // remove(props.el, props.type, props.fn, props.capture); // delete _wrappers[props.key]; // delete _el_events[props.domkey][props.key]; * 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) // get native events as well * 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