keybindingSrv.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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() {
  60. var search = _.extend(this.$location.search(), {editview: 'settings'});
  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. this.$rootScope.$broadcast('refresh');
  68. });
  69. this.bind('mod+s', e => {
  70. scope.appEvent('save-dashboard');
  71. });
  72. this.bind('t z', () => {
  73. scope.appEvent('zoom-out', 2);
  74. });
  75. this.bind('ctrl+z', () => {
  76. scope.appEvent('zoom-out', 2);
  77. });
  78. this.bind('t left', () => {
  79. scope.appEvent('shift-time-backward');
  80. });
  81. this.bind('t right', () => {
  82. scope.appEvent('shift-time-forward');
  83. });
  84. // edit panel
  85. this.bind('e', () => {
  86. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  87. this.$rootScope.appEvent('panel-change-view', {
  88. fullscreen: true,
  89. edit: true,
  90. panelId: dashboard.meta.focusPanelId,
  91. toggle: true
  92. });
  93. }
  94. });
  95. // view panel
  96. this.bind('v', () => {
  97. if (dashboard.meta.focusPanelId) {
  98. this.$rootScope.appEvent('panel-change-view', {
  99. fullscreen: true,
  100. edit: null,
  101. panelId: dashboard.meta.focusPanelId,
  102. toggle: true,
  103. });
  104. }
  105. });
  106. // delete panel
  107. this.bind('p r', () => {
  108. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  109. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  110. panelInfo.row.removePanel(panelInfo.panel);
  111. dashboard.meta.focusPanelId = 0;
  112. }
  113. });
  114. // share panel
  115. this.bind('p s', () => {
  116. if (dashboard.meta.focusPanelId) {
  117. var shareScope = scope.$new();
  118. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  119. shareScope.panel = panelInfo.panel;
  120. shareScope.dashboard = dashboard;
  121. appEvents.emit('show-modal', {
  122. src: 'public/app/features/dashboard/partials/shareModal.html',
  123. scope: shareScope
  124. });
  125. }
  126. });
  127. // delete row
  128. this.bind('r r', () => {
  129. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  130. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  131. dashboard.removeRow(panelInfo.row);
  132. dashboard.meta.focusPanelId = 0;
  133. }
  134. });
  135. // collapse row
  136. this.bind('r c', () => {
  137. if (dashboard.meta.focusPanelId) {
  138. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  139. panelInfo.row.toggleCollapse();
  140. dashboard.meta.focusPanelId = 0;
  141. }
  142. });
  143. // collapse all rows
  144. this.bind('d shift+c', () => {
  145. for (let row of dashboard.rows) {
  146. row.collapse = true;
  147. }
  148. });
  149. // expand all rows
  150. this.bind('d shift+e', () => {
  151. for (let row of dashboard.rows) {
  152. row.collapse = false;
  153. }
  154. });
  155. this.bind('d n', e => {
  156. this.$location.url("/dashboard/new");
  157. });
  158. this.bind('d r', () => {
  159. this.$rootScope.$broadcast('refresh');
  160. });
  161. this.bind('d s', () => {
  162. this.showDashEditView();
  163. });
  164. this.bind('d k', () => {
  165. appEvents.emit('toggle-kiosk-mode');
  166. });
  167. this.bind('d v', () => {
  168. appEvents.emit('toggle-view-mode');
  169. });
  170. this.bind('esc', () => {
  171. var popups = $('.popover.in');
  172. if (popups.length > 0) {
  173. return;
  174. }
  175. scope.appEvent('hide-modal');
  176. scope.appEvent('panel-change-view', {fullscreen: false, edit: false});
  177. // close settings view
  178. var search = this.$location.search();
  179. if (search.editview) {
  180. delete search.editview;
  181. this.$location.search(search);
  182. }
  183. });
  184. }
  185. }
  186. coreModule.service('keybindingSrv', KeybindingSrv);