keybindingSrv.ts 5.4 KB

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