context_srv.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. hasAccessToExplore() {
  55. return (this.isEditor || config.viewersCanEdit) && config.exploreEnabled;
  56. }
  57. getTheme(): ThemeName {
  58. return this.user.lightTheme ? ThemeNames.Light : ThemeNames.Dark;
  59. }
  60. }
  61. const contextSrv = new ContextSrv();
  62. export { contextSrv };
  63. coreModule.factory('contextSrv', () => {
  64. return contextSrv;
  65. });