keybindingSrv.ts 5.4 KB

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