keybindingSrv.ts 7.8 KB

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