Skip to content

Instantly share code, notes, and snippets.

@TikiTDO
Last active December 11, 2015 17:18
Show Gist options
  • Save TikiTDO/4633596 to your computer and use it in GitHub Desktop.
Save TikiTDO/4633596 to your computer and use it in GitHub Desktop.
Extends mousetrap.js to provide a special bind which overrides any further events, including browser behaviour
/**
* Adds a bindOverride and possibly bindOverrideGlobal [if bind global is installed]
* These methods override default broser behaviour before calling the callback
*
* usage:
* Mousetrap.bindGlobal('ctrl+s', _saveChanges);
*/
Mousetrap = (function(Mousetrap) {
Mousetrap.bindOverride = function(keys, callback, action) {
var override_callback = function(e) {
if (e.preventDefault) {
e.preventDefault();
} else {
// internet explorer
e.returnValue = false;
}
_saveDraft();
callback(e);
};
Mousetrap.bind(keys, override_callback, action);
};
if (typeof Mousetrap.bindGlobal === 'function') {
Mousetrap.bindOverrideGlobal = function(keys, callback, action) {
var override_callback = function(e) {
if (e.preventDefault) {
e.preventDefault();
} else {
// internet explorer
e.returnValue = false;
}
_saveDraft();
callback(e);
};
Mousetrap.bind(keys, callback, action);
};
}
return Mousetrap;
}) (Mousetrap);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment