context_srv.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. export class User {
  6. isGrafanaAdmin: any;
  7. isSignedIn: any;
  8. orgRole: any;
  9. orgId: number;
  10. orgName: string;
  11. orgCount: number;
  12. timezone: string;
  13. helpFlags1: number;
  14. lightTheme: boolean;
  15. hasEditPermissionInFolders: boolean;
  16. constructor() {
  17. if (config.bootData.user) {
  18. _.extend(this, config.bootData.user);
  19. }
  20. }
  21. }
  22. export class ContextSrv {
  23. pinned: any;
  24. version: any;
  25. user: User;
  26. isSignedIn: any;
  27. isGrafanaAdmin: any;
  28. isEditor: any;
  29. sidemenu: any;
  30. sidemenuSmallBreakpoint = false;
  31. hasEditPermissionInFolders: boolean;
  32. constructor() {
  33. this.sidemenu = store.getBool('grafana.sidemenu', true);
  34. if (!config.bootData) {
  35. config.bootData = { user: {}, settings: {} };
  36. }
  37. this.user = new User();
  38. this.isSignedIn = this.user.isSignedIn;
  39. this.isGrafanaAdmin = this.user.isGrafanaAdmin;
  40. this.isEditor = this.hasRole('Editor') || this.hasRole('Admin');
  41. this.hasEditPermissionInFolders = this.user.hasEditPermissionInFolders;
  42. }
  43. hasRole(role) {
  44. return this.user.orgRole === role;
  45. }
  46. isGrafanaVisible() {
  47. return !!(document.visibilityState === undefined || document.visibilityState === 'visible');
  48. }
  49. toggleSideMenu() {
  50. this.sidemenu = !this.sidemenu;
  51. store.set('grafana.sidemenu', this.sidemenu);
  52. }
  53. }
  54. const contextSrv = new ContextSrv();
  55. export { contextSrv };
  56. coreModule.factory('contextSrv', () => {
  57. return contextSrv;
  58. });