context_srv.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.buildInfo) {
  33. config.buildInfo = {};
  34. }
  35. if (!config.bootData) {
  36. config.bootData = { user: {}, settings: {} };
  37. }
  38. this.version = config.buildInfo.version;
  39. this.user = new User();
  40. this.isSignedIn = this.user.isSignedIn;
  41. this.isGrafanaAdmin = this.user.isGrafanaAdmin;
  42. this.isEditor = this.hasRole('Editor') || this.hasRole('Admin');
  43. this.hasEditPermissionInFolders = this.user.hasEditPermissionInFolders;
  44. }
  45. hasRole(role) {
  46. return this.user.orgRole === role;
  47. }
  48. isGrafanaVisible() {
  49. return !!(document.visibilityState === undefined || document.visibilityState === 'visible');
  50. }
  51. toggleSideMenu() {
  52. this.sidemenu = !this.sidemenu;
  53. store.set('grafana.sidemenu', this.sidemenu);
  54. }
  55. }
  56. var contextSrv = new ContextSrv();
  57. export { contextSrv };
  58. coreModule.factory('contextSrv', function() {
  59. return contextSrv;
  60. });