Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 79 additions & 84 deletions src/getEventListeners.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,81 @@
(function() {
'use strict';

(function () {
"use strict";
// save the original methods before overwriting them
Element.prototype._addEventListener = Element.prototype.addEventListener;
Element.prototype._removeEventListener = Element.prototype.removeEventListener;

/**
* [addEventListener description]
* @param {[type]} type [description]
* @param {[type]} listener [description]
* @param {Boolean} useCapture [description]
*/
Element.prototype.addEventListener = function(type,listener,useCapture=false) {
// declare listener
this._addEventListener(type,listener,useCapture);

if(!this.eventListenerList) this.eventListenerList = {};
if(!this.eventListenerList[type]) this.eventListenerList[type] = [];

// add listener to event tracking list
this.eventListenerList[type].push( {type, listener, useCapture} );
};

/**
* [removeEventListener description]
* @param {[type]} type [description]
* @param {[type]} listener [description]
* @param {Boolean} useCapture [description]
* @return {[type]} [description]
*/
Element.prototype.removeEventListener = function(type,listener,useCapture=false) {
// remove listener
this._removeEventListener(type,listener,useCapture);

if(!this.eventListenerList) this.eventListenerList = {};
if(!this.eventListenerList[type]) this.eventListenerList[type] = [];

// Find the event in the list, If a listener is registered twice, one
// with capture and one without, remove each one separately. Removal of
// a capturing listener does not affect a non-capturing version of the
// same listener, and vice versa.
for(let i=0; i<this.eventListenerList[type].length; i++){
if( this.eventListenerList[type][i].listener===listener && this.eventListenerList[type][i].useCapture===useCapture){
this.eventListenerList[type].splice(i, 1);
break;
[Document, Window, Element].forEach((cst) => {
if (typeof cst === "function") {
cst.prototype._addEventListener = cst.prototype.addEventListener;
cst.prototype._removeEventListener = cst.prototype.removeEventListener;

/**
* [addEventListener description]
* @param {[type]} type [description]
* @param {[type]} listener [description]
* @param {Boolean} useCapture [description]
*/
cst.prototype.addEventListener = function (
type,
listener,
useCapture = false
) {
// declare listener
this._addEventListener(type, listener, useCapture);

if (!this.eventListenerList) this.eventListenerList = {};
if (!this.eventListenerList[type]) this.eventListenerList[type] = [];

// add listener to event tracking list
this.eventListenerList[type].push({ type, listener, useCapture });
};

/**
* [removeEventListener description]
* @param {[type]} type [description]
* @param {[type]} listener [description]
* @param {Boolean} useCapture [description]
* @return {[type]} [description]
*/
cst.prototype.removeEventListener = function (
type,
listener,
useCapture = false
) {
// remove listener
this._removeEventListener(type, listener, useCapture);

if (!this.eventListenerList) this.eventListenerList = {};
if (!this.eventListenerList[type]) this.eventListenerList[type] = [];

// Find the event in the list, If a listener is registered twice, one
// with capture and one without, remove each one separately. Removal of
// a capturing listener does not affect a non-capturing version of the
// same listener, and vice versa.
for (let i = 0; i < this.eventListenerList[type].length; i++) {
if (
this.eventListenerList[type][i].listener === listener &&
this.eventListenerList[type][i].useCapture === useCapture
) {
this.eventListenerList[type].splice(i, 1);
break;
}
}
// if no more events of the removed event type are left,remove the group
if(this.eventListenerList[type].length==0) delete this.eventListenerList[type];
};


/**
* [getEventListeners description]
* @param {[type]} type [description]
* @return {[type]} [description]
*/
Element.prototype.getEventListeners = function(type){
if(!this.eventListenerList) this.eventListenerList = {};

// return reqested listeners type or all them
if(type===undefined) return this.eventListenerList;
return this.eventListenerList[type];
};


/*
Element.prototype.clearEventListeners = function(a){
if(!this.eventListenerList)
this.eventListenerList = {};
if(a==undefined){
for(var x in (this.getEventListeners())) this.clearEventListeners(x);
return;
}
var el = this.getEventListeners(a);
if(el==undefined)
return;
for(var i = el.length - 1; i >= 0; --i) {
var ev = el[i];
this.removeEventListener(a, ev.listener, ev.useCapture);
}
};
*/

})();
}
// if no more events of the removed event type are left,remove the group
if (this.eventListenerList[type].length == 0)
delete this.eventListenerList[type];
};

/**
* [getEventListeners description]
* @param {[type]} type [description]
* @return {[type]} [description]
*/
cst.prototype.getEventListeners = function (type) {
if (!this.eventListenerList) this.eventListenerList = {};

// return reqested listeners type or all them
if (type === undefined) return this.eventListenerList;
return this.eventListenerList[type];
};
}
});
})();