keybindingSrv.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. // edit panel
  92. this.bind('e', () => {
  93. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  94. this.$rootScope.appEvent('panel-change-view', {
  95. fullscreen: true,
  96. edit: true,
  97. panelId: dashboard.meta.focusPanelId,
  98. toggle: true
  99. });
  100. }
  101. });
  102. // view panel
  103. this.bind('v', () => {
  104. if (dashboard.meta.focusPanelId) {
  105. this.$rootScope.appEvent('panel-change-view', {
  106. fullscreen: true,
  107. edit: null,
  108. panelId: dashboard.meta.focusPanelId,
  109. toggle: true,
  110. });
  111. }
  112. });
  113. // delete panel
  114. this.bind('p r', () => {
  115. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  116. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  117. panelInfo.row.removePanel(panelInfo.panel);
  118. dashboard.meta.focusPanelId = 0;
  119. }
  120. });
  121. // share panel
  122. this.bind('p s', () => {
  123. if (dashboard.meta.focusPanelId) {
  124. var shareScope = scope.$new();
  125. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  126. shareScope.panel = panelInfo.panel;
  127. shareScope.dashboard = dashboard;
  128. appEvents.emit('show-modal', {
  129. src: 'public/app/features/dashboard/partials/shareModal.html',
  130. scope: shareScope
  131. });
  132. }
  133. });
  134. // delete row
  135. this.bind('r r', () => {
  136. if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
  137. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  138. dashboard.removeRow(panelInfo.row);
  139. dashboard.meta.focusPanelId = 0;
  140. }
  141. });
  142. // collapse row
  143. this.bind('r c', () => {
  144. if (dashboard.meta.focusPanelId) {
  145. var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
  146. panelInfo.row.toggleCollapse();
  147. dashboard.meta.focusPanelId = 0;
  148. }
  149. });
  150. // collapse all rows
  151. this.bind('d C', () => {
  152. for (let row of dashboard.rows) {
  153. row.collapse = true;
  154. }
  155. });
  156. // expand all rows
  157. this.bind('d E', () => {
  158. for (let row of dashboard.rows) {
  159. row.collapse = false;
  160. }
  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. // close modals
  180. var modalData = $(".modal").data();
  181. if (modalData && modalData.$scope && modalData.$scope.dismiss) {
  182. modalData.$scope.dismiss();
  183. }
  184. scope.appEvent('hide-dash-editor');
  185. scope.appEvent('panel-change-view', {fullscreen: false, edit: false});
  186. });
  187. }
  188. }
  189. coreModule.service('keybindingSrv', KeybindingSrv);