keybindingSrv.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import $ from 'jquery';
  2. import _ from 'lodash';
  3. import config from 'app/core/config';
  4. import coreModule from 'app/core/core_module';
  5. import appEvents from 'app/core/app_events';
  6. import { encodePathComponent } from 'app/core/utils/location_util';
  7. import Mousetrap from 'mousetrap';
  8. import 'mousetrap-global-bind';
  9. export class KeybindingSrv {
  10. helpModal: boolean;
  11. modalOpen = false;
  12. timepickerOpen = false;
  13. /** @ngInject */
  14. constructor(private $rootScope, private $location, private datasourceSrv, private timeSrv, private contextSrv) {
  15. // clear out all shortcuts on route change
  16. $rootScope.$on('$routeChangeSuccess', () => {
  17. Mousetrap.reset();
  18. // rebind global shortcuts
  19. this.setupGlobal();
  20. });
  21. this.setupGlobal();
  22. appEvents.on('show-modal', () => (this.modalOpen = true));
  23. $rootScope.onAppEvent('timepickerOpen', () => (this.timepickerOpen = true));
  24. $rootScope.onAppEvent('timepickerClosed', () => (this.timepickerOpen = false));
  25. }
  26. setupGlobal() {
  27. this.bind(['?', 'h'], this.showHelpModal);
  28. this.bind('g h', this.goToHome);
  29. this.bind('g a', this.openAlerting);
  30. this.bind('g p', this.goToProfile);
  31. this.bind('s s', this.openSearchStarred);
  32. this.bind('s o', this.openSearch);
  33. this.bind('s t', this.openSearchTags);
  34. this.bind('f', this.openSearch);
  35. this.bindGlobal('esc', this.exit);
  36. }
  37. openSearchStarred() {
  38. appEvents.emit('show-dash-search', { starred: true });
  39. }
  40. openSearchTags() {
  41. appEvents.emit('show-dash-search', { tagsMode: true });
  42. }
  43. openSearch() {
  44. appEvents.emit('show-dash-search');
  45. }
  46. openAlerting() {
  47. this.$location.url('/alerting');
  48. }
  49. goToHome() {
  50. this.$location.url('/');
  51. }
  52. goToProfile() {
  53. this.$location.url('/profile');
  54. }
  55. showHelpModal() {
  56. appEvents.emit('show-modal', { templateHtml: '<help-modal></help-modal>' });
  57. }
  58. exit() {
  59. const popups = $('.popover.in');
  60. if (popups.length > 0) {
  61. return;
  62. }
  63. appEvents.emit('hide-modal');
  64. if (!this.modalOpen) {
  65. if (this.timepickerOpen) {
  66. this.$rootScope.appEvent('closeTimepicker');
  67. this.timepickerOpen = false;
  68. } else {
  69. this.$rootScope.appEvent('panel-change-view', { fullscreen: false, edit: false });
  70. }
  71. } else {
  72. this.modalOpen = false;
  73. }
  74. // close settings view
  75. const search = this.$location.search();
  76. if (search.editview) {
  77. delete search.editview;
  78. this.$location.search(search);
  79. }
  80. }
  81. bind(keyArg, fn) {
  82. Mousetrap.bind(
  83. keyArg,
  84. evt => {
  85. evt.preventDefault();
  86. evt.stopPropagation();
  87. evt.returnValue = false;
  88. return this.$rootScope.$apply(fn.bind(this));
  89. },
  90. 'keydown'
  91. );
  92. }
  93. bindGlobal(keyArg, fn) {
  94. Mousetrap.bindGlobal(
  95. keyArg,
  96. evt => {
  97. evt.preventDefault();
  98. evt.stopPropagation();
  99. evt.returnValue = false;
  100. return this.$rootScope.$apply(fn.bind(this));
  101. },
  102. 'keydown'
  103. );
  104. }
  105. showDashEditView() {
  106. const search = _.extend(this.$location.search(), { editview: 'settings' });
  107. this.$location.search(search);
  108. }
  109. setupDashboardBindings(scope, dashboard) {
  110. this.bind('mod+o', () => {
  111. dashboard.graphTooltip = (dashboard.graphTooltip + 1) % 3;
  112. appEvents.emit('graph-hover-clear');
  113. this.$rootScope.$broadcast('refresh');
  114. });
  115. this.bind('mod+s', e => {
  116. scope.appEvent('save-dashboard');
  117. });
  118. this.bind('t z', () => {
  119. scope.appEvent('zoom-out', 2);
  120. });
  121. this.bind('ctrl+z', () => {
  122. scope.appEvent('zoom-out', 2);
  123. });
  124. this.bind('t left', () => {
  125. scope.appEvent('shift-time-backward');
  126. });
  127. this.bind('t right', () => {
  128. scope.appEvent('shift-time-forward');
  129. });
  130. // edit panel
  131. this.bind('e', () => {
  132. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  133. this.$rootScope.appEvent('panel-change-view', {
  134. fullscreen: true,
  135. edit: true,
  136. panelId: dashboard.meta.focusPanelId,
  137. toggle: true,
  138. });
  139. }
  140. });
  141. // view panel
  142. this.bind('v', () => {
  143. if (dashboard.meta.focusPanelId) {
  144. this.$rootScope.appEvent('panel-change-view', {
  145. fullscreen: true,
  146. edit: null,
  147. panelId: dashboard.meta.focusPanelId,
  148. toggle: true,
  149. });
  150. }
  151. });
  152. // jump to explore if permissions allow
  153. if (this.contextSrv.isEditor && config.exploreEnabled) {
  154. this.bind('x', async () => {
  155. if (dashboard.meta.focusPanelId) {
  156. const panel = dashboard.getPanelById(dashboard.meta.focusPanelId);
  157. const datasource = await this.datasourceSrv.get(panel.datasource);
  158. if (datasource && datasource.supportsExplore) {
  159. const range = this.timeSrv.timeRangeForUrl();
  160. const state = {
  161. ...datasource.getExploreState(panel),
  162. range,
  163. };
  164. const exploreState = encodePathComponent(JSON.stringify(state));
  165. this.$location.url(`/explore?state=${exploreState}`);
  166. }
  167. }
  168. });
  169. }
  170. // delete panel
  171. this.bind('p r', () => {
  172. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  173. this.$rootScope.appEvent('panel-remove', {
  174. panelId: dashboard.meta.focusPanelId,
  175. });
  176. dashboard.meta.focusPanelId = 0;
  177. }
  178. });
  179. // duplicate panel
  180. this.bind('p d', () => {
  181. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  182. const panelIndex = dashboard.getPanelInfoById(dashboard.meta.focusPanelId).index;
  183. dashboard.duplicatePanel(dashboard.panels[panelIndex]);
  184. }
  185. });
  186. // share panel
  187. this.bind('p s', () => {
  188. if (dashboard.meta.focusPanelId) {
  189. const shareScope = scope.$new();
  190. const panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  191. shareScope.panel = panelInfo.panel;
  192. shareScope.dashboard = dashboard;
  193. appEvents.emit('show-modal', {
  194. src: 'public/app/features/dashboard/partials/shareModal.html',
  195. scope: shareScope,
  196. });
  197. }
  198. });
  199. // collapse all rows
  200. this.bind('d shift+c', () => {
  201. dashboard.collapseRows();
  202. });
  203. // expand all rows
  204. this.bind('d shift+e', () => {
  205. dashboard.expandRows();
  206. });
  207. this.bind('d n', e => {
  208. this.$location.url('/dashboard/new');
  209. });
  210. this.bind('d r', () => {
  211. this.$rootScope.$broadcast('refresh');
  212. });
  213. this.bind('d s', () => {
  214. this.showDashEditView();
  215. });
  216. this.bind('d k', () => {
  217. appEvents.emit('toggle-kiosk-mode');
  218. });
  219. this.bind('d v', () => {
  220. appEvents.emit('toggle-view-mode');
  221. });
  222. //Autofit panels
  223. this.bind('d a', () => {
  224. // this has to be a full page reload
  225. window.location.href = window.location.href + '&autofitpanels';
  226. });
  227. }
  228. }
  229. coreModule.service('keybindingSrv', KeybindingSrv);