Created
December 29, 2010 19:05
-
-
Save think49/758906 to your computer and use it in GitHub Desktop.
addEvent.js: Node#addEventListener, attachEvent のラッパー関数。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* addEvent.js | |
* | |
* @version 2.1.3 | |
* @author think49 | |
*/ | |
/** | |
* removeEventListener wrapper. | |
* @function | |
* @param {Object} node DOM Node. | |
* @param {String} type event type. | |
* @param {Object} handleEvent event handler. | |
* @param {Boolean} useCapture useCapture. | |
* @returns undefined | |
* @type Undefined | |
*/ | |
var removeEvent = (function () { | |
var removeEvent; | |
if (typeof removeEventListener === 'function') { | |
removeEvent = function (node, type, handleEvent, useCapture) { | |
return node ? node.removeEventListener(type, handleEvent, useCapture) : removeEventListener(type, handleEvent, useCapture); | |
}; | |
} else if (typeof detachEvent === 'function' || typeof detachEvent === 'object') { | |
removeEvent = function (node, type, handleEvent) { | |
return node ? node.detachEvent('on' + type, handleEvent) : detachEvent('on' + type, handleEvent); | |
}; | |
} | |
// DefinitionError: removeEvent couldn't be defined. | |
if (typeof removeEvent !== 'function') { | |
throw new TypeError('removeEvent is not a function (' + removeEvent + ')'); | |
} | |
return removeEvent; | |
})(); | |
/** | |
* addEventListener wrapper. | |
* @function | |
* @param {Object} node DOM Node. | |
* @param {String} type event type. | |
* @param {Object} handleEvent event handler. | |
* @param {Boolean} useCapture useCapture. | |
* @returns undefined | |
* @type Undefined | |
*/ | |
var addEvent = (function (removeEvent) { | |
var addEvent, listenersCache, handleUnload; | |
listenersCache = (function () { | |
function ListenersCache () { return this; } | |
(function () { | |
var listeners = []; // EventListener's cache. | |
this.push = function (node, type, handleEvent, useCapture) { | |
var target, i; | |
i = listeners.length | |
while (i--) { | |
target = listeners[i]; | |
if (target[0] === node && target[1] === type && target[2] === handleEvent && target[3] === useCapture) { | |
listeners = listeners.slice(0, i).concat(listeners.slice(i + 1)); | |
break; | |
} | |
} | |
return listeners.push([node, type, handleEvent, useCapture]); | |
}; | |
this.shift = function () { | |
return listeners.shift(); | |
}; | |
this.getValueAll = function () { | |
return listeners; | |
}; | |
}).call(ListenersCache.prototype); | |
return new ListenersCache; | |
})(); | |
if (typeof addEventListener === 'function') { | |
addEvent = function (node, type, handleEvent, useCapture) { | |
var removeAllListener; | |
node ? node.addEventListener(type, handleEvent, useCapture) : addEventListener(type, handleEvent, useCapture); | |
listenersCache.push(node, type, handleEvent, useCapture); | |
if (handleEvent !== handleUnload && type === 'unload' && (!node || typeof node === 'object' && node.window === node)) { | |
removeEvent(null, 'unload', handleUnload, false, false); | |
addEvent(null, 'unload', handleUnload, false, false); | |
} | |
}; | |
} else if (typeof attachEvent === 'function' || typeof attachEvent === 'object') { | |
addEvent = function (node, type, handleEvent, useCapture) { | |
node ? node.attachEvent('on' + type, handleEvent) : attachEvent('on' + type, handleEvent); | |
listenersCache.push(node, type, handleEvent, useCapture); | |
}; | |
} | |
// DefinitionError: addEvent couldn't be defined. | |
if (typeof addEvent !== 'function') { | |
throw new TypeError('addEvent is not a function (' + addEvent + ')'); | |
} | |
function handleUnload () { | |
var listeners, targetListener; | |
listeners = listenersCache.getValueAll(); | |
targetListener = listeners.shift(); | |
while (targetListener) { | |
removeEvent.apply(null, targetListener); | |
targetListener = listeners.shift(); | |
} | |
} | |
// Remove all EventListeners on window unload. | |
addEvent(null, 'unload', handleUnload, false); | |
return addEvent; | |
})(removeEvent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考URL