keybindingSrv.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. ///<reference path="../../headers/common.d.ts" />
  2. import $ from 'jquery';
  3. import _ from 'lodash';
  4. import coreModule from 'app/core/core_module';
  5. import appEvents from 'app/core/app_events';
  6. import Mousetrap from 'mousetrap';
  7. export class KeybindingSrv {
  8. helpModal: boolean;
  9. /** @ngInject */
  10. constructor(
  11. private $rootScope,
  12. private $modal,
  13. private $location,
  14. private contextSrv,
  15. private $timeout) {
  16. // clear out all shortcuts on route change
  17. $rootScope.$on('$routeChangeSuccess', () => {
  18. Mousetrap.reset();
  19. // rebind global shortcuts
  20. this.setupGlobal();
  21. });
  22. this.setupGlobal();
  23. }
  24. setupGlobal() {
  25. this.bind(['?', 'h'], this.showHelpModal);
  26. this.bind("g h", this.goToHome);
  27. this.bind("g a", this.openAlerting);
  28. this.bind("g p", this.goToProfile);
  29. this.bind("s s", this.openSearchStarred);
  30. this.bind('s o', this.openSearch);
  31. this.bind('s t', this.openSearchTags);
  32. this.bind('f', this.openSearch);
  33. }
  34. openSearchStarred() {
  35. this.$rootScope.appEvent('show-dash-search', {starred: true});
  36. }
  37. openSearchTags() {
  38. this.$rootScope.appEvent('show-dash-search', {tagsMode: true});
  39. }
  40. openSearch() {
  41. this.$rootScope.appEvent('show-dash-search');
  42. }
  43. openAlerting() {
  44. this.$location.url("/alerting");
  45. }
  46. goToHome() {
  47. this.$location.url("/");
  48. }
  49. goToProfile() {
  50. this.$location.url("/profile");
  51. }
  52. showHelpModal() {
  53. appEvents.emit('show-modal', {templateHtml: '<help-modal></help-modal>'});
  54. }
  55. bind(keyArg, fn) {
  56. Mousetrap.bind(keyArg, evt => {
  57. evt.preventDefault();
  58. evt.stopPropagation();
  59. evt.returnValue = false;
  60. return this.$rootScope.$apply(fn.bind(this));
  61. }, 'keydown');
  62. }
  63. showDashEditView(view) {
  64. var search = _.extend(this.$location.search(), {editview: view});
  65. this.$location.search(search);
  66. }
  67. setupDashboardBindings(scope, dashboard) {
  68. this.bind('mod+o', () => {
  69. dashboard.graphTooltip = (dashboard.graphTooltip + 1) % 3;
  70. appEvents.emit('graph-hover-clear');
  71. scope.broadcastRefresh();
  72. });
  73. this.bind('mod+h', () => {
  74. dashboard.hideControls = !dashboard.hideControls;
  75. });
  76. this.bind('mod+s', e => {
  77. scope.appEvent('save-dashboard');
  78. });
  79. this.bind('t z', () => {
  80. scope.appEvent('zoom-out', 2);
  81. });
  82. this.bind('ctrl+z', () => {
  83. scope.appEvent('zoom-out', 2);
  84. });
  85. this.bind('t left', () => {
  86. scope.appEvent('shift-time-backward');
  87. });
  88. this.bind('t right', () => {
  89. scope.appEvent('shift-time-forward');
  90. });
  91. this.bind('mod+i', () => {
  92. scope.appEvent('quick-snapshot');
  93. });
  94. // edit panel
  95. this.bind('e', () => {
  96. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  97. this.$rootScope.appEvent('panel-change-view', {
  98. fullscreen: true,
  99. edit: true,
  100. panelId: dashboard.meta.focusPanelId,
  101. toggle: true
  102. });
  103. }
  104. });
  105. // view panel
  106. this.bind('v', () => {
  107. if (dashboard.meta.focusPanelId) {
  108. this.$rootScope.appEvent('panel-change-view', {
  109. fullscreen: true,
  110. edit: null,
  111. panelId: dashboard.meta.focusPanelId,
  112. toggle: true,
  113. });
  114. }
  115. });
  116. // delete panel
  117. this.bind('p r', () => {
  118. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  119. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  120. panelInfo.row.removePanel(panelInfo.panel);
  121. dashboard.meta.focusPanelId = 0;
  122. }
  123. });
  124. // share panel
  125. this.bind('p s', () => {
  126. if (dashboard.meta.focusPanelId) {
  127. var shareScope = scope.$new();
  128. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  129. shareScope.panel = panelInfo.panel;
  130. shareScope.dashboard = dashboard;
  131. appEvents.emit('show-modal', {
  132. src: 'public/app/features/dashboard/partials/shareModal.html',
  133. scope: shareScope
  134. });
  135. }
  136. });
  137. // delete row
  138. this.bind('r r', () => {
  139. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  140. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  141. dashboard.removeRow(panelInfo.row);
  142. dashboard.meta.focusPanelId = 0;
  143. }
  144. });
  145. // collapse row
  146. this.bind('r c', () => {
  147. if (dashboard.meta.focusPanelId) {
  148. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  149. panelInfo.row.toggleCollapse();
  150. dashboard.meta.focusPanelId = 0;
  151. }
  152. });
  153. // collapse all rows
  154. this.bind('d C', () => {
  155. for (let row of dashboard.rows) {
  156. row.collapse = true;
  157. }
  158. });
  159. // expand all rows
  160. this.bind('d E', () => {
  161. for (let row of dashboard.rows) {
  162. row.collapse = false;
  163. }
  164. });
  165. this.bind('d r', () => {
  166. scope.broadcastRefresh();
  167. });
  168. this.bind('d s', () => {
  169. this.showDashEditView('settings');
  170. });
  171. this.bind('d k', () => {
  172. appEvents.emit('toggle-kiosk-mode');
  173. });
  174. this.bind('d v', () => {
  175. appEvents.emit('toggle-view-mode');
  176. });
  177. this.bind('esc', () => {
  178. var popups = $('.popover.in');
  179. if (popups.length > 0) {
  180. return;
  181. }
  182. // close modals
  183. var modalData = $(".modal").data();
  184. if (modalData && modalData.$scope && modalData.$scope.dismiss) {
  185. modalData.$scope.dismiss();
  186. }
  187. scope.appEvent('hide-dash-editor');
  188. scope.exitFullscreen();
  189. });
  190. }
  191. }
  192. coreModule.service('keybindingSrv', KeybindingSrv);