@ -3,7 +3,7 @@ import Mousetrap from 'mousetrap';
import 'mousetrap-global-bind' ;
import 'mousetrap/plugins/global-bind/mousetrap-global-bind' ;
import { LegacyGraphHoverClearEvent , locationUtil } from '@grafana/data' ;
import { config , l ocationService } from '@grafana/runtime' ;
import { config , L ocationService } from '@grafana/runtime' ;
import appEvents from 'app/core/app_events' ;
import { getExploreUrl } from 'app/core/utils/explore' ;
import { SaveDashboardDrawer } from 'app/features/dashboard/components/SaveDashboard/SaveDashboardDrawer' ;
@ -20,20 +20,20 @@ import {
ZoomOutEvent ,
AbsoluteTimeEvent ,
} from '../../types/events' ;
import { AppChromeService } from '../components/AppChrome/AppChromeService' ;
import { HelpModal } from '../components/help/HelpModal' ;
import { contextSrv } from '../core' ;
import { exitKioskMode , toggleKioskMode } from '../navigation/kiosk' ;
import { toggleTheme } from './toggleTheme' ;
import { withFocusedPanel } from './withFocusedPanelId' ;
export class KeybindingSrv {
reset() {
constructor ( private locationService : LocationService , private chromeService : AppChromeService ) { }
clearAndInitGlobalBindings() {
Mousetrap . reset ( ) ;
}
initGlobals() {
if ( locationService . getLocation ( ) . pathname !== '/login' ) {
if ( this . locationService . getLocation ( ) . pathname !== '/login' ) {
this . bind ( [ '?' , 'h' ] , this . showHelpModal ) ;
this . bind ( 'g h' , this . goToHome ) ;
this . bind ( 'g a' , this . openAlerting ) ;
@ -42,7 +42,7 @@ export class KeybindingSrv {
this . bind ( 't a' , this . makeAbsoluteTime ) ;
this . bind ( 'f' , this . openSearch ) ;
this . bind ( 'esc' , this . exit ) ;
this . bindGlobal ( 'esc' , this . globalEsc ) ;
this . bindGlobalEsc ( ) ;
}
this . bind ( 't t' , ( ) = > toggleTheme ( false ) ) ;
@ -53,6 +53,10 @@ export class KeybindingSrv {
}
}
bindGlobalEsc() {
this . bindGlobal ( 'esc' , this . globalEsc ) ;
}
globalEsc() {
const anyDoc = document as any ;
const activeElement = anyDoc . activeElement ;
@ -82,29 +86,29 @@ export class KeybindingSrv {
toggleNav() {
window . location . href =
config . appSubUrl +
locationUtil . getUrlForPartial ( locationService . getLocation ( ) , {
locationUtil . getUrlForPartial ( this . locationService . getLocation ( ) , {
'__feature.topnav' : ( ! config . featureToggles . topnav ) . toString ( ) ,
} ) ;
}
private openSearch() {
locationService . partial ( { search : 'open' } ) ;
this . locationService . partial ( { search : 'open' } ) ;
}
private closeSearch() {
locationService . partial ( { search : null } ) ;
this . locationService . partial ( { search : null } ) ;
}
private openAlerting() {
locationService . push ( '/alerting' ) ;
this . locationService . push ( '/alerting' ) ;
}
private goToHome() {
locationService . push ( '/' ) ;
this . locationService . push ( '/' ) ;
}
private goToProfile() {
locationService . push ( '/profile' ) ;
this . locationService . push ( '/profile' ) ;
}
private makeAbsoluteTime() {
@ -116,30 +120,31 @@ export class KeybindingSrv {
}
private exit() {
const search = locationService . getSearchObject ( ) ;
const search = this . locationService . getSearchObject ( ) ;
if ( search . editview ) {
locationService . partial ( { editview : null , editIndex : null } ) ;
this . locationService . partial ( { editview : null , editIndex : null } ) ;
return ;
}
if ( search . inspect ) {
locationService . partial ( { inspect : null , inspectTab : null } ) ;
this . locationService . partial ( { inspect : null , inspectTab : null } ) ;
return ;
}
if ( search . editPanel ) {
locationService . partial ( { editPanel : null , tab : null } ) ;
this . locationService . partial ( { editPanel : null , tab : null } ) ;
return ;
}
if ( search . viewPanel ) {
locationService . partial ( { viewPanel : null , tab : null } ) ;
this . locationService . partial ( { viewPanel : null , tab : null } ) ;
return ;
}
if ( search . kiosk ) {
exitKioskMode ( ) ;
const { kioskMode } = this . chromeService . state . getValue ( ) ;
if ( kioskMode ) {
this . chromeService . exitKioskMode ( ) ;
}
if ( search . search ) {
@ -148,7 +153,7 @@ export class KeybindingSrv {
}
private showDashEditView() {
locationService . partial ( {
this . locationService . partial ( {
editview : 'settings' ,
} ) ;
}
@ -230,15 +235,15 @@ export class KeybindingSrv {
// edit panel
this . bindWithPanelId ( 'e' , ( panelId ) = > {
if ( dashboard . canEditPanelById ( panelId ) ) {
const isEditing = locationService . getSearchObject ( ) . editPanel !== undefined ;
locationService . partial ( { editPanel : isEditing ? null : panelId } ) ;
const isEditing = this . locationService . getSearchObject ( ) . editPanel !== undefined ;
this . locationService . partial ( { editPanel : isEditing ? null : panelId } ) ;
}
} ) ;
// view panel
this . bindWithPanelId ( 'v' , ( panelId ) = > {
const isViewing = locationService . getSearchObject ( ) . viewPanel !== undefined ;
locationService . partial ( { viewPanel : isViewing ? null : panelId } ) ;
const isViewing = this . locationService . getSearchObject ( ) . viewPanel !== undefined ;
this . locationService . partial ( { viewPanel : isViewing ? null : panelId } ) ;
} ) ;
//toggle legend
@ -252,7 +257,7 @@ export class KeybindingSrv {
} ) ;
this . bindWithPanelId ( 'i' , ( panelId ) = > {
locationService . partial ( { inspect : panelId } ) ;
this . locationService . partial ( { inspect : panelId } ) ;
} ) ;
// jump to explore if permissions allow
@ -268,7 +273,7 @@ export class KeybindingSrv {
if ( url ) {
const urlWithoutBase = locationUtil . stripBaseFromUrl ( url ) ;
if ( urlWithoutBase ) {
locationService . push ( urlWithoutBase ) ;
this . locationService . push ( urlWithoutBase ) ;
}
}
} ) ;
@ -322,7 +327,7 @@ export class KeybindingSrv {
} ) ;
this . bind ( 'd n' , ( ) = > {
locationService . push ( '/dashboard/new' ) ;
this . locationService . push ( '/dashboard/new' ) ;
} ) ;
this . bind ( 'd r' , ( ) = > {
@ -334,17 +339,15 @@ export class KeybindingSrv {
} ) ;
this . bind ( 'd k' , ( ) = > {
t oggleKioskMode( ) ;
this . chromeService . onT oggleKioskMode( ) ;
} ) ;
//Autofit panels
this . bind ( 'd a' , ( ) = > {
// this has to be a full page reload
const queryParams = locationService . getSearchObject ( ) ;
const queryParams = this . locationService . getSearchObject ( ) ;
const newUrlParam = queryParams . autofitpanels ? '' : '&autofitpanels' ;
window . location . href = window . location . href + newUrlParam ;
} ) ;
}
}
export const keybindingSrv = new KeybindingSrv ( ) ;