keybindingSrv.ts 5.8 KB

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