keybindingSrv.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import $ from 'jquery';
  2. import _ from 'lodash';
  3. import config from 'app/core/config';
  4. import coreModule from 'app/core/core_module';
  5. import appEvents from 'app/core/app_events';
  6. import { getExploreUrl } from 'app/core/utils/explore';
  7. import Mousetrap from 'mousetrap';
  8. import 'mousetrap-global-bind';
  9. export class KeybindingSrv {
  10. helpModal: boolean;
  11. modalOpen = false;
  12. timepickerOpen = false;
  13. /** @ngInject */
  14. constructor(
  15. private $rootScope,
  16. private $location,
  17. private $timeout,
  18. private datasourceSrv,
  19. private timeSrv,
  20. private contextSrv
  21. ) {
  22. // clear out all shortcuts on route change
  23. $rootScope.$on('$routeChangeSuccess', () => {
  24. Mousetrap.reset();
  25. // rebind global shortcuts
  26. this.setupGlobal();
  27. });
  28. this.setupGlobal();
  29. appEvents.on('show-modal', () => (this.modalOpen = true));
  30. $rootScope.onAppEvent('timepickerOpen', () => (this.timepickerOpen = true));
  31. $rootScope.onAppEvent('timepickerClosed', () => (this.timepickerOpen = false));
  32. }
  33. setupGlobal() {
  34. this.bind(['?', 'h'], this.showHelpModal);
  35. this.bind('g h', this.goToHome);
  36. this.bind('g a', this.openAlerting);
  37. this.bind('g p', this.goToProfile);
  38. this.bind('s s', this.openSearchStarred);
  39. this.bind('s o', this.openSearch);
  40. this.bind('s t', this.openSearchTags);
  41. this.bind('f', this.openSearch);
  42. this.bindGlobal('esc', this.exit);
  43. }
  44. openSearchStarred() {
  45. appEvents.emit('show-dash-search', { starred: true });
  46. }
  47. openSearchTags() {
  48. appEvents.emit('show-dash-search', { tagsMode: true });
  49. }
  50. openSearch() {
  51. appEvents.emit('show-dash-search');
  52. }
  53. openAlerting() {
  54. this.$location.url('/alerting');
  55. }
  56. goToHome() {
  57. this.$location.url('/');
  58. }
  59. goToProfile() {
  60. this.$location.url('/profile');
  61. }
  62. showHelpModal() {
  63. appEvents.emit('show-modal', { templateHtml: '<help-modal></help-modal>' });
  64. }
  65. exit() {
  66. const popups = $('.popover.in');
  67. if (popups.length > 0) {
  68. return;
  69. }
  70. appEvents.emit('hide-modal');
  71. if (this.modalOpen) {
  72. this.modalOpen = false;
  73. return;
  74. }
  75. if (this.timepickerOpen) {
  76. this.$rootScope.appEvent('closeTimepicker');
  77. this.timepickerOpen = false;
  78. return;
  79. }
  80. // close settings view
  81. const search = this.$location.search();
  82. if (search.editview) {
  83. delete search.editview;
  84. this.$location.search(search);
  85. return;
  86. }
  87. if (search.fullscreen) {
  88. this.$rootScope.appEvent('panel-change-view', { fullscreen: false, edit: false });
  89. return;
  90. }
  91. if (search.kiosk) {
  92. this.$rootScope.appEvent('toggle-kiosk-mode', { exit: true });
  93. }
  94. }
  95. bind(keyArg, fn) {
  96. Mousetrap.bind(
  97. keyArg,
  98. evt => {
  99. evt.preventDefault();
  100. evt.stopPropagation();
  101. evt.returnValue = false;
  102. return this.$rootScope.$apply(fn.bind(this));
  103. },
  104. 'keydown'
  105. );
  106. }
  107. bindGlobal(keyArg, fn) {
  108. Mousetrap.bindGlobal(
  109. keyArg,
  110. evt => {
  111. evt.preventDefault();
  112. evt.stopPropagation();
  113. evt.returnValue = false;
  114. return this.$rootScope.$apply(fn.bind(this));
  115. },
  116. 'keydown'
  117. );
  118. }
  119. showDashEditView() {
  120. const search = _.extend(this.$location.search(), { editview: 'settings' });
  121. this.$location.search(search);
  122. }
  123. setupDashboardBindings(scope, dashboard) {
  124. this.bind('mod+o', () => {
  125. dashboard.graphTooltip = (dashboard.graphTooltip + 1) % 3;
  126. appEvents.emit('graph-hover-clear');
  127. dashboard.startRefresh();
  128. });
  129. this.bind('mod+s', e => {
  130. scope.appEvent('save-dashboard');
  131. });
  132. this.bind('t z', () => {
  133. scope.appEvent('zoom-out', 2);
  134. });
  135. this.bind('ctrl+z', () => {
  136. scope.appEvent('zoom-out', 2);
  137. });
  138. this.bind('t left', () => {
  139. scope.appEvent('shift-time-backward');
  140. });
  141. this.bind('t right', () => {
  142. scope.appEvent('shift-time-forward');
  143. });
  144. // edit panel
  145. this.bind('e', () => {
  146. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  147. this.$rootScope.appEvent('panel-change-view', {
  148. fullscreen: true,
  149. edit: true,
  150. panelId: dashboard.meta.focusPanelId,
  151. toggle: true,
  152. });
  153. }
  154. });
  155. // view panel
  156. this.bind('v', () => {
  157. if (dashboard.meta.focusPanelId) {
  158. this.$rootScope.appEvent('panel-change-view', {
  159. fullscreen: true,
  160. edit: null,
  161. panelId: dashboard.meta.focusPanelId,
  162. toggle: true,
  163. });
  164. }
  165. });
  166. // jump to explore if permissions allow
  167. if (this.contextSrv.isEditor && config.exploreEnabled) {
  168. this.bind('x', async () => {
  169. if (dashboard.meta.focusPanelId) {
  170. const panel = dashboard.getPanelById(dashboard.meta.focusPanelId);
  171. const datasource = await this.datasourceSrv.get(panel.datasource);
  172. const url = await getExploreUrl(panel, panel.targets, datasource, this.datasourceSrv, this.timeSrv);
  173. if (url) {
  174. this.$timeout(() => this.$location.url(url));
  175. }
  176. }
  177. });
  178. }
  179. // delete panel
  180. this.bind('p r', () => {
  181. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  182. this.$rootScope.appEvent('panel-remove', {
  183. panelId: dashboard.meta.focusPanelId,
  184. });
  185. dashboard.meta.focusPanelId = 0;
  186. }
  187. });
  188. // duplicate panel
  189. this.bind('p d', () => {
  190. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  191. const panelIndex = dashboard.getPanelInfoById(dashboard.meta.focusPanelId).index;
  192. dashboard.duplicatePanel(dashboard.panels[panelIndex]);
  193. }
  194. });
  195. // share panel
  196. this.bind('p s', () => {
  197. if (dashboard.meta.focusPanelId) {
  198. const shareScope = scope.$new();
  199. const panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  200. shareScope.panel = panelInfo.panel;
  201. shareScope.dashboard = dashboard;
  202. appEvents.emit('show-modal', {
  203. src: 'public/app/features/dashboard/partials/shareModal.html',
  204. scope: shareScope,
  205. });
  206. }
  207. });
  208. // toggle panel legend
  209. this.bind('p l', () => {
  210. if (dashboard.meta.focusPanelId) {
  211. const panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  212. if (panelInfo.panel.legend) {
  213. const panelRef = dashboard.getPanelById(dashboard.meta.focusPanelId);
  214. panelRef.legend.show = !panelRef.legend.show;
  215. panelRef.refresh();
  216. }
  217. }
  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', e => {
  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. window.location.href = window.location.href + '&autofitpanels';
  246. });
  247. }
  248. }
  249. coreModule.service('keybindingSrv', KeybindingSrv);