keybindingSrv.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import Mousetrap from 'mousetrap';
  2. import 'mousetrap-global-bind';
  3. import 'mousetrap/plugins/global-bind/mousetrap-global-bind';
  4. import { LegacyGraphHoverClearEvent, locationUtil } from '@grafana/data';
  5. import { locationService } from '@grafana/runtime';
  6. import appEvents from 'app/core/app_events';
  7. import { getExploreUrl } from 'app/core/utils/explore';
  8. import { SaveDashboardDrawer } from 'app/features/dashboard/components/SaveDashboard/SaveDashboardDrawer';
  9. import { ShareModal } from 'app/features/dashboard/components/ShareModal';
  10. import { DashboardModel } from 'app/features/dashboard/state';
  11. import { getTimeSrv } from '../../features/dashboard/services/TimeSrv';
  12. import { getDatasourceSrv } from '../../features/plugins/datasource_srv';
  13. import {
  14. RemovePanelEvent,
  15. ShiftTimeEvent,
  16. ShiftTimeEventDirection,
  17. ShowModalReactEvent,
  18. ZoomOutEvent,
  19. AbsoluteTimeEvent,
  20. } from '../../types/events';
  21. import { HelpModal } from '../components/help/HelpModal';
  22. import { contextSrv } from '../core';
  23. import { exitKioskMode, toggleKioskMode } from '../navigation/kiosk';
  24. import { toggleTheme } from './toggleTheme';
  25. import { withFocusedPanel } from './withFocusedPanelId';
  26. export class KeybindingSrv {
  27. reset() {
  28. Mousetrap.reset();
  29. }
  30. initGlobals() {
  31. if (locationService.getLocation().pathname !== '/login') {
  32. this.bind(['?', 'h'], this.showHelpModal);
  33. this.bind('g h', this.goToHome);
  34. this.bind('g a', this.openAlerting);
  35. this.bind('g p', this.goToProfile);
  36. this.bind('s o', this.openSearch);
  37. this.bind('t a', this.makeAbsoluteTime);
  38. this.bind('f', this.openSearch);
  39. this.bind('esc', this.exit);
  40. this.bindGlobal('esc', this.globalEsc);
  41. }
  42. this.bind('t t', () => toggleTheme(false));
  43. this.bind('t r', () => toggleTheme(true));
  44. }
  45. globalEsc() {
  46. const anyDoc = document as any;
  47. const activeElement = anyDoc.activeElement;
  48. // typehead needs to handle it
  49. const typeaheads = document.querySelectorAll('.slate-typeahead--open');
  50. if (typeaheads.length > 0) {
  51. return;
  52. }
  53. // second check if we are in an input we can blur
  54. if (activeElement && activeElement.blur) {
  55. if (
  56. activeElement.nodeName === 'INPUT' ||
  57. activeElement.nodeName === 'TEXTAREA' ||
  58. activeElement.hasAttribute('data-slate-editor')
  59. ) {
  60. anyDoc.activeElement.blur();
  61. return;
  62. }
  63. }
  64. // ok no focused input or editor that should block this, let exist!
  65. this.exit();
  66. }
  67. private openSearch() {
  68. locationService.partial({ search: 'open' });
  69. }
  70. private closeSearch() {
  71. locationService.partial({ search: null });
  72. }
  73. private openAlerting() {
  74. locationService.push('/alerting');
  75. }
  76. private goToHome() {
  77. locationService.push('/');
  78. }
  79. private goToProfile() {
  80. locationService.push('/profile');
  81. }
  82. private makeAbsoluteTime() {
  83. appEvents.publish(new AbsoluteTimeEvent());
  84. }
  85. private showHelpModal() {
  86. appEvents.publish(new ShowModalReactEvent({ component: HelpModal }));
  87. }
  88. private exit() {
  89. const search = locationService.getSearchObject();
  90. if (search.editview) {
  91. locationService.partial({ editview: null });
  92. return;
  93. }
  94. if (search.inspect) {
  95. locationService.partial({ inspect: null, inspectTab: null });
  96. return;
  97. }
  98. if (search.editPanel) {
  99. locationService.partial({ editPanel: null, tab: null });
  100. return;
  101. }
  102. if (search.viewPanel) {
  103. locationService.partial({ viewPanel: null, tab: null });
  104. return;
  105. }
  106. if (search.kiosk) {
  107. exitKioskMode();
  108. }
  109. if (search.search) {
  110. this.closeSearch();
  111. }
  112. }
  113. private showDashEditView() {
  114. locationService.partial({
  115. editview: 'settings',
  116. });
  117. }
  118. bind(keyArg: string | string[], fn: () => void) {
  119. Mousetrap.bind(
  120. keyArg,
  121. (evt: any) => {
  122. evt.preventDefault();
  123. evt.stopPropagation();
  124. evt.returnValue = false;
  125. fn.call(this);
  126. },
  127. 'keydown'
  128. );
  129. }
  130. bindGlobal(keyArg: string, fn: () => void) {
  131. Mousetrap.bindGlobal(
  132. keyArg,
  133. (evt: any) => {
  134. evt.preventDefault();
  135. evt.stopPropagation();
  136. evt.returnValue = false;
  137. fn.call(this);
  138. },
  139. 'keydown'
  140. );
  141. }
  142. unbind(keyArg: string, keyType?: string) {
  143. Mousetrap.unbind(keyArg, keyType);
  144. }
  145. bindWithPanelId(keyArg: string, fn: (panelId: number) => void) {
  146. this.bind(keyArg, withFocusedPanel(fn));
  147. }
  148. setupTimeRangeBindings(updateUrl = true) {
  149. this.bind('t z', () => {
  150. appEvents.publish(new ZoomOutEvent({ scale: 2, updateUrl }));
  151. });
  152. this.bind('ctrl+z', () => {
  153. appEvents.publish(new ZoomOutEvent({ scale: 2, updateUrl }));
  154. });
  155. this.bind('t left', () => {
  156. appEvents.publish(new ShiftTimeEvent({ direction: ShiftTimeEventDirection.Left, updateUrl }));
  157. });
  158. this.bind('t right', () => {
  159. appEvents.publish(new ShiftTimeEvent({ direction: ShiftTimeEventDirection.Right, updateUrl }));
  160. });
  161. }
  162. setupDashboardBindings(dashboard: DashboardModel) {
  163. this.bind('mod+o', () => {
  164. dashboard.graphTooltip = (dashboard.graphTooltip + 1) % 3;
  165. dashboard.events.publish(new LegacyGraphHoverClearEvent());
  166. dashboard.startRefresh();
  167. });
  168. this.bind('mod+s', () => {
  169. if (dashboard.meta.canSave) {
  170. appEvents.publish(
  171. new ShowModalReactEvent({
  172. component: SaveDashboardDrawer,
  173. props: {
  174. dashboard,
  175. },
  176. })
  177. );
  178. }
  179. });
  180. this.setupTimeRangeBindings();
  181. // edit panel
  182. this.bindWithPanelId('e', (panelId) => {
  183. if (dashboard.canEditPanelById(panelId)) {
  184. const isEditing = locationService.getSearchObject().editPanel !== undefined;
  185. locationService.partial({ editPanel: isEditing ? null : panelId });
  186. }
  187. });
  188. // view panel
  189. this.bindWithPanelId('v', (panelId) => {
  190. const isViewing = locationService.getSearchObject().viewPanel !== undefined;
  191. locationService.partial({ viewPanel: isViewing ? null : panelId });
  192. });
  193. this.bindWithPanelId('i', (panelId) => {
  194. locationService.partial({ inspect: panelId });
  195. });
  196. // jump to explore if permissions allow
  197. if (contextSrv.hasAccessToExplore()) {
  198. this.bindWithPanelId('x', async (panelId) => {
  199. const panel = dashboard.getPanelById(panelId)!;
  200. const url = await getExploreUrl({
  201. panel,
  202. datasourceSrv: getDatasourceSrv(),
  203. timeSrv: getTimeSrv(),
  204. });
  205. if (url) {
  206. const urlWithoutBase = locationUtil.stripBaseFromUrl(url);
  207. if (urlWithoutBase) {
  208. locationService.push(urlWithoutBase);
  209. }
  210. }
  211. });
  212. }
  213. // delete panel
  214. this.bindWithPanelId('p r', (panelId) => {
  215. if (dashboard.canEditPanelById(panelId) && !(dashboard.panelInView || dashboard.panelInEdit)) {
  216. appEvents.publish(new RemovePanelEvent(panelId));
  217. }
  218. });
  219. // duplicate panel
  220. this.bindWithPanelId('p d', (panelId) => {
  221. if (dashboard.canEditPanelById(panelId)) {
  222. const panelIndex = dashboard.getPanelInfoById(panelId)!.index;
  223. dashboard.duplicatePanel(dashboard.panels[panelIndex]);
  224. }
  225. });
  226. // share panel
  227. this.bindWithPanelId('p s', (panelId) => {
  228. const panelInfo = dashboard.getPanelInfoById(panelId);
  229. appEvents.publish(
  230. new ShowModalReactEvent({
  231. component: ShareModal,
  232. props: {
  233. dashboard: dashboard,
  234. panel: panelInfo?.panel,
  235. },
  236. })
  237. );
  238. });
  239. // toggle panel legend
  240. this.bindWithPanelId('p l', (panelId) => {
  241. const panelInfo = dashboard.getPanelInfoById(panelId)!;
  242. if (panelInfo.panel.legend) {
  243. panelInfo.panel.legend.show = !panelInfo.panel.legend.show;
  244. panelInfo.panel.render();
  245. }
  246. });
  247. // toggle all panel legends
  248. this.bind('d l', () => {
  249. dashboard.toggleLegendsForAll();
  250. });
  251. // collapse all rows
  252. this.bind('d shift+c', () => {
  253. dashboard.collapseRows();
  254. });
  255. // expand all rows
  256. this.bind('d shift+e', () => {
  257. dashboard.expandRows();
  258. });
  259. this.bind('d n', () => {
  260. locationService.push('/dashboard/new');
  261. });
  262. this.bind('d r', () => {
  263. dashboard.startRefresh();
  264. });
  265. this.bind('d s', () => {
  266. this.showDashEditView();
  267. });
  268. this.bind('d k', () => {
  269. toggleKioskMode();
  270. });
  271. //Autofit panels
  272. this.bind('d a', () => {
  273. // this has to be a full page reload
  274. const queryParams = locationService.getSearchObject();
  275. const newUrlParam = queryParams.autofitpanels ? '' : '&autofitpanels';
  276. window.location.href = window.location.href + newUrlParam;
  277. });
  278. }
  279. }
  280. export const keybindingSrv = new KeybindingSrv();