context_srv.ts 1.5 KB

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