keybindingSrv.ts 5.4 KB

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