grafana/public/app/core/services/keybindingSrv.ts

336 lines
8.6 KiB
TypeScript
Raw Normal View History

2017-12-20 05:33:33 -06:00
import _ from 'lodash';
2017-12-20 05:33:33 -06:00
import coreModule from 'app/core/core_module';
import appEvents from 'app/core/app_events';
import { getExploreUrl } from 'app/core/utils/explore';
import locationUtil from 'app/core/utils/location_util';
import { store } from 'app/store/store';
2017-12-20 05:33:33 -06:00
import Mousetrap from 'mousetrap';
import 'mousetrap-global-bind';
import { ContextSrv } from './context_srv';
import { ILocationService, ITimeoutService } from 'angular';
export class KeybindingSrv {
helpModal: boolean;
modalOpen = false;
2018-03-29 02:15:15 -05:00
timepickerOpen = false;
/** @ngInject */
constructor(
private $rootScope: any,
private $location: ILocationService,
private $timeout: ITimeoutService,
private datasourceSrv: any,
private timeSrv: any,
private contextSrv: ContextSrv
) {
// clear out all shortcuts on route change
2017-12-20 05:33:33 -06:00
$rootScope.$on('$routeChangeSuccess', () => {
Mousetrap.reset();
// rebind global shortcuts
this.setupGlobal();
});
this.setupGlobal();
appEvents.on('show-modal', () => (this.modalOpen = true));
appEvents.on('timepickerOpen', () => (this.timepickerOpen = true));
appEvents.on('timepickerClosed', () => (this.timepickerOpen = false));
}
setupGlobal() {
if (!(this.$location.path() === '/login')) {
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);
DataLinks: enable access to labels & field names (#18918) * POC: trying to see if there is a way to support objects in template interpolations * Added support for nested objects, and arrays * Added accessor cache * fixed unit tests * First take * Use links supplier in graph * Add field's index to cache items * Get field index from field cache * CHange FiledCacheItem to FieldWithIndex * Add refId to TimeSeries class * Make field link supplier work with _series, _field and _value vars * use field link supplier in graph * Fix yaxis settings * Update dashboard schema version and add migration for data links variables * Update snapshots * Update build in data link variables * FieldCache - idx -> index * Add current query results to panel editor * WIP Updated data links dropdown to display new variables * Fix build * Update variables syntac in field display, update migration * Field links supplier: review updates * Add data frame view and field name to TimeSeries for later inspection * Retrieve data frame from TimeSeries when clicking on plot graph * Use data frame's index instead of view * Retrieve data frame by index instead of view on TimeSeries * Update data links prism regex * Fix typecheck * Add value variables to suggestions list * UI update * Rename field to config in DisplayProcessorOptions * Proces single value of a field instead of entire data frame * Updated font size from 10px to 12px for auto complete * Replace fieldName with fieldIndex in TimeSeries * Don't use .entries() for iterating in field cache * Don't use FieldCache when retrieving field for datalinks in graph * Add value calculation variable to data links (#19031) * Add support for labels with dots in the name (#19033) * Docs update * Use field name instead of removed series.fieldName * Add test dashboard * Typos fix * Make visualization tab subscribe to query results * Added tags to dashboard so it shows up in lists * minor docs fix * Update singlestat-ish variables suggestions to contain series variables * Decrease suggestions update debounce * Enable whitespace characters(new line, space) in links and strip them when processing the data link * minor data links UI update * DataLinks: Add __from and __to variables suggestions to data links (#19093) * Add from and to variables suggestions to data links * Update docs * UI update and added info text * Change ESC global bind to bind (doesn't capture ESC on input) * Close datalinks suggestions on ESC * Remove unnecessary fragment
2019-09-13 09:38:21 -05:00
this.bind('esc', this.exit);
this.bindGlobal('esc', this.globalEsc);
}
}
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();
}
openSearch() {
2017-12-20 05:33:33 -06:00
appEvents.emit('show-dash-search');
}
openAlerting() {
2017-12-20 05:33:33 -06:00
this.$location.url('/alerting');
}
goToHome() {
2017-12-20 05:33:33 -06:00
this.$location.url('/');
}
goToProfile() {
2017-12-20 05:33:33 -06:00
this.$location.url('/profile');
}
showHelpModal() {
2017-12-20 05:33:33 -06:00
appEvents.emit('show-modal', { templateHtml: '<help-modal></help-modal>' });
}
exit() {
appEvents.emit('hide-modal');
if (this.modalOpen) {
this.modalOpen = false;
return;
}
if (this.timepickerOpen) {
this.$rootScope.appEvent('closeTimepicker');
this.timepickerOpen = false;
return;
}
// close settings view
const search = this.$location.search();
if (search.editview) {
delete search.editview;
this.$location.search(search);
return;
}
if (search.fullscreen) {
appEvents.emit('panel-change-view', { fullscreen: false, edit: false });
return;
}
if (search.kiosk) {
this.$rootScope.appEvent('toggle-kiosk-mode', { exit: true });
}
}
bind(keyArg: string | string[], fn: () => void) {
Mousetrap.bind(
keyArg,
(evt: any) => {
evt.preventDefault();
evt.stopPropagation();
evt.returnValue = false;
return this.$rootScope.$apply(fn.bind(this));
},
2017-12-20 05:33:33 -06:00
'keydown'
);
}
bindGlobal(keyArg: string, fn: () => void) {
Mousetrap.bindGlobal(
keyArg,
(evt: any) => {
evt.preventDefault();
evt.stopPropagation();
evt.returnValue = false;
return this.$rootScope.$apply(fn.bind(this));
},
'keydown'
);
}
unbind(keyArg: string, keyType?: string) {
Mousetrap.unbind(keyArg, keyType);
}
2017-12-08 08:53:26 -06:00
showDashEditView() {
const search = _.extend(this.$location.search(), { editview: 'settings' });
this.$location.search(search);
}
setupDashboardBindings(scope: any, dashboard: any) {
2017-12-20 05:33:33 -06:00
this.bind('mod+o', () => {
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();
});
this.bind('mod+s', () => {
2017-12-20 05:33:33 -06:00
scope.appEvent('save-dashboard');
});
2017-12-20 05:33:33 -06:00
this.bind('t z', () => {
scope.appEvent('zoom-out', 2);
});
2017-12-20 05:33:33 -06:00
this.bind('ctrl+z', () => {
scope.appEvent('zoom-out', 2);
});
2017-12-20 05:33:33 -06:00
this.bind('t left', () => {
scope.appEvent('shift-time', -1);
});
2017-12-20 05:33:33 -06:00
this.bind('t right', () => {
scope.appEvent('shift-time', 1);
});
2016-11-03 07:19:40 -05:00
// edit panel
2017-12-20 05:33:33 -06:00
this.bind('e', () => {
if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
appEvents.emit('panel-change-view', {
fullscreen: true,
edit: true,
panelId: dashboard.meta.focusPanelId,
2017-12-20 05:33:33 -06:00
toggle: true,
});
}
});
2016-11-03 07:19:40 -05:00
// view panel
2017-12-20 05:33:33 -06:00
this.bind('v', () => {
if (dashboard.meta.focusPanelId) {
appEvents.emit('panel-change-view', {
fullscreen: true,
panelId: dashboard.meta.focusPanelId,
2017-12-20 05:33:33 -06:00
toggle: true,
});
}
});
// jump to explore if permissions allow
if (this.contextSrv.hasAccessToExplore()) {
this.bind('x', async () => {
if (dashboard.meta.focusPanelId) {
const panel = dashboard.getPanelById(dashboard.meta.focusPanelId);
const datasource = await this.datasourceSrv.get(panel.datasource);
const url = await getExploreUrl(panel, panel.targets, datasource, this.datasourceSrv, this.timeSrv);
const urlWithoutBase = locationUtil.stripBaseFromUrl(url);
if (urlWithoutBase) {
this.$timeout(() => this.$location.url(urlWithoutBase));
}
}
});
}
2016-11-03 07:19:40 -05:00
// delete panel
2017-12-20 05:33:33 -06:00
this.bind('p r', () => {
if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
appEvents.emit('remove-panel', dashboard.meta.focusPanelId);
dashboard.meta.focusPanelId = 0;
}
});
// duplicate panel
this.bind('p d', () => {
if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
const panelIndex = dashboard.getPanelInfoById(dashboard.meta.focusPanelId).index;
dashboard.duplicatePanel(dashboard.panels[panelIndex]);
}
});
// share panel
2017-12-20 05:33:33 -06:00
this.bind('p s', () => {
if (dashboard.meta.focusPanelId) {
const shareScope = scope.$new();
const panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
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,
});
}
});
// 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;
panelRef.render();
}
}
});
// toggle all panel legends
this.bind('d l', () => {
dashboard.toggleLegendsForAll();
});
// collapse all rows
2017-12-20 05:33:33 -06:00
this.bind('d shift+c', () => {
dashboard.collapseRows();
});
// expand all rows
2017-12-20 05:33:33 -06:00
this.bind('d shift+e', () => {
dashboard.expandRows();
});
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();
});
2017-12-20 05:33:33 -06:00
this.bind('d s', () => {
2017-12-08 08:53:26 -06:00
this.showDashEditView();
});
2017-12-20 05:33:33 -06:00
this.bind('d k', () => {
appEvents.emit('toggle-kiosk-mode');
});
2017-12-20 05:33:33 -06:00
this.bind('d v', () => {
appEvents.emit('toggle-view-mode');
});
2018-08-03 04:00:27 -05:00
//Autofit panels
this.bind('d a', () => {
// this has to be a full page reload
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
});
}
}
2017-12-20 05:33:33 -06:00
coreModule.service('keybindingSrv', KeybindingSrv);
/**
* Code below exports the service to react components
*/
let singletonInstance: KeybindingSrv;
export function setKeybindingSrv(instance: KeybindingSrv) {
singletonInstance = instance;
}
export function getKeybindingSrv(): KeybindingSrv {
return singletonInstance;
}