keybindingSrv.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ///<reference path="../../headers/common.d.ts" />
  2. import $ from 'jquery';
  3. import coreModule from 'app/core/core_module';
  4. import appEvents from 'app/core/app_events';
  5. import Mousetrap from 'mousetrap';
  6. export class KeybindingSrv {
  7. helpModal: boolean;
  8. /** @ngInject */
  9. constructor(private $rootScope, private $modal, private $location, private contextSrv) {
  10. // clear out all shortcuts on route change
  11. $rootScope.$on('$routeChangeSuccess', () => {
  12. Mousetrap.reset();
  13. // rebind global shortcuts
  14. this.setupGlobal();
  15. });
  16. this.setupGlobal();
  17. }
  18. setupGlobal() {
  19. this.bind("?", this.showHelpModal);
  20. this.bind("g h", this.goToHome);
  21. this.bind("g a", this.openAlerting);
  22. this.bind("g p", this.goToProfile);
  23. this.bind("s s", this.openSearchStarred);
  24. this.bind(['f'], this.openSearch);
  25. }
  26. openSearchStarred() {
  27. this.$rootScope.appEvent('show-dash-search', {starred: true});
  28. }
  29. openSearch() {
  30. this.$rootScope.appEvent('show-dash-search');
  31. }
  32. openAlerting() {
  33. this.$location.url("/alerting");
  34. }
  35. goToHome() {
  36. this.$location.url("/");
  37. }
  38. goToProfile() {
  39. this.$location.url("/profile");
  40. }
  41. showHelpModal() {
  42. console.log('showing help modal');
  43. appEvents.emit('show-modal', {
  44. src: 'public/app/partials/help_modal.html',
  45. model: {}
  46. });
  47. }
  48. bind(keyArg, fn) {
  49. Mousetrap.bind(keyArg, evt => {
  50. evt.preventDefault();
  51. evt.stopPropagation();
  52. return this.$rootScope.$apply(fn.bind(this));
  53. });
  54. }
  55. setupDashboardBindings(scope, dashboard) {
  56. this.bind('b', () => {
  57. dashboard.toggleEditMode();
  58. });
  59. this.bind('ctrl+o', () => {
  60. dashboard.sharedCrosshair = !dashboard.sharedCrosshair;
  61. scope.broadcastRefresh();
  62. });
  63. this.bind(['ctrl+s', 'command+s'], () => {
  64. scope.appEvent('save-dashboard');
  65. });
  66. this.bind('r', () => {
  67. scope.broadcastRefresh();
  68. });
  69. this.bind('ctrl+z', () => {
  70. scope.appEvent('zoom-out');
  71. });
  72. this.bind('left', () => {
  73. scope.appEvent('shift-time-backward');
  74. });
  75. this.bind('right', () => {
  76. scope.appEvent('shift-time-forward');
  77. });
  78. this.bind('ctrl+i', () => {
  79. scope.appEvent('quick-snapshot');
  80. });
  81. this.bind('e', () => {
  82. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  83. this.$rootScope.appEvent('panel-change-view', {
  84. fullscreen: true,
  85. edit: true,
  86. panelId: dashboard.meta.focusPanelId,
  87. toggle: true
  88. });
  89. }
  90. });
  91. this.bind('v', () => {
  92. if (dashboard.meta.focusPanelId) {
  93. this.$rootScope.appEvent('panel-change-view', {
  94. fullscreen: true,
  95. edit: null,
  96. panelId: dashboard.meta.focusPanelId,
  97. toggle: true,
  98. });
  99. }
  100. });
  101. this.bind('d', () => {
  102. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  103. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  104. panelInfo.row.removePanel(panelInfo.panel);
  105. dashboard.meta.focusPanelId = 0;
  106. }
  107. });
  108. this.bind('esc', () => {
  109. var popups = $('.popover.in');
  110. if (popups.length > 0) {
  111. return;
  112. }
  113. // close modals
  114. var modalData = $(".modal").data();
  115. if (modalData && modalData.$scope && modalData.$scope.dismiss) {
  116. modalData.$scope.dismiss();
  117. }
  118. scope.appEvent('hide-dash-editor');
  119. scope.exitFullscreen();
  120. });
  121. }
  122. }
  123. coreModule.service('keybindingSrv', KeybindingSrv);