context_srv.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import config from 'app/core/config';
  2. import _ from 'lodash';
  3. import coreModule from 'app/core/core_module';
  4. import store from 'app/core/store';
  5. import { ThemeNames, ThemeName } from '@grafana/ui';
  6. export class User {
  7. isGrafanaAdmin: any;
  8. isSignedIn: any;
  9. orgRole: any;
  10. orgId: number;
  11. orgName: string;
  12. orgCount: number;
  13. timezone: string;
  14. helpFlags1: number;
  15. lightTheme: boolean;
  16. hasEditPermissionInFolders: boolean;
  17. constructor() {
  18. if (config.bootData.user) {
  19. _.extend(this, config.bootData.user);
  20. }
  21. }
  22. }
  23. export class ContextSrv {
  24. pinned: any;
  25. version: any;
  26. user: User;
  27. isSignedIn: any;
  28. isGrafanaAdmin: any;
  29. isEditor: any;
  30. sidemenu: any;
  31. sidemenuSmallBreakpoint = false;
  32. hasEditPermissionInFolders: boolean;
  33. constructor() {
  34. this.sidemenu = store.getBool('grafana.sidemenu', true);
  35. if (!config.bootData) {
  36. config.bootData = { user: {}, settings: {} };
  37. }
  38. this.user = new User();
  39. this.isSignedIn = this.user.isSignedIn;
  40. this.isGrafanaAdmin = this.user.isGrafanaAdmin;
  41. this.isEditor = this.hasRole('Editor') || this.hasRole('Admin');
  42. this.hasEditPermissionInFolders = this.user.hasEditPermissionInFolders;
  43. }
  44. hasRole(role) {
  45. return this.user.orgRole === role;
  46. }
  47. isGrafanaVisible() {
  48. return !!(document.visibilityState === undefined || document.visibilityState === 'visible');
  49. }
  50. toggleSideMenu() {
  51. this.sidemenu = !this.sidemenu;
  52. store.set('grafana.sidemenu', this.sidemenu);
  53. }
  54. getTheme(): ThemeName {
  55. return this.user.lightTheme ? ThemeNames.Light : ThemeNames.Dark;
  56. }
  57. }
  58. const contextSrv = new ContextSrv();
  59. export { contextSrv };
  60. coreModule.factory('contextSrv', () => {
  61. return contextSrv;
  62. });