context_srv.ts 1.5 KB

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