|
| 1 | +(function() { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + // todo : use a weakmap to store the references |
| 5 | + const _eventListeners = new WeakMap(); |
| 6 | + |
| 7 | + // save the original methods before overwriting them |
| 8 | + Element.prototype._addEventListener = Element.prototype.addEventListener; |
| 9 | + Element.prototype._removeEventListener = Element.prototype.removeEventListener; |
| 10 | + |
| 11 | + |
| 12 | + /** |
| 13 | + * [addEventListener description] |
| 14 | + * @param {[type]} a [description] |
| 15 | + * @param {[type]} b [description] |
| 16 | + * @param {[type]} c [description] |
| 17 | + */ |
| 18 | + Element.prototype.addEventListener = function(a,b,c) { |
| 19 | + if(c==undefined) c=false; |
| 20 | + this._addEventListener(a,b,c); |
| 21 | + |
| 22 | + if(!this.eventListenerList) this.eventListenerList = {}; |
| 23 | + if(!this.eventListenerList[a]) this.eventListenerList[a] = []; |
| 24 | + |
| 25 | + //this.removeEventListener(a,b,c); // TODO - handle duplicates.. |
| 26 | + this.eventListenerList[a].push({listener:b,useCapture:c}); |
| 27 | + }; |
| 28 | + |
| 29 | + /** |
| 30 | + * [removeEventListener description] |
| 31 | + * @param {[type]} a [description] |
| 32 | + * @param {[type]} b [description] |
| 33 | + * @param {[type]} c [description] |
| 34 | + * @return {[type]} [description] |
| 35 | + */ |
| 36 | + Element.prototype.removeEventListener = function(a,b,c) { |
| 37 | + if(c==undefined) |
| 38 | + c=false; |
| 39 | + this._removeEventListener(a,b,c); |
| 40 | + if(!this.eventListenerList) this.eventListenerList = {}; |
| 41 | + if(!this.eventListenerList[a]) this.eventListenerList[a] = []; |
| 42 | + |
| 43 | + // Find the event in the list |
| 44 | + for(var i=0;i<this.eventListenerList[a].length;i++){ |
| 45 | + if(this.eventListenerList[a][i].listener==b && this.eventListenerList[a][i].useCapture==c){ // Hmm.. |
| 46 | + this.eventListenerList[a].splice(i, 1); |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if(this.eventListenerList[a].length==0) delete this.eventListenerList[a]; |
| 52 | + }; |
| 53 | + |
| 54 | + |
| 55 | + /** |
| 56 | + * [getEventListeners description] |
| 57 | + * @param {[type]} a [description] |
| 58 | + * @return {[type]} [description] |
| 59 | + */ |
| 60 | + Element.prototype.getEventListeners = function(a){ |
| 61 | + if(!this.eventListenerList) this.eventListenerList = {}; |
| 62 | + if(a==undefined) return this.eventListenerList; |
| 63 | + return this.eventListenerList[a]; |
| 64 | + }; |
| 65 | + |
| 66 | + |
| 67 | + /* |
| 68 | + Element.prototype.clearEventListeners = function(a){ |
| 69 | + if(!this.eventListenerList) |
| 70 | + this.eventListenerList = {}; |
| 71 | + if(a==undefined){ |
| 72 | + for(var x in (this.getEventListeners())) this.clearEventListeners(x); |
| 73 | + return; |
| 74 | + } |
| 75 | + var el = this.getEventListeners(a); |
| 76 | + if(el==undefined) |
| 77 | + return; |
| 78 | + for(var i = el.length - 1; i >= 0; --i) { |
| 79 | + var ev = el[i]; |
| 80 | + this.removeEventListener(a, ev.listener, ev.useCapture); |
| 81 | + } |
| 82 | + }; |
| 83 | + */ |
| 84 | + |
| 85 | +})(); |
0 commit comments