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

250 lines
5.9 KiB
TypeScript
Raw Normal View History

2017-12-20 12:33:33 +01:00
import $ from 'jquery';
import _ from 'lodash';
2017-12-20 12:33:33 +01:00
import coreModule from 'app/core/core_module';
import appEvents from 'app/core/app_events';
2017-12-20 12:33:33 +01:00
import Mousetrap from 'mousetrap';
import 'mousetrap-global-bind';
export class KeybindingSrv {
helpModal: boolean;
modalOpen = false;
/** @ngInject */
constructor(private $rootScope, private $location) {
// clear out all shortcuts on route change
2017-12-20 12:33:33 +01:00
$rootScope.$on('$routeChangeSuccess', () => {
Mousetrap.reset();
// rebind global shortcuts
this.setupGlobal();
});
this.setupGlobal();
appEvents.on('show-modal', () => (this.modalOpen = true));
}
setupGlobal() {
2017-12-20 12:33:33 +01: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 s', this.openSearchStarred);
this.bind('s o', this.openSearch);
this.bind('s t', this.openSearchTags);
this.bind('f', this.openSearch);
this.bindGlobal('esc', this.exit);
}
openSearchStarred() {
2017-12-20 12:33:33 +01:00
appEvents.emit('show-dash-search', { starred: true });
}
openSearchTags() {
2017-12-20 12:33:33 +01:00
appEvents.emit('show-dash-search', { tagsMode: true });
}
openSearch() {
2017-12-20 12:33:33 +01:00
appEvents.emit('show-dash-search');
}
openAlerting() {
2017-12-20 12:33:33 +01:00
this.$location.url('/alerting');
}
goToHome() {
2017-12-20 12:33:33 +01:00
this.$location.url('/');
}
goToProfile() {
2017-12-20 12:33:33 +01:00
this.$location.url('/profile');
}
showHelpModal() {
2017-12-20 12:33:33 +01:00
appEvents.emit('show-modal', { templateHtml: '<help-modal></help-modal>' });
}
exit() {
var popups = $('.popover.in');
if (popups.length > 0) {
return;
}
appEvents.emit('hide-modal');
if (!this.modalOpen) {
this.$rootScope.appEvent('panel-change-view', { fullscreen: false, edit: false });
} else {
this.modalOpen = false;
}
// close settings view
var search = this.$location.search();
if (search.editview) {
delete search.editview;
this.$location.search(search);
}
}
bind(keyArg, fn) {
Mousetrap.bind(
keyArg,
evt => {
evt.preventDefault();
evt.stopPropagation();
evt.returnValue = false;
return this.$rootScope.$apply(fn.bind(this));
},
2017-12-20 12:33:33 +01:00
'keydown'
);
}
bindGlobal(keyArg, fn) {
Mousetrap.bindGlobal(
keyArg,
evt => {
evt.preventDefault();
evt.stopPropagation();
evt.returnValue = false;
return this.$rootScope.$apply(fn.bind(this));
},
'keydown'
);
}
2017-12-08 15:53:26 +01:00
showDashEditView() {
2017-12-20 12:33:33 +01:00
var search = _.extend(this.$location.search(), { editview: 'settings' });
this.$location.search(search);
}
setupDashboardBindings(scope, dashboard) {
2017-12-20 12:33:33 +01:00
this.bind('mod+o', () => {
dashboard.graphTooltip = (dashboard.graphTooltip + 1) % 3;
2017-12-20 12:33:33 +01:00
appEvents.emit('graph-hover-clear');
this.$rootScope.$broadcast('refresh');
});
2017-12-20 12:33:33 +01:00
this.bind('mod+s', e => {
scope.appEvent('save-dashboard');
});
2017-12-20 12:33:33 +01:00
this.bind('t z', () => {
scope.appEvent('zoom-out', 2);
});
2017-12-20 12:33:33 +01:00
this.bind('ctrl+z', () => {
scope.appEvent('zoom-out', 2);
});
2017-12-20 12:33:33 +01:00
this.bind('t left', () => {
scope.appEvent('shift-time-backward');
});
2017-12-20 12:33:33 +01:00
this.bind('t right', () => {
scope.appEvent('shift-time-forward');
});
2016-11-03 13:19:40 +01:00
// edit panel
2017-12-20 12:33:33 +01:00
this.bind('e', () => {
if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
2017-12-20 12:33:33 +01:00
this.$rootScope.appEvent('panel-change-view', {
fullscreen: true,
edit: true,
panelId: dashboard.meta.focusPanelId,
2017-12-20 12:33:33 +01:00
toggle: true,
});
}
});
2016-11-03 13:19:40 +01:00
// view panel
2017-12-20 12:33:33 +01:00
this.bind('v', () => {
if (dashboard.meta.focusPanelId) {
2017-12-20 12:33:33 +01:00
this.$rootScope.appEvent('panel-change-view', {
fullscreen: true,
edit: null,
panelId: dashboard.meta.focusPanelId,
2017-12-20 12:33:33 +01:00
toggle: true,
});
}
});
2016-11-03 13:19:40 +01:00
// delete panel
2017-12-20 12:33:33 +01:00
this.bind('p r', () => {
if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
panelInfo.row.removePanel(panelInfo.panel);
dashboard.meta.focusPanelId = 0;
}
});
// share panel
2017-12-20 12:33:33 +01:00
this.bind('p s', () => {
if (dashboard.meta.focusPanelId) {
var shareScope = scope.$new();
var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
shareScope.panel = panelInfo.panel;
shareScope.dashboard = dashboard;
2017-12-20 12:33:33 +01:00
appEvents.emit('show-modal', {
src: 'public/app/features/dashboard/partials/shareModal.html',
scope: shareScope,
});
}
});
// delete row
2017-12-20 12:33:33 +01:00
this.bind('r r', () => {
if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) {
var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
dashboard.removeRow(panelInfo.row);
dashboard.meta.focusPanelId = 0;
}
});
// collapse row
2017-12-20 12:33:33 +01:00
this.bind('r c', () => {
if (dashboard.meta.focusPanelId) {
var panelInfo = dashboard.getPanelInfoById(dashboard.meta.focusPanelId);
panelInfo.row.toggleCollapse();
dashboard.meta.focusPanelId = 0;
}
});
// collapse all rows
2017-12-20 12:33:33 +01:00
this.bind('d shift+c', () => {
for (let row of dashboard.rows) {
row.collapse = true;
}
});
// expand all rows
2017-12-20 12:33:33 +01:00
this.bind('d shift+e', () => {
for (let row of dashboard.rows) {
row.collapse = false;
}
});
2017-12-20 12:33:33 +01:00
this.bind('d n', e => {
this.$location.url('/dashboard/new');
2017-07-18 13:07:39 +09:00
});
2017-12-20 12:33:33 +01:00
this.bind('d r', () => {
this.$rootScope.$broadcast('refresh');
});
2017-12-20 12:33:33 +01:00
this.bind('d s', () => {
2017-12-08 15:53:26 +01:00
this.showDashEditView();
});
2017-12-20 12:33:33 +01:00
this.bind('d k', () => {
appEvents.emit('toggle-kiosk-mode');
});
2017-12-20 12:33:33 +01:00
this.bind('d v', () => {
appEvents.emit('toggle-view-mode');
});
}
}
2017-12-20 12:33:33 +01:00
coreModule.service('keybindingSrv', KeybindingSrv);