keybindingSrv.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 { getExploreUrl } from 'app/core/utils/explore';
  6. import { store } from 'app/store/store';
  7. import Mousetrap from 'mousetrap';
  8. import 'mousetrap-global-bind';
  9. import { ContextSrv } from './context_srv';
  10. import { ILocationService, ITimeoutService } from 'angular';
  11. export class KeybindingSrv {
  12. helpModal: boolean;
  13. modalOpen = false;
  14. timepickerOpen = false;
  15. /** @ngInject */
  16. constructor(
  17. private $rootScope: any,
  18. private $location: ILocationService,
  19. private $timeout: ITimeoutService,
  20. private datasourceSrv: any,
  21. private timeSrv: any,
  22. private contextSrv: ContextSrv
  23. ) {
  24. // clear out all shortcuts on route change
  25. $rootScope.$on('$routeChangeSuccess', () => {
  26. Mousetrap.reset();
  27. // rebind global shortcuts
  28. this.setupGlobal();
  29. });
  30. this.setupGlobal();
  31. appEvents.on('show-modal', () => (this.modalOpen = true));
  32. appEvents.on('timepickerOpen', () => (this.timepickerOpen = true));
  33. appEvents.on('timepickerClosed', () => (this.timepickerOpen = false));
  34. }
  35. setupGlobal() {
  36. if (!(this.$location.path() === '/login')) {
  37. this.bind(['?', 'h'], this.showHelpModal);
  38. this.bind('g h', this.goToHome);
  39. this.bind('g a', this.openAlerting);
  40. this.bind('g p', this.goToProfile);
  41. this.bind('s o', this.openSearch);
  42. this.bind('f', this.openSearch);
  43. this.bindGlobal('esc', this.exit);
  44. }
  45. }
  46. openSearch() {
  47. appEvents.emit('show-dash-search');
  48. }
  49. openAlerting() {
  50. this.$location.url('/alerting');
  51. }
  52. goToHome() {
  53. this.$location.url('/');
  54. }
  55. goToProfile() {
  56. this.$location.url('/profile');
  57. }
  58. showHelpModal() {
  59. appEvents.emit('show-modal', { templateHtml: '<help-modal></help-modal>' });
  60. }
  61. exit() {
  62. const popups = $('.popover.in, .slate-typeahead');
  63. if (popups.length > 0) {
  64. return;
  65. }
  66. appEvents.emit('hide-modal');
  67. if (this.modalOpen) {
  68. this.modalOpen = false;
  69. return;
  70. }
  71. if (this.timepickerOpen) {
  72. this.$rootScope.appEvent('closeTimepicker');
  73. this.timepickerOpen = false;
  74. return;
  75. }
  76. // close settings view
  77. const search = this.$location.search();
  78. if (search.editview) {
  79. delete search.editview;
  80. this.$location.search(search);
  81. return;
  82. }
  83. if (search.fullscreen) {
  84. appEvents.emit('panel-change-view', { fullscreen: false, edit: false });
  85. return;
  86. }
  87. if (search.kiosk) {
  88. this.$rootScope.appEvent('toggle-kiosk-mode', { exit: true });
  89. }
  90. }
  91. bind(keyArg: string | string[], fn: () => void) {
  92. Mousetrap.bind(
  93. keyArg,
  94. (evt: any) => {
  95. evt.preventDefault();
  96. evt.stopPropagation();
  97. evt.returnValue = false;
  98. return this.$rootScope.$apply(fn.bind(this));
  99. },
  100. 'keydown'
  101. );
  102. }
  103. bindGlobal(keyArg: string, fn: () => void) {
  104. Mousetrap.bindGlobal(
  105. keyArg,
  106. (evt: any) => {
  107. evt.preventDefault();
  108. evt.stopPropagation();
  109. evt.returnValue = false;
  110. return this.$rootScope.$apply(fn.bind(this));
  111. },
  112. 'keydown'
  113. );
  114. }
  115. unbind(keyArg: string, keyType?: string) {
  116. Mousetrap.unbind(keyArg, keyType);
  117. }
  118. showDashEditView() {
  119. const search = _.extend(this.$location.search(), { editview: 'settings' });
  120. this.$location.search(search);
  121. }
  122. setupDashboardBindings(scope: any, dashboard: any) {
  123. this.bind('mod+o', () => {
  124. dashboard.graphTooltip = (dashboard.graphTooltip + 1) % 3;
  125. appEvents.emit('graph-hover-clear');
  126. dashboard.startRefresh();
  127. });
  128. this.bind('mod+s', () => {
  129. scope.appEvent('save-dashboard');
  130. });
  131. this.bind('t z', () => {
  132. scope.appEvent('zoom-out', 2);
  133. });
  134. this.bind('ctrl+z', () => {
  135. scope.appEvent('zoom-out', 2);
  136. });
  137. this.bind('t left', () => {
  138. scope.appEvent('shift-time', -1);
  139. });
  140. this.bind('t right', () => {
  141. scope.appEvent('shift-time', 1);
  142. });
  143. // edit panel
  144. this.bind('e', () => {
  145. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  146. appEvents.emit('panel-change-view', {
  147. fullscreen: true,
  148. edit: true,
  149. panelId: dashboard.meta.focusPanelId,
  150. toggle: true,
  151. });
  152. }
  153. });
  154. // view panel
  155. this.bind('v', () => {
  156. if (dashboard.meta.focusPanelId) {
  157. appEvents.emit('panel-change-view', {
  158. fullscreen: true,
  159. edit: null,
  160. panelId: dashboard.meta.focusPanelId,
  161. toggle: true,
  162. });
  163. }
  164. });
  165. // jump to explore if permissions allow
  166. if (this.contextSrv.hasAccessToExplore()) {
  167. this.bind('x', async () => {
  168. if (dashboard.meta.focusPanelId) {
  169. const panel = dashboard.getPanelById(dashboard.meta.focusPanelId);
  170. const datasource = await this.datasourceSrv.get(panel.datasource);
  171. const url = await getExploreUrl(panel, panel.targets, datasource, this.datasourceSrv, this.timeSrv);
  172. if (url) {
  173. this.$timeout(() => this.$location.url(url));
  174. }
  175. }
  176. });
  177. }
  178. // delete panel
  179. this.bind('p r', () => {
  180. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  181. appEvents.emit('remove-panel', dashboard.meta.focusPanelId);
  182. dashboard.meta.focusPanelId = 0;
  183. }
  184. });
  185. // duplicate panel
  186. this.bind('p d', () => {
  187. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  188. const panelIndex = dashboard.getPanelInfoById(dashboard.meta.focusPanelId).index;
  189. dashboard.duplicatePanel(dashboard.panels[panelIndex]);
  190. }
  191. });
  192. // share panel
  193. this.bind('p s', () => {
  194. if (dashboard.meta.focusPanelId) {
  195. const shareScope = scope.$new();
  196. const panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  197. shareScope.panel = panelInfo.panel;
  198. shareScope.dashboard = dashboard;
  199. appEvents.emit('show-modal', {
  200. src: 'public/app/features/dashboard/components/ShareModal/template.html',
  201. scope: shareScope,
  202. });
  203. }
  204. });
  205. // toggle panel legend
  206. this.bind('p l', () => {
  207. if (dashboard.meta.focusPanelId) {
  208. const panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  209. if (panelInfo.panel.legend) {
  210. const panelRef = dashboard.getPanelById(dashboard.meta.focusPanelId);
  211. panelRef.legend.show = !panelRef.legend.show;
  212. panelRef.render();
  213. }
  214. }
  215. });
  216. // toggle all panel legends
  217. this.bind('d l', () => {
  218. dashboard.toggleLegendsForAll();
  219. });
  220. // collapse all rows
  221. this.bind('d shift+c', () => {
  222. dashboard.collapseRows();
  223. });
  224. // expand all rows
  225. this.bind('d shift+e', () => {
  226. dashboard.expandRows();
  227. });
  228. this.bind('d n', () => {
  229. this.$location.url('/dashboard/new');
  230. });
  231. this.bind('d r', () => {
  232. dashboard.startRefresh();
  233. });
  234. this.bind('d s', () => {
  235. this.showDashEditView();
  236. });
  237. this.bind('d k', () => {
  238. appEvents.emit('toggle-kiosk-mode');
  239. });
  240. this.bind('d v', () => {
  241. appEvents.emit('toggle-view-mode');
  242. });
  243. //Autofit panels
  244. this.bind('d a', () => {
  245. // this has to be a full page reload
  246. const queryParams = store.getState().location.query;
  247. const newUrlParam = queryParams.autofitpanels ? '' : '&autofitpanels';
  248. window.location.href = window.location.href + newUrlParam;
  249. });
  250. }
  251. }
  252. coreModule.service('keybindingSrv', KeybindingSrv);
  253. /**
  254. * Code below exports the service to react components
  255. */
  256. let singletonInstance: KeybindingSrv;
  257. export function setKeybindingSrv(instance: KeybindingSrv) {
  258. singletonInstance = instance;
  259. }
  260. export function getKeybindingSrv(): KeybindingSrv {
  261. return singletonInstance;
  262. }