mxDefaultKeyHandler.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxDefaultKeyHandler
  7. *
  8. * Binds keycodes to actionnames in an editor. This aggregates an internal
  9. * <handler> and extends the implementation of <mxKeyHandler.escape> to not
  10. * only cancel the editing, but also hide the properties dialog and fire an
  11. * <mxEditor.escape> event via <editor>. An instance of this class is created
  12. * by <mxEditor> and stored in <mxEditor.keyHandler>.
  13. *
  14. * Example:
  15. *
  16. * Bind the delete key to the delete action in an existing editor.
  17. *
  18. * (code)
  19. * var keyHandler = new mxDefaultKeyHandler(editor);
  20. * keyHandler.bindAction(46, 'delete');
  21. * (end)
  22. *
  23. * Codec:
  24. *
  25. * This class uses the <mxDefaultKeyHandlerCodec> to read configuration
  26. * data into an existing instance. See <mxDefaultKeyHandlerCodec> for a
  27. * description of the configuration format.
  28. *
  29. * Keycodes:
  30. *
  31. * See <mxKeyHandler>.
  32. *
  33. * An <mxEvent.ESCAPE> event is fired via the editor if the escape key is
  34. * pressed.
  35. *
  36. * Constructor: mxDefaultKeyHandler
  37. *
  38. * Constructs a new default key handler for the <mxEditor.graph> in the
  39. * given <mxEditor>. (The editor may be null if a prototypical instance for
  40. * a <mxDefaultKeyHandlerCodec> is created.)
  41. *
  42. * Parameters:
  43. *
  44. * editor - Reference to the enclosing <mxEditor>.
  45. */
  46. function mxDefaultKeyHandler(editor)
  47. {
  48. if (editor != null)
  49. {
  50. this.editor = editor;
  51. this.handler = new mxKeyHandler(editor.graph);
  52. // Extends the escape function of the internal key
  53. // handle to hide the properties dialog and fire
  54. // the escape event via the editor instance
  55. var old = this.handler.escape;
  56. this.handler.escape = function(evt)
  57. {
  58. old.apply(this, arguments);
  59. editor.hideProperties();
  60. editor.fireEvent(new mxEventObject(mxEvent.ESCAPE, 'event', evt));
  61. };
  62. }
  63. };
  64. /**
  65. * Variable: editor
  66. *
  67. * Reference to the enclosing <mxEditor>.
  68. */
  69. mxDefaultKeyHandler.prototype.editor = null;
  70. /**
  71. * Variable: handler
  72. *
  73. * Holds the <mxKeyHandler> for key event handling.
  74. */
  75. mxDefaultKeyHandler.prototype.handler = null;
  76. /**
  77. * Function: bindAction
  78. *
  79. * Binds the specified keycode to the given action in <editor>. The
  80. * optional control flag specifies if the control key must be pressed
  81. * to trigger the action.
  82. *
  83. * Parameters:
  84. *
  85. * code - Integer that specifies the keycode.
  86. * action - Name of the action to execute in <editor>.
  87. * control - Optional boolean that specifies if control must be pressed.
  88. * Default is false.
  89. */
  90. mxDefaultKeyHandler.prototype.bindAction = function (code, action, control)
  91. {
  92. var keyHandler = mxUtils.bind(this, function()
  93. {
  94. this.editor.execute(action);
  95. });
  96. // Binds the function to control-down keycode
  97. if (control)
  98. {
  99. this.handler.bindControlKey(code, keyHandler);
  100. }
  101. // Binds the function to the normal keycode
  102. else
  103. {
  104. this.handler.bindKey(code, keyHandler);
  105. }
  106. };
  107. /**
  108. * Function: destroy
  109. *
  110. * Destroys the <handler> associated with this object. This does normally
  111. * not need to be called, the <handler> is destroyed automatically when the
  112. * window unloads (in IE) by <mxEditor>.
  113. */
  114. mxDefaultKeyHandler.prototype.destroy = function ()
  115. {
  116. this.handler.destroy();
  117. this.handler = null;
  118. };
  119. __mxOutput.mxDefaultKeyHandler = typeof mxDefaultKeyHandler !== 'undefined' ? mxDefaultKeyHandler : undefined;