keybindingSrv.ts 7.8 KB

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