keybindingSrv.ts 8.0 KB

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