keybindingSrv.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.bind('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. panelId: dashboard.meta.focusPanelId,
  160. toggle: true,
  161. });
  162. }
  163. });
  164. // jump to explore if permissions allow
  165. if (this.contextSrv.hasAccessToExplore()) {
  166. this.bind('x', async () => {
  167. if (dashboard.meta.focusPanelId) {
  168. const panel = dashboard.getPanelById(dashboard.meta.focusPanelId);
  169. const datasource = await this.datasourceSrv.get(panel.datasource);
  170. const url = await getExploreUrl(panel, panel.targets, datasource, this.datasourceSrv, this.timeSrv);
  171. if (url) {
  172. this.$timeout(() => this.$location.url(url));
  173. }
  174. }
  175. });
  176. }
  177. // delete panel
  178. this.bind('p r', () => {
  179. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  180. appEvents.emit('remove-panel', dashboard.meta.focusPanelId);
  181. dashboard.meta.focusPanelId = 0;
  182. }
  183. });
  184. // duplicate panel
  185. this.bind('p d', () => {
  186. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  187. const panelIndex = dashboard.getPanelInfoById(dashboard.meta.focusPanelId).index;
  188. dashboard.duplicatePanel(dashboard.panels[panelIndex]);
  189. }
  190. });
  191. // share panel
  192. this.bind('p s', () => {
  193. if (dashboard.meta.focusPanelId) {
  194. const shareScope = scope.$new();
  195. const panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  196. shareScope.panel = panelInfo.panel;
  197. shareScope.dashboard = dashboard;
  198. appEvents.emit('show-modal', {
  199. src: 'public/app/features/dashboard/components/ShareModal/template.html',
  200. scope: shareScope,
  201. });
  202. }
  203. });
  204. // toggle panel legend
  205. this.bind('p l', () => {
  206. if (dashboard.meta.focusPanelId) {
  207. const panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  208. if (panelInfo.panel.legend) {
  209. const panelRef = dashboard.getPanelById(dashboard.meta.focusPanelId);
  210. panelRef.legend.show = !panelRef.legend.show;
  211. panelRef.render();
  212. }
  213. }
  214. });
  215. // toggle all panel legends
  216. this.bind('d l', () => {
  217. dashboard.toggleLegendsForAll();
  218. });
  219. // collapse all rows
  220. this.bind('d shift+c', () => {
  221. dashboard.collapseRows();
  222. });
  223. // expand all rows
  224. this.bind('d shift+e', () => {
  225. dashboard.expandRows();
  226. });
  227. this.bind('d n', () => {
  228. this.$location.url('/dashboard/new');
  229. });
  230. this.bind('d r', () => {
  231. dashboard.startRefresh();
  232. });
  233. this.bind('d s', () => {
  234. this.showDashEditView();
  235. });
  236. this.bind('d k', () => {
  237. appEvents.emit('toggle-kiosk-mode');
  238. });
  239. this.bind('d v', () => {
  240. appEvents.emit('toggle-view-mode');
  241. });
  242. //Autofit panels
  243. this.bind('d a', () => {
  244. // this has to be a full page reload
  245. const queryParams = store.getState().location.query;
  246. const newUrlParam = queryParams.autofitpanels ? '' : '&autofitpanels';
  247. window.location.href = window.location.href + newUrlParam;
  248. });
  249. }
  250. }
  251. coreModule.service('keybindingSrv', KeybindingSrv);
  252. /**
  253. * Code below exports the service to react components
  254. */
  255. let singletonInstance: KeybindingSrv;
  256. export function setKeybindingSrv(instance: KeybindingSrv) {
  257. singletonInstance = instance;
  258. }
  259. export function getKeybindingSrv(): KeybindingSrv {
  260. return singletonInstance;
  261. }