mousetrap-bind-dictionary.js 1000 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Overwrites default Mousetrap.bind method to optionally accept
  3. * an object to bind multiple key events in a single call
  4. *
  5. * You can pass it in like:
  6. *
  7. * Mousetrap.bind({
  8. * 'a': function() { console.log('a'); },
  9. * 'b': function() { console.log('b'); }
  10. * });
  11. *
  12. * And can optionally pass in 'keypress', 'keydown', or 'keyup'
  13. * as a second argument
  14. *
  15. */
  16. /* global Mousetrap:true */
  17. (function(Mousetrap) {
  18. var _oldBind = Mousetrap.prototype.bind;
  19. var args;
  20. Mousetrap.prototype.bind = function() {
  21. var self = this;
  22. args = arguments;
  23. // normal call
  24. if (typeof args[0] == 'string' || args[0] instanceof Array) {
  25. return _oldBind.call(self, args[0], args[1], args[2]);
  26. }
  27. // object passed in
  28. for (var key in args[0]) {
  29. if (args[0].hasOwnProperty(key)) {
  30. _oldBind.call(self, key, args[0][key], args[1]);
  31. }
  32. }
  33. };
  34. Mousetrap.init();
  35. }) (Mousetrap);