2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
2016-11-02 06:55:58 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
import coreModule from 'app/core/core_module';
|
|
|
|
import appEvents from 'app/core/app_events';
|
2018-09-24 10:47:43 -05:00
|
|
|
import { getExploreUrl } from 'app/core/utils/explore';
|
2019-09-27 03:17:07 -05:00
|
|
|
import locationUtil from 'app/core/utils/location_util';
|
2019-03-07 03:32:36 -06:00
|
|
|
import { store } from 'app/store/store';
|
2019-11-07 06:49:45 -06:00
|
|
|
import { AppEventEmitter, CoreEvents } from 'app/types';
|
2016-11-02 06:55:58 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
import Mousetrap from 'mousetrap';
|
2018-02-16 02:14:32 -06:00
|
|
|
import 'mousetrap-global-bind';
|
2019-01-21 01:47:41 -06:00
|
|
|
import { ContextSrv } from './context_srv';
|
2019-11-07 06:49:45 -06:00
|
|
|
import { ILocationService, IRootScopeService, ITimeoutService } from 'angular';
|
2019-10-14 03:27:47 -05:00
|
|
|
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
|
2019-11-04 13:53:44 -06:00
|
|
|
import { getLocationSrv } from '@grafana/runtime';
|
2020-01-03 05:18:39 -06:00
|
|
|
import { DashboardModel } from '../../features/dashboard/state';
|
2020-03-04 03:59:30 -06:00
|
|
|
import { ShareModal } from 'app/features/dashboard/components/ShareModal';
|
2020-03-03 01:22:26 -06:00
|
|
|
import { SaveDashboardModalProxy } from '../../features/dashboard/components/SaveDashboard/SaveDashboardModalProxy';
|
2016-11-02 06:55:58 -05:00
|
|
|
|
|
|
|
export class KeybindingSrv {
|
|
|
|
helpModal: boolean;
|
2018-02-16 02:14:32 -06:00
|
|
|
modalOpen = false;
|
2018-03-29 02:15:15 -05:00
|
|
|
timepickerOpen = false;
|
2016-11-02 06:55:58 -05:00
|
|
|
|
|
|
|
/** @ngInject */
|
2018-09-24 10:47:43 -05:00
|
|
|
constructor(
|
2019-10-14 03:27:47 -05:00
|
|
|
private $rootScope: GrafanaRootScope,
|
2019-04-28 02:58:12 -05:00
|
|
|
private $location: ILocationService,
|
|
|
|
private $timeout: ITimeoutService,
|
|
|
|
private datasourceSrv: any,
|
|
|
|
private timeSrv: any,
|
2019-01-21 01:47:41 -06:00
|
|
|
private contextSrv: ContextSrv
|
2018-09-24 10:47:43 -05:00
|
|
|
) {
|
2016-11-02 06:55:58 -05:00
|
|
|
// clear out all shortcuts on route change
|
2017-12-20 05:33:33 -06:00
|
|
|
$rootScope.$on('$routeChangeSuccess', () => {
|
2016-11-02 06:55:58 -05:00
|
|
|
Mousetrap.reset();
|
|
|
|
// rebind global shortcuts
|
|
|
|
this.setupGlobal();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setupGlobal();
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.on(CoreEvents.showModal, () => (this.modalOpen = true));
|
|
|
|
appEvents.on(CoreEvents.timepickerOpen, () => (this.timepickerOpen = true));
|
|
|
|
appEvents.on(CoreEvents.timepickerClosed, () => (this.timepickerOpen = false));
|
2016-11-02 06:55:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setupGlobal() {
|
2019-08-02 01:15:36 -05:00
|
|
|
if (!(this.$location.path() === '/login')) {
|
2019-07-31 08:52:46 -05:00
|
|
|
this.bind(['?', 'h'], this.showHelpModal);
|
|
|
|
this.bind('g h', this.goToHome);
|
|
|
|
this.bind('g a', this.openAlerting);
|
|
|
|
this.bind('g p', this.goToProfile);
|
|
|
|
this.bind('s o', this.openSearch);
|
|
|
|
this.bind('f', this.openSearch);
|
2019-09-13 09:38:21 -05:00
|
|
|
this.bind('esc', this.exit);
|
2019-09-24 02:03:14 -05:00
|
|
|
this.bindGlobal('esc', this.globalEsc);
|
2019-07-31 08:52:46 -05:00
|
|
|
}
|
2016-11-02 06:55:58 -05:00
|
|
|
}
|
|
|
|
|
2019-09-24 02:03:14 -05:00
|
|
|
globalEsc() {
|
|
|
|
const anyDoc = document as any;
|
|
|
|
const activeElement = anyDoc.activeElement;
|
|
|
|
|
|
|
|
// typehead needs to handle it
|
|
|
|
const typeaheads = document.querySelectorAll('.slate-typeahead--open');
|
|
|
|
if (typeaheads.length > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// second check if we are in an input we can blur
|
|
|
|
if (activeElement && activeElement.blur) {
|
|
|
|
if (
|
|
|
|
activeElement.nodeName === 'INPUT' ||
|
|
|
|
activeElement.nodeName === 'TEXTAREA' ||
|
|
|
|
activeElement.hasAttribute('data-slate-editor')
|
|
|
|
) {
|
|
|
|
anyDoc.activeElement.blur();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok no focused input or editor that should block this, let exist!
|
|
|
|
this.exit();
|
|
|
|
}
|
|
|
|
|
2016-11-02 06:55:58 -05:00
|
|
|
openSearch() {
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.emit(CoreEvents.showDashSearch);
|
2016-11-02 06:55:58 -05:00
|
|
|
}
|
|
|
|
|
2016-11-02 16:08:17 -05:00
|
|
|
openAlerting() {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.$location.url('/alerting');
|
2016-11-02 16:08:17 -05:00
|
|
|
}
|
|
|
|
|
2016-11-02 06:55:58 -05:00
|
|
|
goToHome() {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.$location.url('/');
|
2016-11-02 06:55:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
goToProfile() {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.$location.url('/profile');
|
2016-11-02 06:55:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
showHelpModal() {
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.emit(CoreEvents.showModal, { templateHtml: '<help-modal></help-modal>' });
|
2016-11-02 06:55:58 -05:00
|
|
|
}
|
|
|
|
|
2018-02-16 02:14:32 -06:00
|
|
|
exit() {
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.emit(CoreEvents.hideModal);
|
2018-02-16 02:14:32 -06:00
|
|
|
|
2018-08-30 04:52:31 -05:00
|
|
|
if (this.modalOpen) {
|
2018-02-16 02:14:32 -06:00
|
|
|
this.modalOpen = false;
|
2018-08-30 04:52:31 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.timepickerOpen) {
|
2019-10-14 03:27:47 -05:00
|
|
|
this.$rootScope.appEvent(CoreEvents.closeTimepicker);
|
2018-08-30 04:52:31 -05:00
|
|
|
this.timepickerOpen = false;
|
|
|
|
return;
|
2018-02-16 02:14:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// close settings view
|
2018-08-26 14:52:57 -05:00
|
|
|
const search = this.$location.search();
|
2018-02-16 02:14:32 -06:00
|
|
|
if (search.editview) {
|
|
|
|
delete search.editview;
|
|
|
|
this.$location.search(search);
|
2018-08-30 04:52:31 -05:00
|
|
|
return;
|
2019-12-16 02:18:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (search.editPanel) {
|
|
|
|
delete search.editPanel;
|
2020-04-04 08:44:55 -05:00
|
|
|
delete search.tab;
|
2019-12-16 02:18:48 -06:00
|
|
|
this.$location.search(search);
|
|
|
|
return;
|
2018-08-30 04:52:31 -05:00
|
|
|
}
|
|
|
|
|
2020-04-10 09:37:26 -05:00
|
|
|
if (search.viewPanel) {
|
|
|
|
delete search.viewPanel;
|
|
|
|
this.$location.search(search);
|
2018-08-30 04:52:31 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (search.kiosk) {
|
2019-10-14 03:27:47 -05:00
|
|
|
this.$rootScope.appEvent(CoreEvents.toggleKioskMode, { exit: true });
|
2018-02-16 02:14:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-28 02:58:12 -05:00
|
|
|
bind(keyArg: string | string[], fn: () => void) {
|
2017-12-19 09:06:54 -06:00
|
|
|
Mousetrap.bind(
|
|
|
|
keyArg,
|
2019-04-28 02:58:12 -05:00
|
|
|
(evt: any) => {
|
2017-12-19 09:06:54 -06:00
|
|
|
evt.preventDefault();
|
|
|
|
evt.stopPropagation();
|
|
|
|
evt.returnValue = false;
|
|
|
|
return this.$rootScope.$apply(fn.bind(this));
|
|
|
|
},
|
2017-12-20 05:33:33 -06:00
|
|
|
'keydown'
|
2017-12-19 09:06:54 -06:00
|
|
|
);
|
2016-11-02 06:55:58 -05:00
|
|
|
}
|
|
|
|
|
2019-04-28 02:58:12 -05:00
|
|
|
bindGlobal(keyArg: string, fn: () => void) {
|
2018-02-16 02:14:32 -06:00
|
|
|
Mousetrap.bindGlobal(
|
|
|
|
keyArg,
|
2019-04-28 02:58:12 -05:00
|
|
|
(evt: any) => {
|
2018-02-16 02:14:32 -06:00
|
|
|
evt.preventDefault();
|
|
|
|
evt.stopPropagation();
|
|
|
|
evt.returnValue = false;
|
|
|
|
return this.$rootScope.$apply(fn.bind(this));
|
|
|
|
},
|
|
|
|
'keydown'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:44:00 -06:00
|
|
|
unbind(keyArg: string, keyType?: string) {
|
|
|
|
Mousetrap.unbind(keyArg, keyType);
|
|
|
|
}
|
|
|
|
|
2017-12-08 08:53:26 -06:00
|
|
|
showDashEditView() {
|
2018-08-26 14:52:57 -05:00
|
|
|
const search = _.extend(this.$location.search(), { editview: 'settings' });
|
2016-11-03 09:14:44 -05:00
|
|
|
this.$location.search(search);
|
|
|
|
}
|
|
|
|
|
2020-01-03 05:18:39 -06:00
|
|
|
setupDashboardBindings(scope: IRootScopeService & AppEventEmitter, dashboard: DashboardModel) {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('mod+o', () => {
|
2016-12-14 07:33:33 -06:00
|
|
|
dashboard.graphTooltip = (dashboard.graphTooltip + 1) % 3;
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.emit(CoreEvents.graphHoverClear);
|
2018-10-19 03:05:48 -05:00
|
|
|
dashboard.startRefresh();
|
2016-11-02 06:55:58 -05:00
|
|
|
});
|
|
|
|
|
2019-04-28 02:58:12 -05:00
|
|
|
this.bind('mod+s', () => {
|
2020-03-03 01:22:26 -06:00
|
|
|
appEvents.emit(CoreEvents.showModalReact, {
|
|
|
|
component: SaveDashboardModalProxy,
|
|
|
|
props: {
|
|
|
|
dashboard,
|
|
|
|
},
|
|
|
|
});
|
2016-11-02 06:55:58 -05:00
|
|
|
});
|
2016-11-04 05:43:44 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('t z', () => {
|
2019-10-14 03:27:47 -05:00
|
|
|
scope.appEvent(CoreEvents.zoomOut, 2);
|
2016-12-06 00:42:22 -06:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('ctrl+z', () => {
|
2019-10-14 03:27:47 -05:00
|
|
|
scope.appEvent(CoreEvents.zoomOut, 2);
|
2016-11-02 06:55:58 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('t left', () => {
|
2019-10-14 03:27:47 -05:00
|
|
|
scope.appEvent(CoreEvents.shiftTime, -1);
|
2016-11-02 06:55:58 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('t right', () => {
|
2019-10-14 03:27:47 -05:00
|
|
|
scope.appEvent(CoreEvents.shiftTime, 1);
|
2016-11-02 06:55:58 -05:00
|
|
|
});
|
|
|
|
|
2016-11-03 07:19:40 -05:00
|
|
|
// edit panel
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('e', () => {
|
2020-01-03 05:18:39 -06:00
|
|
|
if (dashboard.canEditPanelById(dashboard.meta.focusPanelId)) {
|
2020-04-10 09:37:26 -05:00
|
|
|
const search = _.extend(this.$location.search(), { editPanel: dashboard.meta.focusPanelId });
|
|
|
|
this.$location.search(search);
|
2016-11-02 09:16:48 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-11-03 07:19:40 -05:00
|
|
|
// view panel
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('v', () => {
|
2016-11-02 11:03:14 -05:00
|
|
|
if (dashboard.meta.focusPanelId) {
|
2020-04-10 09:37:26 -05:00
|
|
|
const search = _.extend(this.$location.search(), { viewPanel: dashboard.meta.focusPanelId });
|
|
|
|
this.$location.search(search);
|
2016-11-02 11:03:14 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-30 06:13:29 -05:00
|
|
|
// jump to explore if permissions allow
|
2019-01-21 03:50:34 -06:00
|
|
|
if (this.contextSrv.hasAccessToExplore()) {
|
2018-05-30 06:13:29 -05:00
|
|
|
this.bind('x', async () => {
|
|
|
|
if (dashboard.meta.focusPanelId) {
|
|
|
|
const panel = dashboard.getPanelById(dashboard.meta.focusPanelId);
|
|
|
|
const datasource = await this.datasourceSrv.get(panel.datasource);
|
2019-11-07 06:49:45 -06:00
|
|
|
const url = await getExploreUrl({
|
|
|
|
panel,
|
|
|
|
panelTargets: panel.targets,
|
|
|
|
panelDatasource: datasource,
|
|
|
|
datasourceSrv: this.datasourceSrv,
|
|
|
|
timeSrv: this.timeSrv,
|
|
|
|
});
|
2019-09-27 03:17:07 -05:00
|
|
|
const urlWithoutBase = locationUtil.stripBaseFromUrl(url);
|
|
|
|
|
|
|
|
if (urlWithoutBase) {
|
|
|
|
this.$timeout(() => this.$location.url(urlWithoutBase));
|
2018-05-30 06:13:29 -05:00
|
|
|
}
|
2018-04-29 07:02:32 -05:00
|
|
|
}
|
2018-05-30 06:13:29 -05:00
|
|
|
});
|
|
|
|
}
|
2018-04-29 07:02:32 -05:00
|
|
|
|
2016-11-03 07:19:40 -05:00
|
|
|
// delete panel
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('p r', () => {
|
2020-01-03 05:18:39 -06:00
|
|
|
if (dashboard.canEditPanelById(dashboard.meta.focusPanelId)) {
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.emit(CoreEvents.removePanel, dashboard.meta.focusPanelId);
|
2016-11-02 09:16:48 -05:00
|
|
|
dashboard.meta.focusPanelId = 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-15 11:22:11 -05:00
|
|
|
// duplicate panel
|
|
|
|
this.bind('p d', () => {
|
2020-01-03 05:18:39 -06:00
|
|
|
if (dashboard.canEditPanelById(dashboard.meta.focusPanelId)) {
|
2018-08-26 10:14:40 -05:00
|
|
|
const panelIndex = dashboard.getPanelInfoById(dashboard.meta.focusPanelId).index;
|
2018-03-15 11:22:11 -05:00
|
|
|
dashboard.duplicatePanel(dashboard.panels[panelIndex]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-11-04 11:19:03 -05:00
|
|
|
// share panel
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('p s', () => {
|
2016-11-04 11:19:03 -05:00
|
|
|
if (dashboard.meta.focusPanelId) {
|
2018-08-26 14:52:57 -05:00
|
|
|
const panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
|
2016-11-04 11:19:03 -05:00
|
|
|
|
2020-03-03 06:04:28 -06:00
|
|
|
appEvents.emit(CoreEvents.showModalReact, {
|
|
|
|
component: ShareModal,
|
|
|
|
props: {
|
|
|
|
dashboard: dashboard,
|
|
|
|
panel: panelInfo?.panel,
|
|
|
|
},
|
2016-11-04 11:19:03 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-04 13:53:44 -06:00
|
|
|
// inspect panel
|
|
|
|
this.bind('p i', () => {
|
|
|
|
if (dashboard.meta.focusPanelId) {
|
|
|
|
getLocationSrv().update({ partial: true, query: { inspect: dashboard.meta.focusPanelId } });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-19 15:33:23 -05:00
|
|
|
// toggle panel legend
|
|
|
|
this.bind('p l', () => {
|
|
|
|
if (dashboard.meta.focusPanelId) {
|
|
|
|
const panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
|
|
|
|
if (panelInfo.panel.legend) {
|
|
|
|
const panelRef = dashboard.getPanelById(dashboard.meta.focusPanelId);
|
|
|
|
panelRef.legend.show = !panelRef.legend.show;
|
2019-02-01 13:16:27 -06:00
|
|
|
panelRef.render();
|
2018-10-19 15:33:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-03-04 09:57:22 -06:00
|
|
|
// toggle all panel legends
|
|
|
|
this.bind('d l', () => {
|
|
|
|
dashboard.toggleLegendsForAll();
|
|
|
|
});
|
|
|
|
|
2017-02-13 07:27:12 -06:00
|
|
|
// collapse all rows
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('d shift+c', () => {
|
2018-02-22 11:03:29 -06:00
|
|
|
dashboard.collapseRows();
|
2017-02-13 07:27:12 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// expand all rows
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('d shift+e', () => {
|
2018-02-22 11:03:29 -06:00
|
|
|
dashboard.expandRows();
|
2017-02-13 07:27:12 -06:00
|
|
|
});
|
|
|
|
|
2019-04-28 02:58:12 -05:00
|
|
|
this.bind('d n', () => {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.$location.url('/dashboard/new');
|
2017-07-17 23:07:39 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('d r', () => {
|
2018-10-19 03:05:48 -05:00
|
|
|
dashboard.startRefresh();
|
2016-11-03 09:14:44 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('d s', () => {
|
2017-12-08 08:53:26 -06:00
|
|
|
this.showDashEditView();
|
2016-11-03 09:14:44 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('d k', () => {
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.emit(CoreEvents.toggleKioskMode);
|
2016-11-05 08:23:37 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('d v', () => {
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.emit(CoreEvents.toggleViewMode);
|
2016-11-05 08:23:37 -05:00
|
|
|
});
|
2018-08-03 04:00:27 -05:00
|
|
|
|
|
|
|
//Autofit panels
|
|
|
|
this.bind('d a', () => {
|
2018-08-14 05:25:19 -05:00
|
|
|
// this has to be a full page reload
|
2019-03-07 03:32:36 -06:00
|
|
|
const queryParams = store.getState().location.query;
|
|
|
|
const newUrlParam = queryParams.autofitpanels ? '' : '&autofitpanels';
|
|
|
|
window.location.href = window.location.href + newUrlParam;
|
2018-08-03 04:00:27 -05:00
|
|
|
});
|
2016-11-02 06:55:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
coreModule.service('keybindingSrv', KeybindingSrv);
|
2019-01-31 08:44:00 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Code below exports the service to react components
|
|
|
|
*/
|
|
|
|
|
|
|
|
let singletonInstance: KeybindingSrv;
|
|
|
|
|
|
|
|
export function setKeybindingSrv(instance: KeybindingSrv) {
|
|
|
|
singletonInstance = instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getKeybindingSrv(): KeybindingSrv {
|
|
|
|
return singletonInstance;
|
|
|
|
}
|