Last active
December 15, 2015 17:29
-
-
Save MikeDigitize/410edb9ebb61e38587c2 to your computer and use it in GitHub Desktop.
This file contains 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
<button id="btn">Click Me</button> | |
<select> | |
<option>Option 1</option> | |
<option>Option 2</option> | |
<option>Option 3</option> | |
</select> | |
<button id="select">Select</button> | |
<script> | |
function AoEventListener(target, event, callback, capture) { | |
if (!(target instanceof HTMLElement)) { | |
throw new Error("target not a html element"); | |
} | |
if (typeof event !== "string") { | |
throw new Error("event must be a string"); | |
} | |
if (typeof callback !== "function") { | |
throw new Error("callback must be a function"); | |
} | |
this.event = event; | |
this.target= target; | |
this.capture = typeof capture === "undefined" ? false : capture; | |
this.taggingCallback = function() {}; | |
this.mvtCallback = function() {}; | |
this.allowDefault = true; | |
this.evtCallback = function() { | |
if(this.allowDefault){ | |
callback(); | |
} | |
this.mvtCallback(); | |
this.taggingCallback(); | |
}.bind(this); | |
this.target.addEventListener(this.event, this.evtCallback, this.capture); | |
} | |
AoEventListener.prototype.remove = function() { | |
this.target.removeEventListener(this.event, this.evtCallback, this.capture); | |
}; | |
AoEventListener.prototype.mvtCallback = function(cb) { | |
if (typeof cb !== "function") { | |
throw new Error("MVT callback must be a function"); | |
} | |
this.mvtCallback = cb; | |
}; | |
AoEventListener.prototype.taggingCallback = function(cb) { | |
if (typeof cb !== "function") { | |
throw new Error("MVT callback must be a function"); | |
} | |
this.taggingCallback = cb; | |
}; | |
AoEventListener.prototype.allowDefault = function(allow) { | |
if (typeof allow !== "boolean") { | |
throw new Error("Allow default must be a boolean"); | |
} | |
this.allowDefault = allow; | |
}; | |
var btn = document.querySelector("#btn"); | |
function onBtnClick() { | |
console.log("btn clicked"); | |
} | |
var btnCtrl = new AoEventListener(btn, "click", onBtnClick); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment