Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. /** @jsx React.DOM */
  2.  
  3. // NOTE: This file is formatted for React.js + Browserify
  4. // You might need to make some changes to use it without Browserify
  5.  
  6. var MousetrapMixin,
  7.  
  8. Mousetrap = require('mousetrap');
  9.  
  10. MousetrapMixin = {
  11. /**
  12. * Array for keeping track of shortcuts bindings
  13. */
  14. mousetrapBindings: [],
  15.  
  16. /**
  17. * Bind a function to a keyboard shortcut
  18. *
  19. * @param key
  20. * @param callback
  21. */
  22. bindShortcut: function (key, callback) {
  23. Mousetrap.bind(key, callback);
  24.  
  25. this.mousetrapBindings.push(key);
  26. },
  27.  
  28. /**
  29. * Unbind a keyboard shortcut
  30. *
  31. * @param key
  32. */
  33. unbindShortcut: function (key) {
  34. var index = this.mousetrapBindings.indexOf(key);
  35.  
  36. if (index > -1) {
  37. this.mousetrapBindings.splice(index, 1);
  38. }
  39.  
  40. Mousetrap.unbind(binding);
  41. },
  42.  
  43. /**
  44. * Remove any Mousetrap bindings
  45. */
  46. unbindAllShortcuts: function () {
  47. if (this.mousetrapBindings.length < 1) {
  48. return;
  49. }
  50.  
  51. this.mousetrapBindings.forEach(function (binding) {
  52. Mousetrap.unbind(binding);
  53. });
  54. },
  55.  
  56. /**
  57. * Handle component unmount
  58. */
  59. componentWillUnmount: function () {
  60. // Remove any Mousetrap bindings before unmounting
  61. this.unbindAllShortcuts();
  62. }
  63. };
  64.  
  65. module.exports = MousetrapMixin;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement