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
;(function(handlers) { | |
if (!handlers) { | |
throw new Error('Nothing to handle'); | |
} | |
document.documentElement.addEventListener('click', function(event) { | |
var handler = event.originalTarget.getAttribute('data-handler'); | |
if (!handler) { | |
// nothing to do | |
return; | |
} | |
if (event.originalTarget.tagName === 'A' && (event.metaKey || event.ctrlKey || event.shiftKey)) { | |
// honour default behaviour on <a>s when using modifier keys when clicking | |
// meta / ctrl open in new tab | |
// shift opens in a new window | |
return; | |
} | |
if (typeof handlers[handler] === 'function') { | |
handlers[handler].call(event.originalTarget, event); | |
} | |
else { | |
if (console && console.log) { | |
console.log('unknown handler \'%\'s on %o', handler, event.originalTarget); | |
} | |
} | |
}); | |
}({ | |
'example-handler': function(event) { | |
event.preventDefault(); | |
// I'm the example handler | |
} | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment