Skip to content

Instantly share code, notes, and snippets.

@englishextra
Forked from erikroyall/evento.js
Last active September 23, 2016 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save englishextra/3a959e4da0fcc268b140 to your computer and use it in GitHub Desktop.
Save englishextra/3a959e4da0fcc268b140 to your computer and use it in GitHub Desktop.
modified Evento - v1.0.0
/*!
* modified Evento - v1.0.0
* by Erik Royall <erikroyalL@hotmail.com> (http://erikroyall.github.io)
* Dual licensed under MIT and GPL
* gist.github.com/erikroyall/6618740
* gist.github.com/englishextra/3a959e4da0fcc268b140
* removed Array.prototype.indexOf shim
* removed Helio stuff which seems to be added
* for some other library, and works without Helio
* jsbin.com/jilevi/edit?html,js,output
* jsfiddle.net/englishextra/hLxyvmcm/
* changed this or window to self as argument
* added if("undefined"==typeof window||!("document"in window))
* {return console.log("window is undefined or document is not in window"),!1;}
* evento.add(window,"load",function(){});
*/
var evento = (function () {
if ("undefined" == typeof window || !("document" in window)) {
return console.log("window is undefined or document is not in window"),
!1;
}
var win = window,
doc = win.document,
_handlers = {},
addEvent,
removeEvent,
triggerEvent;
addEvent = (function () {
if (typeof doc.addEventListener === "function") {
return function (el, evt, fn) {
el.addEventListener(evt, fn, false);
_handlers[el] = _handlers[el] || {};
_handlers[el][evt] = _handlers[el][evt] || [];
_handlers[el][evt].push(fn);
};
} else if (typeof doc.attachEvent === "function") {
return function (el, evt, fn) {
el.attachEvent(evt, fn);
_handlers[el] = _handlers[el] || {};
_handlers[el][evt] = _handlers[el][evt] || [];
_handlers[el][evt].push(fn);
};
} else {
return function (el, evt, fn) {
el["on" + evt] = fn;
_handlers[el] = _handlers[el] || {};
_handlers[el][evt] = _handlers[el][evt] || [];
_handlers[el][evt].push(fn);
};
}
}
());
// removeEvent
removeEvent = (function () {
if (typeof doc.removeEventListener === "function") {
return function (el, evt, fn) {
el.removeEventListener(evt, fn, false);
/* Helio.each(_handlers[el][evt], function (fun) {
if (fun === fn) {
_handlers[el] = _handlers[el] || {};
_handlers[el][evt] = _handlers[el][evt] || [];
_handlers[el][evt][_handlers[el][evt].indexOf(fun)] = undefined;
}
}); */
};
} else if (typeof doc.detachEvent === "function") {
return function (el, evt, fn) {
el.detachEvent(evt, fn);
/* Helio.each(_handlers[el][evt], function (fun) {
if (fun === fn) {
_handlers[el] = _handlers[el] || {};
_handlers[el][evt] = _handlers[el][evt] || [];
_handlers[el][evt][_handlers[el][evt].indexOf(fun)] = undefined;
}
}); */
};
} else {
return function (el, evt, fn) {
el["on" + evt] = undefined;
/* Helio.each(_handlers[el][evt], function (fun) {
if (fun === fn) {
_handlers[el] = _handlers[el] || {};
_handlers[el][evt] = _handlers[el][evt] || [];
_handlers[el][evt][_handlers[el][evt].indexOf(fun)] = undefined;
}
}); */
};
}
}
());
// triggerEvent
triggerEvent = function (el, evt) {
_handlers[el] = _handlers[el] || {};
_handlers[el][evt] = _handlers[el][evt] || [];
for (var _i = 0, _l = _handlers[el][evt].length; _i < _l; _i += 1) {
_handlers[el][evt][_i]();
}
};
return {
add : addEvent,
remove : removeEvent,
trigger : triggerEvent,
_handlers : _handlers
};
}
());
var sayhello = function () {
/* window.removeEventListener("click", sayhello); */
evento.remove(window, "click", sayhello);
alert(1);
};
evento.add(window, "click", sayhello);
/*!
* modified Evento - v1.0.0
* by Erik Royall <erikroyalL@hotmail.com> (http://erikroyall.github.io)
* Dual licensed under MIT and GPL
* gist.github.com/erikroyall/6618740
* gist.github.com/englishextra/3a959e4da0fcc268b140
* removed Array.prototype.indexOf shim
* removed Helio stuff which seems to be added
* for some other library, and works without Helio
* jsbin.com/jilevi/edit?html,js,output
* jsfiddle.net/englishextra/hLxyvmcm/
* changed this or window to self as argument
* added if("undefined"==typeof window||!("document"in window))
* {return console.log("window is undefined or document is not in window"),!1;}
* evento.add(window,"load",function(){});
* closure-compiler.appspot.com/code/jsca0a9f805d1eb1167be5c7a15934d4085/default.js
*/
var evento=function(){if("undefined"==typeof window||!("document"in window))return console.log("window is undefined or document is not in window"),!1;var e=window.document,b={},f,g;f=function(){return"function"===typeof e.addEventListener?function(a,c,d){a.addEventListener(c,d,!1);b[a]=b[a]||{};b[a][c]=b[a][c]||[];b[a][c].push(d)}:"function"===typeof e.attachEvent?function(a,c,d){a.attachEvent(c,d);b[a]=b[a]||{};b[a][c]=b[a][c]||[];b[a][c].push(d)}:function(a,c,d){a["on"+c]=d;b[a]=b[a]||{};b[a][c]=b[a][c]||[];b[a][c].push(d)}}();g=function(){return"function"===typeof e.removeEventListener?function(a,c,b){a.removeEventListener(c,b,!1)}:"function"===typeof e.detachEvent?function(a,c,b){a.detachEvent(c,b)}:function(a,b,d){a["on"+b]=void 0}}();return{add:f,remove:g,trigger:function(a,c){b[a]=b[a]||{};b[a][c]=b[a][c]||[];for(var d=0,e=b[a][c].length;d<e;d+=1)b[a][c][d]()},_handlers:b}}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment