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';
|
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-04-28 02:58:12 -05:00
|
|
|
import { ILocationService, ITimeoutService } from 'angular';
|
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-04-28 02:58:12 -05:00
|
|
|
private $rootScope: any,
|
|
|
|
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();
|
2018-02-16 02:14:32 -06:00
|
|
|
appEvents.on('show-modal', () => (this.modalOpen = true));
|
2018-11-15 10:33:55 -06:00
|
|
|
appEvents.on('timepickerOpen', () => (this.timepickerOpen = true));
|
|
|
|
appEvents.on('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() {
|
2017-12-20 05:33:33 -06:00
|
|
|
appEvents.emit('show-dash-search');
|
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() {
|
2017-12-20 05:33:33 -06:00
|
|
|
appEvents.emit('show-modal', { templateHtml: '<help-modal></help-modal>' });
|
2016-11-02 06:55:58 -05:00
|
|
|
}
|
|
|
|
|
2018-02-16 02:14:32 -06:00
|
|
|
exit() {
|
|
|
|
appEvents.emit('hide-modal');
|
|
|
|
|
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) {
|
|
|
|
this.$rootScope.appEvent('closeTimepicker');
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (search.fullscreen) {
|
2019-02-04 08:41:57 -06:00
|
|
|
appEvents.emit('panel-change-view', { fullscreen: false, edit: false });
|
2018-08-30 04:52:31 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (search.kiosk) {
|
|
|
|
this.$rootScope.appEvent('toggle-kiosk-mode', { 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);
|
|
|
|
}
|
|
|
|
|
2019-04-28 02:58:12 -05:00
|
|
|
setupDashboardBindings(scope: any, dashboard: any) {
|
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;
|
2017-12-20 05:33:33 -06:00
|
|
|
appEvents.emit('graph-hover-clear');
|
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', () => {
|
2017-12-20 05:33:33 -06:00
|
|
|
scope.appEvent('save-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', () => {
|
|
|
|
scope.appEvent('zoom-out', 2);
|
2016-12-06 00:42:22 -06:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('ctrl+z', () => {
|
|
|
|
scope.appEvent('zoom-out', 2);
|
2016-11-02 06:55:58 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('t left', () => {
|
2019-08-01 03:17:27 -05:00
|
|
|
scope.appEvent('shift-time', -1);
|
2016-11-02 06:55:58 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('t right', () => {
|
2019-08-01 03:17:27 -05:00
|
|
|
scope.appEvent('shift-time', 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', () => {
|
2016-11-02 09:16:48 -05:00
|
|
|
if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
|
2019-02-04 08:41:57 -06:00
|
|
|
appEvents.emit('panel-change-view', {
|
2016-11-02 15:45:46 -05:00
|
|
|
fullscreen: true,
|
|
|
|
edit: true,
|
|
|
|
panelId: dashboard.meta.focusPanelId,
|
2017-12-20 05:33:33 -06:00
|
|
|
toggle: true,
|
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) {
|
2019-02-04 08:41:57 -06:00
|
|
|
appEvents.emit('panel-change-view', {
|
2016-11-02 15:45:46 -05:00
|
|
|
fullscreen: true,
|
|
|
|
panelId: dashboard.meta.focusPanelId,
|
2017-12-20 05:33:33 -06:00
|
|
|
toggle: true,
|
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-09-05 07:44:37 -05:00
|
|
|
const url = await getExploreUrl(panel, panel.targets, datasource, this.datasourceSrv, 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', () => {
|
2016-11-02 09:16:48 -05:00
|
|
|
if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
|
2019-02-05 08:15:15 -06:00
|
|
|
appEvents.emit('remove-panel', 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', () => {
|
|
|
|
if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
|
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 shareScope = scope.$new();
|
|
|
|
const panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
|
2016-11-04 11:19:03 -05:00
|
|
|
shareScope.panel = panelInfo.panel;
|
|
|
|
shareScope.dashboard = dashboard;
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
appEvents.emit('show-modal', {
|
2019-01-23 06:07:20 -06:00
|
|
|
src: 'public/app/features/dashboard/components/ShareModal/template.html',
|
2017-12-20 05:33:33 -06:00
|
|
|
scope: shareScope,
|
2016-11-04 11:19:03 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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', () => {
|
|
|
|
appEvents.emit('toggle-kiosk-mode');
|
2016-11-05 08:23:37 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.bind('d v', () => {
|
|
|
|
appEvents.emit('toggle-view-mode');
|
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;
|
|
|
|
}
|