Last active
December 11, 2015 17:18
-
-
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
This file contains hidden or 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
/** | |
* 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