keybindingSrv.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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) {
  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 p", this.goToProfile);
  22. this.bind("s s", this.openSearchStarred);
  23. this.bind(['f'], this.openSearch);
  24. }
  25. openSearchStarred() {
  26. this.$rootScope.appEvent('show-dash-search', {starred: true});
  27. }
  28. openSearch() {
  29. this.$rootScope.appEvent('show-dash-search');
  30. }
  31. goToHome() {
  32. this.$location.path("/");
  33. }
  34. goToProfile() {
  35. this.$location.path("/profile");
  36. }
  37. showHelpModal() {
  38. console.log('showing help modal');
  39. appEvents.emit('show-modal', {
  40. src: 'public/app/partials/help_modal.html',
  41. model: {}
  42. });
  43. }
  44. bind(keyArg, fn) {
  45. Mousetrap.bind(keyArg, evt => {
  46. evt.preventDefault();
  47. evt.stopPropagation();
  48. return this.$rootScope.$apply(fn.bind(this));
  49. });
  50. }
  51. setupDashboardBindings(scope, dashboard) {
  52. this.bind('b', () => {
  53. dashboard.toggleEditMode();
  54. });
  55. this.bind('ctrl+o', () => {
  56. dashboard.sharedCrosshair = !dashboard.sharedCrosshair;
  57. scope.broadcastRefresh();
  58. });
  59. this.bind(['ctrl+s', 'command+s'], () => {
  60. scope.appEvent('save-dashboard');
  61. });
  62. this.bind('r', () => {
  63. scope.broadcastRefresh();
  64. });
  65. this.bind('ctrl+z', () => {
  66. scope.appEvent('zoom-out');
  67. });
  68. this.bind('left', () => {
  69. scope.appEvent('shift-time-backward');
  70. });
  71. this.bind('right', () => {
  72. scope.appEvent('shift-time-forward');
  73. });
  74. this.bind('ctrl+i', () => {
  75. scope.appEvent('quick-snapshot');
  76. });
  77. this.bind('e', () => {
  78. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  79. this.$rootScope.appEvent('panel-change-view', {
  80. fullscreen: true, edit: true, panelId: dashboard.meta.focusPanelId
  81. });
  82. }
  83. });
  84. this.bind('v', () => {
  85. if (dashboard.meta.focusPanelId) {
  86. this.$rootScope.appEvent('panel-change-view', {
  87. fullscreen: true, edit: null, panelId: dashboard.meta.focusPanelId
  88. });
  89. }
  90. });
  91. this.bind('d', () => {
  92. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  93. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  94. panelInfo.row.removePanel(panelInfo.panel);
  95. dashboard.meta.focusPanelId = 0;
  96. }
  97. });
  98. this.bind('esc', () => {
  99. var popups = $('.popover.in');
  100. if (popups.length > 0) {
  101. return;
  102. }
  103. // close modals
  104. var modalData = $(".modal").data();
  105. if (modalData && modalData.$scope && modalData.$scope.dismiss) {
  106. modalData.$scope.dismiss();
  107. }
  108. scope.appEvent('hide-dash-editor');
  109. scope.exitFullscreen();
  110. });
  111. }
  112. }
  113. coreModule.service('keybindingSrv', KeybindingSrv);