keybindingSrv.ts 5.2 KB

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