keybindingSrv.ts 7.0 KB

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