keybindingSrv.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. ///<reference path="../../headers/common.d.ts" />
  2. import $ from 'jquery';
  3. import _ from 'lodash';
  4. import coreModule from 'app/core/core_module';
  5. import appEvents from 'app/core/app_events';
  6. import Mousetrap from 'mousetrap';
  7. export class KeybindingSrv {
  8. helpModal: boolean;
  9. /** @ngInject */
  10. constructor(
  11. private $rootScope,
  12. private $modal,
  13. private $location,
  14. private contextSrv,
  15. private $timeout) {
  16. // clear out all shortcuts on route change
  17. $rootScope.$on('$routeChangeSuccess', () => {
  18. Mousetrap.reset();
  19. // rebind global shortcuts
  20. this.setupGlobal();
  21. });
  22. this.setupGlobal();
  23. }
  24. setupGlobal() {
  25. this.bind(['?', 'h'], this.showHelpModal);
  26. this.bind("g h", this.goToHome);
  27. this.bind("g a", this.openAlerting);
  28. this.bind("g p", this.goToProfile);
  29. this.bind("s s", this.openSearchStarred);
  30. this.bind('s o', this.openSearch);
  31. this.bind('s t', this.openSearchTags);
  32. this.bind('f', this.openSearch);
  33. }
  34. openSearchStarred() {
  35. this.$rootScope.appEvent('show-dash-search', {starred: true});
  36. }
  37. openSearchTags() {
  38. this.$rootScope.appEvent('show-dash-search', {tagsMode: true});
  39. }
  40. openSearch() {
  41. this.$rootScope.appEvent('show-dash-search');
  42. }
  43. openAlerting() {
  44. this.$location.url("/alerting");
  45. }
  46. goToHome() {
  47. this.$location.url("/");
  48. }
  49. goToProfile() {
  50. this.$location.url("/profile");
  51. }
  52. showHelpModal() {
  53. appEvents.emit('show-modal', {templateHtml: '<help-modal></help-modal>'});
  54. }
  55. bind(keyArg, fn) {
  56. Mousetrap.bind(keyArg, evt => {
  57. evt.preventDefault();
  58. evt.stopPropagation();
  59. evt.returnValue = false;
  60. return this.$rootScope.$apply(fn.bind(this));
  61. });
  62. }
  63. showDashEditView(view) {
  64. var search = _.extend(this.$location.search(), {editview: view});
  65. this.$location.search(search);
  66. }
  67. setupDashboardBindings(scope, dashboard) {
  68. // this.bind('b', () => {
  69. // dashboard.toggleEditMode();
  70. // });
  71. this.bind('mod+o', () => {
  72. dashboard.sharedCrosshair = !dashboard.sharedCrosshair;
  73. scope.broadcastRefresh();
  74. });
  75. this.bind('mod+h', () => {
  76. dashboard.hideControls = !dashboard.hideControls;
  77. });
  78. this.bind('mod+s', e => {
  79. scope.appEvent('save-dashboard');
  80. });
  81. this.bind('t z', () => {
  82. scope.appEvent('zoom-out');
  83. });
  84. this.bind('t left', () => {
  85. scope.appEvent('shift-time-backward');
  86. });
  87. this.bind('t right', () => {
  88. scope.appEvent('shift-time-forward');
  89. });
  90. this.bind('mod+i', () => {
  91. scope.appEvent('quick-snapshot');
  92. });
  93. // edit panel
  94. this.bind('e', () => {
  95. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  96. this.$rootScope.appEvent('panel-change-view', {
  97. fullscreen: true,
  98. edit: true,
  99. panelId: dashboard.meta.focusPanelId,
  100. toggle: true
  101. });
  102. }
  103. });
  104. // view panel
  105. this.bind('v', () => {
  106. if (dashboard.meta.focusPanelId) {
  107. this.$rootScope.appEvent('panel-change-view', {
  108. fullscreen: true,
  109. edit: null,
  110. panelId: dashboard.meta.focusPanelId,
  111. toggle: true,
  112. });
  113. }
  114. });
  115. // delete panel
  116. this.bind('p r', () => {
  117. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  118. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  119. panelInfo.row.removePanel(panelInfo.panel);
  120. dashboard.meta.focusPanelId = 0;
  121. }
  122. });
  123. // share panel
  124. this.bind('p s', () => {
  125. if (dashboard.meta.focusPanelId) {
  126. var shareScope = scope.$new();
  127. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  128. shareScope.panel = panelInfo.panel;
  129. shareScope.dashboard = dashboard;
  130. appEvents.emit('show-modal', {
  131. src: 'public/app/features/dashboard/partials/shareModal.html',
  132. scope: shareScope
  133. });
  134. }
  135. });
  136. // delete row
  137. this.bind('r r', () => {
  138. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  139. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  140. dashboard.removeRow(panelInfo.row);
  141. dashboard.meta.focusPanelId = 0;
  142. }
  143. });
  144. // collapse row
  145. this.bind('r c', () => {
  146. if (dashboard.meta.focusPanelId) {
  147. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  148. panelInfo.row.toggleCollapse();
  149. dashboard.meta.focusPanelId = 0;
  150. }
  151. });
  152. this.bind('d r', () => {
  153. scope.broadcastRefresh();
  154. });
  155. this.bind('d s', () => {
  156. this.showDashEditView('settings');
  157. });
  158. this.bind('d k', () => {
  159. appEvents.emit('toggle-kiosk-mode');
  160. });
  161. this.bind('d v', () => {
  162. appEvents.emit('toggle-view-mode');
  163. });
  164. this.bind('esc', () => {
  165. var popups = $('.popover.in');
  166. if (popups.length > 0) {
  167. return;
  168. }
  169. // close modals
  170. var modalData = $(".modal").data();
  171. if (modalData && modalData.$scope && modalData.$scope.dismiss) {
  172. modalData.$scope.dismiss();
  173. }
  174. scope.appEvent('hide-dash-editor');
  175. scope.exitFullscreen();
  176. });
  177. }
  178. }
  179. coreModule.service('keybindingSrv', KeybindingSrv);